Search in sources :

Example 31 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class RyaDirectExample method main.

public static void main(final String[] args) throws Exception {
    final Configuration conf = getConf();
    conf.set(PrecomputedJoinIndexerConfig.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.name());
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
    log.info("Creating the tables as root.");
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        log.info("Creating PCJ Tables");
        createPCJ(conf);
        log.info("Connecting to Indexing Sail Repository.");
        final Sail extSail = RyaSailFactory.getInstance(conf);
        repository = new SailRepository(extSail);
        conn = repository.getConnection();
        final long start = System.currentTimeMillis();
        log.info("Running SPARQL Example: Add and Delete");
        testAddAndDelete(conn);
        log.info("Running SAIL/SPARQL Example: PCJ Search");
        testPCJSearch(conn);
        log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
        testAddAndTemporalSearchWithPCJ(conn);
        log.info("Running SAIL/SPARQL Example: Add and Free Text Search with PCJ");
        testAddAndFreeTextSearchWithPCJ(conn);
        // log.info("Running SPARQL Example: Add Point and Geo Search with PCJ");
        // //			testAddPointAndWithinSearchWithPCJ(conn);
        // log.info("Running SPARQL Example: Temporal, Freetext, and Geo Search");
        // testTemporalFreeGeoSearch(conn);
        // log.info("Running SPARQL Example: Geo, Freetext, and PCJ Search");
        // testGeoFreetextWithPCJSearch(conn);
        log.info("Running SPARQL Example: Delete Temporal Data");
        testDeleteTemporalData(conn);
        log.info("Running SPARQL Example: Delete Free Text Data");
        testDeleteFreeTextData(conn);
        // log.info("Running SPARQL Example: Delete Geo Data");
        // testDeleteGeoData(conn);
        log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
    } finally {
        log.info("Shutting down");
        closeQuietly(conn);
        closeQuietly(repository);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) AccumuloIndexingConfiguration(org.apache.rya.indexing.accumulo.AccumuloIndexingConfiguration) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Example 32 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class InferenceExamples method main.

public static void main(final String[] args) throws Exception {
    if (IS_DETAILED_LOGGING_ENABLED) {
        setupLogging();
    }
    final Configuration conf = getConf();
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        log.info("Connecting to Indexing Sail Repository.");
        final Sail sail = RyaSailFactory.getInstance(conf);
        repository = new SailRepository(sail);
        conn = repository.getConnection();
        final long start = System.currentTimeMillis();
        testInfer(conn, sail);
        testPropertyChainInference(conn, sail);
        testPropertyChainInferenceAltRepresentation(conn, sail);
        testSomeValuesFromInference(conn, sail);
        testAllValuesFromInference(conn, sail);
        testIntersectionOfInference(conn, sail);
        testOneOfInference(conn, sail);
        log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
    } finally {
        log.info("Shutting down");
        closeQuietly(conn);
        closeQuietly(repository);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) MongoIndexingConfiguration(org.apache.rya.indexing.mongodb.MongoIndexingConfiguration) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Example 33 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class SpinConstructRule method loadSpinRules.

/**
 * Load a set of SPIN rules from a data store.
 * @param conf Contains the connection information. Not null.
 * @return A map of rule identifiers to rule objects.
 * @throws ForwardChainException if connecting, querying for rules, or
 *  parsing rules fails.
 */
public static Ruleset loadSpinRules(RdfCloudTripleStoreConfiguration conf) throws ForwardChainException {
    Preconditions.checkNotNull(conf);
    Map<Resource, Rule> rules = new ConcurrentHashMap<>();
    // Connect to Rya
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        repository = new SailRepository(RyaSailFactory.getInstance(conf));
    } catch (Exception e) {
        throw new ForwardChainException("Couldn't initialize SAIL from configuration", e);
    }
    // Load and parse the individual SPIN rules from the data store
    String ruleQueryString = "SELECT ?type ?rule ?text WHERE {\n" + "  ?type <" + SPIN.RULE_PROPERTY.stringValue() + "> ?rule .\n" + "  {\n" + "    ?rule a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + "    ?rule <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + "  } UNION {\n" + "    ?rule a ?template .\n" + "    ?template <" + SPIN.BODY_PROPERTY + ">? ?body .\n" + "    ?body a <" + SP.CONSTRUCT_CLASS.stringValue() + "> .\n" + "    ?body <" + SP.TEXT_PROPERTY.stringValue() + "> ?text .\n" + "  }\n" + "}";
    SPARQLParser parser = new SPARQLParser();
    try {
        conn = repository.getConnection();
        TupleQuery ruleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, ruleQueryString);
        ruleQuery.evaluate(new AbstractTupleQueryResultHandler() {

            @Override
            public void handleSolution(BindingSet bs) throws TupleQueryResultHandlerException {
                // For each rule identifier found, instantiate a SpinRule
                Value requiredType = bs.getValue("type");
                Value ruleIdentifier = bs.getValue("rule");
                Value ruleText = bs.getValue("text");
                if (requiredType instanceof Resource && ruleIdentifier instanceof Resource && ruleText instanceof Literal) {
                    ParsedQuery parsedRule;
                    try {
                        parsedRule = parser.parseQuery(ruleText.stringValue(), null);
                        if (parsedRule instanceof ParsedGraphQuery) {
                            SpinConstructRule rule = new SpinConstructRule((Resource) requiredType, (Resource) ruleIdentifier, (ParsedGraphQuery) parsedRule);
                            if (rule.hasAnonymousConsequent()) {
                                logger.error("Skipping unsupported rule " + ruleIdentifier + " -- consequent refers to bnode, which is not" + " currently supported (creating new bnodes at each" + " application could lead to infinite recursion).");
                            } else {
                                rules.put((Resource) ruleIdentifier, rule);
                            }
                        }
                    } catch (Exception e) {
                        throw new TupleQueryResultHandlerException(e);
                    }
                }
            }
        });
    } catch (TupleQueryResultHandlerException | QueryEvaluationException | MalformedQueryException | RepositoryException e) {
        throw new ForwardChainException("Couldn't retrieve SPIN rules", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (RepositoryException e) {
                logger.warn("Error closing repository connection", e);
            }
        }
        if (repository.isInitialized()) {
            try {
                repository.shutDown();
            } catch (RepositoryException e) {
                logger.warn("Error shutting down repository", e);
            }
        }
    }
    return new Ruleset(rules.values());
}
Also used : SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) AbstractTupleQueryResultHandler(org.eclipse.rdf4j.query.AbstractTupleQueryResultHandler) Literal(org.eclipse.rdf4j.model.Literal) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Value(org.eclipse.rdf4j.model.Value) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException)

