Search in sources :

Example 36 with Predicate

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

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

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

the class PredicateElement method visitReifiedPredicatePathBinding.

/**
 * Visit reified predicate path binding.
 *
 * @param pathBinding the path binding
 * @param pathIteration the path iteration
 * @return the path binding
 */
private PathBinding visitReifiedPredicatePathBinding(PathBinding pathBinding, Integer pathIteration) {
    StatementBinding predicateEdge;
    Variable sourceVariable = this.getSourceVariable();
    Variable targetVariable = this.getTargetVariable();
    Variable reificationVariable = this.getReifiedVariable();
    Variable intermediateSourceVariable = null;
    Variable intermediateTargetVariable = null;
    Variable priorIntermediateTargetVariable = null;
    Variable intermediateReificationVariable = null;
    for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
        if (iteration == 1) {
            intermediateSourceVariable = sourceVariable;
        }
        if (iteration < getCardinality(pathIteration)) {
            if (iteration > 1)
                intermediateSourceVariable = priorIntermediateTargetVariable;
            intermediateTargetVariable = new Variable(targetVariable.getName() + "_i" + iteration);
            priorIntermediateTargetVariable = intermediateTargetVariable;
        }
        if (iteration == getCardinality(pathIteration)) {
            if (iteration > 1)
                intermediateSourceVariable = priorIntermediateTargetVariable;
            intermediateTargetVariable = targetVariable;
            intermediateReificationVariable = reificationVariable;
        } else {
            intermediateReificationVariable = new Variable(reificationVariable.getName() + "_i" + iteration);
        }
        predicateEdge = new StatementBinding(intermediateSourceVariable, this.reification, intermediateReificationVariable, getPredicateVariable(), getIsDereified() ? this.getReifiedVariable() : intermediateTargetVariable, getIsInverseOf(), getIsDereified());
        pathBinding.add(predicateEdge);
    }
    return pathBinding;
}
Also used : StatementBinding(com.inova8.intelligentgraph.path.StatementBinding)

Example 39 with Predicate

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

the class PredicateElement method pathReifiedPredicatePatternTupleExpr.

