aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala
blob: 693a9af9ad52019af633b9d4f1083b9b2a832027 (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
/*
 * Copyright 2020, 2021 KRR Oxford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.ac.ox.cs.rsacomb.sparql

import java.util.{Map => JMap, HashMap => JHashMap}
import tech.oxfordsemantic.jrdfox.Prefixes
import tech.oxfordsemantic.jrdfox.client.DataStoreConnection
import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName}
import tech.oxfordsemantic.jrdfox.logic.expression.Variable
import tech.oxfordsemantic.jrdfox.logic.sparql.pattern.{
  ConjunctionPattern,
  QueryPattern,
  TriplePattern
}
import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery
import uk.ac.ox.cs.rsacomb.util.RDFoxUtil

/** Factory for [[uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery]]. */
object ConjunctiveQuery {

  /** Creates a new ConjunctiveQuery instance.
    *
    * @param query `SelectQuery` instance representing the actual query
    */
  def apply(id: Int, query: SelectQuery): ConjunctiveQuery =
    new ConjunctiveQuery(id, query)

  /** Creates a new ConjunctiveQuery from a query string
    *
    * @param query a string representing the query in SPARQL format
    * @param prefixes additional prefixes used in the query. Defaults to
    *                 an empty set of prefixes.
    * @return an [[scala.Option]] containing a ConjunctiveQuery if the
    *         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(id, _))

}

/** A conjunctive query
  *
  * A thin layer around
  * [[tech.oxfordsemantics.jrdfox.logic.sparql.statement.SelectQuery]].
  *
  * Instances should be created using the companion object.
  *
  * @todo additional checks need to be performed in order for a
  * `SelectQuery` to be considered a conjunctive query.
  */
class ConjunctiveQuery(
    val id: Int,
    query: SelectQuery,
    val prefixes: Prefixes = new Prefixes()
) {

  /** Simplify conversion between Java and Scala collections */
  import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._

  /** SELECT section of the SPARQL query.
    *
    * Simply exposes the underlying `getSelection` method in
    * [[tech.oxfordsemantics.jrdfox.logic.sparql.statement.SelectQuery]].
    */
  val select = query.getSelection

  /** WHERE section of the SPARQL query.
    *
    * Simply exposes the underlying `getWherePattern` method in
    * [[tech.oxfordsemantics.jrdfox.logic.sparql.statement.QueryBody]].
    */
  val where = query.getQueryBody.getWherePattern

  /** Returns true if it is a boolean CQ.
    *
    * @note checking for `select` being empty is not enough. When a
    * query selects '''all''' variables with `*`, `select` is empty as
    * well.
    */
  val bcq: Boolean = select.isEmpty && !query.getAllPossibleVariables

  /** Returns the query body as a sequence of atoms (triples). */
  def atoms(graph: TupleTableName): List[TupleTableAtom] =
    where
      .asInstanceOf[ConjunctionPattern]
      .getConjuncts
      .collect { case t: TriplePattern =>
        TupleTableAtom.create(graph, t.getSubject, t.getPredicate, t.getObject)
      }
  // where match {
  //   case b: ConjunctionPattern => {
  //     b.getConjuncts.toList.flatMap { conj: QueryPattern =>
  //       conj match {
  //         case c: TriplePattern =>
  //           Seq(
  //             TupleTableAtom.rdf(c.getSubject, c.getPredicate, c.getObject)
  //           )
  //         case _ => List()
  //       }
  //     }
  //   }
  //   case _ => List()
  // }

  /** Returns the full collection of variables involved in the query. */
  val variables: List[Variable] =
    where
      .asInstanceOf[ConjunctionPattern]
      .getConjuncts
      .collect { case t: TriplePattern =>
        Set(t.getSubject, t.getPredicate, t.getObject).collect {
          case v: Variable => v
        }
      }
      .flatten
      .distinct
  // (where match {
  //   case b: ConjunctionPattern => {
  //     b.getConjuncts.toList.flatMap { conj: QueryPattern =>
  //       conj match {
  //         case c: TriplePattern =>
  //           Set(c.getSubject, c.getPredicate, c.getObject).collect {
  //             case v: Variable => v
  //           }
  //         case _ => List()
  //       }
  //     }
  //   }
  //   case _ => List()
  // }).distinct

  /** Returns the collection of answer variables in the query. */
  val answer: List[Variable] =
    if (query.getAllPossibleVariables)
      variables
    else
      select.map(_.getVariable).toList.distinct

  /** Returns the collection of bounded (existential) variables in the query. */
  val bounded: List[Variable] = variables diff answer

  override def toString(): String = query.toString
}