Search in sources :

Example 11 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphEvaluator method nextGraph.

/**
 * Next graph.
 *
 * @return the binding set
 */
private BindingSet nextGraph() {
    BindingSet nextBindingSet = getEvaluator().next();
    try {
        if (nextBindingSet.hasBinding("subject") && nextBindingSet.hasBinding("predicate") && nextBindingSet.hasBinding("object")) {
            if (nextBindingSet.getValue("object").isLiteral()) {
                SimpleLiteral literalValue = (SimpleLiteral) (nextBindingSet.getValue("object"));
                if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
                    QueryBindingSet modifiedBindingSet = new QueryBindingSet();
                    modifiedBindingSet.addBinding(nextBindingSet.getBinding("subject"));
                    modifiedBindingSet.addBinding(nextBindingSet.getBinding("predicate"));
                    IntelligentGraphRepository source = getSource();
                    EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
                    Thing subjectThing = Thing.create(source, nextBindingSet.getValue("subject"), evaluationContext);
                    try {
                        // TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(	(IRI) nextBindingSet.getValue("predicate"),	literalValue,customQueryOptions);
                        com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, (IRI) nextBindingSet.getValue("predicate"), literalValue, customQueryOptions);
                        Binding modifiedBindingValue = new SimpleBinding("object", fact.getValue());
                        modifiedBindingSet.addBinding(modifiedBindingValue);
                        return modifiedBindingSet;
                    } catch (Exception e) {
                        Binding modifiedBindingValue = new SimpleBinding("object", literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
                        // literal(e.getMessage()));
                        modifiedBindingSet.addBinding(modifiedBindingValue);
                        return modifiedBindingSet;
                    }
                } else {
                    return locateCustomQueryOptions(nextBindingSet);
                }
            } else {
                return locateCustomQueryOptions(nextBindingSet);
            }
        } else {
            // Incomplete s p o within dataset so could not calculate
            return nextBindingSet;
        }
    } catch (Exception e) {
        // Should not be any exceptions that are not handled, but even so ...
        return nextBindingSet;
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) Resource(com.inova8.intelligentgraph.model.Resource) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Thing(com.inova8.intelligentgraph.model.Thing) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) NoSuchElementException(java.util.NoSuchElementException)

Example 12 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphEvaluator method nextTuple.

/**
 * Next tuple.
 *
 * @return the binding set
 * @throws QueryEvaluationException the query evaluation exception
 */
