Search in sources :

Example 41 with Predicate

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

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

the class MultiPathPatternQueryExpressionTests method test_3.

/**
 * Test 3.
 */
@Test
@Order(3)
void test_3() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, ":Attribute@:density{1, 42}");
        PathTupleExpr pathTupleExpr = element.pathPatternQuery(0, null);
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   Join\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=n0)\r\n" + "         StatementPattern\r\n" + "            Variable (name=n0)\r\n" + "            Variable (name=isSubjectOf1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#subject)\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/density)\r\n" + "         StatementPattern\r\n" + "            Variable (name=p_n0_n1, value=http://default/density)\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=object1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "         Variable (name=n1)\r\n" + "      StatementPattern\r\n" + "         Variable (name=n1)\r\n" + "         Variable (name=isObjectOf1, value=http://www.w3.org/1999/02/22-rdf-syntax-ns#object)\r\n" + "         Variable (name=r1)\r\n" + "", pathTupleExpr.toString());
        assertEquals("[n0,r1:<http://default/Attribute>@http://default/density,n1,DIRECT,false]\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 43 with Predicate

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

the class PathPatternQueryExpressionTests method test_13.

/**
 * Test 13.
 */
// }
@Test
@Order(13)
void test_13() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, ":Location@:appearsOn[eq id:Calc2Graph1, id:Calc2Graph2]#");
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   Join\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=n0)\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/appearsOn)\r\n" + "   Join\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=n1, value=http://id/Calc2Graph1)\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=n1, value=http://id/Calc2Graph2)\r\n" + "", element.pathPatternQuery().toString());
    } catch (Exception e) {
        assertEquals("", e.getMessage());
    }
}
Also used : PathElement(com.inova8.pathql.element.PathElement) RecognitionException(org.antlr.v4.runtime.RecognitionException) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 44 with Predicate

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

the class PathPatternVisitor method visitPredicateRef.

/**
 * Visit predicate ref.
 *
 * @param ctx the ctx
 * @return the iri ref value element
 */
@Override
public IriRefValueElement visitPredicateRef(PredicateRefContext ctx) {
    // predicateRef :  IRI_REF  | rdfType  |  qname | pname_ns ;
    IriRefValueElement predicateRefElement;
    if (ctx.qname() != null) {
        predicateRefElement = (IriRefValueElement) visit(ctx.qname());
    } else if (ctx.pname_ns() != null) {
        predicateRefElement = (IriRefValueElement) visit(ctx.pname_ns());
    } else if (ctx.rdfType() != null) {
        predicateRefElement = new IriRefValueElement(this.repositoryContext);
        predicateRefElement.setIri(iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
    } else {
        predicateRefElement = new IriRefValueElement(this.repositoryContext);
        String iri = ctx.IRI_REF().getText();
        iri = iri.substring(1, iri.length() - 1);
        predicateRefElement.setIri(iri(iri));
    }
    return predicateRefElement;
}
Also used : IriRefValueElement(com.inova8.pathql.element.IriRefValueElement)

Example 45 with Predicate

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

the class BoundPathElement 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) {
    // Extend(rightPatern(?s,?p,?o),
    Variable bindSourceVariable = new Variable("bind");
    // new Variable("b0_b1");
    Variable bindPredicateVariable = null;
    Variable bindTargetVariable = new Variable("b");
    PathTupleExpr bindPattern = ((FactFilterElement) getLeftPathElement()).filterExpression(bindSourceVariable, bindPredicateVariable, bindTargetVariable, null, customQueryOptions);
    // PathTupleExpr bindPattern = getLeftPathElement().pathPatternQuery(bindSourceVariable,bindPredicateVariable,bindTargetVariable,customQueryOptions);
    PathTupleExpr rightPattern;
    if (bindPattern.getTupleExpr() == null) {
        bindSourceVariable.setName("n0");
        rightPattern = getRightPathElement().pathPatternQuery(bindSourceVariable, predicateVariable, targetVariable, customQueryOptions);
        rightPattern.setBoundVariable(bindSourceVariable);
        return rightPattern;
    } else {
        rightPattern = getRightPathElement().pathPatternQuery(sourceVariable, predicateVariable, targetVariable, customQueryOptions);
        Extension leftPattern = new Extension(bindPattern.getTupleExpr(), new ExtensionElem(bindSourceVariable, "n0"));
        Join boundPattern = new Join(leftPattern, rightPattern.getTupleExpr());
        PathTupleExpr boundPatternTupleExpr = new PathTupleExpr(boundPattern);
        boundPatternTupleExpr.setStatementBinding(rightPattern.getStatementBinding());
        boundPatternTupleExpr.setBoundVariable(bindSourceVariable);
        return boundPatternTupleExpr;
    }
}
Also used : Extension(org.eclipse.rdf4j.query.algebra.Extension) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) ExtensionElem(org.eclipse.rdf4j.query.algebra.ExtensionElem) Join(org.eclipse.rdf4j.query.algebra.Join)

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