Skip to content

Commit

Permalink
Implement FXWorker for OpPropFunc (see #150)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Oct 19, 2023
1 parent 1bb7efd commit 1b7afae
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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
@@ -0,0 +1,92 @@
package io.github.sparqlanything.engine;

import org.apache.jena.sparql.algebra.op.OpPropFunc;
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> {

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

public FXWorkerOpPropFunc(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));
}

@Override
public void extractProperties(Properties p, OpPropFunc op) throws UnboundVariableException {
// Do nop
}

// @Override
// public QueryIterator execute(OpService op, QueryIterator input, ExecutionContext executionContext, DatasetGraph dg, Properties p) {
// return QC.execute(op.getSubOp(), input, Utils.getFacadeXExecutionContext(executionContext, p, dg));
// }

// @Override
// public void extractProperties(Properties properties, OpService opService) throws UnboundVariableException {
// String url = opService.getService().getURI();
// // Parse IRI only if contains properties
// if (!url.equals(FacadeIRIParser.SPARQL_ANYTHING_URI_SCHEMA)) {
// FacadeIRIParser p = new FacadeIRIParser(url);
// properties.putAll(p.getProperties());
// }
//
// // Setting defaults
// if (!properties.containsKey(IRIArgument.NAMESPACE.toString())) {
// logger.trace("Setting default value for namespace: {}", Triplifier.XYZ_NS);
// properties.setProperty(IRIArgument.NAMESPACE.toString(), Triplifier.XYZ_NS);
// }
// // Setting silent
// if (opService.getSilent()) {
// // we can only see if silent was specified at the OpService so we need to stash
// // a boolean
// // at this point so we can use it when we triplify further down the Op tree
// properties.setProperty(IRIArgument.OP_SERVICE_SILENT.toString(), "true");
// }
//
// Op next = opService.getSubOp();
// FXBGPFinder vis = new FXBGPFinder();
// next.visit(vis);
// logger.trace("Has Table {}", vis.hasTable());
//
// if (vis.getBGP() != null) {
// try {
// PropertyExtractor.extractPropertiesFromBGP(properties, vis.getBGP());
// } catch (UnboundVariableException e) {
// if (vis.hasTable()) {
// logger.trace(vis.getOpTable().toString());
// logger.trace("BGP {}", vis.getBGP());
// logger.trace("Contains variable names {}", vis.getOpTable().getTable().getVarNames().contains(e.getVariableName()));
// if (vis.getOpTable().getTable().getVarNames().contains(e.getVariableName())) {
// e.setOpTable(vis.getOpTable());
// }
// }
//
// if (vis.getOpExtend() != null) {
// logger.trace("OpExtend {}", vis.getOpExtend());
// for (Var var : vis.getOpExtend().getVarExprList().getVars()) {
// if (var.getName().equals(e.getVariableName())) {
// e.setOpExtend(vis.getOpExtend());
// }
// }
// }
//
// throw e;
// }
// logger.trace("Number of properties {}: {}", properties.size(), properties);
// } else {
// logger.trace("Couldn't find OpGraph");
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,46 @@ public class FacadeXOpExecutor extends OpExecutor {
private final FXWorkerOpService fxWorkerService;
private final FXWorkerOpBGP fxWorkerOpBGP;

private final FXWorkerOpPropFunc fxWorkerOpPropFunc ;

public FacadeXOpExecutor(ExecutionContext execCxt) {
super(execCxt);
TriplifierRegister triplifierRegister = TriplifierRegister.getInstance();
DatasetGraphCreator dgc = new DatasetGraphCreator(execCxt);
fxWorkerService = new FXWorkerOpService(triplifierRegister, dgc);
fxWorkerOpBGP = new FXWorkerOpBGP(triplifierRegister, dgc);
fxWorkerOpPropFunc = new FXWorkerOpPropFunc(triplifierRegister, dgc);
}

protected QueryIterator execute(final OpPropFunc opPropFunc, QueryIterator input){
logger.trace("OpProp {}", opPropFunc);
return super.execute(opPropFunc, input);
if (this.execCxt.getClass() == FacadeXExecutionContext.class) {

return super.execute(opPropFunc, input);
}else {
try {
return fxWorkerOpPropFunc.execute(opPropFunc, input, this.execCxt);
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException |
IllegalAccessException | NoSuchMethodException | IOException | UnboundVariableException |
TriplifierHTTPException e) {
throw new RuntimeException(e);
}
}
}

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 {
// // extract properties from service URI
// Properties p = new Properties();
//
// // first extract from execution context
// PropertyExtractor.extractPropertiesFromExecutionContext(this.execCxt, p);
// PropertyExtractor.extractPropertiesFromOp(opBGP, p);
//
// Triplifier t = PropertyExtractor.getTriplifier(p, triplifierRegister);
//
// // Execute with default, bulk method
// DatasetGraph dg = dgc.getDatasetGraph(t, p, opBGP);
// Utils.ensureReadingTxn(dg);
//
// ExecutionContext newExecContext = Utils.getNewExecutionContext(execCxt, p, dg);
//
// List<Triple> magicPropertyTriples = Utils.getFacadeXMagicPropertyTriples(opBGP.getPattern());
// if (!magicPropertyTriples.isEmpty()) {
// return super.execute(Utils.excludeMagicPropertyTriples(Utils.excludeFXProperties(opBGP)), executeMagicProperties(input, magicPropertyTriples, newExecContext));
// } else {
// return QC.execute(Utils.excludeFXProperties(opBGP), input, newExecContext);
// }

return fxWorkerOpBGP.execute(opBGP, input, this.execCxt);

} catch (IOException | InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException | ClassNotFoundException | UnboundVariableException |
TriplifierHTTPException e) {
throw new RuntimeException(e);
}
}



