use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphConnection method getResultsIterator.
/**
* Gets the results iterator.
*
* @param source the source
* @param thing the thing
* @param pathElement the path element
* @param pathTupleExpr the path tuple expr
* @param contexts the contexts
* @return the results iterator
* @throws IllegalArgumentException the illegal argument exception
* @throws QueryEvaluationException the query evaluation exception
*/
CloseableIteration<BindingSet, QueryEvaluationException> getResultsIterator(IntelligentGraphRepository source, Thing thing, PathElement pathElement, PathTupleExpr pathTupleExpr, Resource... contexts) throws IllegalArgumentException, QueryEvaluationException {
TupleExpr tupleExpr = pathTupleExpr.getTupleExpr();
SimpleDataset dataset = prepareDataset(pathElement, source, contexts);
BindingSet bindings = new QueryBindingSet();
EvaluationStrategy evaluationStrategy = new StrictEvaluationStrategy(source.getTripleSource(), dataset, null);
CloseableIteration<BindingSet, QueryEvaluationException> resultsIterator = evaluationStrategy.evaluate(tupleExpr, bindings);
return resultsIterator;
}
use of com.inova8.intelligentgraph.path.PathTupleExpr 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;
}
}
use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PredicateElement method pathPredicatePatternTupleExpr.
/**
* Path predicate pattern tuple expr.
*
* @param predicatePattern the predicate pattern
* @param intermediateSourceVariable the intermediate source variable
* @param intermediatePredicateVariable the intermediate predicate variable
* @param intermediateTargetVariable the intermediate target variable
* @param customQueryOptions the custom query options
* @return the path tuple expr
*/
private PathTupleExpr pathPredicatePatternTupleExpr(PathTupleExpr predicatePattern, Variable intermediateSourceVariable, Variable intermediatePredicateVariable, Variable intermediateTargetVariable, CustomQueryOptions customQueryOptions) {
TupleExpr intermediatePredicatePattern;
Variable predicateVariable;
if (isNegated) {
predicateVariable = new Variable(getPredicateSPARQLVariable());
// TODO
Variable variable = new Variable("p2", predicate);
if (isInverseOf) {
StatementPattern inverseOfPattern = new StatementPattern(intermediateTargetVariable, predicateVariable, intermediateSourceVariable);
Compare filterExpression = new Compare(predicateVariable, variable, CompareOp.NE);
intermediatePredicatePattern = new Filter(inverseOfPattern, filterExpression);
} else {
intermediatePredicatePattern = new StatementPattern(intermediateSourceVariable, predicateVariable, intermediateTargetVariable);
}
} else {
intermediatePredicateVariable.setValue(predicate);
// Variable(getPredicateSPARQLVariable(), predicate);
predicateVariable = intermediatePredicateVariable;
if (isInverseOf) {
intermediatePredicatePattern = new StatementPattern(intermediateTargetVariable, predicateVariable, intermediateSourceVariable);
} else {
intermediatePredicatePattern = new StatementPattern(intermediateSourceVariable, predicateVariable, intermediateTargetVariable);
}
}
if (objectFilterElement != null) {
intermediatePredicatePattern = (TupleExpr) objectFilterElement.filterExpression(intermediateTargetVariable, intermediatePredicateVariable, null, intermediatePredicatePattern, customQueryOptions).getTupleExpr();
}
// EdgeBinding
statementBinding = new StatementBinding(intermediateSourceVariable, predicateVariable, intermediateTargetVariable, getIsInverseOf());
if (predicatePattern == null) {
predicatePattern = new PathTupleExpr(intermediatePredicatePattern);
// predicatePattern.setStatementBinding(statementBinding);
} else {
predicatePattern.setTupleExpr(new Join(predicatePattern.getTupleExpr(), intermediatePredicatePattern));
}
predicatePattern.setStatementBinding(statementBinding);
predicatePattern.setBoundVariable(predicatePattern.getStatementBinding().getSourceVariable());
predicatePattern.getPath().add(statementBinding);
return predicatePattern;
}
use of com.inova8.intelligentgraph.path.PathTupleExpr 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;
}
}
use of com.inova8.intelligentgraph.path.PathTupleExpr in project com.inova8.intelligentgraph by peterjohnlawrence.
the class MultiPathPatternQueryExpressionTests method test_8.
/**
* Test 8.
*/
@Test
@Order(8)
void test_8() {
try {
PathElement element = PathParser.parsePathPattern(repositoryContext, "(^:hasProductBatteryLimit/:massThroughput){1, 2}/*");
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)\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,p_n2_n3,n3,DIRECT]\r\n" + "", pathTupleExpr.pathToString());
} catch (Exception e) {
assertEquals("", e.getMessage());
}
}
Aggregations