diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-10-29 19:42:23 +0100 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-10-29 19:42:23 +0100 |
commit | f8a818373d39053d0a539dae689d09d13f8877bc (patch) | |
tree | 75f01b7cb0e3287108627edbb7a6754792dc17cf /src | |
parent | 101ba17e3b39a9b11888219705cde9a150a3bf02 (diff) | |
download | RSAComb-f8a818373d39053d0a539dae689d09d13f8877bc.tar.gz RSAComb-f8a818373d39053d0a539dae689d09d13f8877bc.zip |
Add partial unit tests for filtering program computation
The committed tests will fail, but the reason is still not clear.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/rsacomb/FilteringProgram.scala | 4 | ||||
-rw-r--r-- | src/main/scala/rsacomb/RDFoxUtil.scala | 4 | ||||
-rw-r--r-- | src/test/scala/rsacomb/FilteringProgramSpecs.scala | 174 |
3 files changed, 178 insertions, 4 deletions
diff --git a/src/main/scala/rsacomb/FilteringProgram.scala b/src/main/scala/rsacomb/FilteringProgram.scala index 2be236b..9c19b7f 100644 --- a/src/main/scala/rsacomb/FilteringProgram.scala +++ b/src/main/scala/rsacomb/FilteringProgram.scala | |||
@@ -8,8 +8,8 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { | |||
8 | /* Makes mplicit conversion OWLAPI IRI <-> RDFox IRI available */ | 8 | /* Makes mplicit conversion OWLAPI IRI <-> RDFox IRI available */ |
9 | import RDFoxUtil._ | 9 | import RDFoxUtil._ |
10 | 10 | ||
11 | private val answer: List[Term] = query.getAnswerVariables.asScala.toList | 11 | val answer: List[Term] = query.getAnswerVariables.asScala.toList |
12 | private val bounded: List[Term] = this.getBoundedVariables | 12 | val bounded: List[Term] = this.getBoundedVariables |
13 | 13 | ||
14 | val facts: List[Atom] = constants.map(named) | 14 | val facts: List[Atom] = constants.map(named) |
15 | val rules: List[Rule] = this.generateFilteringProgram().map(reifyRule) | 15 | val rules: List[Rule] = this.generateFilteringProgram().map(reifyRule) |
diff --git a/src/main/scala/rsacomb/RDFoxUtil.scala b/src/main/scala/rsacomb/RDFoxUtil.scala index 8a144c6..4e0a118 100644 --- a/src/main/scala/rsacomb/RDFoxUtil.scala +++ b/src/main/scala/rsacomb/RDFoxUtil.scala | |||
@@ -51,9 +51,9 @@ object RDFoxUtil { | |||
51 | (server, data) | 51 | (server, data) |
52 | } | 52 | } |
53 | 53 | ||
54 | def parseQuery(query: String): Query = { | 54 | def parseQuery(query: String, prefixes: Prefixes = RSA.Prefixes): Query = { |
55 | val parser = new SPARQLParser( | 55 | val parser = new SPARQLParser( |
56 | RSA.Prefixes, | 56 | prefixes, |
57 | new ByteArrayInputStream(query.getBytes()) | 57 | new ByteArrayInputStream(query.getBytes()) |
58 | ) | 58 | ) |
59 | parser.parseSingleQuery() | 59 | parser.parseSingleQuery() |
diff --git a/src/test/scala/rsacomb/FilteringProgramSpecs.scala b/src/test/scala/rsacomb/FilteringProgramSpecs.scala new file mode 100644 index 0000000..76ea2d6 --- /dev/null +++ b/src/test/scala/rsacomb/FilteringProgramSpecs.scala | |||
@@ -0,0 +1,174 @@ | |||
1 | package rsacomb | ||
2 | |||
3 | import java.io.File | ||
4 | import org.scalatest.LoneElement | ||
5 | import org.scalatest.Inspectors | ||
6 | import org.scalatest.flatspec.AnyFlatSpec | ||
7 | import org.scalatest.matchers.should.Matchers | ||
8 | |||
9 | import tech.oxfordsemantic.jrdfox.logic.Variable | ||
10 | import tech.oxfordsemantic.jrdfox.Prefixes | ||
11 | |||
12 | import rsacomb.RDFoxUtil._ | ||
13 | |||
14 | object FilteringProgramSpec { | ||
15 | |||
16 | val prefixes = new Prefixes() | ||
17 | prefixes.declarePrefix( | ||
18 | ":", | ||
19 | "http://slegger.gitlab.io/slegge-obda/ontology/subsurface-exploration#" | ||
20 | ) | ||
21 | prefixes.declarePrefix("rdf:", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") | ||
22 | prefixes.declarePrefix("rdfs:", "http://www.w3.org/2000/01/rdf-schema#") | ||
23 | prefixes.declarePrefix("owl:", "http://www.w3.org/2002/07/owl#") | ||
24 | |||
25 | val query0 = parseQuery(""" | ||
26 | SELECT ?subj | ||
27 | WHERE { | ||
28 | ?subj ?pred ?obj | ||
29 | } | ||
30 | """, prefixes) | ||
31 | |||
32 | val query1 = parseQuery(""" | ||
33 | SELECT * | ||
34 | WHERE { | ||
35 | ?w a :Wellbore | ||
36 | } | ||
37 | """, prefixes) | ||
38 | |||
39 | val query2 = parseQuery( | ||
40 | """ | ||
41 | SELECT * | ||
42 | WHERE { | ||
43 | ?w a :Wellbore ; | ||
44 | :wellboreDocument ?doc . | ||
45 | ?doc :hasURL ?document_hyperlink | ||
46 | } | ||
47 | """, | ||
48 | prefixes | ||
49 | ) | ||
50 | |||
51 | val query3 = parseQuery( | ||
52 | """ | ||
53 | SELECT ?w ?doc ?document_hyperlink | ||
54 | WHERE { | ||
55 | ?w a :Wellbore ; | ||
56 | :wellboreDocument ?doc . | ||
57 | ?doc :hasURL ?document_hyperlink | ||
58 | } | ||
59 | """, | ||
60 | prefixes | ||
61 | ) | ||
62 | |||
63 | val query4 = parseQuery( | ||
64 | """ | ||
65 | SELECT ?w ?doc ?document_hyperlink | ||
66 | WHERE { | ||
67 | ?w a :Wellbore ; | ||
68 | :wellboreDocument ?doc . | ||
69 | ?doc :hasURL ?document_hyperlink | ||
70 | } | ||
71 | """, | ||
72 | prefixes | ||
73 | ) | ||
74 | |||
75 | val query5 = parseQuery( | ||
76 | """ | ||
77 | SELECT ?wellbore ?unit_name ?discovery | ||
78 | WHERE { | ||
79 | ?w a :Wellbore ; | ||
80 | :name ?wellbore ; | ||
81 | :hasWellboreInterval ?c_int ; | ||
82 | :hasWellboreInterval ?f_int . | ||
83 | ?c_int :hasUnit ?c_unit . | ||
84 | ?c_unit :name ?unit_name . | ||
85 | ?f_int a :FluidZone ; | ||
86 | :name ?discovery ; | ||
87 | :overlapsWellboreInterval ?c_int | ||
88 | } | ||
89 | """, | ||
90 | prefixes | ||
91 | ) | ||
92 | |||
93 | val query6 = parseQuery( | ||
94 | """ | ||
95 | SELECT DISTINCT ?wellbore ?content | ||
96 | WHERE { | ||
97 | ?w a :Wellbore ; | ||
98 | :name ?wellbore ; | ||
99 | :hasWellboreInterval ?int . | ||
100 | ?int a :FluidZone ; | ||
101 | :fluidZoneContent ?content | ||
102 | } | ||
103 | """, | ||
104 | prefixes | ||
105 | ) | ||
106 | |||
107 | val query7 = parseQuery( | ||
108 | """ | ||
109 | SELECT ?wName ?sample ?porosity ?top_depth_md ?bot_depth_md | ||
110 | WHERE { | ||
111 | ?w a :Wellbore ; | ||
112 | :name ?wName ; | ||
113 | :hasWellboreInterval ?z . | ||
114 | ?z :hasUnit ?u . | ||
115 | ?u :name ?strat_unit_name . | ||
116 | ?wellbore :hasWellboreInterval ?cored_int . | ||
117 | ?c :extractedFrom ?cored_int ; | ||
118 | :hasCoreSample ?sample . | ||
119 | ?sample :hasDepth ?sample_depth . | ||
120 | ?sample_depth | ||
121 | :inWellboreInterval ?z . | ||
122 | ?sample :hasPorosity ?p . | ||
123 | ?p :valueInStandardUnit ?porosity . | ||
124 | ?z :hasTopDepth ?top . | ||
125 | ?top a :MeasuredDepth ; | ||
126 | :valueInStandardUnit ?top_depth_md . | ||
127 | ?z :hasBottomDepth ?bot . | ||
128 | ?bot a :MeasuredDepth ; | ||
129 | :valueInStandardUnit ?bot_depth_md | ||
130 | } | ||
131 | """, | ||
132 | prefixes | ||
133 | ) | ||
134 | |||
135 | } | ||
136 | |||
137 | class FilteringProgramSpec | ||
138 | extends AnyFlatSpec | ||
139 | with Matchers | ||
140 | with LoneElement | ||
141 | with Inspectors { | ||
142 | |||
143 | import FilteringProgramSpec._ | ||
144 | |||
145 | query0.toString() should "have distinct answer and bounded variables" in { | ||
146 | val program = new FilteringProgram(query0, List()) | ||
147 | forAll(program.answer) { v => program.bounded should not contain v } | ||
148 | forAll(program.bounded) { v => program.answer should not contain v } | ||
149 | } | ||
150 | |||
151 | it should "have {?obj, ?pred} as bounded variables" in { | ||
152 | val pred = Variable.create("obj") | ||
153 | val obj = Variable.create("pred") | ||
154 | val program = new FilteringProgram(query0, List()) | ||
155 | program.bounded should contain theSameElementsAs List(pred, obj) | ||
156 | } | ||
157 | |||
158 | query1.toString() should "have distinct answer and bounded variables" in { | ||
159 | val program = new FilteringProgram(query1, List()) | ||
160 | forAll(program.answer) { v => program.bounded should not contain v } | ||
161 | forAll(program.bounded) { v => program.answer should not contain v } | ||
162 | } | ||
163 | |||
164 | it should "have no bounded variable" in { | ||
165 | val program = new FilteringProgram(query1, List()) | ||
166 | program.bounded shouldBe empty | ||
167 | } | ||
168 | |||
169 | query2.toString() should "have distinct answer and bounded variables" in { | ||
170 | val program = new FilteringProgram(query2, List()) | ||
171 | forAll(program.answer) { v => program.bounded should not contain v } | ||
172 | forAll(program.bounded) { v => program.answer should not contain v } | ||
173 | } | ||
174 | } | ||