From e237c660994978588dea7c8d26043440986ba6df Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 17 Nov 2020 11:45:55 +0000 Subject: Unify use of RSASuffix This commit comes with minor fixes and code simplifications. --- src/main/scala/rsacomb/RDFTriple.scala | 131 ++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 52 deletions(-) (limited to 'src/main/scala/rsacomb/RDFTriple.scala') 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 @@ -package rsacomb +package rsacomb.implicits -import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName} +import tech.oxfordsemantic.jrdfox.logic.Datatype +import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall} +import tech.oxfordsemantic.jrdfox.logic.datalog.{ + BindAtom, + TupleTableAtom, + TupleTableName +} import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} +import scala.collection.JavaConverters._ -trait RDFTriple { - - implicit class RDFTriple(atom: TupleTableAtom) { - - /* Is this the best way to determine if an atom is an RDF triple? - * Note that we can't use `getNumberOfArguments()` because is not - * "consistent": - * - for an atom created with `rdf(, , )`, - * `getNumberOfArguments` returns 3 - * - for an atom created with `Atom.create(, , - * , )`, `getNumberOfArguments()` returns 3 - * - * This is probably because `Atom.rdf(...) is implemented as: - * ```scala - * def rdf(term1: Term, term2: Term, term3: Term): Atom = - * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) - * ``` - */ - def isRdfTriple: Boolean = - atom.getTupleTableName.getName.equals("internal:triple") - - def isClassAssertion: Boolean = - atom.isRdfTriple && atom.getArguments.get(1).equals(IRI.RDF_TYPE) - - def isRoleAssertion: Boolean = - atom.isRdfTriple && !atom.getArguments.get(1).equals(IRI.RDF_TYPE) - - def suffix(sx: String): TupleTableAtom = - if (this.isClassAssertion) { - val newclass = atom.getArguments.get(2) match { - case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") - case other => other - } - TupleTableAtom.rdf( - atom.getArguments.get(0), - atom.getArguments.get(1), - newclass - ) - } else if (this.isRoleAssertion) { - val newrole = atom.getArguments.get(1) match { - case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") - case other => other +import rsacomb.suffix.{RSASuffix, Nth} +import rsacomb.RSA + +/* Is this the best way to determine if an atom is an RDF triple? + * Note that we can't use `getNumberOfArguments()` because is not + * "consistent": + * - for an atom created with `rdf(, , )`, + * `getNumberOfArguments` returns 3 + * - for an atom created with `Atom.create(, , + * , )`, `getNumberOfArguments()` returns 3 + * + * This is probably because `Atom.rdf(...) is implemented as: + * ```scala + * def rdf(term1: Term, term2: Term, term3: Term): Atom = + * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) + * ``` + */ + +trait RSAAtom { + + implicit class RSAAtom(val atom: TupleTableAtom) { + + import rsacomb.RDFoxUtil.stringToRDFoxIRI + + val name: String = atom.getTupleTableName.getName + + val isRDF: Boolean = name == "internal:triple" + + val isClassAssertion: Boolean = { + isRDF && { + val pred = atom.getArguments.get(1) + pred == IRI.RDF_TYPE + } + } + + val isRoleAssertion: Boolean = isRDF && !isClassAssertion + + def <<(suffix: RSASuffix): TupleTableAtom = + if (isRDF) { + val subj = atom.getArguments.get(0) + val pred = atom.getArguments.get(1) + val obj = atom.getArguments.get(2) + if (isClassAssertion) { + val obj1 = obj match { + case iri: IRI => IRI.create(iri.getIRI :: suffix) + case other => other + } + TupleTableAtom.rdf(subj, pred, obj1) + } else { + val pred1 = pred match { + case iri: IRI => IRI.create(iri.getIRI :: suffix) + case other => other + } + TupleTableAtom.rdf(subj, pred1, obj) } - TupleTableAtom.rdf( - atom.getArguments.get(0), - newrole, - atom.getArguments.get(2) - ) } else { - val newname = - TupleTableName.create(s"${atom.getTupleTableName.getName}_$sx") - TupleTableAtom.create(newname, atom.getArguments()) + val ttname = TupleTableName.create(name :: suffix) + TupleTableAtom.create(ttname, atom.getArguments()) + } + + lazy val reified: (Option[BindAtom], List[TupleTableAtom]) = + if (isRDF) { + (None, List(atom)) + } else { + val bvar = RSA.getFreshVariable() + val str = Literal.create(name, Datatype.XSD_STRING) + val args = atom.getArguments.asScala.toList + val skolem = FunctionCall.create("SKOLEM", str :: args: _*) + val bind = BindAtom.create(skolem, bvar) + val atoms = args.zipWithIndex + .map { case (t, i) => TupleTableAtom.rdf(bvar, name :: Nth(i), t) } + (Some(bind), atoms) } } -- cgit v1.2.3