Search in sources :

Example 1 with PathTupleExpr

use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphConnection method getResultsIterator.

/**
 * Gets the results iterator.
 *
 * @param source the source
 * @param thing the thing
 * @param pathElement the path element
 * @param pathTupleExpr the path tuple expr
 * @param contexts the contexts
 * @return the results iterator
 * @throws IllegalArgumentException the illegal argument exception
 * @throws QueryEvaluationException the query evaluation exception
 */
CloseableIteration<BindingSet, QueryEvaluationException> getResultsIterator(IntelligentGraphRepository source, Thing thing, PathElement pathElement, PathTupleExpr pathTupleExpr, Resource... contexts) throws IllegalArgumentException, QueryEvaluationException {
    TupleExpr tupleExpr = pathTupleExpr.getTupleExpr();
    SimpleDataset dataset = prepareDataset(pathElement, source, contexts);
    BindingSet bindings = new QueryBindingSet();
    EvaluationStrategy evaluationStrategy = new StrictEvaluationStrategy(source.getTripleSource(), dataset, null);
    CloseableIteration<BindingSet, QueryEvaluationException> resultsIterator = evaluationStrategy.evaluate(tupleExpr, bindings);
    return resultsIterator;
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) EvaluationStrategy(org.eclipse.rdf4j.query.algebra.evaluation.EvaluationStrategy) StrictEvaluationStrategy(org.eclipse.rdf4j.query.algebra.evaluation.impl.StrictEvaluationStrategy) StrictEvaluationStrategy(org.eclipse.rdf4j.query.algebra.evaluation.impl.StrictEvaluationStrategy) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 2 with PathTupleExpr

use of com.inova8.intelligentgraph.path.PathTupleExpr 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 3 with PathTupleExpr

use of com.inova8.intelligentgraph.path.PathTupleExpr 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)

Example 4 with PathTupleExpr

use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.

