Search in sources :

Example 11 with Predicate

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

the class IntelligentGraphEvaluator method nextTuple.

/**
 * Next tuple.
 *
 * @return the binding set
 * @throws QueryEvaluationException the query evaluation exception
 */
@SuppressWarnings("deprecation")
private BindingSet nextTuple() throws QueryEvaluationException {
    BindingSet nextBindingSet = getEvaluator().next();
    // Iterations.asSet(evaluator)
    QueryBindingSet modifiedBindingSet = new QueryBindingSet();
    for (Binding bindingValue : nextBindingSet) {
        // Only add bindings that were in the original projection
        String modifiedBindingValueName = requiredElement(bindingValue);
        if (modifiedBindingValueName != null) {
            if (bindingValue.getValue().isLiteral()) {
                SimpleLiteral literalValue = (SimpleLiteral) (bindingValue.getValue());
                if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
                    String bindingValueName = bindingValue.getName();
                    List<StatementPattern> statementPatterns = SubjectPredicateCollector.process(getTupleExpr(), bindingValueName);
                    if (!statementPatterns.isEmpty()) {
                        // TODO not all variables need be in projection list so will not match, need the full list of variables to complete this
                        StatementPattern boundStatement = findBound(statementPatterns, nextBindingSet);
                        if (boundStatement != null) {
                            Value subject = getSubjectValue(boundStatement, nextBindingSet);
                            IRI predicate = getPredicateValue(boundStatement, nextBindingSet);
                            ResponseType responseType = getResponseType(bindingValueName);
                            customQueryOptions = getCustomQueryOptions(nextBindingSet);
                            EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
                            switch(responseType) {
                                case VALUE:
                                    Thing subjectThing = Thing.create(getSource(), subject, evaluationContext);
                                    try {
                                        // TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate,	literalValue,customQueryOptions);
                                        com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
                                        Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, fact.getValue());
                                        modifiedBindingSet.addBinding(modifiedBindingValue);
                                    } catch (Exception e) {
                                        Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
                                        modifiedBindingSet.addBinding(modifiedBindingValue);
                                    }
                                    break;
                                case SCRIPT:
                                    modifiedBindingSet.addBinding(bindingValue);
                                    break;
                                case TRACE:
                                    evaluationContext.setTracing(true);
                                    Thing subjectThingTrace = Thing.create(getSource(), subject, evaluationContext);
                                    try {
                                        // TODO subjectThingTrace.getFact(predicate,literalValue,customQueryOptions);
                                        IntelligentEvaluator.processFactObjectValue(subjectThingTrace, predicate, literalValue, customQueryOptions);
                                        Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
                                        modifiedBindingSet.addBinding(modifiedBindingValueTrace);
                                    } catch (Exception e) {
                                        Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
                                        modifiedBindingSet.addBinding(modifiedBindingValueTrace);
                                    }
                                    break;
                            }
                        } else {
                            modifiedBindingSet.addBinding(bindingValue);
                        }
                    } else {
                        EvaluationContext evaluationContext = new EvaluationContext(getCustomQueryOptions(nextBindingSet), getDataset());
                        Thing subjectThing = Thing.create(getSource(), SCRIPT.ANONTHING, evaluationContext);
                        try {
                            // TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(	SCRIPT.ANONPREDICATE,literalValue,null);
                            com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, SCRIPT.ANONPREDICATE, literalValue, customQueryOptions);
                            Binding modifiedBindingValue = new SimpleBinding(bindingValue.getName(), fact.getValue());
                            modifiedBindingSet.addBinding(modifiedBindingValue);
                        } catch (Exception e) {
                            Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
                            modifiedBindingSet.addBinding(modifiedBindingValue);
                        }
                    }
                } else {
                    modifiedBindingSet.addBinding(bindingValue);
                }
            } else {
                if (bindingValue.getName().equals(modifiedBindingValueName))
                    modifiedBindingSet.addBinding(bindingValue);
                else {
                    Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, bindingValue.getValue());
                    modifiedBindingSet.addBinding(modifiedBindingValue);
                }
            }
        }
    }
    return modifiedBindingSet;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) Resource(com.inova8.intelligentgraph.model.Resource) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) NoSuchElementException(java.util.NoSuchElementException) ResponseType(com.inova8.intelligentgraph.sail.IntelligentGraphSail.ResponseType) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) 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 12 with Predicate

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

the class ResourceStatementResults method next.

/**
 * Next.
 *
 * @return the resource
 * @throws QueryEvaluationException the query evaluation exception
 */
