aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/uk/ac/ox/cs
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2020-12-11 17:54:21 +0000
committerFederico Igne <federico.igne@cs.ox.ac.uk>2020-12-17 17:44:11 +0000
commit548ad06610dabeafec983449da597f1279bf6def (patch)
treeba00752cd6c0d536ec1a2100abe48e6d62b42f33 /src/main/scala/uk/ac/ox/cs
parent12ff20661c1848345473a5a4a9c200f3e7c2939a (diff)
downloadRSAComb-548ad06610dabeafec983449da597f1279bf6def.tar.gz
RSAComb-548ad06610dabeafec983449da597f1279bf6def.zip
Port code to RDFox v4.1.0
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs')
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala12
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala3
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala29
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala51
4 files changed, 61 insertions, 34 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala
index b4f5adb..845ec1f 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala
@@ -40,7 +40,7 @@ import tech.oxfordsemantic.jrdfox.logic.datalog.{
40import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, FunctionCall} 40import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, FunctionCall}
41import uk.ac.ox.cs.rsacomb.RSAOntology 41import uk.ac.ox.cs.rsacomb.RSAOntology
42import uk.ac.ox.cs.rsacomb.suffix.{Empty, Inverse, RSASuffix} 42import uk.ac.ox.cs.rsacomb.suffix.{Empty, Inverse, RSASuffix}
43import uk.ac.ox.cs.rsacomb.util.RSA 43import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxUtil}
44 44
45/** Horn-ALCHOIQ to RDFox axiom converter. 45/** Horn-ALCHOIQ to RDFox axiom converter.
46 * 46 *
@@ -328,10 +328,7 @@ trait RDFoxConverter {
328 val (bind, term1) = skolem match { 328 val (bind, term1) = skolem match {
329 case NoSkolem => (None, varX) 329 case NoSkolem => (None, varX)
330 case c: Constant => (None, c.iri) 330 case c: Constant => (None, c.iri)
331 case s: Standard => { 331 case s: Standard => (Some(RDFoxUtil.skolem(s.name, term, varX)), varX)
332 val func = FunctionCall.create("SKOLEM", s.literal, term)
333 (Some(BindAtom.create(func, varX)), varX)
334 }
335 } 332 }
336 val (res, ext) = convert(cls, term1, unsafe, skolem, suffix) 333 val (res, ext) = convert(cls, term1, unsafe, skolem, suffix)
337 val prop = convert(role, term, term1, suffix) 334 val prop = convert(role, term, term1, suffix)
@@ -359,10 +356,7 @@ trait RDFoxConverter {
359 val (bind, term1) = skolem match { 356 val (bind, term1) = skolem match {
360 case NoSkolem => (None, varX) 357 case NoSkolem => (None, varX)
361 case c: Constant => (None, c.iri) 358 case c: Constant => (None, c.iri)
362 case s: Standard => { 359 case s: Standard => (Some(RDFoxUtil.skolem(s.name, term, varX)), varX)
363 val func = FunctionCall.create("SKOLEM", s.literal, term)
364 (Some(BindAtom.create(func, varX)), varX)
365 }
366 } 360 }
367 val prop = convert(role, term, term1, suffix) 361 val prop = convert(role, term, term1, suffix)
368 (List(prop), bind.toList) 362 (List(prop), bind.toList)
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala
index 2142ff3..426a327 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala
@@ -53,8 +53,7 @@ case object NoSkolem extends SkolemStrategy {
53case class Standard(axiom: OWLAxiom)(implicit toString: (OWLAxiom) => String) 53case class Standard(axiom: OWLAxiom)(implicit toString: (OWLAxiom) => String)
54 extends SkolemStrategy { 54 extends SkolemStrategy {
55 def dup(_axiom: OWLAxiom): Standard = copy(axiom = _axiom)(toString) 55 def dup(_axiom: OWLAxiom): Standard = copy(axiom = _axiom)(toString)
56 lazy val literal = 56 lazy val name = s"f_${toString(axiom)}"
57 Literal.create(s"f_${toString(axiom)}", Datatype.XSD_STRING)
58} 57}
59 58
60/** Constant skolemization 59/** Constant skolemization
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala
index 615722b..9b04f0e 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala
@@ -1,17 +1,17 @@
1package uk.ac.ox.cs.rsacomb.implicits 1package uk.ac.ox.cs.rsacomb.implicits
2 2
3import tech.oxfordsemantic.jrdfox.logic.Datatype 3import tech.oxfordsemantic.jrdfox.logic.Datatype
4import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall} 4import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall, Term}
5import tech.oxfordsemantic.jrdfox.logic.datalog.{ 5import tech.oxfordsemantic.jrdfox.logic.datalog.{
6 BindAtom, 6 BindAtom,
7 TupleTableAtom, 7 TupleTableAtom,
8 TupleTableName 8 TupleTableName
9} 9}
10import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} 10import tech.oxfordsemantic.jrdfox.logic.expression.{IRI}
11import scala.collection.JavaConverters._
12 11
13import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Nth}
14import uk.ac.ox.cs.rsacomb.RSAOntology 12import uk.ac.ox.cs.rsacomb.RSAOntology
13import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Nth}
14import uk.ac.ox.cs.rsacomb.util.RDFoxUtil
15 15
16/* Is this the best way to determine if an atom is an RDF triple? 16/* Is this the best way to determine if an atom is an RDF triple?
17 * Note that we can't use `getNumberOfArguments()` because is not 17 * Note that we can't use `getNumberOfArguments()` because is not
@@ -24,7 +24,7 @@ import uk.ac.ox.cs.rsacomb.RSAOntology
24 * This is probably because `Atom.rdf(...) is implemented as: 24 * This is probably because `Atom.rdf(...) is implemented as:
25 * ```scala 25 * ```scala
26 * def rdf(term1: Term, term2: Term, term3: Term): Atom = 26 * def rdf(term1: Term, term2: Term, term3: Term): Atom =
27 * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) 27 * Atom.create(TupleTableName.create("rdfox:DefaultTriples"), term1, term2, term3)
28 * ``` 28 * ```
29 */ 29 */
30 30
@@ -33,10 +33,14 @@ object RSAAtom {
33 implicit class RSAAtom(val atom: TupleTableAtom) { 33 implicit class RSAAtom(val atom: TupleTableAtom) {
34 34
35 import RDFox._ 35 import RDFox._
36 import JavaCollections._
36 37
37 val name: String = atom.getTupleTableName.getName 38 val name: String = atom.getTupleTableName.getName
38 39
39 val isRDF: Boolean = name == "internal:triple" 40 val args: List[Term] = atom.getArguments
41
42 val isRDF: Boolean =
43 name == "http://oxfordsemantic.tech/RDFox#DefaultTriples"
40 44
41 val isClassAssertion: Boolean = { 45 val isClassAssertion: Boolean = {
42 isRDF && { 46 isRDF && {
@@ -70,19 +74,16 @@ object RSAAtom {
70 TupleTableAtom.create(ttname, atom.getArguments()) 74 TupleTableAtom.create(ttname, atom.getArguments())
71 } 75 }
72 76
73 lazy val reified: (Option[BindAtom], List[TupleTableAtom]) = 77 lazy val reified: (Option[TupleTableAtom], List[TupleTableAtom]) =
74 if (isRDF) { 78 if (isRDF) {
75 (None, List(atom)) 79 (None, List(atom))
76 } else { 80 } else {
77 val bvar = RSAOntology.genFreshVariable() 81 val varS = RSAOntology.genFreshVariable()
78 val str = Literal.create(name, Datatype.XSD_STRING) 82 val skolem = RDFoxUtil.skolem(name, (args :+ varS): _*)
79 val args = atom.getArguments.asScala.toList 83 val atom = TupleTableAtom.rdf(varS, IRI.RDF_TYPE, name)
80 val skolem = FunctionCall.create("SKOLEM", str :: args: _*)
81 val bind = BindAtom.create(skolem, bvar)
82 val atoms = args.zipWithIndex 84 val atoms = args.zipWithIndex
83 .map { case (t, i) => TupleTableAtom.rdf(bvar, name :: Nth(i), t) } 85 .map { case (a, i) => TupleTableAtom.rdf(varS, name :: Nth(i), a) }
84 (Some(bind), atoms) 86 (Some(skolem), atom :: atoms)
85 } 87 }
86 } 88 }
87
88} 89}
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 68a23a3..d072e48 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
@@ -1,7 +1,6 @@
1package uk.ac.ox.cs.rsacomb.util 1package uk.ac.ox.cs.rsacomb.util
2 2
3import java.io.File 3import java.io.{OutputStream, File, StringReader}
4import java.io.StringReader
5import tech.oxfordsemantic.jrdfox.Prefixes 4import tech.oxfordsemantic.jrdfox.Prefixes
6import tech.oxfordsemantic.jrdfox.client.{ 5import tech.oxfordsemantic.jrdfox.client.{
7 ComponentInfo, 6 ComponentInfo,
@@ -11,6 +10,7 @@ import tech.oxfordsemantic.jrdfox.client.{
11 UpdateType 10 UpdateType
12} 11}
13import tech.oxfordsemantic.jrdfox.formats.SPARQLParser 12import tech.oxfordsemantic.jrdfox.formats.SPARQLParser
13import tech.oxfordsemantic.jrdfox.logic.Datatype
14import tech.oxfordsemantic.jrdfox.logic.datalog.{ 14import tech.oxfordsemantic.jrdfox.logic.datalog.{
15 Rule, 15 Rule,
16 BodyFormula, 16 BodyFormula,
@@ -18,7 +18,12 @@ import tech.oxfordsemantic.jrdfox.logic.datalog.{
18 TupleTableAtom, 18 TupleTableAtom,
19 TupleTableName 19 TupleTableName
20} 20}
21import tech.oxfordsemantic.jrdfox.logic.expression.{Resource} 21import tech.oxfordsemantic.jrdfox.logic.expression.{
22 Literal,
23 Resource,
24 Variable,
25 Term
26}
22import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery 27import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery
23import uk.ac.ox.cs.rsacomb.suffix.Nth 28import uk.ac.ox.cs.rsacomb.suffix.Nth
24import uk.ac.ox.cs.rsacomb.util.Logger 29import uk.ac.ox.cs.rsacomb.util.Logger
@@ -69,6 +74,13 @@ object RDFoxUtil {
69 (server, data) 74 (server, data)
70 } 75 }
71 76
77 /** Create a built-in `rdfox:SKOLEM` TupleTableAtom. */
78 def skolem(name: String, terms: Term*): TupleTableAtom =
79 TupleTableAtom.create(
80 TupleTableName.SKOLEM,
81 (Literal.create(name, Datatype.XSD_STRING) +: terms): _*
82 )
83
72 /** Prints statistics from RDFox datastore. 84 /** Prints statistics from RDFox datastore.
73 * 85 *
74 * Prints something only when Logger level is set to DEBUG or more. 86 * Prints something only when Logger level is set to DEBUG or more.
@@ -127,6 +139,26 @@ object RDFoxUtil {
127 query 139 query
128 } 140 }
129 141
142 /** Export data in `text/turtle`.
143 *
144 * @param data datastore connection from which to export data.
145 * @param rules output stream for rules
146 * @param facts output stream for facts
147 */
148 def export(
149 data: DataStoreConnection,
150 rules: OutputStream,
151 facts: OutputStream
152 ): Unit = {
153 data.exportData(Prefixes.s_emptyPrefixes, facts, "text/turtle", RDFoxOpts())
154 data.exportData(
155 Prefixes.s_emptyPrefixes,
156 rules,
157 "application/x.datalog",
158 RDFoxOpts()
159 )
160 }
161
130 /** Parse a SELECT query from a string in SPARQL format. 162 /** Parse a SELECT query from a string in SPARQL format.
131 * 163 *
132 * @param query the string containing the SPARQL query 164 * @param query the string containing the SPARQL query
@@ -231,11 +263,11 @@ object RDFoxUtil {
231 * built-in `SKOLEM` funtion of RDFox. 263 * built-in `SKOLEM` funtion of RDFox.
232 */ 264 */
233 def reify(rule: Rule): Rule = { 265 def reify(rule: Rule): Rule = {
234 val (bs, as) = rule.getHead.map(_.reified).unzip 266 val (sk, as) = rule.getHead.map(_.reified).unzip
235 val head: List[TupleTableAtom] = as.flatten 267 val head: List[TupleTableAtom] = as.flatten
236 val bind: List[BodyFormula] = bs.flatten 268 val skolem: List[BodyFormula] = sk.flatten
237 val body: List[BodyFormula] = rule.getBody.map(reify).flatten 269 val body: List[BodyFormula] = rule.getBody.map(reify).flatten
238 Rule.create(head, bind ::: body) 270 Rule.create(head, skolem ::: body)
239 } 271 }
240 272
241 /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.BodyFormula BodyFormula]]. 273 /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.BodyFormula BodyFormula]].
@@ -250,15 +282,16 @@ object RDFoxUtil {
250 formula match { 282 formula match {
251 case atom: TupleTableAtom => atom.reified._2 283 case atom: TupleTableAtom => atom.reified._2
252 case neg: Negation => { 284 case neg: Negation => {
253 val (bs, as) = neg.getNegatedAtoms 285 val (sk, as) = neg.getNegatedAtoms
254 .map({ 286 .map({
255 case a: TupleTableAtom => a.reified 287 case a: TupleTableAtom => a.reified
256 case a => (None, List(a)) 288 case a => (None, List(a))
257 }) 289 })
258 .unzip 290 .unzip
259 val bind = bs.flatten.map(_.getBoundVariable) 291 val skolem =
292 sk.flatten.map(_.getArguments.last).collect { case v: Variable => v }
260 val atoms = as.flatten 293 val atoms = as.flatten
261 List(Negation.create(bind, atoms)) 294 List(Negation.create(skolem, atoms))
262 } 295 }
263 case other => List(other) 296 case other => List(other)
264 } 297 }