Search in sources :

Example 1 with UnionBinding

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

Aggregations

PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)1 UnionBinding (com.inova8.intelligentgraph.path.UnionBinding)1 Join (org.eclipse.rdf4j.query.algebra.Join)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 Union (org.eclipse.rdf4j.query.algebra.Union)1