use of com.inova8.intelligentgraph.evaluator.EvaluationContext in project com.inova8.intelligentgraph by peterjohnlawrence.
the class ObjectProvenance 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.trace("Evaluate for <{}> with args <{}>", tripleSource, args);
if (args.length < 3) {
String message = "At least subject,predicate, and objectscript 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);
}
SimpleLiteral literalValue;
try {
literalValue = (SimpleLiteral) args[2];
if (isScriptEngine(literalValue.getDatatype())) {
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);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( predicate,literalValue,customQueryOptions);
if (fact != null) {
fact.getValue();
logger.debug("Trace\r\n" + evaluationContext.getTrace());
return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
} else {
return null;
}
} else {
return args[2];
}
} 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 ObjectValue 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, tripleSource.getValueFactory(), args);
if (args.length < 3) {
String message = "At least subject,predicate, and objectscript 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 = String.format("Subject and predicate must be valid IRI. Subject %s, Object %s", args[0], args[1]);
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
}
SimpleLiteral literalValue;
try {
literalValue = (SimpleLiteral) args[2];
if (isScriptEngine(literalValue.getDatatype())) {
Value[] argumentArray = Arrays.copyOfRange(args, 3, args.length);
// sources.getSource(tripleSource, argumentArray );
IntelligentGraphRepository source = IntelligentGraphRepository.create(tripleSource);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
// Thing subjectThing = source.thingFactory( null, subject, new Stack<String>(),customQueryOptions);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate,literalValue,customQueryOptions);
if (fact != null) {
Value result = fact.getValue();
// source.writeModelToCache(result, cacheContext);
logger.debug("ObjectValue = {}", result);
return result;
} else {
return null;
}
} else {
return args[2];
}
} 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 Thing method create.
/**
* Creates the.
*
* @param source the source
* @param graphIri the graph iri
* @param superValue the super value
* @param evaluationContext the evaluation context
* @return the thing
*/
@SuppressWarnings("deprecation")
public static Thing create(IntelligentGraphRepository source, IRI graphIri, org.eclipse.rdf4j.model.Value superValue, EvaluationContext evaluationContext) {
Thing thing;
// graphIri.stringValue()+"~"+ superValue.stringValue();
String graphThingKey = superValue.stringValue();
if (superValue != null && source != null && source.getThings().containsKey(graphThingKey)) {
thing = source.getThings().get(graphThingKey);
thing.setSource(source);
if (evaluationContext != null) {
// if(thing.evaluationContext.getPrefixes()==null || thing.evaluationContext.getPrefixes().isEmpty())thing.evaluationContext.setPrefixes(evaluationContext.getPrefixes());
if (evaluationContext.getCustomQueryOptions() != null && !evaluationContext.getCustomQueryOptions().isEmpty())
thing.evaluationContext.setCustomQueryOptions(evaluationContext.getCustomQueryOptions());
if (evaluationContext.getTracer() != null && evaluationContext.getTracer().isTracing())
thing.evaluationContext.setTracer(evaluationContext.getTracer());
if (evaluationContext.getDataset() != null)
thing.evaluationContext.setDataset(evaluationContext.getDataset());
thing.evaluationContext.setContexts(evaluationContext.getContexts());
}
// Overwrite the graphName if not null
if (graphIri != null)
thing.graphName = graphIri;
return thing;
} else {
thing = new Thing(source, superValue, evaluationContext);
if (source != null)
source.getThings().put(graphThingKey, thing);
if (graphIri == null)
graphIri = Graph.DEFAULTGRAPH;
thing.graphName = graphIri;
}
if (evaluationContext != null)
thing.evaluationContext = evaluationContext;
else if (thing.evaluationContext == null)
thing.evaluationContext = new EvaluationContext();
return thing;
}
Aggregations