diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-18 18:07:53 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-18 18:07:53 +0000 |
commit | 4df351d3b1d11fc045005323c38ba3528de631ea (patch) | |
tree | 35f809e3def7d55602ed124f7b194db29407ba33 /src/main/scala/rsacomb/util/RDFoxHelpers.scala | |
parent | e1a04294ed8737444e40323474f4084cb64c1d55 (diff) | |
download | RSAComb-4df351d3b1d11fc045005323c38ba3528de631ea.tar.gz RSAComb-4df351d3b1d11fc045005323c38ba3528de631ea.zip |
Rework RSA as a utility object
Diffstat (limited to 'src/main/scala/rsacomb/util/RDFoxHelpers.scala')
-rw-r--r-- | src/main/scala/rsacomb/util/RDFoxHelpers.scala | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/main/scala/rsacomb/util/RDFoxHelpers.scala b/src/main/scala/rsacomb/util/RDFoxHelpers.scala new file mode 100644 index 0000000..9856e27 --- /dev/null +++ b/src/main/scala/rsacomb/util/RDFoxHelpers.scala | |||
@@ -0,0 +1,100 @@ | |||
1 | package rsacomb.util | ||
2 | |||
3 | import java.util.{Map => JMap, HashMap => JHashMap} | ||
4 | import java.io.StringReader | ||
5 | import tech.oxfordsemantic.jrdfox.Prefixes | ||
6 | import tech.oxfordsemantic.jrdfox.client.{ | ||
7 | ConnectionFactory, | ||
8 | ServerConnection, | ||
9 | DataStoreConnection | ||
10 | } | ||
11 | import tech.oxfordsemantic.jrdfox.formats.SPARQLParser | ||
12 | import tech.oxfordsemantic.jrdfox.logic.expression.Resource | ||
13 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | ||
14 | |||
15 | import rsacomb.suffix.Nth | ||
16 | |||
17 | object RDFoxHelpers { | ||
18 | |||
19 | def openConnection( | ||
20 | dataStore: String, | ||
21 | opts: JMap[String, String] = new JHashMap[String, String]() | ||
22 | ): (ServerConnection, DataStoreConnection) = { | ||
23 | /* Create local server connection | ||
24 | */ | ||
25 | val serverUrl = "rdfox:local" | ||
26 | val role = "" | ||
27 | val password = "" | ||
28 | val server = | ||
29 | ConnectionFactory.newServerConnection(serverUrl, role, password) | ||
30 | |||
31 | /* Create datastore connection | ||
32 | */ | ||
33 | // parameters.put("owl-in-rdf-support", "relaxed") | ||
34 | // parameters.put("equality", "noUNA") | ||
35 | server.createDataStore(dataStore, "par-complex-nn", opts) | ||
36 | val data = server.newDataStoreConnection(dataStore) | ||
37 | |||
38 | (server, data) | ||
39 | } | ||
40 | |||
41 | def parseSelectQuery( | ||
42 | query: String, | ||
43 | prefixes: Prefixes = new Prefixes() | ||
44 | ): Option[SelectQuery] = { | ||
45 | val parser = new SPARQLParser( | ||
46 | prefixes, | ||
47 | new StringReader(query) | ||
48 | ) | ||
49 | parser.parseSingleQuery() match { | ||
50 | case q: SelectQuery => Some(q) | ||
51 | case _ => None | ||
52 | } | ||
53 | } | ||
54 | |||
55 | def submitSelectQuery( | ||
56 | data: DataStoreConnection, | ||
57 | query: String, | ||
58 | prefixes: Prefixes = new Prefixes(), | ||
59 | opts: JMap[String, String] = new JHashMap[String, String]() | ||
60 | ): List[List[Resource]] = { | ||
61 | val cursor = data.createCursor(prefixes, query, opts) | ||
62 | var answers: List[List[Resource]] = List() | ||
63 | var mul = cursor.open() | ||
64 | while (mul > 0) { | ||
65 | val answer = | ||
66 | (0 until cursor.getArity).map(cursor.getResource(_)).toList | ||
67 | answers = answer :: answers | ||
68 | mul = cursor.advance() | ||
69 | } | ||
70 | cursor.close(); | ||
71 | answers | ||
72 | } | ||
73 | |||
74 | def queryInternalPredicate( | ||
75 | data: DataStoreConnection, | ||
76 | pred: String, | ||
77 | arity: Int, | ||
78 | opts: JMap[String, String] = new JHashMap[String, String]() | ||
79 | ): List[List[Resource]] = { | ||
80 | var query = "SELECT" | ||
81 | for (i <- 0 until arity) { | ||
82 | query ++= s" ?X$i" | ||
83 | } | ||
84 | query ++= " WHERE {" | ||
85 | for (i <- 0 until arity) { | ||
86 | query ++= s" ?S rsa:${pred :: Nth(i)} ?X$i ." | ||
87 | } | ||
88 | query ++= " }" | ||
89 | submitSelectQuery(data, query, RSA.Prefixes, opts) | ||
90 | } | ||
91 | |||
92 | def closeConnection( | ||
93 | server: ServerConnection, | ||
94 | data: DataStoreConnection | ||
95 | ): Unit = { | ||
96 | server.close(); | ||
97 | data.close(); | ||
98 | } | ||
99 | |||
100 | } | ||