From b1799a0af7bd243fb24bff66682f6f848557c27d Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Wed, 29 Sep 2021 15:16:25 +0100 Subject: Add JSON format for CQ answers --- build.sbt | 3 ++- project/Dependencies.scala | 2 ++ .../ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala | 5 ++++- .../cs/rsacomb/sparql/ConjunctiveQueryAnswers.scala | 20 +++++++++++++++++--- 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(".")) scalatestFlatSpec % Test, scalatestShouldMatchers % Test, apibinding, - graphcore + graphcore, + ujson ) ) 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 { lazy val scalatestVersion = "3.2.3" lazy val owlapiVersion = "5.1.17" lazy val scalagraphVersion = "1.13.2" + lazy val ujsonVersion = "1.4.1" // Libraries val scalatest = "org.scalatest" %% "scalatest" % scalatestVersion @@ -15,4 +16,5 @@ object Dependencies { val apibinding = "net.sourceforge.owlapi" % "owlapi-apibinding" % owlapiVersion val graphcore = "org.scala-graph" %% "graph-core" % scalagraphVersion + val ujson = "com.lihaoyi" %% "ujson" % ujsonVersion } 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 /** Factory for [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery]]. */ object ConjunctiveQuery { + private var idCounter: Int = 0; + /** Creates a new ConjunctiveQuery instance. * * @param query `SelectQuery` instance representing the actual query */ def apply(query: SelectQuery): ConjunctiveQuery = - new ConjunctiveQuery(query) + new ConjunctiveQuery({ idCounter += 1; idCounter }, query) /** Creates a new ConjunctiveQuery from a query string * @@ -66,6 +68,7 @@ object ConjunctiveQuery { * `SelectQuery` to be considered a conjunctive query. */ class ConjunctiveQuery( + val id: Int, query: SelectQuery, val prefixes: Prefixes = new Prefixes() ) { 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 @@ package uk.ac.ox.cs.rsacomb.sparql +import ujson._ import tech.oxfordsemantic.jrdfox.logic.expression.{ IRI, Literal, @@ -33,19 +34,32 @@ import tech.oxfordsemantic.jrdfox.logic.expression.{ * BCQs, and empty collection represents a ''false'', ''true'' otherwise. */ class ConjunctiveQueryAnswers( - bcq: Boolean, + val query: ConjunctiveQuery, val variables: Seq[Variable], val answers: Seq[(Long, Seq[Resource])] ) { /** Returns number of distinct answers. */ - val length: Int = if (bcq) 0 else answers.length + val length: Int = if (query.bcq) 0 else answers.length /** Returns number of answers taking into account multiplicity. */ val lengthWithMultiplicity: Long = answers.map(_._1).sum + /** Serialise answers as JSON file */ + def toJSON(): ujson.Js.Value = { + ujson.Obj( + "queryID" -> query.id, + "queryText" -> query.toString + .split('\n') + .map(_.trim.filter(_ >= ' ')) + .mkString(" "), + "answerVariables" -> ujson.Arr(query.answer.map(_.toString())), + "answers" -> ujson.Arr(answers.map(_._2.mkString(" ")).sorted) + ) + } + override def toString(): String = - if (bcq) { + if (query.bcq) { if (answers.isEmpty) "FALSE" else "TRUE" } else { if (answers.isEmpty) -- cgit v1.2.3