diff options
Diffstat (limited to 'src')
4 files changed, 60 insertions, 65 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala index 4e533c6..9427735 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala | |||
| @@ -126,9 +126,9 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 126 | if index2 >= 0 | 126 | if index2 >= 0 |
| 127 | } yield Rule.create( | 127 | } yield Rule.create( |
| 128 | RSA.FK, | 128 | RSA.FK, |
| 129 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 129 | role1 << Forward, | 130 | role1 << Forward, |
| 130 | role2 << Forward, | 131 | role2 << Forward, |
| 131 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 132 | not(RSA.Congruent(role1.getArguments get 0, role2.getArguments get 0)) | 132 | not(RSA.Congruent(role1.getArguments get 0, role2.getArguments get 0)) |
| 133 | ) | 133 | ) |
| 134 | val r4b = for { | 134 | val r4b = for { |
| @@ -140,9 +140,9 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 140 | if index2 >= 0 | 140 | if index2 >= 0 |
| 141 | } yield Rule.create( | 141 | } yield Rule.create( |
| 142 | RSA.FK, | 142 | RSA.FK, |
| 143 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 143 | role1 << Forward, | 144 | role1 << Forward, |
| 144 | role2 << Backward, | 145 | role2 << Backward, |
| 145 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 146 | not(RSA.Congruent(role1.getArguments get 0, role2.getArguments get 2)) | 146 | not(RSA.Congruent(role1.getArguments get 0, role2.getArguments get 2)) |
| 147 | ) | 147 | ) |
| 148 | val r4c = for { | 148 | val r4c = for { |
| @@ -154,9 +154,9 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 154 | if index2 >= 0 | 154 | if index2 >= 0 |
| 155 | } yield Rule.create( | 155 | } yield Rule.create( |
| 156 | RSA.FK, | 156 | RSA.FK, |
| 157 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 157 | role1 << Backward, | 158 | role1 << Backward, |
| 158 | role2 << Backward, | 159 | role2 << Backward, |
| 159 | RSA.ID(RSA(index1), RSA(index2)), | ||
| 160 | not(RSA.Congruent(role1.getArguments get 2, role2.getArguments get 2)) | 160 | not(RSA.Congruent(role1.getArguments get 2, role2.getArguments get 2)) |
| 161 | ) | 161 | ) |
| 162 | 162 | ||
| @@ -180,13 +180,13 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 180 | RSA(query.bounded indexOf r1arg0), | 180 | RSA(query.bounded indexOf r1arg0), |
| 181 | RSA(query.bounded indexOf r2arg0) | 181 | RSA(query.bounded indexOf r2arg0) |
| 182 | ), | 182 | ), |
| 183 | role1 << Forward, | ||
| 184 | role2 << Forward, | ||
| 185 | RSA.ID( | 183 | RSA.ID( |
| 186 | RSA(query.bounded indexOf r1arg2), | 184 | RSA(query.bounded indexOf r1arg2), |
| 187 | RSA(query.bounded indexOf r2arg2) | 185 | RSA(query.bounded indexOf r2arg2) |
| 188 | ), | 186 | ), |
| 189 | RSA.Congruent(r1arg0, r2arg0), | 187 | RSA.Congruent(r1arg0, r2arg0), |
| 188 | role1 << Forward, | ||
| 189 | role2 << Forward, | ||
| 190 | not(RSA.NI(r1arg0)) | 190 | not(RSA.NI(r1arg0)) |
| 191 | ) | 191 | ) |
| 192 | val r5b = for { | 192 | val r5b = for { |
| @@ -205,13 +205,13 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 205 | RSA(query.bounded indexOf r1arg0), | 205 | RSA(query.bounded indexOf r1arg0), |
| 206 | RSA(query.bounded indexOf r2arg2) | 206 | RSA(query.bounded indexOf r2arg2) |
| 207 | ), | 207 | ), |
| 208 | role1 << Forward, | ||
| 209 | role2 << Backward, | ||
| 210 | RSA.ID( | 208 | RSA.ID( |
| 211 | RSA(query.bounded indexOf r1arg2), | 209 | RSA(query.bounded indexOf r1arg2), |
| 212 | RSA(query.bounded indexOf r2arg0) | 210 | RSA(query.bounded indexOf r2arg0) |
| 213 | ), | 211 | ), |
| 214 | RSA.Congruent(r1arg0, r2arg2), | 212 | RSA.Congruent(r1arg0, r2arg2), |
| 213 | role1 << Forward, | ||
| 214 | role2 << Backward, | ||
| 215 | not(RSA.NI(r1arg0)) | 215 | not(RSA.NI(r1arg0)) |
| 216 | ) | 216 | ) |
| 217 | val r5c = for { | 217 | val r5c = for { |
| @@ -230,13 +230,13 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) { | |||
| 230 | RSA(query.bounded indexOf r1arg2), | 230 | RSA(query.bounded indexOf r1arg2), |
| 231 | RSA(query.bounded indexOf r2arg2) | 231 | RSA(query.bounded indexOf r2arg2) |
| 232 | ), | 232 | ), |
| 233 | role1 << Backward, | ||
| 234 | role2 << Backward, | ||
| 235 | RSA.ID( | 233 | RSA.ID( |
| 236 | RSA(query.bounded indexOf r1arg0), | 234 | RSA(query.bounded indexOf r1arg0), |
| 237 | RSA(query.bounded indexOf r2arg0) | 235 | RSA(query.bounded indexOf r2arg0) |
| 238 | ), | 236 | ), |
| 239 | RSA.Congruent(r1arg2, r2arg2), | 237 | RSA.Congruent(r1arg2, r2arg2), |
| 238 | role1 << Backward, | ||
| 239 | role2 << Backward, | ||
| 240 | not(RSA.NI(r1arg2)) | 240 | not(RSA.NI(r1arg2)) |
| 241 | ) | 241 | ) |
| 242 | 242 | ||
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 f54884f..bf96a31 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |||
| @@ -1,37 +1,41 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb | 1 | package uk.ac.ox.cs.rsacomb |
| 2 | 2 | ||
| 3 | /* Java imports */ | ||
| 4 | import java.io.File | 3 | import java.io.File |
| 5 | import java.util.HashMap | 4 | import java.util.HashMap |
| 6 | import scala.collection.JavaConverters._ | 5 | import scala.collection.JavaConverters._ |
| 7 | |||
| 8 | import tech.oxfordsemantic.jrdfox.client.UpdateType | 6 | import tech.oxfordsemantic.jrdfox.client.UpdateType |
| 9 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | ||
| 10 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term} | 7 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term} |
| 8 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | ||
| 11 | 9 | ||
| 12 | /* Local imports */ | ||
| 13 | import util.{Logger, RDFoxUtil, RSA} | 10 | import util.{Logger, RDFoxUtil, RSA} |
| 14 | import sparql.ConjunctiveQuery | 11 | import sparql.ConjunctiveQuery |
| 15 | 12 | ||
| 13 | /** Entry point of the program. | ||
| 14 | * | ||
| 15 | * The executable expects a SPARQL query and a non-empty sequence of | ||
| 16 | * ontology files as arguments. The query file is expected to contain | ||
| 17 | * exactly one query, while the ontology files will be programmatically | ||
| 18 | * merged in a single ontology. | ||
| 19 | * | ||
| 20 | * @todo better arguments handling is needed. Look into some library | ||
| 21 | * for this. | ||
| 22 | * @todo at the moment the input ontology is assumed to be Horn-ALCHOIQ. | ||
| 23 | * This might not be the case. | ||
| 24 | */ | ||
| 16 | object RSAComb extends App { | 25 | object RSAComb extends App { |
| 17 | 26 | ||
| 18 | val help: String = """ | 27 | val help: String = """ |
| 19 | rsacomb - combined approach for CQ answering for RSA ontologies. | 28 | rsacomb - combined approach for CQ answering for RSA ontologies. |
| 20 | 29 | ||
| 21 | USAGE | 30 | USAGE |
| 22 | rsacomb <query> <ontology> ... | 31 | rsacomb <query> <ontology> [...] |
| 23 | 32 | ||
| 24 | where | 33 | where |
| 25 | - query: a (single) SPARQL query file. | 34 | - query: path to a file containing a single SPARQL query |
| 26 | - ontology: one or more ontologies. | 35 | - ontology: one or more ontology files |
| 27 | 36 | ||
| 28 | """ | 37 | """ |
| 29 | 38 | ||
| 30 | /* Simple arguments handling | ||
| 31 | * | ||
| 32 | * TODO: use something better later on | ||
| 33 | */ | ||
| 34 | |||
| 35 | if (args.length < 2) { | 39 | if (args.length < 2) { |
| 36 | println(help) | 40 | println(help) |
| 37 | sys.exit; | 41 | sys.exit; |
| @@ -46,52 +50,41 @@ object RSAComb extends App { | |||
| 46 | sys.exit; | 50 | sys.exit; |
| 47 | } | 51 | } |
| 48 | 52 | ||
| 49 | /* TODO: It might be required to check if the ontology in input is | ||
| 50 | * Horn-ALCHOIQ. At the moment we are assuming this is always the | ||
| 51 | * case. | ||
| 52 | */ | ||
| 53 | |||
| 54 | val ontology = RSAOntology(ontoPaths: _*) | 53 | val ontology = RSAOntology(ontoPaths: _*) |
| 55 | if (ontology.isRSA) { | 54 | if (ontology.isRSA) { |
| 56 | 55 | ||
| 57 | Logger print "Ontology is RSA!" | 56 | Logger print "Ontology is RSA!" |
| 58 | 57 | ||
| 59 | /** Read SPARQL query from file */ | 58 | val query = RDFoxUtil.loadQueryFromFile(queryPath.getAbsoluteFile) |
| 60 | val strQuery = RDFoxUtil.loadQueryFromFile(queryPath.getAbsoluteFile) | ||
| 61 | val query = ConjunctiveQuery parse strQuery | ||
| 62 | 59 | ||
| 63 | query match { | 60 | ConjunctiveQuery.parse(query) match { |
| 64 | case Some(query) => { | 61 | case Some(query) => { |
| 65 | val answers = ontology ask query | 62 | val answers = ontology ask query |
| 66 | Logger.print(s"$answers", Logger.QUIET) | 63 | Logger.print(s"$answers", Logger.QUIET) |
| 67 | Logger print s"Number of answer: ${answers.length} (${answers.lengthWithMultiplicity})" | 64 | Logger print s"Number of answers: ${answers.length} (${answers.lengthWithMultiplicity})" |
| 68 | 65 | ||
| 69 | val unfiltered = ontology askUnfiltered query | 66 | /* Additional DEBUG information */ |
| 70 | unfiltered map { u => | 67 | if (Logger.level >= Logger.DEBUG) { |
| 71 | Logger.print( | 68 | /* Unfiltered rules */ |
| 72 | s"Number of unfiltered answers: ${u.length} (${u.map(_._1).sum}).", | 69 | val unfiltered = ontology askUnfiltered query |
| 73 | Logger.DEBUG | 70 | unfiltered map { u => |
| 74 | ) | 71 | Logger print s"Number of unfiltered answers: ${u.length} (${u.map(_._1).sum})." |
| 75 | //u foreach println | 72 | |
| 76 | val spurious = { | 73 | /* Spurious answers */ |
| 77 | val sp = | 74 | val spurious = { |
| 78 | RDFoxUtil.buildDescriptionQuery("SP", query.variables.length) | 75 | val variables = query.variables.length |
| 79 | ontology.queryDataStore(query, sp, RSA.Prefixes) | 76 | val sp = RDFoxUtil.buildDescriptionQuery("SP", variables) |
| 80 | } | 77 | ontology.queryDataStore(query, sp, RSA.Prefixes) |
| 81 | spurious map { s => | 78 | } |
| 82 | Logger.print( | 79 | spurious map { s => |
| 83 | s"Number of spurious answers: ${s.length} (${s.map(_._1).sum})", | 80 | Logger print s"Number of spurious answers: ${s.length} (${s.map(_._1).sum})" |
| 84 | Logger.DEBUG | 81 | |
| 85 | ) | 82 | /* Spurious/unfiltered percentage */ |
| 86 | //s foreach println | 83 | val perc = |
| 87 | val perc = | 84 | if (u.length > 0) (s.length / u.length.toFloat) * 100 else 0 |
| 88 | if (u.length > 0) (s.length / u.length.toFloat) * 100 else 0 | 85 | Logger print s"Percentage of spurious answers: $perc%" |
| 89 | Logger.print( | 86 | } |
| 90 | s"Percentage of spurious answers: $perc%", | ||
| 91 | Logger.DEBUG | ||
| 92 | ) | ||
| 93 | } | 87 | } |
| 94 | |||
| 95 | } | 88 | } |
| 96 | } | 89 | } |
| 97 | case None => | 90 | case None => |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala index 4ac5a77..8d5bf4c 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
| @@ -114,9 +114,9 @@ class RSAOntology(val ontology: OWLOntology) { | |||
| 114 | 114 | ||
| 115 | val axioms: List[OWLLogicalAxiom] = abox ::: tbox ::: rbox | 115 | val axioms: List[OWLLogicalAxiom] = abox ::: tbox ::: rbox |
| 116 | 116 | ||
| 117 | Logger.print(s"Original TBox: ${tbox.length} axioms", Logger.DEBUG) | 117 | Logger.print(s"Original TBox: ${tbox.length}", Logger.DEBUG) |
| 118 | Logger.print(s"Original RBox: ${rbox.length} axioms", Logger.DEBUG) | 118 | Logger.print(s"Original RBox: ${rbox.length}", Logger.DEBUG) |
| 119 | Logger.print(s"Original ABox: ${abox.length} axioms", Logger.DEBUG) | 119 | Logger.print(s"Original ABox: ${abox.length}", Logger.DEBUG) |
| 120 | 120 | ||
| 121 | /* Retrieve individuals in the original ontology | 121 | /* Retrieve individuals in the original ontology |
| 122 | */ | 122 | */ |
| @@ -358,16 +358,18 @@ class RSAOntology(val ontology: OWLOntology) { | |||
| 358 | 358 | ||
| 359 | //data.beginTransaction(TransactionType.READ_WRITE) | 359 | //data.beginTransaction(TransactionType.READ_WRITE) |
| 360 | 360 | ||
| 361 | Logger print s"Canonical model: ${canon.rules.length} rules" | 361 | Logger print s"Canonical model rules: ${canon.rules.length}" |
| 362 | RDFoxUtil.addRules(data, this.canonicalModel.rules) | 362 | RDFoxUtil.addRules(data, this.canonicalModel.rules) |
| 363 | 363 | ||
| 364 | Logger print s"Canonical model: ${canon.facts.length} facts" | 364 | Logger print s"Canonical model facts: ${canon.facts.length}" |
| 365 | RDFoxUtil.addFacts(data, this.canonicalModel.facts) | 365 | RDFoxUtil.addFacts(data, this.canonicalModel.facts) |
| 366 | 366 | ||
| 367 | Logger print s"Filtering program: ${filter.facts.length} facts" | 367 | RDFoxUtil printStatisticsFor data |
| 368 | |||
| 369 | Logger print s"Filtering program facts: ${filter.facts.length}" | ||
| 368 | RDFoxUtil.addFacts(data, filter.facts) | 370 | RDFoxUtil.addFacts(data, filter.facts) |
| 369 | 371 | ||
| 370 | Logger print s"Filtering program: ${filter.rules.length} rules" | 372 | Logger print s"Filtering program rules: ${filter.rules.length}" |
| 371 | RDFoxUtil.addRules(data, filter.rules) | 373 | RDFoxUtil.addRules(data, filter.rules) |
| 372 | 374 | ||
| 373 | //data.commitTransaction() | 375 | //data.commitTransaction() |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/Logger.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/Logger.scala index 74797a2..56e9de0 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/Logger.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/Logger.scala | |||
| @@ -38,7 +38,7 @@ object Logger { | |||
| 38 | print(s"$desc (START)", lvl) | 38 | print(s"$desc (START)", lvl) |
| 39 | val result = expr | 39 | val result = expr |
| 40 | val t1 = System.currentTimeMillis() | 40 | val t1 = System.currentTimeMillis() |
| 41 | print(s"$desc (END): ${(t1 - t0).toFloat / 1000}s", lvl) | 41 | print(s"$desc (END): ${(t1 - t0).toFloat / 1000}", lvl) |
| 42 | result | 42 | result |
| 43 | } | 43 | } |
| 44 | 44 | ||
