Search in sources :

Example 1 with IntelligentGraphRepository

use of com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository in project com.inova8.intelligentgraph by peterjohnlawrence.

the class IntelligentGraphRepository method prefix.

/**
 * Prefix.
 *
 * @param prefix the prefix
 * @param IRI the iri
 * @return the intelligent graph repository
 */
public IntelligentGraphRepository prefix(String prefix, String IRI) {
    org.eclipse.rdf4j.model.IRI iri = Utilities.trimAndCheckIRIString(IRI);
    if (iri != null) {
        // } else {
        try {
            RepositoryConnection connection = this.getRepository().getConnection();
            connection.setNamespace(prefix, iri.stringValue());
            getRepositoryContext().getPrefixes().put(prefix, iri);
            logger.debug("Added prefix {} for namespace {} ", prefix, iri);
        } catch (Exception qe) {
            throw new ServerException(FAILEDTOREMOVEGRAPH_EXCEPTION, String.format("Failed to add prefix/namespace", prefix, iri), qe);
        }
    // }
    } else {
        logger.error("Invalid IRI specified. Ensure enclosed in <...> ", IRI);
    }
    return this;
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) ServerException(com.inova8.intelligentgraph.exceptions.ServerException) IRI(org.eclipse.rdf4j.model.IRI) NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) ScriptException(javax.script.ScriptException) ServerException(com.inova8.intelligentgraph.exceptions.ServerException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) PathPatternException(com.inova8.pathql.processor.PathPatternException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with IntelligentGraphRepository

use of com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository 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 3 with IntelligentGraphRepository

use of com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository 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 4 with IntelligentGraphRepository

use of com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository 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 5 with IntelligentGraphRepository

use of com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository 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)

Aggregations

IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)37 Thing (com.inova8.intelligentgraph.model.Thing)36 Order (org.junit.jupiter.api.Order)24 Test (org.junit.jupiter.api.Test)24 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)24 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)20 Resource (com.inova8.intelligentgraph.model.Resource)15 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)11 IRI (org.eclipse.rdf4j.model.IRI)9 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)9 Repository (org.eclipse.rdf4j.repository.Repository)8 EvaluationContext (com.inova8.intelligentgraph.evaluator.EvaluationContext)7 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)7 Trace (com.inova8.intelligentgraph.evaluator.Trace)6 Value (org.eclipse.rdf4j.model.Value)5 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)5 ValueExprEvaluationException (org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException)5 PathElement (com.inova8.pathql.element.PathElement)4 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4