diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-19 15:06:33 +0100 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-19 15:21:37 +0100 |
commit | bd2ab8c75be9ad7f8d0442fc86bb1bd7b7ee229a (patch) | |
tree | 631cfa755afc5fc6b77917bbb49b5532e6a203a2 /src | |
parent | e2d78d7b3314104d681be8ba07427a2cbfa12824 (diff) | |
download | RSAComb-bd2ab8c75be9ad7f8d0442fc86bb1bd7b7ee229a.tar.gz RSAComb-bd2ab8c75be9ad7f8d0442fc86bb1bd7b7ee229a.zip |
refactor(rdfox-dsl): simplify creation of Datalog rules
Diffstat (limited to 'src')
4 files changed, 94 insertions, 14 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/FilteringProgram.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/FilteringProgram.scala index c732784..957784b 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/FilteringProgram.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/FilteringProgram.scala | |||
@@ -21,16 +21,6 @@ import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Variable} | |||
21 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery | 21 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery |
22 | import uk.ac.ox.cs.rsacomb.util.Versioned | 22 | import uk.ac.ox.cs.rsacomb.util.Versioned |
23 | 23 | ||
24 | object RDFoxDSL { | ||
25 | |||
26 | import scala.collection.JavaConverters._ | ||
27 | |||
28 | implicit class MyVariable(private val str: StringContext) extends AnyVal { | ||
29 | def v(args: Any*): Variable = Variable.create(s"${str.s(args: _*)}") | ||
30 | } | ||
31 | |||
32 | } | ||
33 | |||
34 | /** Type of filtering strategy. | 24 | /** Type of filtering strategy. |
35 | * | 25 | * |
36 | * Mainly for testing different approaches and techniques. | 26 | * Mainly for testing different approaches and techniques. |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram.scala index 541fdff..85794b0 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram.scala | |||
@@ -35,7 +35,7 @@ import tech.oxfordsemantic.jrdfox.logic.expression.{ | |||
35 | } | 35 | } |
36 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery | 36 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery |
37 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} | 37 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} |
38 | import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxUtil} | 38 | import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxUtil, RDFoxDSL} |
39 | 39 | ||
40 | /** Factory for [[uk.ac.ox.cs.rsacomb.FilteringProgram FilteringProgram]] */ | 40 | /** Factory for [[uk.ac.ox.cs.rsacomb.FilteringProgram FilteringProgram]] */ |
41 | object RevisedFilteringProgram { | 41 | object RevisedFilteringProgram { |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram2.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram2.scala index a282d8a..3d70b03 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram2.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/filtering/RevisedFilteringProgram2.scala | |||
@@ -66,7 +66,7 @@ class RevisedFilteringProgram2( | |||
66 | val query: ConjunctiveQuery | 66 | val query: ConjunctiveQuery |
67 | ) extends FilteringProgram { | 67 | ) extends FilteringProgram { |
68 | 68 | ||
69 | import RDFoxDSL._ | 69 | import uk.ac.ox.cs.rsacomb.util.RDFoxDSL._ |
70 | 70 | ||
71 | /** Extends capabilities of | 71 | /** Extends capabilities of |
72 | * [[tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom TupleTableAtom]] | 72 | * [[tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom TupleTableAtom]] |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala index 6e10b30..81702f5 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala | |||
@@ -18,6 +18,7 @@ package uk.ac.ox.cs.rsacomb.util | |||
18 | 18 | ||
19 | import java.io.{OutputStream, File, StringReader} | 19 | import java.io.{OutputStream, File, StringReader} |
20 | import scala.collection.JavaConverters._ | 20 | import scala.collection.JavaConverters._ |
21 | import org.semanticweb.owlapi.model.OWLLogicalAxiom | ||
21 | import tech.oxfordsemantic.jrdfox.Prefixes | 22 | import tech.oxfordsemantic.jrdfox.Prefixes |
22 | import tech.oxfordsemantic.jrdfox.client.{ | 23 | import tech.oxfordsemantic.jrdfox.client.{ |
23 | ComponentInfo, | 24 | ComponentInfo, |
@@ -47,6 +48,68 @@ import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery | |||
47 | import uk.ac.ox.cs.rsacomb.suffix.Nth | 48 | import uk.ac.ox.cs.rsacomb.suffix.Nth |
48 | import uk.ac.ox.cs.rsacomb.util.Logger | 49 | import uk.ac.ox.cs.rsacomb.util.Logger |
49 | 50 | ||
51 | object RDFoxDSL { | ||
52 | |||
53 | import scala.collection.JavaConverters._ | ||
54 | import tech.oxfordsemantic.jrdfox.logic.datalog._ | ||
55 | //import tech.oxfordsemantic.jrdfox.logic.expression._ | ||
56 | |||
57 | |||
58 | /* String contexts */ | ||
59 | implicit class MyVariable(private val str: StringContext) extends AnyVal { | ||
60 | def v(args: Any*): Variable = Variable.create(s"${str.s(args: _*)}") | ||
61 | } | ||
62 | implicit class MyIRI(private val str: StringContext) extends AnyVal { | ||
63 | def i(args: Any*): IRI = IRI.create(s"${str.s(args: _*)}") | ||
64 | } | ||
65 | |||
66 | |||
67 | /** Sequence of [[TupleTableAtom]]s (a.k.a. head of a rule) */ | ||
68 | case class SeqTupleTableAtoms(atoms: TupleTableAtom*) { | ||
69 | /** Atoms conjunction */ | ||
70 | def +(atom: TupleTableAtom): SeqTupleTableAtoms = | ||
71 | SeqTupleTableAtoms(atoms :+ atom: _*) | ||
72 | /** Implication (rule constructor) */ | ||
73 | def :-(body: SeqBodyFormulas): Rule = { | ||
74 | Rule.create(atoms.asJava, body.atoms.asJava) | ||
75 | } | ||
76 | } | ||
77 | implicit def seqTupleTableAtoms(atom: TupleTableAtom): SeqTupleTableAtoms = | ||
78 | SeqTupleTableAtoms(atom) | ||
79 | |||
80 | /** Sequence of [[BodyFormula]]e (a.k.a. body) */ | ||
81 | case class SeqBodyFormulas(atoms: BodyFormula*) { | ||
82 | /** Atoms conjunction */ | ||
83 | def +(atom: BodyFormula): SeqBodyFormulas = SeqBodyFormulas(atoms :+ atom: _*) | ||
84 | } | ||
85 | implicit def seqBodyFormulas(atom: BodyFormula): SeqBodyFormulas = | ||
86 | SeqBodyFormulas(atom) | ||
87 | implicit def atoms2bodyFormulas(ttas: SeqTupleTableAtoms): SeqBodyFormulas = | ||
88 | SeqBodyFormulas(ttas.atoms: _*) | ||
89 | |||
90 | |||
91 | |||
92 | |||
93 | trait RDFoxPredicate { | ||
94 | def apply(terms: Term*): TupleTableAtom | ||
95 | } | ||
96 | |||
97 | implicit class RDFoxGraph(val tt: IRI) extends RDFoxPredicate { | ||
98 | def apply(terms: Term*): TupleTableAtom = | ||
99 | TupleTableAtom.create(TupleTableName.create(tt.getIRI), terms:_*) | ||
100 | } | ||
101 | //class RDFoxTerm() extends RDFoxPredicate {} | ||
102 | |||
103 | //implicit class RDFoxPrefix(val name: String) { | ||
104 | // def ::(prefix: String): IRI = IRI.create(prefix + name) | ||
105 | //} | ||
106 | |||
107 | //val owlpre = "http://example.com/owl#" | ||
108 | //val prova = TupleTableAtom.rdf(v"x", v"y", v"z") & TupleTableAtom.rdf(v"x", v"y", v"z") | ||
109 | //val prova2 = i"iri_di_prova"(v"x", v"y", v"z") & owlpre::"subClassOf"(v"x", v"y", v"z") | ||
110 | |||
111 | } | ||
112 | |||
50 | /** A collection of helper methods for RDFox */ | 113 | /** A collection of helper methods for RDFox */ |
51 | object RDFoxUtil { | 114 | object RDFoxUtil { |
52 | 115 | ||
@@ -62,7 +125,7 @@ object RDFoxUtil { | |||
62 | * [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.Query]]. | 125 | * [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.Query]]. |
63 | */ | 126 | */ |
64 | private type QueryAnswers = Seq[(Long, Seq[Resource])] | 127 | private type QueryAnswers = Seq[(Long, Seq[Resource])] |
65 | private def QueryAnswers() = List.empty[(Long, Seq[Resource])] | 128 | def QueryAnswers() = List.empty[(Long, Seq[Resource])] |
66 | 129 | ||
67 | /** Type alias for <option => value> RDFox options. */ | 130 | /** Type alias for <option => value> RDFox options. */ |
68 | private type RDFoxOpts = java.util.Map[String, String] | 131 | private type RDFoxOpts = java.util.Map[String, String] |
@@ -180,6 +243,33 @@ object RDFoxUtil { | |||
180 | Logger.DEBUG | 243 | Logger.DEBUG |
181 | ) | 244 | ) |
182 | 245 | ||
246 | /** Adds a collection of facts to a data store. | ||
247 | * | ||
248 | * @param data datastore connection | ||
249 | * @param facts collection of facts to be added to the data store | ||
250 | */ | ||
251 | def addAxioms( | ||
252 | data: DataStoreConnection, | ||
253 | gsource: IRI, | ||
254 | gtarget: IRI, | ||
255 | axioms: Seq[OWLLogicalAxiom] | ||
256 | ): Unit = | ||
257 | Logger.timed( | ||
258 | if (axioms.length > 0) { | ||
259 | data.importData( | ||
260 | gsource.getIRI, | ||
261 | UpdateType.ADDITION, | ||
262 | RSA.Prefixes, | ||
263 | axioms | ||
264 | .map(_.toString) | ||
265 | .mkString("Ontology(\n","\n",")") | ||
266 | ) | ||
267 | data.importAxiomsFromTriples(gsource.getIRI, true, gtarget.getIRI, UpdateType.ADDITION) | ||
268 | }, | ||
269 | s"Loading ${axioms.length} axioms", | ||
270 | Logger.DEBUG | ||
271 | ) | ||
272 | |||
183 | /** Imports a sequence of files directly into a datastore. | 273 | /** Imports a sequence of files directly into a datastore. |
184 | * | 274 | * |
185 | * @param data datastore connection. | 275 | * @param data datastore connection. |
@@ -376,7 +466,7 @@ object RDFoxUtil { | |||
376 | def submitQuery( | 466 | def submitQuery( |
377 | data: DataStoreConnection, | 467 | data: DataStoreConnection, |
378 | query: String, | 468 | query: String, |
379 | prefixes: Prefixes = new Prefixes(), | 469 | prefixes: Prefixes = RSA.Prefixes, |
380 | opts: RDFoxOpts = RDFoxOpts() | 470 | opts: RDFoxOpts = RDFoxOpts() |
381 | ): Option[QueryAnswers] = | 471 | ): Option[QueryAnswers] = |
382 | parseSelectQuery(query, prefixes).map(submitSelectQuery(data, _, opts)) | 472 | parseSelectQuery(query, prefixes).map(submitSelectQuery(data, _, opts)) |