/**
 * Path reified predicate pattern tuple expr.
 *
 * @param predicatePattern the predicate pattern
 * @param sourceVariable the source variable
 * @param intermediatePredicateVariable the intermediate predicate variable
 * @param targetVariable the target variable
 * @param reificationVariable the reification variable
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
private PathTupleExpr pathReifiedPredicatePatternTupleExpr(PathTupleExpr predicatePattern, Variable sourceVariable, Variable intermediatePredicateVariable, Variable targetVariable, Variable reificationVariable, CustomQueryOptions customQueryOptions) {
    ArrayList<Variable> targetVariables = new ArrayList<Variable>();
    if (objectFilterElement != null)
        targetVariables = objectFilterElement.bindTargetVariable(targetVariable, customQueryOptions);
    if (targetVariables.size() == 0)
        targetVariables.add(targetVariable);
    TupleExpr reifiedPredicatePattern = null;
    intermediatePredicateVariable.setValue(getPredicate());
    // getPredicateVariable();
    Variable predicateVariable = intermediatePredicateVariable;
    IRI subject = getReifications().getReificationSubject(reification);
    IRI isSubjectOf = getReifications().getReificationIsSubjectOf(reification);
    if (subject == null && isSubjectOf == null)
        throw new QueryException("Undefined Reification", "No subject/isSubjectOf for reification " + reification.stringValue());
    IRI property = getReifications().getReificationPredicate(reification);
    IRI isPropertyOf = getReifications().getReificationIsPredicateOf(reification);
    if (property == null && isPropertyOf == null)
        throw new QueryException("Undefined Reification", "No property/isPropertyOf for reification " + reification.stringValue());
    IRI object = getReifications().getReificationObject(reification);
    IRI isObjectOf = getReifications().getReificationIsObjectOf(reification);
    if (object == null && isObjectOf == null)
        throw new QueryException("Undefined Reification", "No object/isObjectOf for reification " + reification.stringValue());
    Variable subjectVariable = new Variable("subject" + getExitIndex(), subject);
    Variable isSubjectOfVariable = new Variable("isSubjectOf" + getExitIndex(), isSubjectOf);
    Variable propertyVariable = new Variable("property" + getExitIndex(), property);
    Variable isPropertyOfVariable = new Variable("isPropertyOf" + getExitIndex(), isPropertyOf);
    Variable objectVariable = new Variable("object" + getExitIndex(), object);
    Variable isObjectOfVariable = new Variable("isObjectOf" + getExitIndex(), isObjectOf);
    // Part1
    TupleExpr part1Pattern = null;
    if (isInverseOf) {
        if (object != null && isObjectOf != null) {
            StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
            StatementPattern isObjectOfPattern = new StatementPattern(sourceVariable, isObjectOfVariable, reificationVariable);
            part1Pattern = new Union(objectPattern, isObjectOfPattern);
        } else if (object != null) {
            StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
            part1Pattern = objectPattern;
        } else if (isObjectOf != null) {
            StatementPattern isObjectOfPattern = new StatementPattern(targetVariable, isObjectOfVariable, sourceVariable);
            part1Pattern = isObjectOfPattern;
        } else {
        }
    } else {
        if (subject != null && isSubjectOf != null) {
            StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
            StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
            part1Pattern = new Union(subjectPattern, isSubjectOfPattern);
        } else if (subject != null) {
            StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
            part1Pattern = subjectPattern;
        } else if (isSubjectOf != null) {
            StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
            part1Pattern = isSubjectOfPattern;
        } else {
        }
    }
    // Part2
    TupleExpr part2Pattern = null;
    if (property != null && isPropertyOf != null) {
        StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
        StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
        part2Pattern = new Union(propertyPattern, isPropertyOfPattern);
    } else if (property != null) {
        StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
        part2Pattern = propertyPattern;
    } else if (isPropertyOf != null) {
        StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
        part2Pattern = isPropertyOfPattern;
    } else {
    }
    TupleExpr part12Pattern = null;
    if (part1Pattern != null && part2Pattern != null)
        part12Pattern = new Join(part1Pattern, part2Pattern);
    else if (part1Pattern != null)
        part12Pattern = part1Pattern;
    else if (part2Pattern != null)
        part12Pattern = part2Pattern;
    // Part3
    TupleExpr part3Pattern = null;
    for (Variable theTargetVariable : targetVariables) {
        Variable aTargetVariable = new Variable(theTargetVariable.getName(), theTargetVariable.getValue());
        TupleExpr aTargetPattern = null;
        if (isInverseOf) {
            if (subject != null && isSubjectOf != null) {
                StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
                StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
                aTargetPattern = new Union(subjectPattern, isSubjectOfPattern);
            } else if (subject != null) {
                StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
                aTargetPattern = subjectPattern;
            } else if (isSubjectOf != null) {
                StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
                aTargetPattern = isSubjectOfPattern;
            } else {
            }
        } else {
            if (object != null && isObjectOf != null) {
                StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
                StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
                aTargetPattern = new Union(objectPattern, isObjectOfPattern);
            } else if (object != null) {
                StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
                aTargetPattern = objectPattern;
            } else if (isObjectOf != null) {
                StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
                aTargetPattern = isObjectOfPattern;
            } else {
            }
        }
        if (part3Pattern != null) {
            Join newPart3Pattern = new Join(part3Pattern, aTargetPattern);
            part3Pattern = newPart3Pattern;
        } else {
            part3Pattern = aTargetPattern;
        }
    }
    if (part12Pattern != null && part3Pattern != null)
        reifiedPredicatePattern = new Join(part12Pattern, part3Pattern);
    else if (part12Pattern != null)
        reifiedPredicatePattern = part12Pattern;
    else if (part3Pattern != null)
        reifiedPredicatePattern = part3Pattern;
    Variable objectFilterTargetVariable;
    if (objectFilterElement != null) {
        objectFilterTargetVariable = new Variable(targetVariable.getName(), targetVariable.getValue());
        reifiedPredicatePattern = objectFilterElement.filterExpression(objectFilterTargetVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
    }
    if (statementFilterElement != null) {
        reifiedPredicatePattern = statementFilterElement.filterExpression(reificationVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
    }
    StatementBinding statementBinding = new StatementBinding(sourceVariable, getReification(), reificationVariable, predicateVariable, getIsDereified() ? reificationVariable : targetVariable, getIsInverseOf(), getIsDereified());
    if (predicatePattern == null) {
        predicatePattern = new PathTupleExpr(reifiedPredicatePattern);
    } else {
        predicatePattern.setTupleExpr(new Join(predicatePattern.getTupleExpr(), reifiedPredicatePattern));
    }
    predicatePattern.setStatementBinding(statementBinding);
    predicatePattern.setBoundVariable(predicatePattern.getStatementBinding().getSourceVariable());
    predicatePattern.getPath().add(statementBinding);
    return predicatePattern;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryException(com.inova8.intelligentgraph.exceptions.QueryException) StatementBinding(com.inova8.intelligentgraph.path.StatementBinding) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) ArrayList(java.util.ArrayList) Join(org.eclipse.rdf4j.query.algebra.Join) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Example 40 with Predicate

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

the class PredicateElement method pathReifiedPredicatePatternQuery.

/**
 * Path reified predicate pattern query.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param reificationVariable the reification variable
 * @param pathIteration the path iteration
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
private PathTupleExpr pathReifiedPredicatePatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Variable reificationVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
    PathTupleExpr predicatePattern = null;
    if (getCardinality(pathIteration) > 0) {
        Variable intermediateSourceVariable = null;
        Variable intermediatePredicateVariable = null;
        Variable intermediateTargetVariable = null;
        Variable priorIntermediateTargetVariable = null;
        Variable intermediateReificationVariable = null;
        for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
            if (iteration == 1) {
                intermediateSourceVariable = sourceVariable;
            }
            if (iteration < getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = new Variable(targetVariable.getName() + "_i" + iteration);
                priorIntermediateTargetVariable = intermediateTargetVariable;
            }
            if (iteration == getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = targetVariable;
                intermediateReificationVariable = reificationVariable;
            } else {
                intermediateReificationVariable = new Variable(reificationVariable.getName() + "_i" + iteration);
            }
            // intermediatePredicateVariable = new Variable("p_"+ sourceVariable.getName()+"_"+targetVariable.getName());
            intermediatePredicateVariable = deducePredicateVariable(sourceVariable, predicateVariable, targetVariable);
            predicatePattern = pathReifiedPredicatePatternTupleExpr(predicatePattern, intermediateSourceVariable, intermediatePredicateVariable, intermediateTargetVariable, intermediateReificationVariable, customQueryOptions);
        }
        // predicatePattern.getPath().addAll(getPathBindings().get(pathIteration));
        if (getIsReified()) {
            this.targetVariable = intermediateReificationVariable;
        }
        return predicatePattern;
    } else {
        return null;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr)

Aggregations

Order (org.junit.jupiter.api.Order)26 Test (org.junit.jupiter.api.Test)26 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)26 PathElement (com.inova8.pathql.element.PathElement)22 IRI (org.eclipse.rdf4j.model.IRI)15 Thing (com.inova8.intelligentgraph.model.Thing)12 PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)10 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)10 RecognitionException (org.antlr.v4.runtime.RecognitionException)9 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)8 CharStream (org.antlr.v4.runtime.CharStream)8 Statement (org.eclipse.rdf4j.model.Statement)8 Value (org.eclipse.rdf4j.model.Value)8 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)7 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)7 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)6 Resource (com.inova8.intelligentgraph.model.Resource)6 Join (org.eclipse.rdf4j.query.algebra.Join)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 PathPatternException (com.inova8.pathql.processor.PathPatternException)5