Search in sources :

Example 21 with Fact

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());
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) ValueExprEvaluationException(org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext) Thing(com.inova8.intelligentgraph.model.Thing)

Example 22 with Fact

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);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) ReificationType(com.inova8.pathql.context.ReificationType) PredicateElement(com.inova8.pathql.element.PredicateElement) Thing(com.inova8.intelligentgraph.model.Thing)

Example 23 with Fact

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));
    }
}
Also used : NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults)

Example 24 with Fact

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());
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) Resource(com.inova8.intelligentgraph.model.Resource) PathBinding(com.inova8.intelligentgraph.path.PathBinding) Fact(com.inova8.intelligentgraph.model.Fact)

Example 25 with Fact

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;
}
Also used : Resource(com.inova8.intelligentgraph.model.Resource) Value(org.eclipse.rdf4j.model.Value) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral)

Aggregations

Thing (com.inova8.intelligentgraph.model.Thing)19 Order (org.junit.jupiter.api.Order)11 Test (org.junit.jupiter.api.Test)11 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)11 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)9 Resource (com.inova8.intelligentgraph.model.Resource)8 IRI (org.eclipse.rdf4j.model.IRI)8 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)6 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)6 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)6 Value (org.eclipse.rdf4j.model.Value)6 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)6 Trace (com.inova8.intelligentgraph.evaluator.Trace)5 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)4 NullValueReturnedException (com.inova8.intelligentgraph.exceptions.NullValueReturnedException)3 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)3 PredicateElement (com.inova8.pathql.element.PredicateElement)3 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 ReificationType (com.inova8.pathql.context.ReificationType)2 NoSuchElementException (java.util.NoSuchElementException)2