aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala
blob: c3db99d65b1b3ad432a755f649e5524486ae6ae4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package uk.ac.ox.cs.rsacomb

/* Java imports */
import java.io.File
import java.util.HashMap
import scala.collection.JavaConverters._

import tech.oxfordsemantic.jrdfox.client.UpdateType
import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery
import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term}

/* Local imports */
import util.{RDFoxHelpers, RSA}

object RSAComb extends App {

  val help: String = """
  rsacomb - combined approach for CQ answering for RSA ontologies.

  USAGE
    rsacomb <path/to/ontology.owl> <path/to/query.sparql>

  where
    the ontology is expected to be an OWL file and the (single)
    query a SPARQL query file.

  """

  /* Simple arguments handling
   *
   * TODO: use something better later on
   */

  if (args.length < 2) {
    println(help)
    sys.exit;
  }

  val ontoPath = new File(args(0))
  val queryPath = new File(args(1))

  if (!ontoPath.isFile || !queryPath.isFile) {
    println("The provided arguments are not regular files.\n\n")
    println(help)
    sys.exit;
  }

  /* Create RSA object from generic OWLOntology
   *
   * TODO: It might be required to check if the ontology in input is
   * Horn-ALCHOIQ. At the moment we are assuming this is always the
   * case.
   */

  val ontology = RSAOntology(ontoPath)
  if (ontology.isRSA) {

    /* Load query */
    val query = RDFoxHelpers.parseSelectQuery(
      """
        PREFIX  :  <http://example.com/rsa_example.owl#>

        SELECT ?X
        WHERE {
          ?X a  :D ;
             :R ?Y .
          ?Y :S ?Z .
          ?Z a  :D .
        }
      """
    )

    /* Compute answers to query */
    query match {
      case Some(query) => {

        import implicits.JavaCollections._

        // Open connection to RDFox
        val (server, data) = RDFoxHelpers.openConnection("AnswerComputation")

        {
          println("\nQuery")
          println(query)
        }

        // Step 1. Computing the canonical model
        val canon = ontology.canonicalModel
        data.addRules(canon.rules)

        {
          println("\nCanonical Model rules:")
          canon.rules.foreach(println)
        }

        // Step 2. Computing the canonical model
        val nis = {
          val query = "SELECT ?Y WHERE { ?X rsa:EquivTo ?Y ; a rsa:Named . }"
          RDFoxHelpers.submitSelectQuery(data, query, RSA.Prefixes).flatten
        }
        val filter = ontology.filteringProgram(query, nis)
        data.addRules(filter.rules)

        {
          println("\nFiltering rules")
          filter.rules.foreach(println)
        }

        // Retrieve answers
        println("\nAnswers:")
        val ans =
          RDFoxHelpers.queryInternalPredicate(data, "Ans", filter.answer.length)
        println(ans)

        /* DEBUG: adding additional checks
         */
        {
          import suffix.{Forward, Backward}

          val arity = filter.answer.length + filter.bounded.length

          println("\nIndividuals:")
          ontology.individuals.foreach(println)

          println("\nThings:")
          val things = RDFoxHelpers.submitSelectQuery(
            data,
            """
             PREFIX  owl:  <http://www.w3.org/2002/07/owl#>

             SELECT ?X {
               ?X a owl:Thing
             }
             """
          )
          println(things)

          println("\nNAMEDs:")
          val named = RDFoxHelpers.submitSelectQuery(
            data,
            """
             SELECT ?X {
               ?X a rsa:Named
             }
             """,
            RSA.Prefixes
          )
          println(named)

          println("\nNIs:")
          val nis = RDFoxHelpers.submitSelectQuery(
            data,
            """
             SELECT ?X {
               ?X a rsa:NI
             }
             """,
            RSA.Prefixes
          )
          println(nis)

          // ID instances
          println("\nIDs:")
          val ids = RDFoxHelpers.queryInternalPredicate(
            data,
            "ID",
            arity + 2
          )
          println(ids)

          println("\nEquivTo:")
          val equivs = RDFoxHelpers.submitSelectQuery(
            data,
            """
              SELECT ?X ?Y {
                ?X rsa:EquivTo ?Y
              }
            """,
            RSA.Prefixes
          )
          println(equivs)

          // Unfiltered answers
          println("\nPossible answers:")
          val qms = RDFoxHelpers.queryInternalPredicate(
            data,
            "QM",
            arity
          )
          println(qms)

          // Cycle detected
          println("\nCycle detection:")
          val aqf = RDFoxHelpers.queryInternalPredicate(
            data,
            "AQ" :: Forward,
            arity + 2
          )
          val aqb = RDFoxHelpers.queryInternalPredicate(
            data,
            "AQ" :: Backward,
            arity + 2
          )
          println(aqf)
          println(aqb)

          // Forks detected
          println("\nForks:")
          val fk = RDFoxHelpers.queryInternalPredicate(
            data,
            "FK",
            arity
          )
          println(fk)

          // Spurious answers
          println("\nSpurious answers")
          val sp = RDFoxHelpers.queryInternalPredicate(
            data,
            "SP",
            arity
          )
          println(sp)
        }

        // Close connection to RDFox
        RDFoxHelpers.closeConnection(server, data)
      }
      case None => {}
    }
  }
}

/* Notes:
 *
 * To establish a connection with a local RDFox instance, do the
 * following:
 *
 * ```
 * val serverConnection : ServerConnection = ConnectionFactory.newServerConnection("rdfox:local", "", "")
 * serverConnection.createDataStore("test","seq",new HashMap())
 * val dataStoreConnection : DataStoreConnection = serverConnection.newDataStoreConnection("test")
 * dataStoreConnection.importData(
 *    UpdateType.ADDITION,
 *    Prefixes.s_emptyPrefixes,
 *    new File("./path/to/file")
 * )
 * ```
 */