// check that the BGP is within a FacadeX-SERVICE clause
if (this.execCxt.getClass() == FacadeXExecutionContext.class) {
// check that the BGP contains FacadeX Magic properties
Expand Down Expand Up @@ -148,46 +138,6 @@ protected QueryIterator execute(final OpService opService, QueryIterator input)
return super.execute(opService, input);
}

// protected QueryIterator executeDefaultFacadeX(OpService opService, QueryIterator input) throws TriplifierHTTPException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException, UnboundVariableException {
//
// // extract properties from service URI
// Properties p = new Properties();
//
// // first extract from execution context
// PropertyExtractor.extractPropertiesFromExecutionContext(this.execCxt, p);
//
// //then, from opservice (so that can be overwritten)
// PropertyExtractor.extractPropertiesFromOp(opService, p);
//
// // guess triplifier
// Triplifier t = PropertyExtractor.getTriplifier(p, triplifierRegister);
//
// if (t == null) {
// logger.warn("No triplifier found");
// return QueryIterNullIterator.create(execCxt);
// }
//
// // check execution with slicing
// if (PropertyUtils.getBooleanProperty(p, IRIArgument.SLICE)) {
// if (t instanceof Slicer) {
// logger.trace("Execute with slicing");
// return new QueryIterSlicer(execCxt, input, t, p, opService);
// } else {
// logger.warn("Slicing is not supported by triplifier: {}", t.getClass().getName());
// }
// }
//
// // Execute with default, bulk method
// DatasetGraph dg = dgc.getDatasetGraph(t, p, opService.getSubOp());
// Utils.ensureReadingTxn(dg);
//
// return QC.execute(opService.getSubOp(), input, Utils.getFacadeXExecutionContext(execCxt, p, dg));
//
// }




private QueryIterator catchUnboundVariableException(Op op, OpBGP opBGP, QueryIterator input, UnboundVariableException e) {
// Proceed with the next operation
OpBGP fakeBGP = Utils.extractFakePattern(opBGP);
Expand Down

0 comments on commit 1b7afae

Please sign in to comment.