Search in sources :

Example 6 with com.inova8.intelligentgraph.model

use of com.inova8.intelligentgraph.model 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 com.inova8.intelligentgraph.model

use of com.inova8.intelligentgraph.model 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 com.inova8.intelligentgraph.model

use of com.inova8.intelligentgraph.model in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphRepository method getCustomQueryOptions.

/**
 * Gets the custom query options.
 *
 * @param customQueryOptionsArray the custom query options array
 * @return the custom query options
 */
public CustomQueryOptions getCustomQueryOptions(Value[] customQueryOptionsArray) {
    CustomQueryOptions customQueryOptions = CustomQueryOptions.create(this, customQueryOptionsArray);
    if (customQueryOptions != null) {
        if (customQueryOptions.contains("service")) {
            com.inova8.intelligentgraph.model.Resource service = customQueryOptions.get("service");
            String serviceURL = service.toString();
            String serviceIRI = null;
            if (serviceURL.indexOf('?') > 0) {
                serviceIRI = serviceURL.substring(0, serviceURL.indexOf('?'));
            }
            if (serviceIRI != null)
                setCacheService(serviceIRI);
        }
    }
    return customQueryOptions;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions)

Example 9 with com.inova8.intelligentgraph.model

use of com.inova8.intelligentgraph.model in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphRepository method getFact.

public com.inova8.intelligentgraph.model.Resource getFact(String boundPredicatePattern, CustomQueryOptions customQueryOptions) {
    logger.debug("getFact{}\n", boundPredicatePattern);
    ResourceResults factValues = getFacts(boundPredicatePattern, customQueryOptions);
    if (factValues == null) {
        // return new NullResource();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with customQueryOptions %s", boundPredicatePattern, customQueryOptions));
    } else if (factValues.hasNext()) {
        return factValues.next();
    } else {
        factValues.close();
        // return new NullResource();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with customQueryOptions %s", boundPredicatePattern, customQueryOptions));
    }
}
Also used : NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults)

Aggregations

CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)6 Thing (com.inova8.intelligentgraph.model.Thing)6 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)5 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)5 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)4 IRI (org.eclipse.rdf4j.model.IRI)4 Value (org.eclipse.rdf4j.model.Value)4 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)3 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 NullValueReturnedException (com.inova8.intelligentgraph.exceptions.NullValueReturnedException)1 com.inova8.intelligentgraph.model (com.inova8.intelligentgraph.model)1 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)1 ResponseType (com.inova8.intelligentgraph.sail.IntelligentGraphSail.ResponseType)1 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)1