@SuppressWarnings("deprecation")
private BindingSet nextTuple() throws QueryEvaluationException {
    BindingSet nextBindingSet = getEvaluator().next();
    // Iterations.asSet(evaluator)
    QueryBindingSet modifiedBindingSet = new QueryBindingSet();
    for (Binding bindingValue : nextBindingSet) {
        // Only add bindings that were in the original projection
        String modifiedBindingValueName = requiredElement(bindingValue);
        if (modifiedBindingValueName != null) {
            if (bindingValue.getValue().isLiteral()) {
                SimpleLiteral literalValue = (SimpleLiteral) (bindingValue.getValue());
                if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
                    String bindingValueName = bindingValue.getName();
                    List<StatementPattern> statementPatterns = SubjectPredicateCollector.process(getTupleExpr(), bindingValueName);
                    if (!statementPatterns.isEmpty()) {
                        // TODO not all variables need be in projection list so will not match, need the full list of variables to complete this
                        StatementPattern boundStatement = findBound(statementPatterns, nextBindingSet);
                        if (boundStatement != null) {
                            Value subject = getSubjectValue(boundStatement, nextBindingSet);
                            IRI predicate = getPredicateValue(boundStatement, nextBindingSet);
                            ResponseType responseType = getResponseType(bindingValueName);
                            customQueryOptions = getCustomQueryOptions(nextBindingSet);
                            EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
                            switch(responseType) {
                                case VALUE:
                                    Thing subjectThing = Thing.create(getSource(), subject, evaluationContext);
                                    try {
                                        // TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate,	literalValue,customQueryOptions);
                                        com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
                                        Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, fact.getValue());
                                        modifiedBindingSet.addBinding(modifiedBindingValue);
                                    } catch (Exception e) {
                                        Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
                                        modifiedBindingSet.addBinding(modifiedBindingValue);
                                    }
                                    break;
                                case SCRIPT:
                                    modifiedBindingSet.addBinding(bindingValue);
                                    break;
                                case TRACE:
                                    evaluationContext.setTracing(true);
                                    Thing subjectThingTrace = Thing.create(getSource(), subject, evaluationContext);
                                    try {
                                        // TODO subjectThingTrace.getFact(predicate,literalValue,customQueryOptions);
                                        IntelligentEvaluator.processFactObjectValue(subjectThingTrace, predicate, literalValue, customQueryOptions);
                                        Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
                                        modifiedBindingSet.addBinding(modifiedBindingValueTrace);
                                    } catch (Exception e) {
                                        Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
                                        modifiedBindingSet.addBinding(modifiedBindingValueTrace);
                                    }
                                    break;
                            }
                        } else {
                            modifiedBindingSet.addBinding(bindingValue);
                        }
                    } else {
                        EvaluationContext evaluationContext = new EvaluationContext(getCustomQueryOptions(nextBindingSet), getDataset());
                        Thing subjectThing = Thing.create(getSource(), SCRIPT.ANONTHING, evaluationContext);
                        try {
                            // TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(	SCRIPT.ANONPREDICATE,literalValue,null);
                            com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, SCRIPT.ANONPREDICATE, literalValue, customQueryOptions);
                            Binding modifiedBindingValue = new SimpleBinding(bindingValue.getName(), fact.getValue());
                            modifiedBindingSet.addBinding(modifiedBindingValue);
                        } catch (Exception e) {
                            Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
                            modifiedBindingSet.addBinding(modifiedBindingValue);
                        }
                    }
                } else {
                    modifiedBindingSet.addBinding(bindingValue);
                }
            } else {
                if (bindingValue.getName().equals(modifiedBindingValueName))
                    modifiedBindingSet.addBinding(bindingValue);
                else {
                    Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, bindingValue.getValue());
                    modifiedBindingSet.addBinding(modifiedBindingValue);
                }
            }
        }
    }
    return modifiedBindingSet;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) Resource(com.inova8.intelligentgraph.model.Resource) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) NoSuchElementException(java.util.NoSuchElementException) ResponseType(com.inova8.intelligentgraph.sail.IntelligentGraphSail.ResponseType) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) Value(org.eclipse.rdf4j.model.Value) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext) Thing(com.inova8.intelligentgraph.model.Thing)

Example 13 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphEvaluator method getCustomQueryOptions.

/**
 * Gets the custom query options.
 *
 * @param nextBindingSet the next binding set
 * @return the custom query options
 */
public CustomQueryOptions getCustomQueryOptions(BindingSet nextBindingSet) {
    if (nextBindingSet.size() == 0) {
        return null;
    } else {
        CustomQueryOptions customQueryOptions = new CustomQueryOptions();
        for (Binding bindingValue : nextBindingSet) {
            String customQueryOptionParameter = bindingValue.getName();
            Value customQueryOptionValue = bindingValue.getValue();
            customQueryOptions.put(customQueryOptionParameter, Resource.create(getSource(), customQueryOptionValue, null));
        }
        return customQueryOptions;
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value)

Example 14 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentStatement method getObject.

/**
 * Gets the object.
 *
 * @return the object
 */
@Override
public Value getObject() {
    if (contextStatement != null) {
        if (contextStatement.getObject().isLiteral()) {
            SimpleLiteral literalValue = (SimpleLiteral) (contextStatement.getObject());
            if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
                Thing subjectThing = Thing.create(getSource(), (IRI) getContext(), contextStatement.getSubject(), getEvaluationContext());
                CustomQueryOptions customQueryOptions = CustomQueryOption.getCustomQueryOptions(getEvaluationContext().getContexts(), source.getRepositoryContext().getPrefixes());
                try {
                    // TODO  com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(contextStatement.getPredicate(),literalValue,customQueryOptions, contexts);
                    com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, contextStatement.getPredicate(), literalValue, customQueryOptions, contexts);
                    return fact.getSuperValue();
                } catch (Exception e) {
                    String exceptionMessage = "";
                    for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
                        exceptionMessage += t.getMessage() + "\n";
                    }
                    if (exceptionMessage == "")
                        exceptionMessage = e.getMessage();
                    if (exceptionMessage == "")
                        exceptionMessage = "Exception w/o message";
                    return literal(exceptionMessage);
                }
            } else {
                return contextStatement.getObject();
            }
        } else {
            return contextStatement.getObject();
        }
    } else {
        return null;
    }
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) Thing(com.inova8.intelligentgraph.model.Thing)

Example 15 with CustomQueryOptions

use of com.inova8.intelligentgraph.context.CustomQueryOptions 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

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