Search in sources :

Example 16 with Predicate

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

the class PredicateElement method visitPredicatePathBinding.

/**
 * Visit predicate path binding.
 *
 * @param pathBinding the path binding
 * @param pathIteration the path iteration
 * @return the path binding
 */
private PathBinding visitPredicatePathBinding(PathBinding pathBinding, Integer pathIteration) {
    StatementBinding predicateEdge;
    Variable sourceVariable = this.getSourceVariable();
    Variable targetVariable = this.getTargetVariable();
    Variable intermediateSourceVariable = 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;
        }
        predicateEdge = new StatementBinding(intermediateSourceVariable, getPredicateVariable(), intermediateTargetVariable, getIsInverseOf());
        pathBinding.add(predicateEdge);
    }
    return pathBinding;
}
Also used : StatementBinding(com.inova8.intelligentgraph.path.StatementBinding)

Example 17 with Predicate

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

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

the class PathPatternVisitor method visitReifiedPredicate.

/**
 * Visit reified predicate.
 *
 * @param ctx the ctx
 * @return the predicate element
 */
@Override
public PredicateElement visitReifiedPredicate(ReifiedPredicateContext ctx) {
    // reifiedPredicate :  iriRef? REIFIER predicateRef  factFilterPattern?  dereifier? ; #statementFilterPattern
    PredicateElement reifiedPredicateElement = new PredicateElement(this.repositoryContext);
    reifiedPredicateElement.setIsReified(true);
    if (ctx.iriRef() != null) {
        reifiedPredicateElement.setReification(((IriRefValueElement) visit(ctx.iriRef())).getIri());
    }
    if (ctx.predicateRef() != null) {
        reifiedPredicateElement.setPredicate(((IriRefValueElement) visit(ctx.predicateRef())).getIri());
    }
    if (ctx.factFilterPattern() != null) {
        reifiedPredicateElement.setObjectFilterElement((FactFilterElement) visit(ctx.factFilterPattern()));
    }
    if (ctx.dereifier() != null) {
        reifiedPredicateElement.setIsDereified(true);
    }
    return reifiedPredicateElement;
}
Also used : PredicateElement(com.inova8.pathql.element.PredicateElement)

Example 19 with Predicate

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

the class PathPatternVisitor method visitPredicate.

/**
 * Visit predicate.
 *
 * @param ctx the ctx
 * @return the predicate element
 */
@Override
public PredicateElement visitPredicate(PredicateContext ctx) {
    // predicate :   ( reifiedPredicate | predicateRef | rdfType | '*' ) factFilterPattern? #objectFilterPattern
    PredicateElement predicateElement = null;
    if (ctx.reifiedPredicate() != null) {
        predicateElement = (PredicateElement) visit(ctx.reifiedPredicate());
        predicateElement.setOperator(PathConstants.Operator.PREDICATE);
    } else if (ctx.predicateRef() != null) {
        predicateElement = new PredicateElement(this.repositoryContext);
        predicateElement.setOperator(PathConstants.Operator.PREDICATE);
        IriRefValueElement predicateRef = ((IriRefValueElement) visit(ctx.predicateRef()));
        predicateElement.setPredicate(predicateRef.getIri());
    } else if (ctx.rdfType() != null) {
        predicateElement = new PredicateElement(this.repositoryContext);
        predicateElement.setOperator(PathConstants.Operator.PREDICATE);
        predicateElement.setPredicate((iri("http://rdftype")));
    } else if (ctx.anyPredicate() != null) {
        predicateElement = new PredicateElement(this.repositoryContext);
        predicateElement.setOperator(PathConstants.Operator.PREDICATE);
        predicateElement.setAnyPredicate(true);
    }
    if (ctx.factFilterPattern() != null) {
        if (ctx.reifiedPredicate() != null) {
            predicateElement.setStatementFilterElement((FactFilterElement) visit(ctx.factFilterPattern()));
        } else {
            predicateElement.setObjectFilterElement((FactFilterElement) visit(ctx.factFilterPattern()));
        }
    }
    return predicateElement;
}
Also used : PredicateElement(com.inova8.pathql.element.PredicateElement) IriRefValueElement(com.inova8.pathql.element.IriRefValueElement)

Example 20 with Predicate

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

the class PathPatternQueryExpressionTests method test_1.

/**
 * Test 1.
 */
@Test
@Order(1)
void test_1() {
    CharStream input = CharStreams.fromString("^:Attribute@:volumeFlow");
    PathElement element = prepareElement(input);
    // Query.assertEqualsWOSpaces
    assertEquals("Join\r\n" + "   Join\r\n" + "      Union\r\n" + "         StatementPattern\r\n" + "            Variable (name=r1)\r\n" + "            Variable (name=object1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "            Variable (name=n0)\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0)\r\n" + "            Variable (name=isObjectOf1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "            Variable (name=r1)\r\n" + "      Union\r\n" + "         StatementPattern\r\n" + "            Variable (name=r1)\r\n" + "            Variable (name=property1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)\r\n" + "            Variable (name=p_n0_n1, value=http://default/volumeFlow)\r\n" + "         StatementPattern\r\n" + "            Variable (name=p_n0_n1, value=http://default/volumeFlow)\r\n" + "            Variable (name=isPropertyOf1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate)\r\n" + "            Variable (name=r1)\r\n" + "   Union\r\n" + "      StatementPattern\r\n" + "         Variable (name=r1)\r\n" + "         Variable (name=subject1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)\r\n" + "         Variable (name=n1)\r\n" + "      StatementPattern\r\n" + "         Variable (name=n1)\r\n" + "         Variable (name=isSubjectOf1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)\r\n" + "         Variable (name=r1)\r\n" + "", element.pathPatternQuery().toString());
}
Also used : PathElement(com.inova8.pathql.element.PathElement) CharStream(org.antlr.v4.runtime.CharStream) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

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