-
Notifications
You must be signed in to change notification settings - Fork 30
/
RdfPartitionerDefaultTests.scala
170 lines (132 loc) · 7.16 KB
/
RdfPartitionerDefaultTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package net.sansa_stack.rdf.common.partition.core
import java.io.ByteArrayInputStream
import org.aksw.obda.jena.r2rml.impl.R2rmlExporter
import net.sansa_stack.rdf.common.partition.core.RdfPartitionerDefault._
import net.sansa_stack.rdf.common.partition.r2rml.R2rmlUtils
import net.sansa_stack.rdf.common.partition.schema.{SchemaStringString, SchemaStringStringLang}
import org.aksw.r2rml.jena.vocab.RR
import org.apache.jena.graph.{Node, NodeFactory, Triple}
import org.apache.jena.rdf.model.{Model, ModelFactory}
import org.apache.jena.riot.{Lang, RDFDataMgr, RDFFormat}
import org.scalatest.FunSuite
/**
* @author Gezim Sejdiu
*/
class RdfPartitionerDefaultTests extends FunSuite {
val triple = Triple.create(
NodeFactory.createURI("http://dbpedia.org/resource/Guy_de_Maupassant"),
NodeFactory.createURI("http://xmlns.com/foaf/0.1/givenName"),
NodeFactory.createLiteral("Guy De"))
test("getting URI or BNode string should match") {
val node = NodeFactory.createURI("http://dbpedia.org/resource/Guy_de_Maupassant")
assert(getUriOrBNodeString(triple.getSubject).matches(node.getURI))
}
test("getting RDF Term type should match") {
assert(getRdfTermType(triple.getSubject) == 1)
}
test("checking if data type is PlainLiteral should match") {
assert(isPlainLiteralDatatype(triple.getObject.getLiteralDatatypeURI))
}
test("checking if [[Node]] is TypedLiteral should match") {
assert(!isTypedLiteral(triple.getObject))
}
test("getting partitioning layout from [[Triple]] should match") {
val expectedPartition = new RdfPartitionStateDefault(1, "http://xmlns.com/foaf/0.1/givenName",
2, "http://www.w3.org/2001/XMLSchema#string", true, Set())
assert(fromTriple(triple).equals(expectedPartition))
}
test("export partition as R2RML should work") {
val expected : Model = ModelFactory.createDefaultModel
RDFDataMgr.read(expected, new ByteArrayInputStream(
"""
| @prefix rr: <http://www.w3.org/ns/r2rml#> .
|
|[ rr:logicalTable [ rr:tableName "http://xmlns.com/foaf/0.1/givenName_XMLSchema#string_lang" ] ;
| rr:predicateObjectMap [ rr:objectMap [ rr:column "o" ;
| rr:langColumn "l"
| ] ;
| rr:predicate <http://xmlns.com/foaf/0.1/givenName>
| ] ;
| rr:subjectMap [ rr:column "s" ;
| rr:datatype rr:IRI
| ]
|] .
|""".stripMargin.getBytes()), Lang.TURTLE)
val partitionState = RdfPartitionStateDefault(1, "http://xmlns.com/foaf/0.1/givenName",
2, "http://www.w3.org/2001/XMLSchema#string", true, Set("en", "de", "fr"))
val model = ModelFactory.createDefaultModel()
val triplesMaps = R2rmlUtils.createR2rmlMappings(RdfPartitionerDefault, partitionState, model, false)
// There must be just a single triples map
assert(triplesMaps.size == 1)
val triplesMap = triplesMaps(0)
val actual = triplesMap.getModel
assert(expected.isIsomorphicWith(actual))
}
test("export partition with lang tags exploded as R2RML should result in a separate TriplesMap per language tag") {
val expected : Model = ModelFactory.createDefaultModel
RDFDataMgr.read(expected, new ByteArrayInputStream(
"""
| @base <http://www.w3.org/ns/r2rml#> .
|[ <#logicalTable> [ <#sqlQuery> "SELECT `s`, `o` FROM `http://xmlns.com/foaf/0.1/givenName_XMLSchema#string_lang` WHERE `l` = 'fr'" ] ;
| <#predicateObjectMap> [ <#objectMap> [ <#column> "o" ;
| <#language> "fr"
| ] ;
| <#predicate> <http://xmlns.com/foaf/0.1/givenName>
| ] ;
| <#subjectMap> [ <#column> "s" ;
| <#datatype> <#IRI>
| ]
|] .
|
|[ <#logicalTable> [ <#sqlQuery> "SELECT `s`, `o` FROM `http://xmlns.com/foaf/0.1/givenName_XMLSchema#string_lang` WHERE `l` = 'de'" ] ;
| <#predicateObjectMap> [ <#objectMap> [ <#column> "o" ;
| <#language> "de"
| ] ;
| <#predicate> <http://xmlns.com/foaf/0.1/givenName>
| ] ;
| <#subjectMap> [ <#column> "s" ;
| <#datatype> <#IRI>
| ]
|] .
|
|[ <#logicalTable> [ <#sqlQuery> "SELECT `s`, `o` FROM `http://xmlns.com/foaf/0.1/givenName_XMLSchema#string_lang` WHERE `l` = 'en'" ] ;
| <#predicateObjectMap> [ <#objectMap> [ <#column> "o" ;
| <#language> "en"
| ] ;
| <#predicate> <http://xmlns.com/foaf/0.1/givenName>
| ] ;
| <#subjectMap> [ <#column> "s" ;
| <#datatype> <#IRI>
| ]
|] .
|""".stripMargin.getBytes()), Lang.TURTLE)
val languages = Set("en", "de", "fr")
val partitionState = RdfPartitionStateDefault(1, "http://xmlns.com/foaf/0.1/givenName",
2, "http://www.w3.org/2001/XMLSchema#string", true, languages)
val actual = ModelFactory.createDefaultModel()
val triplesMaps = R2rmlUtils.createR2rmlMappings(RdfPartitionerDefault, partitionState, actual, true)
RDFDataMgr.write(System.out, actual, RDFFormat.TURTLE_PRETTY)
assert(triplesMaps.size == languages.size)
assert(expected.isIsomorphicWith(actual))
}
test("R2RML import/export should work") {
val partitionState = RdfPartitionStateDefault(1, "http://xmlns.com/foaf/0.1/givenName",
2, "http://www.w3.org/2001/XMLSchema#string", true, Set("en", "de", "fr"))
val exportModel = ModelFactory.createDefaultModel()
val triplesMaps = R2rmlUtils.createR2rmlMappings(RdfPartitionerDefault, partitionState, exportModel, true)
// val exportModel = exportModel // RdfPartitionImportExport.exportAsR2RML(RdfPartitionerDefault, partitionState, true)
exportModel.write(System.out, "Turtle", "http://www.w3.org/ns/r2rml#")
// TODO Add serialization and deserialization
val importedTriplesMaps = RdfPartitionImportExport.importFromR2RML(exportModel)
assert(triplesMaps.size == importedTriplesMaps.size)
assert(importedTriplesMaps.head.getModel.isIsomorphicWith(triplesMaps.head.getModel))
}
test("determining Layout should match") {
val expectedLayout = SchemaStringStringLang("http://dbpedia.org/resource/Guy_de_Maupassant", "Guy De", "")
assert(determineLayout(fromTriple(triple)).fromTriple(triple).equals(expectedLayout))
}
test("determining Layout Datatype should match") {
val expectedLayoutDatatype = SchemaStringString("http://dbpedia.org/resource/Guy_de_Maupassant", "Guy De")
assert(determineLayoutDatatype(triple.getObject.getLiteralDatatypeURI).fromTriple(triple).equals(expectedLayoutDatatype))
}
}