Skip to content

Commit

Permalink
Fix #506
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Oct 11, 2024
1 parent 3849845 commit 2c255af
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.github.sparqlanything.engine.FXSymbol;
import io.github.sparqlanything.engine.FacadeX;
import io.github.sparqlanything.engine.FacadeXOpExecutor;
import io.github.sparqlanything.model.SPARQLAnythingConstants;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -420,6 +421,7 @@ public static ResultSet prepareResultSetFromArgValues(String[] values) {

private static void setConfigurationsToContext(String[] configurations, QueryExecution qExec) {
if (configurations != null) {
qExec.getContext().setTrue(SPARQLAnythingConstants.NO_SERVICE_MODE);
for (String configuration : configurations) {
String[] configurationSplit = configuration.split("=");
qExec.getContext().set(FXSymbol.create(configurationSplit[0]), configurationSplit[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,68 @@

package io.github.sparqlanything.cli;

import com.google.common.collect.Sets;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.sparql.algebra.Algebra;
import org.apache.jena.sys.JenaSystem;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.StringReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

public class OptionsViaCommandLineTest {

public OptionsViaCommandLineTest (){
JenaSystem.init();
}



@Test
public void noServiceModeWithOptional() throws Exception {
String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString();
String q = "SELECT * { ?s ?p ?o OPTIONAL {?s a ?c} } ";
// System.out.println(Algebra.compile(QueryFactory.create(q)));
String out = SPARQLAnything.callMain(new String[]{"-q", q, "-c", "location="+f});
// System.out.println(out);
CSVParser parser = new CSVParser(new StringReader(out), CSVFormat.DEFAULT);
Set<String> actualSet = new HashSet<>();
for (CSVRecord record : parser) {
actualSet.add(record.get(3));
}
Set<String> expectedSet = new HashSet<>();
expectedSet.add("c");
expectedSet.add("http://sparql.xyz/facade-x/ns/root");
expectedSet.add("http://sparql.xyz/facade-x/data/catalog");
expectedSet.add("http://sparql.xyz/facade-x/data/book");
expectedSet.add("http://sparql.xyz/facade-x/data/author");
expectedSet.add("http://sparql.xyz/facade-x/data/price");
expectedSet.add("http://sparql.xyz/facade-x/data/title");
expectedSet.add("http://sparql.xyz/facade-x/data/genre");
expectedSet.add("http://sparql.xyz/facade-x/data/publish_date");
Assert.assertEquals(expectedSet, actualSet);


}

@Ignore
@Test
public void test() throws Exception {
String q = "SELECT ?v { SERVICE <x-sparql-anything:> { ?root a <http://sparql.xyz/facade-x/ns/root> ; <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?v } }";
String out = SPARQLAnything.callMain(new String[]{"-q", q, "-c", "content=abc"});
Assert.assertTrue(out.contains("abc"));
}

@Ignore
@Test
public void testOverride() throws Exception {
String q = "SELECT ?v { SERVICE <x-sparql-anything:content=cde> { ?root a <http://sparql.xyz/facade-x/ns/root> ; <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?v } }";
Expand All @@ -47,12 +92,12 @@ public void testWithoutService() throws Exception {
Assert.assertTrue(out.contains("abc"));
}

@Ignore
@Test
public void testOverrideConfigurationWithBGP() throws Exception {
String q = "PREFIX fx: <http://sparql.xyz/facade-x/ns/> SELECT ?v { ?root a fx:root ; <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?v . fx:properties fx:content \"cde\" } ";
// System.out.println(Algebra.compile(QueryFactory.create(q)));
String out = SPARQLAnything.callMain(new String[]{"-q", q, "-c", "content=abc"});
// System.out.println(out);
System.out.println(out);
Assert.assertTrue(out.contains("cde"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void test() throws Exception {
InputStream fakeIn = new ByteArrayInputStream("abc".getBytes());
System.setIn(fakeIn);

String q = "SELECT ?v { SERVICE <x-sparql-anything:> { ?root a <http://sparql.xyz/facade-x/ns/root> ; <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?v } }";
String q = "SELECT ?v { ?root a <http://sparql.xyz/facade-x/ns/root> ; <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?v } ";
String out = SPARQLAnything.callMain(new String[]{"-q", q, "-c", IRIArgument.READ_FROM_STD_IN.toString().concat("=true")});
Assert.assertEquals("v\r\nabc\r\n",out);
}
Expand Down
26 changes: 26 additions & 0 deletions sparql-anything-cli/src/test/resources/books.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!--
~ Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything
~
~ 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.
-->

<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
</catalog>
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@

package io.github.sparqlanything.engine;

import org.apache.jena.sparql.algebra.op.OpPropFunc;
import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.engine.ExecutionContext;
import org.apache.jena.sparql.engine.QueryIterator;
import org.apache.jena.sparql.engine.main.QC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

public class FXWorkerOpPropFunc extends FXWorker<OpPropFunc> {
public class FXWorkerOp extends FXWorker<Op> {

private static final Logger logger = LoggerFactory.getLogger(FXWorkerOpPropFunc.class);

public FXWorkerOpPropFunc(TriplifierRegister tr, DatasetGraphCreator dgc){
public FXWorkerOp(TriplifierRegister tr, DatasetGraphCreator dgc) {
super(tr, dgc);
}

@Override
public QueryIterator execute(OpPropFunc op, QueryIterator input, ExecutionContext executionContext, DatasetGraph dg, Properties p) {
return QC.execute(op, input, Utils.getFacadeXExecutionContext(executionContext, p, dg));
public void extractProperties(Properties p, Op op) throws UnboundVariableException {

}

@Override
public void extractProperties(Properties p, OpPropFunc op) throws UnboundVariableException {
// Do nop
public QueryIterator execute(Op op, QueryIterator input, ExecutionContext executionContext, DatasetGraph dg, Properties p) {
return QC.execute(op, input, new ExecutionContext(dg));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,42 @@

package io.github.sparqlanything.engine;

import org.apache.jena.sparql.algebra.Op;
import org.apache.jena.sparql.engine.ExecutionContext;
import org.apache.jena.sparql.util.Symbol;

import java.util.HashSet;
import java.util.Set;

public class FacadeXExecutionContext extends ExecutionContext {

private boolean silent = false;
public static final Symbol hasServiceClause = Symbol.create("has-service");
public static final Symbol processed = Symbol.create("processed");

public FacadeXExecutionContext(ExecutionContext other) {
super(other);
other.getContext().set(hasServiceClause, true);
}

public static boolean isAlreadyProcessed(ExecutionContext ex, Op op){
if(!ex.getContext().isDefined(processed)){
return false;
}
HashSet<Op> processedOps = ex.getContext().get(processed);
return processedOps.contains(op);
}

public static void addProcessedOp(ExecutionContext ex, Op op){
if(!ex.getContext().isDefined(processed)){
ex.getContext().set(processed, new HashSet<Op>());
}
Set<Op> executedOps = ex.getContext().get(processed);
executedOps.add(op);
}





public boolean isSilent() {
return silent;
}
Expand Down
Loading

0 comments on commit 2c255af

Please sign in to comment.