Search in sources :

Example 1 with Graph

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

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

the class IntelligentGraphRepository method closeGraph.

/**
 * Close graph.
 *
 * @param graphName the graph name
 * @return the boolean
 */
@SuppressWarnings("deprecation")
public Boolean closeGraph(String graphName) {
    RepositoryConnection connection = this.getContextAwareConnection();
    IRI graphNameIri = null;
    try {
        graphNameIri = PathParser.parseIriRef(getRepositoryContext(), graphName).getIri();
        Boolean contextExists = connection.getContextIDs().asList().contains(graphNameIri);
        if (contextExists) {
            getPublicContexts().remove(graphNameIri);
            return true;
        } else {
            return false;
        }
    } catch (Exception qe) {
        throw new ServerException(FAILEDTOCLOSEGRAPH_EXCEPTION, String.format("Failed to close graph %s with exception %s", graphName, qe.getMessage()), qe);
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) ServerException(com.inova8.intelligentgraph.exceptions.ServerException) 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 3 with Graph

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

the class IntelligentGraphRepository method removeGraph.

/**
 * Removes the graph.
 *
 * @param graphName the graph name
 * @return the iri
 */
public IRI removeGraph(String graphName) {
    // RepositoryConnection connection = this.getRepository().getConnection();//.getContextAwareConnection();
    IRI graphNameIri = null;
    try (RepositoryConnection connection = this.getRepository().getConnection()) {
        graphNameIri = PathParser.parseIriRef(getRepositoryContext(), graphName).getIri();
        @SuppressWarnings("deprecation") Boolean contextExists = connection.getContextIDs().asList().contains(graphNameIri);
        if (contextExists) {
            Resource[] contexts = { graphNameIri };
            // connection.begin(IsolationLevels.SERIALIZABLE);
            // connection.clear(graphNameIri);
            // connection.clear();
            connection.remove((IRI) null, (IRI) null, null, contexts);
            // connection.remove(graphNameIri, (IRI) null, null,contexts);//graphNameIri);
            // connection.remove((Resource) null, (IRI) null, null);//graphNameIri);
            // connection.commit();
            logger.debug("Removed graph {} ", graphNameIri.stringValue());
        } else {
            logger.error("Failed to remove graph {} ", graphNameIri.stringValue());
            return null;
        // throw new ServerException(FAILEDTOREMOVEGRAPH_EXCEPTION,  String.format("Failed to remove graph %s. Does not exist", graphName));
        }
    } catch (Exception qe) {
        throw new ServerException(FAILEDTOREMOVEGRAPH_EXCEPTION, String.format("Failed to remove graph %s", graphName, qe.getMessage()), qe);
    }
    return graphNameIri;
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) ServerException(com.inova8.intelligentgraph.exceptions.ServerException) Resource(org.eclipse.rdf4j.model.Resource) 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 4 with Graph

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

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

the class Local_GetContextFact_Tests method test_2.

/**
 * Test 2.
 */
@Test
@Order(2)
void test_2() {
    try {
        IntelligentGraphRepository source = IntelligentGraphRepository.create(workingRep);
        source.removeGraph("<http://inova8.com/calc2graph/contextGraph>");
        Graph graph = source.addGraph("<http://inova8.com/calc2graph/contextGraph>");
        Thing myCountry = graph.getThing(":myCountry");
        myCountry.addFact(":myOption", "_customQueryOptions.get(\"time\").integerValue() ;", SCRIPT.GROOVY);
        CustomQueryOptions customQueryOptions1 = new CustomQueryOptions();
        customQueryOptions1.add("time", 42);
        customQueryOptions1.add("name", "Peter");
        Thing myCountry1 = graph.getThing(":myCountry");
        Trace result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>", customQueryOptions1);
        Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'&lt;http://inova8.com/calc2graph/def/myOption&gt;'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[name=&quot;Peter&quot;&amp;time=&quot;42&quot;^^&lt;http://www.w3.org/2001/XMLSchema#int&gt;]</li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get(&quot;time&quot;).integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=42^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
        CustomQueryOptions customQueryOptions2 = new CustomQueryOptions();
        customQueryOptions2.add("time", 43);
        result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>", customQueryOptions2);
        Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'&lt;http://inova8.com/calc2graph/def/myOption&gt;'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...usingoptions:[time=&quot;43&quot;^^&lt;http://www.w3.org/2001/XMLSchema#int&gt;]</li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get(&quot;time&quot;).integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=43^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
        result = myCountry1.traceFact("<http://inova8.com/calc2graph/def/myOption>&time='2019'^^xsd:int");
        Query.assertEqualsWOSpaces("<olstyle='list-style-type:none;'><li>Gettingfacts'&lt;http://inova8.com/calc2graph/def/myOption&gt;&amp;time='2019'^^xsd:int'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a></li></li><li>...withincontexts:[file://src/test/resources/calc2graph.def.ttl,http://inova8.com/calc2graph/contextGraph,file://src/test/resources/calc2graph.data.ttl]</li></li><olstyle='list-style-type:none;'><li>Evaluatingpredicate<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>,byinvoking<b>groovy</b>script</li></li><li><divstyle='border:1pxsolidblack;'><pre><code>_customQueryOptions.get(&quot;time&quot;).integerValue();</code></pre></div></li><olstyle='list-style-type:none;'></ol><li>Evaluated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li></ol><li>Calculated<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Retrievedcachedvalue<ahref='http://inova8.com/calc2graph/def/myOption'target='_blank'>myOption</a>of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><li>Returnedfact'http://inova8.com/calc2graph/def/myOption'of<ahref='http://inova8.com/calc2graph/def/myCountry'target='_blank'>myCountry</a>=2019^^<ahref='http://www.w3.org/2001/XMLSchema#int'target='_blank'>int</a></li></li><p></ol>", result.asHTML());
        ResourceResults results = myCountry1.getFacts("<http://inova8.com/calc2graph/def/myOption>&time='2020'^^xsd:int");
        for (Resource result1 : results) {
            Query.assertEqualsWOSpaces("2020", result1.stringValue());
        }
    } catch (Exception e) {
        assertEquals("", e.getMessage());
        e.printStackTrace();
    }
}
Also used : Trace(com.inova8.intelligentgraph.evaluator.Trace) IntelligentGraphRepository(com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository) Graph(com.inova8.intelligentgraph.intelligentGraphRepository.Graph) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(com.inova8.intelligentgraph.model.Resource) Thing(com.inova8.intelligentgraph.model.Thing) ResourceResults(com.inova8.intelligentgraph.results.ResourceResults) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

Thing (com.inova8.intelligentgraph.model.Thing)53 Graph (com.inova8.intelligentgraph.intelligentGraphRepository.Graph)45 Order (org.junit.jupiter.api.Order)44 Test (org.junit.jupiter.api.Test)44 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)44 IntelligentGraphRepository (com.inova8.intelligentgraph.intelligentGraphRepository.IntelligentGraphRepository)22 Resource (com.inova8.intelligentgraph.model.Resource)22 ResourceResults (com.inova8.intelligentgraph.results.ResourceResults)14 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)13 PathPatternException (com.inova8.pathql.processor.PathPatternException)12 RecognitionException (org.antlr.v4.runtime.RecognitionException)12 CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)8 Repository (org.eclipse.rdf4j.repository.Repository)8 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)6 NullValueReturnedException (com.inova8.intelligentgraph.exceptions.NullValueReturnedException)5 ServerException (com.inova8.intelligentgraph.exceptions.ServerException)5 ScriptException (javax.script.ScriptException)5 IRI (org.eclipse.rdf4j.model.IRI)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)5 Trace (com.inova8.intelligentgraph.evaluator.Trace)4