From d99c80db73e8456c969b262a4b99714bb693bfe0 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 15 May 2022 13:16:38 +0100 Subject: Add compatibility layer between RSAComb and PAGOdA query answers --- .../cs/acqua/implicits/RSACombAnswerTuples.scala | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/main/scala/uk/ac/ox/cs/acqua/implicits/RSACombAnswerTuples.scala (limited to 'src/main/scala/uk/ac/ox') diff --git a/src/main/scala/uk/ac/ox/cs/acqua/implicits/RSACombAnswerTuples.scala b/src/main/scala/uk/ac/ox/cs/acqua/implicits/RSACombAnswerTuples.scala new file mode 100644 index 0000000..4f19e62 --- /dev/null +++ b/src/main/scala/uk/ac/ox/cs/acqua/implicits/RSACombAnswerTuples.scala @@ -0,0 +1,146 @@ +/* + * Copyright 2021,2022 KRR Oxford + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.ac.ox.cs.acqua.implicits + +import uk.ac.ox.cs.JRDFox.model.{ + BlankNode => OldBlankNode, + Datatype => OldDatatype, + Individual => OldIndividual, + Literal => OldLiteral, +} +import tech.oxfordsemantic.jrdfox.logic.Datatype +import tech.oxfordsemantic.jrdfox.logic.expression.{ + BlankNode, + IRI, + Literal, + Resource +} +import uk.ac.ox.cs.pagoda.query.{AnswerTuple,AnswerTuples} +import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers + +/** Implicit wrapper around [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers]] + * + * It implicitly converts a [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers]] + * into a [[uk.ac.ox.cs.pagoda.query.AnswerTuples]] to maintain + * compatibility betweren RSAComb and PAGOdA. + */ +object RSACombAnswerTuples { + + implicit class RSACombAnswerTuples( + val answers: ConjunctiveQueryAnswers + ) extends AnswerTuples { + + /* TODO: this might not be the best choice, since the internal + * iterator in a collection is a single traverse iterator. + * We might be messing with internal state. + */ + private var iter = answers.answers.iterator + + /** Reset the iterator over the answers. + * + * @note this operation is currently not supported. + */ + def reset(): Unit = ??? + + /** True if the iterator can provide more items. */ + def isValid: Boolean = iter.hasNext + + /** Get arity of answer variables. */ + def getArity: Int = answers.query.answer.length + + /** Get array of answer variable names */ + def getAnswerVariables: Array[String] = + answers.query.answer.map(_.getName).toArray + + /** Advance iterator state */ + def moveNext(): Unit = { } + + /** Get next [[uk.ac.ox.cs.pagoda.query.AnswerTuple]] from the iterator */ + def getTuple: AnswerTuple = iter.next() + + /** Return true if the input tuple is part of this collection. + * + * @param tuple the answer to be checked. + */ + def contains(tuple: AnswerTuple): Boolean = + answers.contains(tuple) + + /** Skip one item in the iterator. + * + * @note that the semantic of this method is not clear to the + * author and the description is just an assumption. + */ + def remove(): Unit = iter.next() + } + + /** Implicit convertion from RSAComb-style answers to [[uk.ac.ox.cs.pagoda.query.AnswerTuple]] */ + private implicit def asAnswerTuple( + answer: (Long,Seq[Resource]) + ): AnswerTuple = new AnswerTuple(answer._2.map(res => + res match { + case r: IRI => OldIndividual.create(r.getIRI) + case r: BlankNode => OldBlankNode.create(r.getID) + case r: Literal => OldLiteral.create(r.getLexicalForm,r.getDatatype) + } + ).toArray) + + /** Implicit convertion from [[tech.oxfordsemantic.jrdfox.logic.Datatype]] to [[uk.ac.ox.cs.JRDFox.model.Datatype]] + * + * @note this might not be 100% accurate since the two interfaces are + * slightly different. + */ + private implicit def asOldDatatype( + datatype: Datatype + ): OldDatatype = datatype match { + case Datatype.BLANK_NODE => OldDatatype.BLANK_NODE + case Datatype.IRI_REFERENCE => OldDatatype.IRI_REFERENCE + case Datatype.RDF_PLAIN_LITERAL => OldDatatype.RDF_PLAIN_LITERAL + case Datatype.RDFS_LITERAL => OldDatatype.RDFS_LITERAL + case Datatype.XSD_ANY_URI => OldDatatype.XSD_ANY_URI + case Datatype.XSD_BOOLEAN => OldDatatype.XSD_BOOLEAN + case Datatype.XSD_BYTE => OldDatatype.XSD_BYTE + case Datatype.XSD_DATE => OldDatatype.XSD_DATE + case Datatype.XSD_DATE_TIME => OldDatatype.XSD_DATE_TIME + case Datatype.XSD_DATE_TIME_STAMP => OldDatatype.XSD_DATE_TIME_STAMP + case Datatype.XSD_DECIMAL => OldDatatype.XSD_DECIMAL + case Datatype.XSD_DOUBLE => OldDatatype.XSD_DOUBLE + case Datatype.XSD_DURATION => OldDatatype.XSD_DURATION + case Datatype.XSD_FLOAT => OldDatatype.XSD_FLOAT + case Datatype.XSD_G_DAY => OldDatatype.XSD_G_DAY + case Datatype.XSD_G_MONTH => OldDatatype.XSD_G_MONTH + case Datatype.XSD_G_MONTH_DAY => OldDatatype.XSD_G_MONTH_DAY + case Datatype.XSD_G_YEAR => OldDatatype.XSD_G_YEAR + case Datatype.XSD_G_YEAR_MONTH => OldDatatype.XSD_G_YEAR_MONTH + case Datatype.XSD_INT => OldDatatype.XSD_INT + case Datatype.XSD_INTEGER => OldDatatype.XSD_INTEGER + case Datatype.XSD_LONG => OldDatatype.XSD_LONG + case Datatype.XSD_NEGATIVE_INTEGER => OldDatatype.XSD_NEGATIVE_INTEGER + case Datatype.XSD_NON_NEGATIVE_INTEGER => OldDatatype.XSD_NON_NEGATIVE_INTEGER + case Datatype.XSD_NON_POSITIVE_INTEGER => OldDatatype.XSD_NON_POSITIVE_INTEGER + case Datatype.XSD_POSITIVE_INTEGER => OldDatatype.XSD_POSITIVE_INTEGER + case Datatype.XSD_SHORT => OldDatatype.XSD_SHORT + case Datatype.XSD_STRING => OldDatatype.XSD_STRING + case Datatype.XSD_TIME => OldDatatype.XSD_TIME + case Datatype.XSD_UNSIGNED_BYTE => OldDatatype.XSD_UNSIGNED_BYTE + case Datatype.XSD_UNSIGNED_INT => OldDatatype.XSD_UNSIGNED_INT + case Datatype.XSD_UNSIGNED_LONG => OldDatatype.XSD_UNSIGNED_LONG + case Datatype.XSD_UNSIGNED_SHORT => OldDatatype.XSD_UNSIGNED_SHORT + case Datatype.XSD_DAY_TIME_DURATION => OldDatatype.XSD_DURATION + case Datatype.XSD_YEAR_MONTH_DURATION => OldDatatype.XSD_DURATION + case Datatype.INVALID_DATATYPE => OldDatatype.XSD_ANY_URI + } +} -- cgit v1.2.3