Search in sources :

Example 6 with EvaluationContext

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());
        }
    }
}
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 7 with EvaluationContext

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());
        }
    }
}
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 8 with EvaluationContext

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;
}
Also used : EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext)

Aggregations

EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)8 Thing (com.inova8.intelligentgraph.model.Thing)7 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)6 IRI (org.eclipse.rdf4j.model.IRI)6 Value (org.eclipse.rdf4j.model.Value)6 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)5 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)5 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)5 Resource (com.inova8.intelligentgraph.model.Resource)2 NoSuchElementException (java.util.NoSuchElementException)2 Binding (org.eclipse.rdf4j.query.Binding)2 BindingSet (org.eclipse.rdf4j.query.BindingSet)2 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)2 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)2 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)2 ResponseType (com.inova8.intelligentgraph.sail.IntelligentGraphSail.ResponseType)1 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1