Search in sources :

Example 1 with Fact

use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Thing method getFact.

/**
 * Gets the fact.
 *
 * @param predicatePattern the predicate pattern
 * @param bindValues the bind values
 * @return the fact
 */
public Resource getFact(String predicatePattern, Value... bindValues) {
    logger.debug("getFact{}\n", predicatePattern);
    // this.getEvaluationContext().getTracer().traceFact(this, predicatePattern);
    ResourceResults factValues = getFacts(predicatePattern, bindValues);
    if (factValues == null) {
        this.getEvaluationContext().getTracer().traceFactReturnNull(this, predicatePattern);
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s", predicatePattern, this));
    } else if (factValues.hasNext()) {
        Resource nextFact = factValues.next();
        this.getEvaluationContext().getTracer().traceFactReturnValue(this, predicatePattern, nextFact);
        return nextFact;
    } else {
        this.getEvaluationContext().getTracer().traceFactEmpty(this, predicatePattern);
        factValues.close();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s", predicatePattern, this));
    }
}
Also used : NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults)

Example 2 with Fact

use of com.inova8.intelligentgraph.model.Fact 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 Fact

use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Thing method getFact.

/**
 * Gets the fact.
 *
 * @param predicatePattern the predicate pattern
 * @param customQueryOptions the custom query options
 * @return the fact
 */
public Resource getFact(String predicatePattern, CustomQueryOptions customQueryOptions) {
    logger.debug("getFact{}\n", predicatePattern);
    ResourceResults factValues = getFacts(predicatePattern, customQueryOptions);
    if (factValues == null) {
        // return new NullResource();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s and customQueryOptions %s", predicatePattern, this, customQueryOptions));
    } else if (factValues.hasNext()) {
        return factValues.next();
    } else {
        factValues.close();
        // return new NullResource();
        throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s and customQueryOptions %s", predicatePattern, this, customQueryOptions));
    }
}
Also used : NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults)

Example 4 with Fact

use of com.inova8.intelligentgraph.model.Fact 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 Fact

use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.

the class FactValue 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("Evaluate for {} with args <{}>", 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.toString());
        }
        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);
            Thing subjectThing = Thing.create(source, subject, evaluationContext);
            // new PredicateElement(source,predicate));
            com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact("<" + predicate.stringValue() + ">");
            if (fact != null && fact.getValue() != null) {
                Value result = fact.getValue();
                logger.debug("FactValue = {}", result);
                return result;
            } else {
                return tripleSource.getValueFactory().createLiteral("");
            }
        } 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)

Aggregations

Thing (com.inova8.intelligentgraph.model.Thing)19 Order (org.junit.jupiter.api.Order)11 Test (org.junit.jupiter.api.Test)11 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)11 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)9 Resource (com.inova8.intelligentgraph.model.Resource)8 IRI (org.eclipse.rdf4j.model.IRI)8 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)6 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)6 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)6 Value (org.eclipse.rdf4j.model.Value)6 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)6 Trace (com.inova8.intelligentgraph.evaluator.Trace)5 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)4 NullValueReturnedException (com.inova8.intelligentgraph.exceptions.NullValueReturnedException)3 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)3 PredicateElement (com.inova8.pathql.element.PredicateElement)3 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 ReificationType (com.inova8.pathql.context.ReificationType)2 NoSuchElementException (java.util.NoSuchElementException)2