Search in sources :

Example 16 with CustomQueryOptions

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;
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) StatementBinding(com.inova8.intelligentgraph.path.StatementBinding) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Filter(org.eclipse.rdf4j.query.algebra.Filter) Join(org.eclipse.rdf4j.query.algebra.Join) Compare(org.eclipse.rdf4j.query.algebra.Compare) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr)

Example 17 with CustomQueryOptions

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;
    }
}
Also used : PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) Join(org.eclipse.rdf4j.query.algebra.Join) PathTupleExpr(com.inova8.intelligentgraph.path.PathTupleExpr) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 18 with CustomQueryOptions

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;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value)

Example 19 with 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;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) QueryOptionContext(com.inova8.pathql.pathPattern.PathPatternParser.QueryOptionContext) LiteralValueElement(com.inova8.pathql.element.LiteralValueElement) QueryOptionsPathElement(com.inova8.pathql.element.QueryOptionsPathElement) IriRefValueElement(com.inova8.pathql.element.IriRefValueElement) Literal(org.eclipse.rdf4j.model.Literal)

Example 20 with CustomQueryOptions

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'&lt;http://inova8.com/calc2graph/def/myOption&gt;'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[name=&quot;Peter&quot;&amp;time=&quot;42&quot;^^&lt;http://www.w3.org/2001/XMLSchema#int&gt;]</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(&quot;time&quot;).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'&lt;http://inova8.com/calc2graph/def/myOption&gt;'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[time=&quot;43&quot;^^&lt;http://www.w3.org/2001/XMLSchema#int&gt;]</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(&quot;time&quot;).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'&lt;http://inova8.com/calc2graph/def/myOption&gt;&amp;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(&quot;time&quot;).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();
    }
}
Also used : Trace(com.inova8.intelligentgraph.evaluator.Trace) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) Graph(com.inova8.intelligentgraph.intelligentGraphRepository.Graph) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(com.inova8.intelligentgraph.model.Resource) Thing(com.inova8.intelligentgraph.model.Thing) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)27 Thing (com.inova8.intelligentgraph.model.Thing)16 IRI (org.eclipse.rdf4j.model.IRI)14 Resource (com.inova8.intelligentgraph.model.Resource)12 Value (org.eclipse.rdf4j.model.Value)11 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)10 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)9 PathTupleExpr (com.inova8.intelligentgraph.path.PathTupleExpr)9 Order (org.junit.jupiter.api.Order)9 Test (org.junit.jupiter.api.Test)9 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)9 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)8 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)7 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)7 SimpleDataset (org.eclipse.rdf4j.query.impl.SimpleDataset)7 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)6 Statement (org.eclipse.rdf4j.model.Statement)6 Join (org.eclipse.rdf4j.query.algebra.Join)6 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)6 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)5