diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-20 13:10:44 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-20 13:11:14 +0000 |
commit | 0ac35ab057257eadc297d430666d0c1da41756f6 (patch) | |
tree | fbbd64a74675989039c9c9c36e85b9aadd21fb97 /src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |
parent | c804109db7bb08cce1246acbca2030c3b979f242 (diff) | |
download | RSAComb-0ac35ab057257eadc297d430666d0c1da41756f6.tar.gz RSAComb-0ac35ab057257eadc297d430666d0c1da41756f6.zip |
Simplify workflow for query execution
Input query is now read from file.
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala')
-rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | 186 |
1 files changed, 7 insertions, 179 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala index db52793..678a5fa 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |||
@@ -46,9 +46,7 @@ object RSAComb extends App { | |||
46 | sys.exit; | 46 | sys.exit; |
47 | } | 47 | } |
48 | 48 | ||
49 | /* Create RSA object from generic OWLOntology | 49 | /* TODO: It might be required to check if the ontology in input is |
50 | * | ||
51 | * TODO: It might be required to check if the ontology in input is | ||
52 | * Horn-ALCHOIQ. At the moment we are assuming this is always the | 50 | * Horn-ALCHOIQ. At the moment we are assuming this is always the |
53 | * case. | 51 | * case. |
54 | */ | 52 | */ |
@@ -56,183 +54,13 @@ object RSAComb extends App { | |||
56 | val ontology = RSAOntology(ontoPath) | 54 | val ontology = RSAOntology(ontoPath) |
57 | if (ontology.isRSA) { | 55 | if (ontology.isRSA) { |
58 | 56 | ||
59 | /* Load query */ | 57 | /** Read SPARQL query from file */ |
60 | val query = """ | 58 | val source = io.Source.fromFile(queryPath.getAbsoluteFile) |
61 | PREFIX : <http://example.com/rsa_example.owl#> | 59 | val query = source.getLines mkString "\n" |
62 | 60 | source.close() | |
63 | SELECT ?X | ||
64 | WHERE { | ||
65 | ?X a :D ; | ||
66 | :R ?Y . | ||
67 | ?Y :S ?Z . | ||
68 | ?Z a :D . | ||
69 | } | ||
70 | """ | ||
71 | 61 | ||
72 | /* Compute answers to query */ | 62 | /* Compute answers to query */ |
73 | ConjunctiveQuery(query) match { | 63 | val answers = ConjunctiveQuery(query).map(ontology ask _) |
74 | case Some(query) => { | 64 | answers map (_.toString) foreach println |
75 | |||
76 | import implicits.JavaCollections._ | ||
77 | |||
78 | // Open connection to RDFox | ||
79 | val (server, data) = RDFoxHelpers.openConnection("AnswerComputation") | ||
80 | |||
81 | { | ||
82 | println("\nQuery") | ||
83 | println(query) | ||
84 | } | ||
85 | |||
86 | val canon = ontology.canonicalModel | ||
87 | data.addRules(canon.rules) | ||
88 | val filter = ontology.filteringProgram(query) | ||
89 | data.addRules(filter.rules) | ||
90 | |||
91 | { | ||
92 | println("\nCanonical Model rules:") | ||
93 | canon.rules.foreach(println) | ||
94 | println("\nFiltering rules") | ||
95 | filter.rules.foreach(println) | ||
96 | } | ||
97 | |||
98 | // Retrieve answers | ||
99 | println("\nAnswers:") | ||
100 | val ans = | ||
101 | RDFoxHelpers.queryInternalPredicate(data, "Ans", filter.answer.length) | ||
102 | println(ans) | ||
103 | |||
104 | /* DEBUG: adding additional checks | ||
105 | */ | ||
106 | { | ||
107 | import suffix.{Forward, Backward} | ||
108 | |||
109 | val arity = filter.answer.length + filter.bounded.length | ||
110 | |||
111 | println("\nIndividuals:") | ||
112 | ontology.individuals.foreach(println) | ||
113 | |||
114 | println("\nThings:") | ||
115 | val things = RDFoxHelpers.submitQuery( | ||
116 | data, | ||
117 | """ | ||
118 | PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
119 | |||
120 | SELECT ?X { | ||
121 | ?X a owl:Thing | ||
122 | } | ||
123 | """ | ||
124 | ) | ||
125 | println(things) | ||
126 | |||
127 | println("\nNAMEDs:") | ||
128 | val named = RDFoxHelpers.submitQuery( | ||
129 | data, | ||
130 | """ | ||
131 | SELECT ?X { | ||
132 | ?X a rsa:Named | ||
133 | } | ||
134 | """, | ||
135 | RSA.Prefixes | ||
136 | ) | ||
137 | println(named) | ||
138 | |||
139 | println("\nNIs:") | ||
140 | val nis = RDFoxHelpers.submitQuery( | ||
141 | data, | ||
142 | """ | ||
143 | SELECT ?X { | ||
144 | ?X a rsa:NI | ||
145 | } | ||
146 | """, | ||
147 | RSA.Prefixes | ||
148 | ) | ||
149 | println(nis) | ||
150 | |||
151 | // ID instances | ||
152 | println("\nIDs:") | ||
153 | val ids = RDFoxHelpers.queryInternalPredicate( | ||
154 | data, | ||
155 | "ID", | ||
156 | arity + 2 | ||
157 | ) | ||
158 | println(ids) | ||
159 | |||
160 | println("\nCongruent:") | ||
161 | val equivs = RDFoxHelpers.submitQuery( | ||
162 | data, | ||
163 | """ | ||
164 | SELECT ?X ?Y { | ||
165 | ?X rsa:congruent ?Y | ||
166 | } | ||
167 | """, | ||
168 | RSA.Prefixes | ||
169 | ) | ||
170 | println(equivs) | ||
171 | |||
172 | // Unfiltered answers | ||
173 | println("\nPossible answers:") | ||
174 | val qms = RDFoxHelpers.queryInternalPredicate( | ||
175 | data, | ||
176 | "QM", | ||
177 | arity | ||
178 | ) | ||
179 | println(qms) | ||
180 | |||
181 | // Cycle detected | ||
182 | println("\nCycle detection:") | ||
183 | val aqf = RDFoxHelpers.queryInternalPredicate( | ||
184 | data, | ||
185 | "AQ" :: Forward, | ||
186 | arity + 2 | ||
187 | ) | ||
188 | val aqb = RDFoxHelpers.queryInternalPredicate( | ||
189 | data, | ||
190 | "AQ" :: Backward, | ||
191 | arity + 2 | ||
192 | ) | ||
193 | println(aqf) | ||
194 | println(aqb) | ||
195 | |||
196 | // Forks detected | ||
197 | println("\nForks:") | ||
198 | val fk = RDFoxHelpers.queryInternalPredicate( | ||
199 | data, | ||
200 | "FK", | ||
201 | arity | ||
202 | ) | ||
203 | println(fk) | ||
204 | |||
205 | // Spurious answers | ||
206 | println("\nSpurious answers") | ||
207 | val sp = RDFoxHelpers.queryInternalPredicate( | ||
208 | data, | ||
209 | "SP", | ||
210 | arity | ||
211 | ) | ||
212 | println(sp) | ||
213 | } | ||
214 | |||
215 | // Close connection to RDFox | ||
216 | RDFoxHelpers.closeConnection(server, data) | ||
217 | } | ||
218 | case None => {} | ||
219 | } | ||
220 | } | 65 | } |
221 | } | 66 | } |
222 | |||
223 | /* Notes: | ||
224 | * | ||
225 | * To establish a connection with a local RDFox instance, do the | ||
226 | * following: | ||
227 | * | ||
228 | * ``` | ||
229 | * val serverConnection : ServerConnection = ConnectionFactory.newServerConnection("rdfox:local", "", "") | ||
230 | * serverConnection.createDataStore("test","seq",new HashMap()) | ||
231 | * val dataStoreConnection : DataStoreConnection = serverConnection.newDataStoreConnection("test") | ||
232 | * dataStoreConnection.importData( | ||
233 | * UpdateType.ADDITION, | ||
234 | * Prefixes.s_emptyPrefixes, | ||
235 | * new File("./path/to/file") | ||
236 | * ) | ||
237 | * ``` | ||
238 | */ | ||