use of com.inova8.intelligentgraph.model.Fact 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.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphConnection method addFact.
/**
* Adds the fact.
*
* @param thingresource the thingresource
* @param pathQL the path QL
* @param value the value
* @param contexts the contexts
* @throws RecognitionException the recognition exception
* @throws PathPatternException the path pattern exception
*/
private void addFact(Resource thingresource, String pathQL, Value value, Resource... contexts) throws RecognitionException, PathPatternException {
IntelligentGraphRepository source = IntelligentGraphRepository.create(this);
Thing thing = Thing.create(source, thingresource, null);
PredicateElement predicateElement = (PredicateElement) PathParser.parsePathPattern(source.getRepositoryContext(), pathQL);
predicateElement.getSourceVariable().setValue(thing.getValue());
if (predicateElement.getIsReified()) {
ReificationType reificationType = source.getRepositoryContext().getReificationTypes().get(predicateElement.getReification().stringValue());
if (reificationType != null) {
IRI reification = iri(reificationType.getReificationType().stringValue() + "/" + thing.getIRI().hashCode() + "." + predicateElement.getPredicate().hashCode() + "." + value.hashCode());
super.addStatement(reification, RDF.TYPE, predicateElement.getReification(), contexts);
super.addStatement(reification, reificationType.getReificationSubject(), thing.getIRI(), contexts);
super.addStatement(reification, reificationType.getReificationPredicate(), predicateElement.getPredicate(), contexts);
super.addStatement(reification, reificationType.getReificationObject(), value, contexts);
} else {
logger.error("Reified type not supported:" + predicateElement.toString());
}
} else {
IRI propertyIri = predicateElement.getPredicate();
super.addStatement(thing.getIRI(), propertyIri, value, contexts);
checkReificationsChanged(propertyIri);
}
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Thing method getFact.
// public Resource getFact(IRI predicate, SimpleLiteral scriptString, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource ... contexts) throws HandledException {
// Resource fact = processFactObjectValue(predicate,scriptString, customQueryOptions ,contexts);
// return fact;
/**
* Gets the fact.
*
* @param predicatePattern the predicate pattern
* @param customQueryOptions the custom query options
* @param bindValues the bind values
* @return the fact
*/
// }
public Resource getFact(String predicatePattern, CustomQueryOptions customQueryOptions, Value... bindValues) {
logger.debug("getFact{}\n", predicatePattern);
ResourceResults factValues = getFacts(predicatePattern, customQueryOptions, bindValues);
if (factValues == null) {
throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
} else if (factValues.hasNext()) {
return factValues.next();
} else {
factValues.close();
throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
}
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class FactResults method nextResource.
/**
* Next resource.
*
* @return the resource
* @throws QueryEvaluationException the query evaluation exception
*/
@Override
public Resource nextResource() throws QueryEvaluationException {
BindingSet next = getResourceSet().next();
PathBinding path = new PathBinding();
path = pathElement.visitPathBinding(path, 0);
// for(Edge edge:path) {
// TODO
// Value sourceValue = next.getBinding(edge.getSourceVariable().getName()).getValue();
// Value predicateValue = next.getBinding(edge.getPredicateVariable().getName()).getValue();
// Value reificationValue = edge.getReification();
// Value targetValue = next.getBinding(edge.getTargetVariable().getName()).getValue();
// Boolean direction = edge.isInverse();
// Boolean isDereified = edge.getIsDereified();
// Fact edgeFact = new Fact(sourceValue, predicateValue, targetValue);
// }
Resource objectValue = calculateValue(next);
return new Fact(next.getBinding(getPathElement().getTargetSubject().getName()).getValue(), next.getBinding(getPathElement().getTargetPredicate().getName()).getValue(), objectValue.getValue());
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentEvaluator method processFactObjectValue.
/**
* Process fact object value.
*
* @param thing the thing
* @param predicate the predicate
* @param objectValue the object value
* @param customQueryOptions the custom query options
* @param contexts the contexts
* @return the resource
* @throws QueryEvaluationException the query evaluation exception
*/
public static Resource processFactObjectValue(Thing thing, IRI predicate, Value objectValue, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource... contexts) throws QueryEvaluationException {
Resource returnResult = null;
SimpleLiteral literalValue;
{
literalValue = (SimpleLiteral) objectValue;
if (Evaluator.isScriptEngine(literalValue.getDatatype())) {
org.eclipse.rdf4j.model.Resource customQueryOptionsContext;
if (customQueryOptions != null)
customQueryOptionsContext = customQueryOptions.getContext();
else
customQueryOptionsContext = CustomQueryOptions.getEmptyContext();
FactCache factCache = thing.getSource().getIntelligentGraphConnection().getIntelligentGraphSail().getFactCache();
if (/*notTracing() && */
factCache.contains(thing.getIRI(), predicate, customQueryOptionsContext)) {
Value resultValue = factCache.getFacts(thing.getIRI(), predicate, customQueryOptionsContext);
logger.debug("Retrieved cache {} of {} = {}", predicate.stringValue(), thing.getSuperValue().stringValue(), resultValue.stringValue());
thing.getEvaluationContext().getTracer().traceRetrievedCache(thing, predicate, resultValue);
returnResult = Resource.create(thing.getSource(), resultValue, thing.getEvaluationContext());
} else {
Resource result = handleScript(thing, literalValue, predicate, customQueryOptions, contexts);
if (result != null) {
// TODO validate caching
factCache.addFact(thing.getIRI(), predicate, result.getSuperValue(), customQueryOptionsContext);
thing.getEvaluationContext().getTracer().traceCalculated(thing, predicate, result);
}
returnResult = result;
}
} else {
thing.getEvaluationContext().getTracer().traceRetrievedLiteral(thing, predicate, objectValue);
returnResult = Resource.create(thing.getSource(), objectValue, thing.getEvaluationContext());
}
}
return returnResult;
}
Aggregations