use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class FactDebug method evaluate.
/**
* Evaluate.
*
* @param tripleSource the triple source
* @param args the args
* @return the value
* @throws ValueExprEvaluationException the value expr evaluation exception
*/
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
// logger.debug("Trace Evaluate for <{}>, {} with args <{}>",tripleSource, tripleSource.getValueFactory(),Arrays.toString(args));
if (args.length < 3) {
String message = "At least subject, predicate, and script arguments required";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
} else {
IRI subject;
IRI predicate;
SimpleLiteral scriptLiteral;
try {
subject = (IRI) args[0];
predicate = (IRI) args[1];
scriptLiteral = (SimpleLiteral) args[2];
} catch (Exception e) {
String message = "Subject and predicate must be valid IRI, and script must be a literal";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message.toString());
}
try {
Value[] argumentArray = Arrays.copyOfRange(args, 3, args.length);
IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
evaluationContext.setTracing(true);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, scriptLiteral, customQueryOptions);
// TODO subjectThing.getFact(predicate, scriptLiteral,customQueryOptions);
logger.debug("Trace\r\n" + evaluationContext.getTrace());
return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class FactProvenance method evaluate.
/**
* Evaluate.
*
* @param tripleSource the triple source
* @param args the args
* @return the value
* @throws ValueExprEvaluationException the value expr evaluation exception
*/
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
logger.debug("Trace Evaluate for <{}>, {} with args <{}>", tripleSource, tripleSource.getValueFactory(), args);
if (args.length < 2) {
String message = "At least subject, and predicate arguments required";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
} else {
IRI subject;
IRI predicate;
try {
subject = (IRI) args[0];
predicate = (IRI) args[1];
} catch (Exception e) {
String message = "Subject and predicate must be valid IRI";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
}
try {
Value[] argumentArray = Arrays.copyOfRange(args, 2, args.length);
IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
evaluationContext.setTracing(true);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
// olgap.Value fact =
// PredicateElement(source,predicate));
subjectThing.getFact("<" + predicate.stringValue() + ">");
logger.debug("Trace\r\n" + evaluationContext.getTrace());
return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class FactValue method evaluate.
/**
* Evaluate.
*
* @param tripleSource the triple source
* @param args the args
* @return the value
* @throws ValueExprEvaluationException the value expr evaluation exception
*/
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
logger.debug("Evaluate for {} with args <{}>", tripleSource.getValueFactory(), args);
if (args.length < 2) {
String message = "At least subject, and predicate arguments required";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
} else {
IRI subject;
IRI predicate;
try {
subject = (IRI) args[0];
predicate = (IRI) args[1];
} catch (Exception e) {
String message = "Subject and predicate must be valid IRI";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message.toString());
}
try {
Value[] argumentArray = Arrays.copyOfRange(args, 2, args.length);
IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
// new PredicateElement(source,predicate));
com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact("<" + predicate.stringValue() + ">");
if (fact != null && fact.getValue() != null) {
Value result = fact.getValue();
logger.debug("FactValue = {}", result);
return result;
} else {
return tripleSource.getValueFactory().createLiteral("");
}
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphEvaluator method nextGraph.
/**
* Next graph.
*
* @return the binding set
*/
private BindingSet nextGraph() {
BindingSet nextBindingSet = getEvaluator().next();
try {
if (nextBindingSet.hasBinding("subject") && nextBindingSet.hasBinding("predicate") && nextBindingSet.hasBinding("object")) {
if (nextBindingSet.getValue("object").isLiteral()) {
SimpleLiteral literalValue = (SimpleLiteral) (nextBindingSet.getValue("object"));
if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
QueryBindingSet modifiedBindingSet = new QueryBindingSet();
modifiedBindingSet.addBinding(nextBindingSet.getBinding("subject"));
modifiedBindingSet.addBinding(nextBindingSet.getBinding("predicate"));
IntelligentGraphRepository source = getSource();
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
Thing subjectThing = Thing.create(source, nextBindingSet.getValue("subject"), evaluationContext);
try {
// TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( (IRI) nextBindingSet.getValue("predicate"), literalValue,customQueryOptions);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, (IRI) nextBindingSet.getValue("predicate"), literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding("object", fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
return modifiedBindingSet;
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding("object", literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
// literal(e.getMessage()));
modifiedBindingSet.addBinding(modifiedBindingValue);
return modifiedBindingSet;
}
} else {
return locateCustomQueryOptions(nextBindingSet);
}
} else {
return locateCustomQueryOptions(nextBindingSet);
}
} else {
// Incomplete s p o within dataset so could not calculate
return nextBindingSet;
}
} catch (Exception e) {
// Should not be any exceptions that are not handled, but even so ...
return nextBindingSet;
}
}
use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphEvaluator method nextTuple.
/**
* Next tuple.
*
* @return the binding set
* @throws QueryEvaluationException the query evaluation exception
*/
@SuppressWarnings("deprecation")
private BindingSet nextTuple() throws QueryEvaluationException {
BindingSet nextBindingSet = getEvaluator().next();
// Iterations.asSet(evaluator)
QueryBindingSet modifiedBindingSet = new QueryBindingSet();
for (Binding bindingValue : nextBindingSet) {
// Only add bindings that were in the original projection
String modifiedBindingValueName = requiredElement(bindingValue);
if (modifiedBindingValueName != null) {
if (bindingValue.getValue().isLiteral()) {
SimpleLiteral literalValue = (SimpleLiteral) (bindingValue.getValue());
if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
String bindingValueName = bindingValue.getName();
List<StatementPattern> statementPatterns = SubjectPredicateCollector.process(getTupleExpr(), bindingValueName);
if (!statementPatterns.isEmpty()) {
// TODO not all variables need be in projection list so will not match, need the full list of variables to complete this
StatementPattern boundStatement = findBound(statementPatterns, nextBindingSet);
if (boundStatement != null) {
Value subject = getSubjectValue(boundStatement, nextBindingSet);
IRI predicate = getPredicateValue(boundStatement, nextBindingSet);
ResponseType responseType = getResponseType(bindingValueName);
customQueryOptions = getCustomQueryOptions(nextBindingSet);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
switch(responseType) {
case VALUE:
Thing subjectThing = Thing.create(getSource(), subject, evaluationContext);
try {
// TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate, literalValue,customQueryOptions);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
modifiedBindingSet.addBinding(modifiedBindingValue);
}
break;
case SCRIPT:
modifiedBindingSet.addBinding(bindingValue);
break;
case TRACE:
evaluationContext.setTracing(true);
Thing subjectThingTrace = Thing.create(getSource(), subject, evaluationContext);
try {
// TODO subjectThingTrace.getFact(predicate,literalValue,customQueryOptions);
IntelligentEvaluator.processFactObjectValue(subjectThingTrace, predicate, literalValue, customQueryOptions);
Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
modifiedBindingSet.addBinding(modifiedBindingValueTrace);
} catch (Exception e) {
Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
modifiedBindingSet.addBinding(modifiedBindingValueTrace);
}
break;
}
} else {
modifiedBindingSet.addBinding(bindingValue);
}
} else {
EvaluationContext evaluationContext = new EvaluationContext(getCustomQueryOptions(nextBindingSet), getDataset());
Thing subjectThing = Thing.create(getSource(), SCRIPT.ANONTHING, evaluationContext);
try {
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( SCRIPT.ANONPREDICATE,literalValue,null);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, SCRIPT.ANONPREDICATE, literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding(bindingValue.getName(), fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
modifiedBindingSet.addBinding(modifiedBindingValue);
}
}
} else {
modifiedBindingSet.addBinding(bindingValue);
}
} else {
if (bindingValue.getName().equals(modifiedBindingValueName))
modifiedBindingSet.addBinding(bindingValue);
else {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, bindingValue.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
}
}
}
}
return modifiedBindingSet;
}
Aggregations