Example 34 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project RDFConverter by Mat-O-Lab.

the class RDFValidator method validateRDF.

protected static String validateRDF(String rdf, String shaclShapes) throws UnsupportedEncodingException {
    String ret = null;
    boolean bValid = true;
    ShaclSail shaclSail = new ShaclSail(new MemoryStore());
    SailRepository sailRepository = new SailRepository(shaclSail);
    sailRepository.init();
    try {
        SailRepositoryConnection connection = sailRepository.getConnection();
        connection.begin();
        StringReader shaclRules = new StringReader(shaclShapes);
        connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
        connection.commit();
        connection.begin();
        StringReader rdfData = new StringReader(rdf);
        connection.add(rdfData, "", RDFFormat.TURTLE);
        connection.commit();
    } catch (Exception exception) {
        Throwable cause = exception.getCause();
        bValid = false;
        if (cause instanceof ShaclSailValidationException) {
            Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
            ret = baos.toString(String.valueOf(StandardCharsets.UTF_8));
        }
        if (cause == null) {
            ret = "fail: " + exception.getMessage();
        } else {
            ret = "fail: " + ret;
        }
    }
    if (bValid) {
        ret = "VALID";
    }
    return ret;
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ServletException(jakarta.servlet.ServletException)

Example 35 with SailRepositoryConnection

use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project RDFConverter by Mat-O-Lab.

the class ShaclSampleCode method main.

public static void main(String[] args) throws IOException {
    boolean bValid = true;
    ShaclSail shaclSail = new ShaclSail(new MemoryStore());
    // Logger root = (Logger) LoggerFactory.getLogger(ShaclSail.class.getName());
    // root.setLevel(Level.INFO);
    // shaclSail.setLogValidationPlans(true);
    // shaclSail.setGlobalLogValidationExecution(true);
    // shaclSail.setLogValidationViolations(true);
    SailRepository sailRepository = new SailRepository(shaclSail);
    sailRepository.init();
    try (SailRepositoryConnection connection = sailRepository.getConnection()) {
        connection.begin();
        StringReader shaclRules = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix sh: <http://www.w3.org/ns/shacl#> .", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "ex:PersonShape", "  a sh:NodeShape  ;", "  sh:targetClass foaf:Person ;", "  sh:property ex:PersonShapeProperty .", "ex:PersonShapeProperty ", "  sh:path foaf:age ;", "  sh:datatype xsd:int ;", "  sh:maxCount 1 ;", "  sh:minCount 1 ."));
        connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
        connection.commit();
        connection.begin();
        StringReader invalidSampleData = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "ex:peter a foaf:Person ;", "  foaf:age 20, \"30\"^^xsd:int  ."));
        connection.add(invalidSampleData, "", RDFFormat.TURTLE);
        try {
            connection.commit();
        } catch (Exception exception) {
            Throwable cause = exception.getCause();
            if (cause instanceof ShaclSailValidationException) {
                Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
                // Rio.write(validationReportModel, System.out, RDFFormat.TURTLE);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
                System.err.println(baos.toString(StandardCharsets.UTF_8.toString()));
                bValid = false;
            }
            if (!bValid) {
                System.err.println("RDF NOT VALID !!! Throw Exception !!!");
            }
            throw exception;
        }
        if (bValid) {
            System.out.println("RDF VALID !!!");
        }
    }
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) StringReader(java.io.StringReader) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) IOException(java.io.IOException)

Aggregations

SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)65 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)44 Sail (org.eclipse.rdf4j.sail.Sail)37 Test (org.junit.Test)35 BindingSet (org.eclipse.rdf4j.query.BindingSet)24 HashSet (java.util.HashSet)17 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)15 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)13 Statement (org.eclipse.rdf4j.model.Statement)11 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)9 ArrayList (java.util.ArrayList)8 Connector (org.apache.accumulo.core.client.Connector)8 Configuration (org.apache.hadoop.conf.Configuration)8 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)8 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)8 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)7 StatefulMongoDBRdfConfiguration (org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration)7 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)7 RyaIRI (org.apache.rya.api.domain.RyaIRI)6