diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip | |
[pagoda] Move project to Scala
This commit includes a few changes:
- The repository still uses Maven to manage dependency but it is now a
Scala project.
- The code has been ported from OWLAPI 3.4.10 to 5.1.20
- A proof of concept program using both RSAComb and PAGOdA has been
added.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java | 282 |
1 files changed, 0 insertions, 282 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java b/src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java deleted file mode 100644 index 1e53b9c..0000000 --- a/src/uk/ac/ox/cs/pagoda/util/SparqlHelper.java +++ /dev/null | |||
| @@ -1,282 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util; | ||
| 2 | |||
| 3 | import com.hp.hpl.jena.graph.Node; | ||
| 4 | import com.hp.hpl.jena.query.Query; | ||
| 5 | import com.hp.hpl.jena.query.QueryFactory; | ||
| 6 | import com.hp.hpl.jena.sparql.core.TriplePath; | ||
| 7 | import com.hp.hpl.jena.sparql.core.Var; | ||
| 8 | import com.hp.hpl.jena.sparql.syntax.*; | ||
| 9 | import org.semanticweb.HermiT.model.*; | ||
| 10 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 11 | import uk.ac.ox.cs.pagoda.hermit.RuleHelper; | ||
| 12 | |||
| 13 | import java.util.Collection; | ||
| 14 | import java.util.HashSet; | ||
| 15 | import java.util.Set; | ||
| 16 | |||
| 17 | public class SparqlHelper { | ||
| 18 | |||
| 19 | public static String getSPARQLQuery(Atom[] atoms, String... vars) { | ||
| 20 | Set<Variable> undistinguishedVars = new HashSet<Variable>(); | ||
| 21 | for (int i = 0; i < atoms.length; ++i) { | ||
| 22 | atoms[i].getVariables(undistinguishedVars); | ||
| 23 | } | ||
| 24 | int xIndex = 1; | ||
| 25 | while (undistinguishedVars.contains(Variable.create("X" + xIndex))) ++xIndex; | ||
| 26 | |||
| 27 | for (String var: vars) | ||
| 28 | if (var != null && !var.isEmpty()) | ||
| 29 | undistinguishedVars.remove(Variable.create(var)); | ||
| 30 | |||
| 31 | StringBuffer buffer = new StringBuffer(); | ||
| 32 | if (vars.length > 0) | ||
| 33 | buffer.append("SELECT DISTINCT "); | ||
| 34 | else | ||
| 35 | buffer.append("SELECT *"); | ||
| 36 | for (int i = 0; i < vars.length; ++i) { | ||
| 37 | if (vars[i] != null && !vars[i].isEmpty()) | ||
| 38 | buffer.append("?").append(vars[i]).append(" "); | ||
| 39 | } | ||
| 40 | buffer.append( " WHERE {"); | ||
| 41 | for (Atom atom: atoms) | ||
| 42 | if (atom.getDLPredicate() instanceof AtLeastConcept) { | ||
| 43 | AtLeastConcept atLeast = (AtLeastConcept) atom.getDLPredicate(); | ||
| 44 | int number = atLeast.getNumber(); | ||
| 45 | for (int i = 0; i < number; ++i) { | ||
| 46 | Variable newVar = Variable.create("X" + (xIndex + i)); | ||
| 47 | |||
| 48 | Atom tAtom; | ||
| 49 | if (atLeast.getOnRole() instanceof AtomicRole) | ||
| 50 | tAtom = Atom.create( | ||
| 51 | (AtomicRole) atLeast.getOnRole(), | ||
| 52 | atom.getArgument(0), | ||
| 53 | newVar); | ||
| 54 | else | ||
| 55 | tAtom = Atom.create( | ||
| 56 | (AtomicRole) atLeast.getOnRole().getInverse(), | ||
| 57 | newVar, | ||
| 58 | atom.getArgument(0)); | ||
| 59 | buffer.append(" "); | ||
| 60 | buffer.append(toSPARQLClause(tAtom, undistinguishedVars)); | ||
| 61 | buffer.append(" ."); | ||
| 62 | |||
| 63 | if (!atLeast.getToConcept().equals(AtomicConcept.THING)) { | ||
| 64 | if (atLeast.getToConcept() instanceof AtomicConcept); | ||
| 65 | |||
| 66 | tAtom = Atom.create((AtomicConcept) atLeast.getToConcept(), newVar); | ||
| 67 | buffer.append(" "); | ||
| 68 | buffer.append(toSPARQLClause(tAtom, undistinguishedVars)); | ||
| 69 | buffer.append(" ."); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | for (int i = 0; i < number; ++i) | ||
| 74 | for (int j = i + 1; j < number; ++j) { | ||
| 75 | Atom tAtom = Atom.create(Inequality.INSTANCE, Variable.create("X" + (xIndex + i)), Variable.create("X" + (xIndex + j))); | ||
| 76 | buffer.append(" "); | ||
| 77 | buffer.append(toSPARQLClause(tAtom, undistinguishedVars)); | ||
| 78 | buffer.append(" ."); | ||
| 79 | } | ||
| 80 | |||
| 81 | xIndex += number; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | buffer.append(" "); | ||
| 85 | buffer.append(toSPARQLClause(atom, undistinguishedVars)); | ||
| 86 | buffer.append(" ."); | ||
| 87 | } | ||
| 88 | buffer.append(" ").append("}"); | ||
| 89 | return buffer.toString(); | ||
| 90 | } | ||
| 91 | |||
| 92 | private static String toSPARQLClause(Atom atom, Set<Variable> undisVars ) { | ||
| 93 | DLPredicate predicate = atom.getDLPredicate(); | ||
| 94 | String r, a, b; | ||
| 95 | |||
| 96 | if (predicate instanceof Equality || predicate instanceof AnnotatedEquality) | ||
| 97 | atom = Atom.create(predicate = AtomicRole.create(Namespace.EQUALITY), atom.getArgument(0), atom.getArgument(1)); | ||
| 98 | else if (predicate instanceof Inequality) | ||
| 99 | atom = Atom.create(predicate = AtomicRole.create(Namespace.INEQUALITY), atom.getArgument(0), atom.getArgument(1)); | ||
| 100 | |||
| 101 | if (predicate instanceof AtomicConcept) { | ||
| 102 | r = Namespace.RDF_TYPE_QUOTED; | ||
| 103 | a = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(getName(atom.getArgument(0), undisVars)); | ||
| 104 | b = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(RuleHelper.getText(predicate)); | ||
| 105 | } | ||
| 106 | else if (predicate instanceof AtomicRole) { | ||
| 107 | r = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(RuleHelper.getText(predicate)); | ||
| 108 | a = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(getName(atom.getArgument(0), undisVars)); | ||
| 109 | b = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(getName(atom.getArgument(1), undisVars)); | ||
| 110 | } | ||
| 111 | else if (predicate instanceof AtomicDataRange) { | ||
| 112 | r = Namespace.RDF_TYPE_QUOTED; | ||
| 113 | a = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(getName(atom.getArgument(0), undisVars)); | ||
| 114 | b = MyPrefixes.PAGOdAPrefixes.getQuotedIRI(RuleHelper.getText(predicate)); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | Utility.logError("error!!!!!!!!!!!"); | ||
| 118 | return null; | ||
| 119 | } | ||
| 120 | |||
| 121 | return a + " " + r + " " + b; | ||
| 122 | } | ||
| 123 | |||
| 124 | private static String getName(Term t, Set<Variable> undisVars) { | ||
| 125 | if (t instanceof Variable) | ||
| 126 | if (undisVars.contains(t)) | ||
| 127 | return "_:" + ((Variable) t).getName(); | ||
| 128 | else return "?" + ((Variable) t).getName(); | ||
| 129 | return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(t.toString()); | ||
| 130 | } | ||
| 131 | |||
| 132 | public static Query parse(String text, Collection<String> vars, Collection<Atom> atoms) { | ||
| 133 | Query query = QueryFactory.create(text); | ||
| 134 | if (vars != null) { | ||
| 135 | vars.clear(); | ||
| 136 | for (Var var: query.getProjectVars()) | ||
| 137 | vars.add(var.getName()); | ||
| 138 | } | ||
| 139 | ElementVisitor visitor = new MySparqlElementVisitor(atoms); | ||
| 140 | query.getQueryPattern().visit(visitor); | ||
| 141 | return query; | ||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | class MySparqlElementVisitor implements ElementVisitor { | ||
| 147 | |||
| 148 | Collection<Atom> atoms; | ||
| 149 | |||
| 150 | public MySparqlElementVisitor(Collection<Atom> atoms) { | ||
| 151 | this.atoms = atoms; | ||
| 152 | } | ||
| 153 | |||
| 154 | @Override | ||
| 155 | public void visit(ElementSubQuery el) { | ||
| 156 | Utility.logError("ElmentSubQuery: " + el); | ||
| 157 | } | ||
| 158 | |||
| 159 | @Override | ||
| 160 | public void visit(ElementService el) { | ||
| 161 | // TODO Auto-generated method stub | ||
| 162 | Utility.logError("ElementService: " + el); | ||
| 163 | } | ||
| 164 | |||
| 165 | @Override | ||
| 166 | public void visit(ElementMinus el) { | ||
| 167 | // TODO Auto-generated method stub | ||
| 168 | Utility.logError("ElementMinus: " + el); | ||
| 169 | } | ||
| 170 | |||
| 171 | @Override | ||
| 172 | public void visit(ElementNotExists el) { | ||
| 173 | // TODO Auto-generated method stub | ||
| 174 | Utility.logError("ElementNotExists: " + el); | ||
| 175 | } | ||
| 176 | |||
| 177 | @Override | ||
| 178 | public void visit(ElementExists el) { | ||
| 179 | // TODO Auto-generated method stub | ||
| 180 | Utility.logError("ElementExists: " + el); | ||
| 181 | } | ||
| 182 | |||
| 183 | @Override | ||
| 184 | public void visit(ElementNamedGraph el) { | ||
| 185 | // TODO Auto-generated method stub | ||
| 186 | Utility.logError("ElementNamedGraph: " + el); | ||
| 187 | } | ||
| 188 | |||
| 189 | @Override | ||
| 190 | public void visit(ElementDataset el) { | ||
| 191 | // TODO Auto-generated method stub | ||
| 192 | Utility.logError("ElementDataset: " + el); | ||
| 193 | } | ||
| 194 | |||
| 195 | @Override | ||
| 196 | public void visit(ElementGroup el) { | ||
| 197 | // TODO Auto-generated method stub | ||
| 198 | for (Element e: el.getElements()) | ||
| 199 | e.visit(this); | ||
| 200 | } | ||
| 201 | |||
| 202 | @Override | ||
| 203 | public void visit(ElementOptional el) { | ||
| 204 | // TODO Auto-generated method stub | ||
| 205 | Utility.logError("ElementOptional: " + el); | ||
| 206 | } | ||
| 207 | |||
| 208 | @Override | ||
| 209 | public void visit(ElementUnion el) { | ||
| 210 | // TODO Auto-generated method stub | ||
| 211 | Utility.logError("ElementUnion: " + el); | ||
| 212 | } | ||
| 213 | |||
| 214 | @Override | ||
| 215 | public void visit(ElementBind el) { | ||
| 216 | // TODO Auto-generated method stub | ||
| 217 | Utility.logError("ElementBind: " + el); | ||
| 218 | } | ||
| 219 | |||
| 220 | @Override | ||
| 221 | public void visit(ElementAssign el) { | ||
| 222 | // TODO Auto-generated method stub | ||
| 223 | Utility.logError("ElementAssign: " + el); | ||
| 224 | } | ||
| 225 | |||
| 226 | @Override | ||
| 227 | public void visit(ElementFilter el) { | ||
| 228 | // TODO Auto-generated method stub | ||
| 229 | Utility.logError("ElementFilter: " + el); | ||
| 230 | } | ||
| 231 | |||
| 232 | @Override | ||
| 233 | public void visit(ElementPathBlock el) { | ||
| 234 | // TODO Auto-generated method stub | ||
| 235 | for (TriplePath p: el.getPattern().getList()) { | ||
| 236 | if (p.getPredicate().isVariable()) { | ||
| 237 | AtomicRole r = AtomicRole.create("?" + p.getPredicate().getName()); | ||
| 238 | Term a = getTerm(p.getSubject()), b = getTerm(p.getObject()); | ||
| 239 | atoms.add(Atom.create(r, a, b)); | ||
| 240 | } | ||
| 241 | else if (p.getPredicate().getURI().equals(Namespace.RDF_TYPE) && !p.getObject().isVariable()) { | ||
| 242 | AtomicConcept A = AtomicConcept.create(p.getObject().getURI()); | ||
| 243 | Term c = getTerm(p.getSubject()); | ||
| 244 | atoms.add(Atom.create(A, c)); | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | AtomicRole r = AtomicRole.create(p.getPredicate().getURI()); | ||
| 248 | Term a = getTerm(p.getSubject()), b = getTerm(p.getObject()); | ||
| 249 | atoms.add(Atom.create(r, a, b)); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | private Term getTerm(Node node) { | ||
| 255 | if (node.isVariable()) | ||
| 256 | return Variable.create(node.getName()); | ||
| 257 | if (node.isLiteral()) | ||
| 258 | if (node.getLiteralDatatypeURI() == null) | ||
| 259 | return Constant.create(node.getLiteralLexicalForm(), Namespace.XSD_STRING); | ||
| 260 | else | ||
| 261 | return Constant.create(node.getLiteralLexicalForm(), node.getLiteralDatatypeURI()); | ||
| 262 | |||
| 263 | |||
| 264 | if (node.isURI()) | ||
| 265 | return Individual.create(node.getURI()); | ||
| 266 | Utility.logError("unknown node: " + node); | ||
| 267 | return null; | ||
| 268 | } | ||
| 269 | |||
| 270 | @Override | ||
| 271 | public void visit(ElementTriplesBlock el) { | ||
| 272 | // TODO Auto-generated method stub | ||
| 273 | |||
| 274 | Utility.logError("ElementTriplesBlock: " + el); | ||
| 275 | } | ||
| 276 | |||
| 277 | @Override | ||
| 278 | public void visit(ElementData el) { | ||
| 279 | // TODO Auto-generated method stub | ||
| 280 | |||
| 281 | } | ||
| 282 | } \ No newline at end of file | ||