the class SequencePathElement 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 intermediateJoinPattern = null;
    PathTupleExpr joinPattern = 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;
                // getLeftPathElement().getTargetVariable();
                intermediateVariable = new Variable(getLeftPathElement().getTargetVariable().getName(), getLeftPathElement().getTargetVariable().getValue());
                intermediateTargetVariable = targetVariable;
            }
            if (iteration < getCardinality(pathIteration)) {
                if (iteration > 1)
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                intermediateTargetVariable = new Variable(sourceVariable.getName() + "_i" + iteration);
                intermediateVariable = new Variable(sourceVariable.getName() + "_in" + iteration);
                priorIntermediateTargetVariable = intermediateTargetVariable;
            }
            if (iteration == getCardinality(pathIteration)) {
                if (iteration > 1) {
                    intermediateSourceVariable = priorIntermediateTargetVariable;
                    intermediateVariable = new Variable(sourceVariable.getName() + "_in" + iteration);
                    intermediateTargetVariable = targetVariable;
                }
            }
            PathTupleExpr leftPattern = getLeftPathElement().pathPatternQuery(intermediateSourceVariable, predicateVariable, intermediateVariable, pathIteration, customQueryOptions);
            PathTupleExpr rightPattern;
            if (leftPattern == null) {
                intermediateVariable.setValue(intermediateSourceVariable.getValue());
                // intermediateVariable.setName(intermediateSourceVariable.getName());
                rightPattern = getRightPathElement().pathPatternQuery(intermediateVariable, predicateVariable, intermediateTargetVariable, pathIteration, customQueryOptions);
            } else {
                rightPattern = getRightPathElement().pathPatternQuery(leftPattern.getStatementBinding().getTargetVariable(), predicateVariable, intermediateTargetVariable, pathIteration, customQueryOptions);
            }
            if (leftPattern != null)
                if (rightPattern != null)
                    intermediateJoinPattern = new Join(leftPattern.getTupleExpr(), rightPattern.getTupleExpr());
                else {
                    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    intermediateVariable.setName(intermediateTargetVariable.getName());
                    // Projection bindRight = new Projection((TupleExpr) intermediateVariable);
                    intermediateJoinPattern = leftPattern.getTupleExpr();
                }
            else {
                if (rightPattern != null)
                    intermediateJoinPattern = rightPattern.getTupleExpr();
                else
                    intermediateJoinPattern = null;
            }
            if (joinPattern == null) {
                joinPattern = new PathTupleExpr((TupleExpr) intermediateJoinPattern);
                if (leftPattern != null) {
                    joinPattern.getPath().addAll(leftPattern.getPath());
                    joinPattern.setStatementBinding(leftPattern.getStatementBinding());
                }
                if (rightPattern != null) {
                    joinPattern.getPath().addAll(rightPattern.getPath());
                    joinPattern.setStatementBinding(rightPattern.getStatementBinding());
                }
            } else {
                joinPattern.setTupleExpr(new Join(joinPattern.getTupleExpr(), intermediateJoinPattern));
                if (leftPattern != null) {
                    joinPattern.getPath().addAll(leftPattern.getPath());
                    joinPattern.setStatementBinding(leftPattern.getStatementBinding());
                }
                if (rightPattern != null) {
                    joinPattern.getPath().addAll(rightPattern.getPath());
                    joinPattern.setStatementBinding(rightPattern.getStatementBinding());
                }
            }
        }
        joinPattern.setBoundVariable(joinPattern.getStatementBinding().getSourceVariable());
        return joinPattern;
    } else {
        return null;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Join(org.eclipse.rdf4j.query.algebra.Join) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 5 with PathTupleExpr

use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.

the class MultiPathPatternQueryExpressionTests method test_8.

/**
 * Test 8.
 */
@Test
@Order(8)
void test_8() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, "(^:hasProductBatteryLimit/:massThroughput){1, 2}/*");
        PathTupleExpr pathTupleExpr = element.pathPatternQuery(1, null);
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   Join\r\n" + "      Join\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0_in1)\r\n" + "            Variable (name=p_n0_n0_in1, value=http://default/hasProductBatteryLimit)\r\n" + "            Variable (name=n0)\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0_in1)\r\n" + "            Variable (name=p_n0_in1_n0_i1, value=http://default/massThroughput)\r\n" + "            Variable (name=n0_i1)\r\n" + "      Join\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0_in2)\r\n" + "            Variable (name=p_n0_i1_n0_in2, value=http://default/hasProductBatteryLimit)\r\n" + "            Variable (name=n0_i1)\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0_in2)\r\n" + "            Variable (name=p_n0_in2_n2, value=http://default/massThroughput)\r\n" + "            Variable (name=n2)\r\n" + "   StatementPattern\r\n" + "      Variable (name=n2)\r\n" + "      Variable (name=p_n2_n3)\r\n" + "      Variable (name=n3)\r\n" + "", pathTupleExpr.toString());
        assertEquals("[n0,http://default/hasProductBatteryLimit,n0_in1,INVERSE]\r\n" + "[n0_in1,http://default/massThroughput,n0_i1,DIRECT]\r\n" + "[n0_i1,http://default/hasProductBatteryLimit,n0_in2,INVERSE]\r\n" + "[n0_in2,http://default/massThroughput,n2,DIRECT]\r\n" + "[n2,p_n2_n3,n3,DIRECT]\r\n" + "", pathTupleExpr.pathToString());
    } catch (Exception e) {
        assertEquals("", e.getMessage());
    }
}
Also used : PathElement(com.inova8.pathql.element.PathElement) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)19 PathElement (com.inova8.pathql.element.PathElement)10 Order (org.junit.jupiter.api.Order)10 Test (org.junit.jupiter.api.Test)10 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)10 Join (org.eclipse.rdf4j.query.algebra.Join)6 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)6 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)3 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)2 StatementBinding (com.inova8.intelligentgraph.path.StatementBinding)2 Filter (org.eclipse.rdf4j.query.algebra.Filter)2 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)2 Union (org.eclipse.rdf4j.query.algebra.Union)2 QueryException (com.inova8.intelligentgraph.exceptions.QueryException)1 UnionBinding (com.inova8.intelligentgraph.path.UnionBinding)1 Iterations (com.inova8.pathql.element.Iterations)1 ArrayList (java.util.ArrayList)1 IRI (org.eclipse.rdf4j.model.IRI)1 BindingSet (org.eclipse.rdf4j.query.BindingSet)1 And (org.eclipse.rdf4j.query.algebra.And)1