use of com.inova8.intelligentgraph.context.CustomQueryOptions 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.context.CustomQueryOptions 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.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.
the class CustomQueryOption method splitQuery.
/**
* Split query.
*
* @param query the query
* @param prefixes the prefixes
* @return the custom query options
*/
private static CustomQueryOptions splitQuery(String query, Prefixes prefixes) {
if (query == null || query.isEmpty()) {
return null;
}
query = query.substring(query.indexOf("?") + 1);
CustomQueryOptions customQueryOptions = new CustomQueryOptions();
for (String queryParam : query.split("&")) {
Pair<String, Value> pair = splitQueryParameter(queryParam, prefixes);
customQueryOptions.add(pair.a, pair.b);
}
return customQueryOptions;
}
use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PathPatternVisitor method visitQueryOptions.
/**
* Visit query options.
*
* @param ctx the ctx
* @return the query options path element
*/
@Override
public QueryOptionsPathElement visitQueryOptions(QueryOptionsContext ctx) {
// queryOptions : ( queryOption )+;
// queryOption: KEY '=' literal ('^^' type )?;
// KEY : '&' [a-zA-Z]+ ;
CustomQueryOptions customQueryOptions = new CustomQueryOptions();
for (QueryOptionContext queryOption : ctx.queryOption()) {
String key = queryOption.KEY().getText().substring(1);
LiteralValueElement literal = (LiteralValueElement) visit(queryOption.literal());
if (queryOption.type() != null) {
IriRefValueElement type = (IriRefValueElement) visit(queryOption.type());
Literal typeLiteral = Values.literal(Values.getValueFactory(), literal.getLiteral().getLabel(), type.getIri());
customQueryOptions.add(key, typeLiteral);
} else {
customQueryOptions.add(key, literal.getLiteral());
}
}
QueryOptionsPathElement queryOptionsPathElement = new QueryOptionsPathElement(this.repositoryContext);
queryOptionsPathElement.setCustomQueryOptions(customQueryOptions);
return queryOptionsPathElement;
}
use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Local_GetContextFact_Tests method test_2.
/**
* Test 2.
*/
@Test
@Order(2)
void test_2() {
try {
IntelligentGraphRepository source = IntelligentGraphRepository.create(workingRep);
source.removeGraph("<http://inova8.com/calc2graph/contextGraph>");
Graph graph = source.addGraph("<http://inova8.com/calc2graph/contextGraph>");
Thing myCountry = graph.getThing(":myCountry");
myCountry.addFact(":myOption", "_customQueryOptions.get(\"time\").integerValue() ;", SCRIPT.GROOVY);
CustomQueryOptions customQueryOptions1 = new CustomQueryOptions();
customQueryOptions1.add("time", 42);
customQueryOptions1.add("name", "Peter");
Thing myCountry1 = graph.getThing(":myCountry");
Trace result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>", customQueryOptions1);
Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'<http://inova8.com/calc2graph/def/myOption>'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[name="Peter"&time="42"^^<http://www.w3.org/2001/XMLSchema#int>]</li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get("time").integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
CustomQueryOptions customQueryOptions2 = new CustomQueryOptions();
customQueryOptions2.add("time", 43);
result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>", customQueryOptions2);
Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'<http://inova8.com/calc2graph/def/myOption>'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[time="43"^^<http://www.w3.org/2001/XMLSchema#int>]</li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get("time").integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>&time='2019'^^xsd:int");
Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'<http://inova8.com/calc2graph/def/myOption>&time='2019'^^xsd:int'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get("time").integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
ResourceResults results = myCountry1.getFacts("<http://inova8.com/calc2graph/def/myOption>&time='2020'^^xsd:int");
for (Resource result1 : results) {
Query.assertEqualsWOSpaces("2020", result1.stringValue());
}
} catch (Exception e) {
assertEquals("", e.getMessage());
e.printStackTrace();
}
}
Aggregations