diff options
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs/acqua')
| -rw-r--r-- | src/main/scala/uk/ac/ox/cs/acqua/implicits/RSACombAnswerTuples.scala | 146 |
1 files changed, 146 insertions, 0 deletions
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 @@ | |||
| 1 | /* | ||
| 2 | * Copyright 2021,2022 KRR Oxford | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | |||
| 17 | package uk.ac.ox.cs.acqua.implicits | ||
| 18 | |||
| 19 | import uk.ac.ox.cs.JRDFox.model.{ | ||
| 20 | BlankNode => OldBlankNode, | ||
| 21 | Datatype => OldDatatype, | ||
| 22 | Individual => OldIndividual, | ||
| 23 | Literal => OldLiteral, | ||
| 24 | } | ||
| 25 | import tech.oxfordsemantic.jrdfox.logic.Datatype | ||
| 26 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
| 27 | BlankNode, | ||
| 28 | IRI, | ||
| 29 | Literal, | ||
| 30 | Resource | ||
| 31 | } | ||
| 32 | import uk.ac.ox.cs.pagoda.query.{AnswerTuple,AnswerTuples} | ||
| 33 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers | ||
| 34 | |||
| 35 | /** Implicit wrapper around [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers]] | ||
| 36 | * | ||
| 37 | * It implicitly converts a [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQueryAnswers]] | ||
| 38 | * into a [[uk.ac.ox.cs.pagoda.query.AnswerTuples]] to maintain | ||
| 39 | * compatibility betweren RSAComb and PAGOdA. | ||
| 40 | */ | ||
| 41 | object RSACombAnswerTuples { | ||
| 42 | |||
| 43 | implicit class RSACombAnswerTuples( | ||
| 44 | val answers: ConjunctiveQueryAnswers | ||
| 45 | ) extends AnswerTuples { | ||
| 46 | |||
| 47 | /* TODO: this might not be the best choice, since the internal | ||
| 48 | * iterator in a collection is a single traverse iterator. | ||
| 49 | * We might be messing with internal state. | ||
| 50 | */ | ||
| 51 | private var iter = answers.answers.iterator | ||
| 52 | |||
| 53 | /** Reset the iterator over the answers. | ||
| 54 | * | ||
| 55 | * @note this operation is currently not supported. | ||
| 56 | */ | ||
| 57 | def reset(): Unit = ??? | ||
| 58 | |||
| 59 | /** True if the iterator can provide more items. */ | ||
| 60 | def isValid: Boolean = iter.hasNext | ||
| 61 | |||
| 62 | /** Get arity of answer variables. */ | ||
| 63 | def getArity: Int = answers.query.answer.length | ||
| 64 | |||
| 65 | /** Get array of answer variable names */ | ||
| 66 | def getAnswerVariables: Array[String] = | ||
| 67 | answers.query.answer.map(_.getName).toArray | ||
| 68 | |||
| 69 | /** Advance iterator state */ | ||
| 70 | def moveNext(): Unit = { } | ||
| 71 | |||
| 72 | /** Get next [[uk.ac.ox.cs.pagoda.query.AnswerTuple]] from the iterator */ | ||
| 73 | def getTuple: AnswerTuple = iter.next() | ||
| 74 | |||
| 75 | /** Return true if the input tuple is part of this collection. | ||
| 76 | * | ||
| 77 | * @param tuple the answer to be checked. | ||
| 78 | */ | ||
| 79 | def contains(tuple: AnswerTuple): Boolean = | ||
| 80 | answers.contains(tuple) | ||
| 81 | |||
| 82 | /** Skip one item in the iterator. | ||
| 83 | * | ||
| 84 | * @note that the semantic of this method is not clear to the | ||
| 85 | * author and the description is just an assumption. | ||
| 86 | */ | ||
| 87 | def remove(): Unit = iter.next() | ||
| 88 | } | ||
| 89 | |||
| 90 | /** Implicit convertion from RSAComb-style answers to [[uk.ac.ox.cs.pagoda.query.AnswerTuple]] */ | ||
| 91 | private implicit def asAnswerTuple( | ||
| 92 | answer: (Long,Seq[Resource]) | ||
| 93 | ): AnswerTuple = new AnswerTuple(answer._2.map(res => | ||
| 94 | res match { | ||
| 95 | case r: IRI => OldIndividual.create(r.getIRI) | ||
| 96 | case r: BlankNode => OldBlankNode.create(r.getID) | ||
| 97 | case r: Literal => OldLiteral.create(r.getLexicalForm,r.getDatatype) | ||
| 98 | } | ||
| 99 | ).toArray) | ||
| 100 | |||
| 101 | /** Implicit convertion from [[tech.oxfordsemantic.jrdfox.logic.Datatype]] to [[uk.ac.ox.cs.JRDFox.model.Datatype]] | ||
| 102 | * | ||
| 103 | * @note this might not be 100% accurate since the two interfaces are | ||
| 104 | * slightly different. | ||
| 105 | */ | ||
| 106 | private implicit def asOldDatatype( | ||
| 107 | datatype: Datatype | ||
| 108 | ): OldDatatype = datatype match { | ||
| 109 | case Datatype.BLANK_NODE => OldDatatype.BLANK_NODE | ||
| 110 | case Datatype.IRI_REFERENCE => OldDatatype.IRI_REFERENCE | ||
| 111 | case Datatype.RDF_PLAIN_LITERAL => OldDatatype.RDF_PLAIN_LITERAL | ||
| 112 | case Datatype.RDFS_LITERAL => OldDatatype.RDFS_LITERAL | ||
| 113 | case Datatype.XSD_ANY_URI => OldDatatype.XSD_ANY_URI | ||
| 114 | case Datatype.XSD_BOOLEAN => OldDatatype.XSD_BOOLEAN | ||
| 115 | case Datatype.XSD_BYTE => OldDatatype.XSD_BYTE | ||
| 116 | case Datatype.XSD_DATE => OldDatatype.XSD_DATE | ||
| 117 | case Datatype.XSD_DATE_TIME => OldDatatype.XSD_DATE_TIME | ||
| 118 | case Datatype.XSD_DATE_TIME_STAMP => OldDatatype.XSD_DATE_TIME_STAMP | ||
| 119 | case Datatype.XSD_DECIMAL => OldDatatype.XSD_DECIMAL | ||
| 120 | case Datatype.XSD_DOUBLE => OldDatatype.XSD_DOUBLE | ||
| 121 | case Datatype.XSD_DURATION => OldDatatype.XSD_DURATION | ||
| 122 | case Datatype.XSD_FLOAT => OldDatatype.XSD_FLOAT | ||
| 123 | case Datatype.XSD_G_DAY => OldDatatype.XSD_G_DAY | ||
| 124 | case Datatype.XSD_G_MONTH => OldDatatype.XSD_G_MONTH | ||
| 125 | case Datatype.XSD_G_MONTH_DAY => OldDatatype.XSD_G_MONTH_DAY | ||
| 126 | case Datatype.XSD_G_YEAR => OldDatatype.XSD_G_YEAR | ||
| 127 | case Datatype.XSD_G_YEAR_MONTH => OldDatatype.XSD_G_YEAR_MONTH | ||
| 128 | case Datatype.XSD_INT => OldDatatype.XSD_INT | ||
| 129 | case Datatype.XSD_INTEGER => OldDatatype.XSD_INTEGER | ||
| 130 | case Datatype.XSD_LONG => OldDatatype.XSD_LONG | ||
| 131 | case Datatype.XSD_NEGATIVE_INTEGER => OldDatatype.XSD_NEGATIVE_INTEGER | ||
| 132 | case Datatype.XSD_NON_NEGATIVE_INTEGER => OldDatatype.XSD_NON_NEGATIVE_INTEGER | ||
| 133 | case Datatype.XSD_NON_POSITIVE_INTEGER => OldDatatype.XSD_NON_POSITIVE_INTEGER | ||
| 134 | case Datatype.XSD_POSITIVE_INTEGER => OldDatatype.XSD_POSITIVE_INTEGER | ||
| 135 | case Datatype.XSD_SHORT => OldDatatype.XSD_SHORT | ||
| 136 | case Datatype.XSD_STRING => OldDatatype.XSD_STRING | ||
| 137 | case Datatype.XSD_TIME => OldDatatype.XSD_TIME | ||
| 138 | case Datatype.XSD_UNSIGNED_BYTE => OldDatatype.XSD_UNSIGNED_BYTE | ||
| 139 | case Datatype.XSD_UNSIGNED_INT => OldDatatype.XSD_UNSIGNED_INT | ||
| 140 | case Datatype.XSD_UNSIGNED_LONG => OldDatatype.XSD_UNSIGNED_LONG | ||
| 141 | case Datatype.XSD_UNSIGNED_SHORT => OldDatatype.XSD_UNSIGNED_SHORT | ||
| 142 | case Datatype.XSD_DAY_TIME_DURATION => OldDatatype.XSD_DURATION | ||
| 143 | case Datatype.XSD_YEAR_MONTH_DURATION => OldDatatype.XSD_DURATION | ||
| 144 | case Datatype.INVALID_DATATYPE => OldDatatype.XSD_ANY_URI | ||
| 145 | } | ||
| 146 | } | ||
