Search in sources :

Example 11 with PathTupleExpr

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

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

Example 13 with PathTupleExpr

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

the class PredicateElement method pathPredicatePatternQuery.

/**
 * Path predicate 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
 */
private PathTupleExpr pathPredicatePatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
    PathTupleExpr predicatePattern = null;
    if (getCardinality(pathIteration) > 0) {
        Variable intermediateSourceVariable = null;
        Variable intermediatePredicateVariable = null;
        Variable intermediateTargetVariable = null;
        Variable priorIntermediateTargetVariable = 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;
            }
            intermediatePredicateVariable = deducePredicateVariable(sourceVariable, predicateVariable, targetVariable);
            predicatePattern = pathPredicatePatternTupleExpr(predicatePattern, intermediateSourceVariable, intermediatePredicateVariable, intermediateTargetVariable, customQueryOptions);
        }
        return predicatePattern;
    } else {
        return null;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr)

Example 14 with PathTupleExpr

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

the class MultiPathPatternQueryExpressionTests method test_7.

/**
 * Test 7.
 */
@Test
@Order(7)
void test_7() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, "(^:hasProductBatteryLimit/:massThroughput){1, 2}/:massThroughput");
        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, value=http://default/massThroughput)\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,http://default/massThroughput,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)

Example 15 with PathTupleExpr

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

the class MultiPathPatternQueryExpressionTests method test_5.

/**
 * Test 5.
 */
@Test
@Order(5)
void test_5() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, "(^:hasProductBatteryLimit/:massThroughput){1,2}");
        PathTupleExpr pathTupleExpr = element.pathPatternQuery(0, null);
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   StatementPattern\r\n" + "      Variable (name=n1)\r\n" + "      Variable (name=p_n0_n1, value=http://default/hasProductBatteryLimit)\r\n" + "      Variable (name=n0)\r\n" + "   StatementPattern\r\n" + "      Variable (name=n1)\r\n" + "      Variable (name=p_n1_n2, value=http://default/massThroughput)\r\n" + "      Variable (name=n2)\r\n" + "", pathTupleExpr.toString());
        assertEquals("[n0,http://default/hasProductBatteryLimit,n1,INVERSE]\r\n" + "[n1,http://default/massThroughput,n2,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