aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-09-29 15:16:25 +0100
committerFederico Igne <git@federicoigne.com>2021-09-29 16:41:57 +0100
commitb1799a0af7bd243fb24bff66682f6f848557c27d (patch)
tree42aecf4aa3e5c6c94993a1afcb94729c664d6b03
parent0f52fea098088d8e90410b97213f60c52407f8ff (diff)
downloadRSAComb-b1799a0af7bd243fb24bff66682f6f848557c27d.tar.gz
RSAComb-b1799a0af7bd243fb24bff66682f6f848557c27d.zip
Add JSON format for CQ answers
-rw-r--r--build.sbt3
-rw-r--r--project/Dependencies.scala2
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala5
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala20
4 files changed, 25 insertions, 5 deletions
diff --git a/build.sbt b/build.sbt
index 6dc705f..a9ee841 100644
--- a/build.sbt
+++ b/build.sbt
@@ -35,7 +35,8 @@ lazy val root = (project in file("."))
35 scalatestFlatSpec % Test, 35 scalatestFlatSpec % Test,
36 scalatestShouldMatchers % Test, 36 scalatestShouldMatchers % Test,
37 apibinding, 37 apibinding,
38 graphcore 38 graphcore,
39 ujson
39 ) 40 )
40 ) 41 )
41 42
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index a185615..2742ace 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -5,6 +5,7 @@ object Dependencies {
5 lazy val scalatestVersion = "3.2.3" 5 lazy val scalatestVersion = "3.2.3"
6 lazy val owlapiVersion = "5.1.17" 6 lazy val owlapiVersion = "5.1.17"
7 lazy val scalagraphVersion = "1.13.2" 7 lazy val scalagraphVersion = "1.13.2"
8 lazy val ujsonVersion = "1.4.1"
8 9
9 // Libraries 10 // Libraries
10 val scalatest = "org.scalatest" %% "scalatest" % scalatestVersion 11 val scalatest = "org.scalatest" %% "scalatest" % scalatestVersion
@@ -15,4 +16,5 @@ object Dependencies {
15 val apibinding = 16 val apibinding =
16 "net.sourceforge.owlapi" % "owlapi-apibinding" % owlapiVersion 17 "net.sourceforge.owlapi" % "owlapi-apibinding" % owlapiVersion
17 val graphcore = "org.scala-graph" %% "graph-core" % scalagraphVersion 18 val graphcore = "org.scala-graph" %% "graph-core" % scalagraphVersion
19 val ujson = "com.lihaoyi" %% "ujson" % ujsonVersion
18} 20}
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala
index 37a21e7..c405008 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala
@@ -32,12 +32,14 @@ import uk.ac.ox.cs.rsacomb.util.RDFoxUtil
32/** Factory for [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery]]. */ 32/** Factory for [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery]]. */
33object ConjunctiveQuery { 33object ConjunctiveQuery {
34 34
35 private var idCounter: Int = 0;
36
35 /** Creates a new ConjunctiveQuery instance. 37 /** Creates a new ConjunctiveQuery instance.
36 * 38 *
37 * @param query `SelectQuery` instance representing the actual query 39 * @param query `SelectQuery` instance representing the actual query
38 */ 40 */
39 def apply(query: SelectQuery): ConjunctiveQuery = 41 def apply(query: SelectQuery): ConjunctiveQuery =
40 new ConjunctiveQuery(query) 42 new ConjunctiveQuery({ idCounter += 1; idCounter }, query)
41 43
42 /** Creates a new ConjunctiveQuery from a query string 44 /** Creates a new ConjunctiveQuery from a query string
43 * 45 *
@@ -66,6 +68,7 @@ object ConjunctiveQuery {
66 * `SelectQuery` to be considered a conjunctive query. 68 * `SelectQuery` to be considered a conjunctive query.
67 */ 69 */
68class ConjunctiveQuery( 70class ConjunctiveQuery(
71 val id: Int,
69 query: SelectQuery, 72 query: SelectQuery,
70 val prefixes: Prefixes = new Prefixes() 73 val prefixes: Prefixes = new Prefixes()
71) { 74) {
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala
index 4166655..3d16351 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala
@@ -16,6 +16,7 @@
16 16
17package uk.ac.ox.cs.rsacomb.sparql 17package uk.ac.ox.cs.rsacomb.sparql
18 18
19import ujson._
19import tech.oxfordsemantic.jrdfox.logic.expression.{ 20import tech.oxfordsemantic.jrdfox.logic.expression.{
20 IRI, 21 IRI,
21 Literal, 22 Literal,
@@ -33,19 +34,32 @@ import tech.oxfordsemantic.jrdfox.logic.expression.{
33 * BCQs, and empty collection represents a ''false'', ''true'' otherwise. 34 * BCQs, and empty collection represents a ''false'', ''true'' otherwise.
34 */ 35 */
35class ConjunctiveQueryAnswers( 36class ConjunctiveQueryAnswers(
36 bcq: Boolean, 37 val query: ConjunctiveQuery,
37 val variables: Seq[Variable], 38 val variables: Seq[Variable],
38 val answers: Seq[(Long, Seq[Resource])] 39 val answers: Seq[(Long, Seq[Resource])]
39) { 40) {
40 41
41 /** Returns number of distinct answers. */ 42 /** Returns number of distinct answers. */
42 val length: Int = if (bcq) 0 else answers.length 43 val length: Int = if (query.bcq) 0 else answers.length
43 44
44 /** Returns number of answers taking into account multiplicity. */ 45 /** Returns number of answers taking into account multiplicity. */
45 val lengthWithMultiplicity: Long = answers.map(_._1).sum 46 val lengthWithMultiplicity: Long = answers.map(_._1).sum
46 47
48 /** Serialise answers as JSON file */
49 def toJSON(): ujson.Js.Value = {
50 ujson.Obj(
51 "queryID" -> query.id,
52 "queryText" -> query.toString
53 .split('\n')
54 .map(_.trim.filter(_ >= ' '))
55 .mkString(" "),
56 "answerVariables" -> ujson.Arr(query.answer.map(_.toString())),
57 "answers" -> ujson.Arr(answers.map(_._2.mkString(" ")).sorted)
58 )
59 }
60
47 override def toString(): String = 61 override def toString(): String =
48 if (bcq) { 62 if (query.bcq) {
49 if (answers.isEmpty) "FALSE" else "TRUE" 63 if (answers.isEmpty) "FALSE" else "TRUE"
50 } else { 64 } else {
51 if (answers.isEmpty) 65 if (answers.isEmpty)