From 0cd63c26fdfafaf09734950add28f63d500ac330 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 3 Oct 2021 11:16:33 +0100 Subject: Assign queries integer identifier The ID needs to be specified at creation time or in a query file preceeding the query with the syntax ``` ^[Query] ``` where `` is the id of the query. --- .../uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala | 9 ++++----- src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala | 17 ++++++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) 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 105f425..693a9af 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,14 +32,12 @@ 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({ idCounter += 1; idCounter }, query) + def apply(id: Int, query: SelectQuery): ConjunctiveQuery = + new ConjunctiveQuery(id, query) /** Creates a new ConjunctiveQuery from a query string * @@ -50,10 +48,11 @@ object ConjunctiveQuery { * input query represents one, None is returned otherwise. */ def parse( + id: Int, query: String, prefixes: Prefixes = new Prefixes() ): Option[ConjunctiveQuery] = - RDFoxUtil.parseSelectQuery(query, prefixes).map(ConjunctiveQuery(_)) + RDFoxUtil.parseSelectQuery(query, prefixes).map(ConjunctiveQuery(id, _)) } diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala index aa501cd..46f1160 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala @@ -243,21 +243,24 @@ object RDFoxUtil { path: os.Path, prefixes: Prefixes = new Prefixes() ): List[ConjunctiveQuery] = { + val pattern = raw"\^\[Query(\d+)\]".r val queries = os.read .lines(path) .map(_.trim.filter(_ >= ' ')) .filterNot(_ == "") - .foldRight((List.empty[List[String]], List.empty[String])) { + .foldRight((List.empty[Option[ConjunctiveQuery]], List.empty[String])) { case (line, (acc, query)) => { - if ("^#\\^\\[Query\\d+\\]$".r.matches(line)) - (query :: acc, List.empty) - else - (acc, line :: query) + line match { + case pattern(id) => { + val cq = + ConjunctiveQuery.parse(id.toInt, query.mkString(" "), prefixes) + (cq :: acc, List.empty) + } + case _ => (acc, line :: query) + } } } ._1 - .map(_.mkString(" ")) - .map(ConjunctiveQuery.parse(_, prefixes)) .collect { case Some(q) => q } Logger print s"Loaded ${queries.length} queries from $path" queries -- cgit v1.2.3