aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-10-03 11:16:33 +0100
committerFederico Igne <git@federicoigne.com>2021-10-03 11:16:33 +0100
commit0cd63c26fdfafaf09734950add28f63d500ac330 (patch)
tree244a4d11478e563feb6e29544b6ce4da882a5d96
parent297c88902b27030cb9f6004c51ccae18eb453933 (diff)
downloadRSAComb-0cd63c26fdfafaf09734950add28f63d500ac330.tar.gz
RSAComb-0cd63c26fdfafaf09734950add28f63d500ac330.zip
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<id>] ``` where `<id>` is the id of the query.
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala9
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala17
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
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
37 /** Creates a new ConjunctiveQuery instance. 35 /** Creates a new ConjunctiveQuery instance.
38 * 36 *
39 * @param query `SelectQuery` instance representing the actual query 37 * @param query `SelectQuery` instance representing the actual query
40 */ 38 */
41 def apply(query: SelectQuery): ConjunctiveQuery = 39 def apply(id: Int, query: SelectQuery): ConjunctiveQuery =
42 new ConjunctiveQuery({ idCounter += 1; idCounter }, query) 40 new ConjunctiveQuery(id, query)
43 41
44 /** Creates a new ConjunctiveQuery from a query string 42 /** Creates a new ConjunctiveQuery from a query string
45 * 43 *
@@ -50,10 +48,11 @@ object ConjunctiveQuery {
50 * input query represents one, None is returned otherwise. 48 * input query represents one, None is returned otherwise.
51 */ 49 */
52 def parse( 50 def parse(
51 id: Int,
53 query: String, 52 query: String,
54 prefixes: Prefixes = new Prefixes() 53 prefixes: Prefixes = new Prefixes()
55 ): Option[ConjunctiveQuery] = 54 ): Option[ConjunctiveQuery] =
56 RDFoxUtil.parseSelectQuery(query, prefixes).map(ConjunctiveQuery(_)) 55 RDFoxUtil.parseSelectQuery(query, prefixes).map(ConjunctiveQuery(id, _))
57 56
58} 57}
59 58
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 {
243 path: os.Path, 243 path: os.Path,
244 prefixes: Prefixes = new Prefixes() 244 prefixes: Prefixes = new Prefixes()
245 ): List[ConjunctiveQuery] = { 245 ): List[ConjunctiveQuery] = {
246 val pattern = raw"\^\[Query(\d+)\]".r
246 val queries = os.read 247 val queries = os.read
247 .lines(path) 248 .lines(path)
248 .map(_.trim.filter(_ >= ' ')) 249 .map(_.trim.filter(_ >= ' '))
249 .filterNot(_ == "") 250 .filterNot(_ == "")
250 .foldRight((List.empty[List[String]], List.empty[String])) { 251 .foldRight((List.empty[Option[ConjunctiveQuery]], List.empty[String])) {
251 case (line, (acc, query)) => { 252 case (line, (acc, query)) => {
252 if ("^#\\^\\[Query\\d+\\]$".r.matches(line)) 253 line match {
253 (query :: acc, List.empty) 254 case pattern(id) => {
254 else 255 val cq =
255 (acc, line :: query) 256 ConjunctiveQuery.parse(id.toInt, query.mkString(" "), prefixes)
257 (cq :: acc, List.empty)
258 }
259 case _ => (acc, line :: query)
260 }
256 } 261 }
257 } 262 }
258 ._1 263 ._1
259 .map(_.mkString(" "))
260 .map(ConjunctiveQuery.parse(_, prefixes))
261 .collect { case Some(q) => q } 264 .collect { case Some(q) => q }
262 Logger print s"Loaded ${queries.length} queries from $path" 265 Logger print s"Loaded ${queries.length} queries from $path"
263 queries 266 queries