From 300abd6482de3b02e0364913300cc803f26f4a86 Mon Sep 17 00:00:00 2001 From: Luigi Asprino Date: Mon, 13 Nov 2023 15:18:08 +0100 Subject: [PATCH] Fix #330 --- .../engine/FacadeXOpExecutor.java | 10 +++++ .../github/sparqlanything/engine/Utils.java | 6 +-- .../github/sparqlanything/it/IssuesTest.java | 40 +++++++++++++++++++ .../src/test/resources/issues/issue330.sparql | 13 ++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 sparql-anything-it/src/test/resources/issues/issue330.sparql diff --git a/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/FacadeXOpExecutor.java b/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/FacadeXOpExecutor.java index 5e254896..a2aa7bb8 100644 --- a/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/FacadeXOpExecutor.java +++ b/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/FacadeXOpExecutor.java @@ -22,6 +22,7 @@ import org.apache.jena.sparql.algebra.op.OpBGP; import org.apache.jena.sparql.algebra.op.OpPropFunc; import org.apache.jena.sparql.algebra.op.OpService; +import org.apache.jena.sparql.algebra.optimize.TransformPropertyFunction; import org.apache.jena.sparql.engine.ExecutionContext; import org.apache.jena.sparql.engine.QueryIterator; import org.apache.jena.sparql.engine.iterator.QueryIterAssign; @@ -72,6 +73,8 @@ protected QueryIterator execute(final OpPropFunc opPropFunc, QueryIterator input protected QueryIterator execute(final OpBGP opBGP, QueryIterator input) { logger.trace("Execute OpBGP {}", opBGP.getPattern().toString()); + + if(hasFXSymbols(this.execCxt) && !this.execCxt.getContext().isDefined(FacadeXExecutionContext.hasServiceClause)){ try { return fxWorkerOpBGP.execute(opBGP, input, this.execCxt); @@ -97,7 +100,14 @@ protected QueryIterator execute(final OpBGP opBGP, QueryIterator input) { return QC.execute(Utils.excludeFXProperties(opBGP), input, new ExecutionContext(execCxt)); } } + + Op opTransformed = TransformPropertyFunction.transform(opBGP, this.execCxt.getContext()); + if(!opTransformed.equals(opBGP)){ + return super.executeOp(opTransformed, input); + } logger.trace("Execute with default Jena execution"); + + // go with the default Jena execution return super.execute(opBGP, input); } diff --git a/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/Utils.java b/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/Utils.java index b7f7ff59..cadcd0ed 100644 --- a/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/Utils.java +++ b/sparql-anything-engine/src/main/java/io/github/sparqlanything/engine/Utils.java @@ -60,7 +60,7 @@ static boolean isPropertyOp(Node node) { return node.isURI() && node.getURI().equals(Triplifier.FACADE_X_TYPE_PROPERTIES); } - static String queryIteratorToString(QueryIterator q) { + public static String queryIteratorToString(QueryIterator q) { StringBuilder sb = new StringBuilder(); while (q.hasNext()) { Binding binding = q.next(); @@ -69,13 +69,13 @@ static String queryIteratorToString(QueryIterator q) { return sb.toString(); } - static String printDatasetGraph(DatasetGraph dg){ + public static String printDatasetGraph(DatasetGraph dg){ StringBuilder sb = new StringBuilder(); dg.find().forEachRemaining(q-> sb.append(q.toString()).append("\n")); return sb.toString(); } - static String bindingToString(Binding binding) { + public static String bindingToString(Binding binding) { StringBuilder sb = new StringBuilder(); Iterator vars = binding.vars(); while (vars.hasNext()) { diff --git a/sparql-anything-it/src/test/java/io/github/sparqlanything/it/IssuesTest.java b/sparql-anything-it/src/test/java/io/github/sparqlanything/it/IssuesTest.java index 631d97ef..d27e99d5 100644 --- a/sparql-anything-it/src/test/java/io/github/sparqlanything/it/IssuesTest.java +++ b/sparql-anything-it/src/test/java/io/github/sparqlanything/it/IssuesTest.java @@ -885,5 +885,45 @@ public void testIssue421() throws URISyntaxException, IOException { } + @Test + public void testIssue330() throws URISyntaxException, IOException { + Dataset ds = DatasetFactory.createGeneral(); + QC.setFactory(ARQ.getContext(), FacadeX.ExecutorFactory); + Query query; + String queryStr = IOUtils.toString(Objects.requireNonNull(getClass().getClassLoader().getResource("issues/issue330.sparql")).toURI(), StandardCharsets.UTF_8); + query = QueryFactory.create(queryStr); + +// System.out.println(Algebra.compile(query)); +// System.out.println(query.toString(Syntax.defaultSyntax)); + + QueryExecution qExec1 = QueryExecutionFactory.create(query, ds); +// System.out.println(ResultSetFormatter.asText(qExec1.execSelect())); + Set result = new HashSet<>(); + ResultSet rs = qExec1.execSelect(); + while (rs.hasNext()){ + result.add(rs.next().get("a").asLiteral().toString()); + } + assertEquals(Sets.newHashSet("abc", "cde"), result); + +// Graph g = GraphFactory.createGraphMem(); +// g.add(NodeFactory.createBlankNode(), RDF.li(1).asNode(), NodeFactory.createLiteral("abc;cde")); +// Query qq = QueryFactory.create("PREFIX apf: PREFIX rdf: SELECT ?a { ?s rdf:_1 ?o . ?a apf:strSplit(?o \";\") }"); +// System.out.println(qq.toString(Syntax.syntaxSPARQL_11)); +// Op op = Algebra.compile(qq); +// System.out.println(op); +// OpBGP bgp = (OpBGP) Algebra.parse("(bgp\n" + +// " (triple ?s ?o)\n" + +// " (triple ?a ??0)\n" + +// " (triple ??0 ?o)\n" + +// " (triple ??0 ??1)\n" + +// " (triple ??1 \";\")\n" + +// " (triple ??1 )\n" + +// " )"); +// System.out.println(TransformPropertyFunction.transform(bgp, ARQ.getContext())); +// QueryIterator qi = Algebra.exec(op, g); +// System.out.println(Utils.queryIteratorToString(qi)); + + } + } diff --git a/sparql-anything-it/src/test/resources/issues/issue330.sparql b/sparql-anything-it/src/test/resources/issues/issue330.sparql new file mode 100644 index 00000000..4c116785 --- /dev/null +++ b/sparql-anything-it/src/test/resources/issues/issue330.sparql @@ -0,0 +1,13 @@ +PREFIX fx: +PREFIX xyz: +PREFIX rdf: +PREFIX rdfs: +PREFIX apf: +SELECT ?a WHERE { + SERVICE { + fx:properties fx:content "abc;cde" . + ?s rdf:_1 ?o . + ?a apf:strSplit(?o ";") . + } + +} \ No newline at end of file