Search in sources :

Example 1 with PredicateElement

use of com.inova8.pathql.element.PredicateElement 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 2 with PredicateElement

use of com.inova8.pathql.element.PredicateElement 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)

Example 3 with PredicateElement

use of com.inova8.pathql.element.PredicateElement in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphConnection method addFact.

/**
 * Adds the fact.
 *
 * @param modify the modify
 * @param thingresource the thingresource
 * @param pathQL the path QL
 * @param value the value
 * @param contexts the contexts
 * @throws RecognitionException the recognition exception
 * @throws PathPatternException the path pattern exception
 */
private void addFact(UpdateContext modify, Resource thingresource, String pathQL, Value value, Resource... contexts) throws RecognitionException, PathPatternException {
    IntelligentGraphRepository source = IntelligentGraphRepository.create(this);
    Thing thing = Thing.create(source, thingresource, null);
    PredicateElement predicateElement = (PredicateElement) PathParser.parsePathPattern(source.getRepositoryContext(), pathQL);
    predicateElement.getSourceVariable().setValue(thing.getValue());
    if (predicateElement.getIsReified()) {
        ReificationType reificationType = source.getRepositoryContext().getReificationTypes().get(predicateElement.getReification().stringValue());
        if (reificationType != null) {
            IRI reification = iri(reificationType.getReificationType().stringValue() + "/" + thing.getIRI().hashCode() + "." + predicateElement.getPredicate().hashCode() + "." + value.hashCode());
            super.addStatement(modify, reification, RDF.TYPE, predicateElement.getReification(), contexts);
            super.addStatement(modify, reification, reificationType.getReificationSubject(), thing.getIRI(), contexts);
            super.addStatement(modify, reification, reificationType.getReificationPredicate(), predicateElement.getPredicate(), contexts);
            super.addStatement(modify, reification, reificationType.getReificationObject(), value, contexts);
        } else {
            logger.error("Reified type not supported:" + predicateElement.toString());
        }
    } else {
        IRI propertyIri = predicateElement.getPredicate();
        super.addStatement(modify, thing.getIRI(), propertyIri, value, contexts);
        checkReificationsChanged(propertyIri);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) ReificationType(com.inova8.pathql.context.ReificationType) PredicateElement(com.inova8.pathql.element.PredicateElement) Thing(com.inova8.intelligentgraph.model.Thing)

Example 4 with PredicateElement

use of com.inova8.pathql.element.PredicateElement in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphConnection method deleteThingFacts.

/**
 * Delete thing facts.
 *
 * @param modify the modify
 * @param source the source
 * @param thing the thing
 * @param pathElement the path element
 * @param contexts the contexts
 * @throws PathPatternException the path pattern exception
 */
private void deleteThingFacts(UpdateContext modify, IntelligentGraphRepository source, Thing thing, PathElement pathElement, Resource... contexts) throws PathPatternException {
    CloseableIteration<BindingSet, QueryEvaluationException> resultsIterator = getResultsIterator(source, thing, pathElement, 0, contexts);
    if (((PredicateElement) pathElement).getIsReified()) {
        String reified = ((PredicateElement) pathElement).getReifiedVariable().getName();
        while (resultsIterator.hasNext()) {
            BindingSet bindingSet = resultsIterator.next();
            super.removeStatements((Resource) bindingSet.getBinding(reified).getValue(), null, null, contexts);
        }
    } else {
        String subj = pathElement.getTargetSubject().getName();
        String pred = pathElement.getTargetPredicate().getName();
        String obj = pathElement.getTargetVariable().getName();
        while (resultsIterator.hasNext()) {
            BindingSet bindingSet = resultsIterator.next();
            IRI predicate = (IRI) bindingSet.getBinding(pred).getValue();
            super.removeStatements((Resource) bindingSet.getBinding(subj).getValue(), (IRI) bindingSet.getBinding(pred).getValue(), bindingSet.getBinding(obj).getValue(), contexts);
            checkReificationsChanged(predicate);
        }
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) PredicateElement(com.inova8.pathql.element.PredicateElement)

Example 5 with PredicateElement

use of com.inova8.pathql.element.PredicateElement in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitReifiedPredicate.

/**
 * Visit reified predicate.
 *
 * @param ctx the ctx
 * @return the predicate element
 */
@Override
public PredicateElement visitReifiedPredicate(ReifiedPredicateContext ctx) {
    // reifiedPredicate :  iriRef? REIFIER predicateRef  factFilterPattern?  dereifier? ; #statementFilterPattern
    PredicateElement reifiedPredicateElement = new PredicateElement(this.repositoryContext);
    reifiedPredicateElement.setIsReified(true);
    if (ctx.iriRef() != null) {
        reifiedPredicateElement.setReification(((IriRefValueElement) visit(ctx.iriRef())).getIri());
    }
    if (ctx.predicateRef() != null) {
        reifiedPredicateElement.setPredicate(((IriRefValueElement) visit(ctx.predicateRef())).getIri());
    }
    if (ctx.factFilterPattern() != null) {
        reifiedPredicateElement.setObjectFilterElement((FactFilterElement) visit(ctx.factFilterPattern()));
    }
    if (ctx.dereifier() != null) {
        reifiedPredicateElement.setIsDereified(true);
    }
    return reifiedPredicateElement;
}
Also used : PredicateElement(com.inova8.pathql.element.PredicateElement)

Aggregations

PredicateElement (com.inova8.pathql.element.PredicateElement)7 IRI (org.eclipse.rdf4j.model.IRI)5 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)4 Thing (com.inova8.intelligentgraph.model.Thing)4 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)2 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)2 ReificationType (com.inova8.pathql.context.ReificationType)2 IriRefValueElement (com.inova8.pathql.element.IriRefValueElement)2 PathElement (com.inova8.pathql.element.PathElement)2 Value (org.eclipse.rdf4j.model.Value)2 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)2 AlternativePathElement (com.inova8.pathql.element.AlternativePathElement)1 BoundPathElement (com.inova8.pathql.element.BoundPathElement)1 FactFilterElement (com.inova8.pathql.element.FactFilterElement)1 FilterOperatorValueElement (com.inova8.pathql.element.FilterOperatorValueElement)1 LiteralValueElement (com.inova8.pathql.element.LiteralValueElement)1 ObjectListValueElement (com.inova8.pathql.element.ObjectListValueElement)1 QueryOptionsPathElement (com.inova8.pathql.element.QueryOptionsPathElement)1 SequencePathElement (com.inova8.pathql.element.SequencePathElement)1 ValueElement (com.inova8.pathql.element.ValueElement)1