Search in sources :

Example 16 with PathTupleExpr

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

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

the class MultiPathPatternQueryExpressionTests method test_10.

/**
 * Test 10.
 */
@Test
@Order(10)
void test_10() {
    try {
        PathElement element = PathParser.parsePathPattern(repositoryContext, "(*){1, 2}/:massThroughput");
        PathTupleExpr pathTupleExpr = element.pathPatternQuery(1, null);
        // Query.assertEqualsWOSpaces
        assertEquals("Join\r\n" + "   Join\r\n" + "      StatementPattern\r\n" + "         Variable (name=n0)\r\n" + "         Variable (name=p_n0_n1)\r\n" + "         Variable (name=n1_i1)\r\n" + "      StatementPattern\r\n" + "         Variable (name=n1_i1)\r\n" + "         Variable (name=p_n0_n1)\r\n" + "         Variable (name=n1)\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,p_n0_n1,n1_i1,DIRECT]\r\n" + "[n1_i1,p_n0_n1,n1,DIRECT]\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)

Example 18 with PathTupleExpr

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

Example 19 with PathTupleExpr

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

the class FactFilterElement method filterExpression.

/**
 * Filter expression.
 *
 * @param sourceVariable the source variable
 * @param predicateVariable the predicate variable
 * @param targetVariable the target variable
 * @param filterExpression the filter expression
 * @param customQueryOptions the custom query options
 * @return the path tuple expr
 */
public PathTupleExpr filterExpression(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, TupleExpr filterExpression, CustomQueryOptions customQueryOptions) {
    // QueryModelNode filterExpression = null;
    if (propertyListNotEmpty != null) {
        int verbObjectListCount = 0;
        for (VerbObjectList verbObjectList : propertyListNotEmpty) {
            verbObjectListCount++;
            Variable verbObjectListTargetVariable = null;
            if (targetVariable != null)
                verbObjectListTargetVariable = new Variable(targetVariable.getName() + verbObjectListCount);
            QueryModelNode verbObjectListExpression = verbObjectList.filterExpression(sourceVariable, predicateVariable, verbObjectListTargetVariable, customQueryOptions);
            if (filterExpression == null)
                filterExpression = (TupleExpr) verbObjectListExpression;
            else if (verbObjectListExpression == null) {
            // Ignore it for some unknown reason
            } else {
                if (filterExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                    if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                        filterExpression = (TupleExpr) new And((ValueExpr) filterExpression, (ValueExpr) verbObjectListExpression);
                    else
                        filterExpression = new Filter((TupleExpr) verbObjectListExpression, (ValueExpr) filterExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.StatementPattern"))
                    filterExpression = new Join((TupleExpr) filterExpression, (TupleExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Join"))
                    filterExpression = new Join((TupleExpr) filterExpression, (TupleExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Compare"))
                    filterExpression = new Filter(filterExpression, (ValueExpr) verbObjectListExpression);
                else if (verbObjectListExpression.getClass().getName().equals("org.eclipse.rdf4j.query.algebra.Filter")) {
                    TupleExpr arg = ((Filter) verbObjectListExpression).getArg();
                    filterExpression = new Filter(new Join(filterExpression, arg), ((Filter) verbObjectListExpression).getCondition());
                } else
                    filterExpression = new Filter((TupleExpr) filterExpression, (ValueExpr) verbObjectListExpression);
            }
        }
    }
    return new PathTupleExpr(filterExpression);
}
Also used : ValueExpr(org.eclipse.rdf4j.query.algebra.ValueExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) And(org.eclipse.rdf4j.query.algebra.And) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) Join(org.eclipse.rdf4j.query.algebra.Join) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 20 with PathTupleExpr

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

the class IntelligentStatementResults method hasNext.

/**
 * Checks for next.
 *
 * @return true, if successful
 * @throws QueryEvaluationException the query evaluation exception
 */
@Override
public boolean hasNext() throws QueryEvaluationException {
    if (resultsIterator != null && resultsIterator.hasNext()) {
        return true;
    } else {
        while (pathIteration < this.sortedIterations.size()) {
            // source.getIntelligentGraphConnection().getPrefixes());
            CustomQueryOptions customQueryOptions = CustomQueryOption.getCustomQueryOptions(contexts, source.getRepositoryContext().getPrefixes());
            pathTupleExpr = pathElement.pathPatternQuery(pathIteration, customQueryOptions);
            if (this.thing == null && pathTupleExpr.getBoundVariable() == null) {
                throw new QueryEvaluationException("Facts query unbound");
            } else {
                this.boundVariableName = pathTupleExpr.getBoundVariable().getName();
            }
            pathIteration++;
            this.resultsIterator = intelligentGraphConnection.getResultsIterator(source, thing, pathElement, pathTupleExpr, contexts);
            boolean hasNext = resultsIterator.hasNext();
            if (hasNext) {
                predicateVariable = pathTupleExpr.getStatementBinding().getPredicateVariable();
                subj = pathTupleExpr.getStatementBinding().getSourceVariable().getName();
                pathTupleExpr.getStatementBinding().getPredicateVariable().getName();
                obj = pathTupleExpr.getStatementBinding().getTargetVariable().getName();
                return true;
            }
        }
        return false;
    }
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException)

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