@Override
public Resource next() throws QueryEvaluationException {
    if (statementSet != null) {
        Statement next = getStatementSet().next();
        // return Resource.create(thing.getSource(), next.getObject(), getEvaluationContext());
        // Resource predicate, Boolean direction, IRI reification, Boolean isDereified,
        Resource subject = Resource.create(getSource(), next.getSubject(), getEvaluationContext());
        Predicate predicate;
        try {
            predicate = new Predicate(next.getPredicate());
        } catch (URISyntaxException e) {
            throw new QueryEvaluationException(e);
        }
        if (getEvaluationContext() != null)
            getEvaluationContext().getTracer().traceFactNext(thing, predicate, next.getObject());
        return Resource.create(getSource(), subject, predicate, next.getObject(), getEvaluationContext());
    }
    if (localStatementIterator != null) {
        Statement next = localStatementIterator.next();
        return Resource.create(getSource(), next.getObject(), getEvaluationContext());
    }
    return null;
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Statement(org.eclipse.rdf4j.model.Statement) Resource(com.inova8.intelligentgraph.model.Resource) URISyntaxException(java.net.URISyntaxException) Predicate(com.inova8.intelligentgraph.model.Predicate)

Example 13 with Predicate

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

the class IntelligentGraphConnection method deleteThingFacts.

/**
 * Delete thing facts.
 *
 * @param modify the modify
 * @param source the source
 * @param thing the thing
 * @param pathElement the path element
 * @param contexts the contexts
 * @throws PathPatternException the path pattern exception
 */
private void deleteThingFacts(UpdateContext modify, IntelligentGraphRepository source, Thing thing, PathElement pathElement, Resource... contexts) throws PathPatternException {
    CloseableIteration<BindingSet, QueryEvaluationException> resultsIterator = getResultsIterator(source, thing, pathElement, 0, contexts);
    if (((PredicateElement) pathElement).getIsReified()) {
        String reified = ((PredicateElement) pathElement).getReifiedVariable().getName();
        while (resultsIterator.hasNext()) {
            BindingSet bindingSet = resultsIterator.next();
            super.removeStatements((Resource) bindingSet.getBinding(reified).getValue(), null, null, contexts);
        }
    } else {
        String subj = pathElement.getTargetSubject().getName();
        String pred = pathElement.getTargetPredicate().getName();
        String obj = pathElement.getTargetVariable().getName();
        while (resultsIterator.hasNext()) {
            BindingSet bindingSet = resultsIterator.next();
            IRI predicate = (IRI) bindingSet.getBinding(pred).getValue();
            super.removeStatements((Resource) bindingSet.getBinding(subj).getValue(), (IRI) bindingSet.getBinding(pred).getValue(), bindingSet.getBinding(obj).getValue(), contexts);
            checkReificationsChanged(predicate);
        }
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) PredicateElement(com.inova8.pathql.element.PredicateElement)

Example 14 with Predicate

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

the class AlternativePathElement method pathPatternQuery.

/**
 * Path pattern query.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param pathIteration the path iteration
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
@Override
public // }
PathTupleExpr pathPatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
    if (sourceVariable == null)
        sourceVariable = this.getSourceVariable();
    if (targetVariable == null)
        targetVariable = this.getTargetVariable();
    TupleExpr intermediateUnionPattern = null;
    PathTupleExpr alternativePathPattern = null;
    if (getCardinality(pathIteration) > 0) {
        Variable intermediateSourceVariable = null;
        Variable intermediateVariable = null;
        Variable intermediateTargetVariable = null;
        Variable priorIntermediateTargetVariable = null;
        for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
            if (iteration == 1) {
                intermediateSourceVariable = sourceVariable;
                intermediateVariable = new Variable(sourceVariable.getName() + "_i" + iteration);
                // intermediateVariable=  new Variable(getLeftPathElement().getTargetVariable().getName(),getLeftPathElement().getTargetVariable().getValue());   //getLeftPathElement().getTargetVariable();
                intermediateTargetVariable = targetVariable;
            }
            if (iteration < getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = new Variable(sourceVariable.getName() + "_i" + iteration);
                intermediateVariable = new Variable(sourceVariable.getName() + "_i" + iteration);
                priorIntermediateTargetVariable = intermediateTargetVariable;
            }
            if (iteration == getCardinality(pathIteration)) {
                if (iteration > 1) {
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                    // new Variable(sourceVariable.getName() + "_i" + iteration);
                    intermediateVariable = targetVariable;
                    intermediateTargetVariable = targetVariable;
                }
            }
            predicateVariable = deduceLeftPredicateVariable(predicateVariable);
            PathTupleExpr leftPattern = getLeftPathElement().pathPatternQuery(intermediateSourceVariable, predicateVariable, intermediateTargetVariable, pathIteration, customQueryOptions);
            PathTupleExpr rightPattern;
            if (leftPattern == null) {
                intermediateVariable.setValue(intermediateSourceVariable.getValue());
                predicateVariable = deduceRightPredicateVariable(predicateVariable);
                // intermediateVariable.setName(intermediateSourceVariable.getName());
                rightPattern = getRightPathElement().pathPatternQuery(intermediateVariable, predicateVariable, intermediateTargetVariable, pathIteration, customQueryOptions);
            } else {
                predicateVariable = deduceRightPredicateVariable(predicateVariable);
                rightPattern = getRightPathElement().pathPatternQuery(intermediateSourceVariable, predicateVariable, intermediateTargetVariable, pathIteration, customQueryOptions);
            }
            if (leftPattern != null)
                intermediateUnionPattern = new Union(leftPattern.getTupleExpr(), rightPattern.getTupleExpr());
            else {
                intermediateUnionPattern = rightPattern.getTupleExpr();
            }
            if (alternativePathPattern == null) {
                alternativePathPattern = new PathTupleExpr((TupleExpr) intermediateUnionPattern);
                UnionBinding alternativePathPatternBinding = new UnionBinding(leftPattern.getStatementBinding(), rightPattern.getStatementBinding());
                alternativePathPattern.getPath().add(alternativePathPatternBinding);
            // if(leftPattern!=null) alternativePathPattern.getPath().addAll( leftPattern.getPath());
            // alternativePathPattern.getPath().addAll( rightPattern.getPath());
            } else {
                alternativePathPattern.setTupleExpr(new Join(alternativePathPattern.getTupleExpr(), intermediateUnionPattern));
                UnionBinding alternativePathPatternBinding = new UnionBinding(leftPattern.getStatementBinding(), rightPattern.getStatementBinding());
                alternativePathPattern.getPath().add(alternativePathPatternBinding);
            // if(leftPattern!=null)alternativePathPattern.getPath().addAll( leftPattern.getPath());
            // alternativePathPattern.getPath().addAll( rightPattern.getPath());
            }
            alternativePathPattern.setStatementBinding(rightPattern.getStatementBinding());
            alternativePathPattern.setBoundVariable(alternativePathPattern.getStatementBinding().getSourceVariable());
        }
        return alternativePathPattern;
    } else {
        return null;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) UnionBinding(com.inova8.intelligentgraph.path.UnionBinding) Join(org.eclipse.rdf4j.query.algebra.Join) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) Union(org.eclipse.rdf4j.query.algebra.Union)

Example 15 with Predicate

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

the class PredicateElement method pathPredicatePatternTupleExpr.

/**
 * Path predicate pattern tuple expr.
 *
 * @param predicatePattern the predicate pattern
 * @param intermediateSourceVariable the intermediate source variable
 * @param intermediatePredicateVariable the intermediate predicate variable
 * @param intermediateTargetVariable the intermediate target variable
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
private PathTupleExpr pathPredicatePatternTupleExpr(PathTupleExpr predicatePattern, Variable intermediateSourceVariable, Variable intermediatePredicateVariable, Variable intermediateTargetVariable, CustomQueryOptions customQueryOptions) {
    TupleExpr intermediatePredicatePattern;
    Variable predicateVariable;
    if (isNegated) {
        predicateVariable = new Variable(getPredicateSPARQLVariable());
        // TODO
        Variable variable = new Variable("p2", predicate);
        if (isInverseOf) {
            StatementPattern inverseOfPattern = new StatementPattern(intermediateTargetVariable, predicateVariable, intermediateSourceVariable);
            Compare filterExpression = new Compare(predicateVariable, variable, CompareOp.NE);
            intermediatePredicatePattern = new Filter(inverseOfPattern, filterExpression);
        } else {
            intermediatePredicatePattern = new StatementPattern(intermediateSourceVariable, predicateVariable, intermediateTargetVariable);
        }
    } else {
        intermediatePredicateVariable.setValue(predicate);
        // Variable(getPredicateSPARQLVariable(), predicate);
        predicateVariable = intermediatePredicateVariable;
        if (isInverseOf) {
            intermediatePredicatePattern = new StatementPattern(intermediateTargetVariable, predicateVariable, intermediateSourceVariable);
        } else {
            intermediatePredicatePattern = new StatementPattern(intermediateSourceVariable, predicateVariable, intermediateTargetVariable);
        }
    }
    if (objectFilterElement != null) {
        intermediatePredicatePattern = (TupleExpr) objectFilterElement.filterExpression(intermediateTargetVariable, intermediatePredicateVariable, null, intermediatePredicatePattern, customQueryOptions).getTupleExpr();
    }
    // EdgeBinding
    statementBinding = new StatementBinding(intermediateSourceVariable, predicateVariable, intermediateTargetVariable, getIsInverseOf());
    if (predicatePattern == null) {
        predicatePattern = new PathTupleExpr(intermediatePredicatePattern);
    // predicatePattern.setStatementBinding(statementBinding);
    } else {
        predicatePattern.setTupleExpr(new Join(predicatePattern.getTupleExpr(), intermediatePredicatePattern));
    }
    predicatePattern.setStatementBinding(statementBinding);
    predicatePattern.setBoundVariable(predicatePattern.getStatementBinding().getSourceVariable());
    predicatePattern.getPath().add(statementBinding);
    return predicatePattern;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) StatementBinding(com.inova8.intelligentgraph.path.StatementBinding) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Join(org.eclipse.rdf4j.query.algebra.Join) Compare(org.eclipse.rdf4j.query.algebra.Compare) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) 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