aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/rsacomb/RDFTriple.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/rsacomb/RDFTriple.scala')
-rw-r--r--src/main/scala/rsacomb/RDFTriple.scala131
1 files changed, 79 insertions, 52 deletions
diff --git a/src/main/scala/rsacomb/RDFTriple.scala b/src/main/scala/rsacomb/RDFTriple.scala
index 4054d42..a65c168 100644
--- a/src/main/scala/rsacomb/RDFTriple.scala
+++ b/src/main/scala/rsacomb/RDFTriple.scala
@@ -1,60 +1,87 @@
1package rsacomb 1package rsacomb.implicits
2 2
3import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName} 3import tech.oxfordsemantic.jrdfox.logic.Datatype
4import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall}
5import tech.oxfordsemantic.jrdfox.logic.datalog.{
6 BindAtom,
7 TupleTableAtom,
8 TupleTableName
9}
4import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} 10import tech.oxfordsemantic.jrdfox.logic.expression.{IRI}
11import scala.collection.JavaConverters._
5 12
6trait RDFTriple { 13import rsacomb.suffix.{RSASuffix, Nth}
7 14import rsacomb.RSA
8 implicit class RDFTriple(atom: TupleTableAtom) { 15
9 16/* Is this the best way to determine if an atom is an RDF triple?
10 /* 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
11 * Note that we can't use `getNumberOfArguments()` because is not 18 * "consistent":
12 * "consistent": 19 * - for an atom created with `rdf(<term1>, <term2>, <term3>)`,
13 * - for an atom created with `rdf(<term1>, <term2>, <term3>)`, 20 * `getNumberOfArguments` returns 3
14 * `getNumberOfArguments` returns 3 21 * - for an atom created with `Atom.create(<tupletablename>, <term1>,
15 * - for an atom created with `Atom.create(<tupletablename>, <term1>, 22 * <term2>, <term3>)`, `getNumberOfArguments()` returns 3
16 * <term2>, <term3>)`, `getNumberOfArguments()` returns 3 23 *
17 * 24 * This is probably because `Atom.rdf(...) is implemented as:
18 * This is probably because `Atom.rdf(...) is implemented as: 25 * ```scala
19 * ```scala 26 * def rdf(term1: Term, term2: Term, term3: Term): Atom =
20 * def rdf(term1: Term, term2: Term, term3: Term): Atom = 27 * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3)
21 * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) 28 * ```
22 * ``` 29 */
23 */ 30
24 def isRdfTriple: Boolean = 31trait RSAAtom {
25 atom.getTupleTableName.getName.equals("internal:triple") 32
26 33 implicit class RSAAtom(val atom: TupleTableAtom) {
27 def isClassAssertion: Boolean = 34
28 atom.isRdfTriple && atom.getArguments.get(1).equals(IRI.RDF_TYPE) 35 import rsacomb.RDFoxUtil.stringToRDFoxIRI
29 36
30 def isRoleAssertion: Boolean = 37 val name: String = atom.getTupleTableName.getName
31 atom.isRdfTriple && !atom.getArguments.get(1).equals(IRI.RDF_TYPE) 38
32 39 val isRDF: Boolean = name == "internal:triple"
33 def suffix(sx: String): TupleTableAtom = 40
34 if (this.isClassAssertion) { 41 val isClassAssertion: Boolean = {
35 val newclass = atom.getArguments.get(2) match { 42 isRDF && {
36 case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") 43 val pred = atom.getArguments.get(1)
37 case other => other 44 pred == IRI.RDF_TYPE
38 } 45 }
39 TupleTableAtom.rdf( 46 }
40 atom.getArguments.get(0), 47
41 atom.getArguments.get(1), 48 val isRoleAssertion: Boolean = isRDF && !isClassAssertion
42 newclass 49
43 ) 50 def <<(suffix: RSASuffix): TupleTableAtom =
44 } else if (this.isRoleAssertion) { 51 if (isRDF) {
45 val newrole = atom.getArguments.get(1) match { 52 val subj = atom.getArguments.get(0)
46 case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") 53 val pred = atom.getArguments.get(1)
47 case other => other 54 val obj = atom.getArguments.get(2)
55 if (isClassAssertion) {
56 val obj1 = obj match {
57 case iri: IRI => IRI.create(iri.getIRI :: suffix)
58 case other => other
59 }
60 TupleTableAtom.rdf(subj, pred, obj1)
61 } else {
62 val pred1 = pred match {
63 case iri: IRI => IRI.create(iri.getIRI :: suffix)
64 case other => other
65 }
66 TupleTableAtom.rdf(subj, pred1, obj)
48 } 67 }
49 TupleTableAtom.rdf(
50 atom.getArguments.get(0),
51 newrole,
52 atom.getArguments.get(2)
53 )
54 } else { 68 } else {
55 val newname = 69 val ttname = TupleTableName.create(name :: suffix)
56 TupleTableName.create(s"${atom.getTupleTableName.getName}_$sx") 70 TupleTableAtom.create(ttname, atom.getArguments())
57 TupleTableAtom.create(newname, atom.getArguments()) 71 }
72
73 lazy val reified: (Option[BindAtom], List[TupleTableAtom]) =
74 if (isRDF) {
75 (None, List(atom))
76 } else {
77 val bvar = RSA.getFreshVariable()
78 val str = Literal.create(name, Datatype.XSD_STRING)
79 val args = atom.getArguments.asScala.toList
80 val skolem = FunctionCall.create("SKOLEM", str :: args: _*)
81 val bind = BindAtom.create(skolem, bvar)
82 val atoms = args.zipWithIndex
83 .map { case (t, i) => TupleTableAtom.rdf(bvar, name :: Nth(i), t) }
84 (Some(bind), atoms)
58 } 85 }
59 } 86 }
60 87