aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/rsacomb/RSAAtom.scala
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2020-11-17 11:50:04 +0000
committerFederico Igne <federico.igne@cs.ox.ac.uk>2020-11-17 11:50:04 +0000
commite6c211b6a9c497b625710f7b97f793d1a796b461 (patch)
tree95903bb395decae347b83794a9fdb17df59188fc /src/main/scala/rsacomb/RSAAtom.scala
parente237c660994978588dea7c8d26043440986ba6df (diff)
downloadRSAComb-e6c211b6a9c497b625710f7b97f793d1a796b461.tar.gz
RSAComb-e6c211b6a9c497b625710f7b97f793d1a796b461.zip
Rename RDFTriple to RSAAtom
Diffstat (limited to 'src/main/scala/rsacomb/RSAAtom.scala')
-rw-r--r--src/main/scala/rsacomb/RSAAtom.scala88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main/scala/rsacomb/RSAAtom.scala b/src/main/scala/rsacomb/RSAAtom.scala
new file mode 100644
index 0000000..a65c168
--- /dev/null
+++ b/src/main/scala/rsacomb/RSAAtom.scala
@@ -0,0 +1,88 @@
1package rsacomb.implicits
2
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}
10import tech.oxfordsemantic.jrdfox.logic.expression.{IRI}
11import scala.collection.JavaConverters._
12
13import rsacomb.suffix.{RSASuffix, Nth}
14import rsacomb.RSA
15
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
18 * "consistent":
19 * - for an atom created with `rdf(<term1>, <term2>, <term3>)`,
20 * `getNumberOfArguments` returns 3
21 * - for an atom created with `Atom.create(<tupletablename>, <term1>,
22 * <term2>, <term3>)`, `getNumberOfArguments()` returns 3
23 *
24 * This is probably because `Atom.rdf(...) is implemented as:
25 * ```scala
26 * def rdf(term1: Term, term2: Term, term3: Term): Atom =
27 * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3)
28 * ```
29 */
30
31trait RSAAtom {
32
33 implicit class RSAAtom(val atom: TupleTableAtom) {
34
35 import rsacomb.RDFoxUtil.stringToRDFoxIRI
36
37 val name: String = atom.getTupleTableName.getName
38
39 val isRDF: Boolean = name == "internal:triple"
40
41 val isClassAssertion: Boolean = {
42 isRDF && {
43 val pred = atom.getArguments.get(1)
44 pred == IRI.RDF_TYPE
45 }
46 }
47
48 val isRoleAssertion: Boolean = isRDF && !isClassAssertion
49
50 def <<(suffix: RSASuffix): TupleTableAtom =
51 if (isRDF) {
52 val subj = atom.getArguments.get(0)
53 val pred = atom.getArguments.get(1)
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)
67 }
68 } else {
69 val ttname = TupleTableName.create(name :: suffix)
70 TupleTableAtom.create(ttname, 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)
85 }
86 }
87
88}