Search in sources :

Example 1 with Trace

use of com.inova8.intelligentgraph.evaluator.Trace in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Tracer method traceFacts.

/**
 * Trace facts.
 *
 * @param thing the thing
 * @param pathQLValue the path QL value
 * @param prefixes the prefixes
 * @param contexts the contexts
 */
public void traceFacts(Thing thing, String pathQLValue, Prefixes prefixes, org.eclipse.rdf4j.model.Resource... contexts) {
    if (!tracing)
        return;
    addTrace(String.format("Getting facts  '%s' of %s", toHTML(pathQLValue), addIRI(thing.getSuperValue())));
    CustomQueryOptions customQueryOptions = CustomQueryOption.getCustomQueryOptions(contexts, prefixes);
    if (!customQueryOptions.isEmpty())
        addTrace(String.format("...using options: [%s]", toHTML(customQueryOptions.toString())));
    ArrayList<org.eclipse.rdf4j.model.Resource> coreContexts = CustomQueryOption.getCoreContexts(contexts);
    if (!coreContexts.isEmpty())
        addTrace(String.format("...within contexts: %s", coreContexts.toString()));
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(com.inova8.intelligentgraph.model.Resource)

Example 2 with Trace

use of com.inova8.intelligentgraph.evaluator.Trace in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Thing method traceFact.

/**
 * Trace fact.
 *
 * @param predicatePattern the predicate pattern
 * @param customQueryOptions the custom query options
 * @return the trace
 */
public Trace traceFact(String predicatePattern, CustomQueryOptions customQueryOptions) {
    SimpleDataset dataset = IntelligentGraphRepository.getDataset(customQueryOptions);
    dataset.addDefaultGraph(this.graphName);
    org.eclipse.rdf4j.model.Resource[] contextArray = dataset.getDefaultGraphs().toArray(new org.eclipse.rdf4j.model.Resource[0]);
    ResourceStatementResults results = null;
    IRI predicate = IntelligentGraphRepository.preparePredicate(PATHQL.traceFacts, predicatePattern);
    CloseableIteration<Statement, RepositoryException> statementIterator = this.getSource().getRepository().getConnection().getStatements(this.getIRI(), predicate, null, contextArray);
    results = new ResourceStatementResults(statementIterator, this, null, customQueryOptions);
    for (Resource result : results) {
        String resultString = result.stringValue();
        results.close();
        return new Trace(resultString);
    }
    results.close();
    return new Trace("results");
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) ResourceStatementResults(com.inova8.intelligentgraph.results.ResourceStatementResults) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) Trace(com.inova8.intelligentgraph.evaluator.Trace)

Example 3 with Trace

use of com.inova8.intelligentgraph.evaluator.Trace in project com.inova8.intelligentgraph by peterjohnlawrence.

the class FactDebug method evaluate.

/**
 * Evaluate.
 *
 * @param tripleSource the triple source
 * @param args the args
 * @return the value
 * @throws ValueExprEvaluationException the value expr evaluation exception
 */
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
    // logger.debug("Trace Evaluate for <{}>, {} with args <{}>",tripleSource, tripleSource.getValueFactory(),Arrays.toString(args));
    if (args.length < 3) {
        String message = "At least subject, predicate, and script arguments required";
        logger.error(message);
        return tripleSource.getValueFactory().createLiteral(message);
    } else {
        IRI subject;
        IRI predicate;
        SimpleLiteral scriptLiteral;
        try {
            subject = (IRI) args[0];
            predicate = (IRI) args[1];
            scriptLiteral = (SimpleLiteral) args[2];
        } catch (Exception e) {
            String message = "Subject and predicate must be valid IRI, and script must be a literal";
            logger.error(message);
            return tripleSource.getValueFactory().createLiteral(message.toString());
        }
        try {
            Value[] argumentArray = Arrays.copyOfRange(args, 3, args.length);
            IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
            CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
            EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
            evaluationContext.setTracing(true);
            Thing subjectThing = Thing.create(source, subject, evaluationContext);
            IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, scriptLiteral, customQueryOptions);
            // TODO 	subjectThing.getFact(predicate,	scriptLiteral,customQueryOptions);
            logger.debug("Trace\r\n" + evaluationContext.getTrace());
            return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
        } catch (Exception e) {
            return tripleSource.getValueFactory().createLiteral(e.getMessage());
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext) ValueExprEvaluationException(org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException) Thing(com.inova8.intelligentgraph.model.Thing)

Example 4 with Trace

use of com.inova8.intelligentgraph.evaluator.Trace in project com.inova8.intelligentgraph by peterjohnlawrence.

the class FactProvenance method evaluate.

/**
 * Evaluate.
 *
 * @param tripleSource the triple source
 * @param args the args
 * @return the value
 * @throws ValueExprEvaluationException the value expr evaluation exception
 */
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
    logger.debug("Trace Evaluate for <{}>, {} with args <{}>", tripleSource, tripleSource.getValueFactory(), args);
    if (args.length < 2) {
        String message = "At least subject, and predicate arguments required";
        logger.error(message);
        return tripleSource.getValueFactory().createLiteral(message);
    } else {
        IRI subject;
        IRI predicate;
        try {
            subject = (IRI) args[0];
            predicate = (IRI) args[1];
        } catch (Exception e) {
            String message = "Subject and predicate must be valid IRI";
            logger.error(message);
            return tripleSource.getValueFactory().createLiteral(message);
        }
        try {
            Value[] argumentArray = Arrays.copyOfRange(args, 2, args.length);
            IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
            CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
            EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
            evaluationContext.setTracing(true);
            Thing subjectThing = Thing.create(source, subject, evaluationContext);
            // olgap.Value fact =
            // PredicateElement(source,predicate));
            subjectThing.getFact("<" + predicate.stringValue() + ">");
            logger.debug("Trace\r\n" + evaluationContext.getTrace());
            return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
        } catch (Exception e) {
            return tripleSource.getValueFactory().createLiteral(e.getMessage());
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value) EvaluationContext(com.inova8.intelligentgraph.evaluator.EvaluationContext) ValueExprEvaluationException(org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException) Thing(com.inova8.intelligentgraph.model.Thing)

Example 5 with Trace

use of com.inova8.intelligentgraph.evaluator.Trace 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)

Aggregations

Thing (com.inova8.intelligentgraph.model.Thing)15 Trace (com.inova8.intelligentgraph.evaluator.Trace)11 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)10 Order (org.junit.jupiter.api.Order)10 Test (org.junit.jupiter.api.Test)10 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)10 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)7 IRI (org.eclipse.rdf4j.model.IRI)6 Resource (com.inova8.intelligentgraph.model.Resource)5 Value (org.eclipse.rdf4j.model.Value)5 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)4 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)3 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)3 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)3 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)2 Binding (org.eclipse.rdf4j.query.Binding)2 BindingSet (org.eclipse.rdf4j.query.BindingSet)2 StatementBinding (com.inova8.intelligentgraph.path.StatementBinding)1 ResourceStatementResults (com.inova8.intelligentgraph.results.ResourceStatementResults)1 ResponseType (com.inova8.intelligentgraph.sail.IntelligentGraphSail.ResponseType)1