Search in sources :

Example 1 with RDFXMLParser

use of org.openrdf.rio.rdfxml.RDFXMLParser in project incubator-rya by apache.

the class ConformanceTest method getTests.

/**
 * Query a connection for conformance test details.
 */
Collection<OwlTest> getTests(final RepositoryConnection conn, final Set<Value> testURIs) throws IOException, OpenRDFException {
    final Map<Value, OwlTest> tests = new HashMap<>();
    final TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, "select * where { " + "?test <" + TYPE + "> ?testType .\n" + "?test <" + TEST_PREMISE + "> ?graph .\n" + "?test <" + TEST_ID + "> ?name .\n" + "?test <" + TEST_DESC + "> ?description .\n" + "?test <" + TEST_PROFILE + "> <" + TEST_RL + "> .\n" + "?test <" + TEST_SEMANTICS + "> <" + TEST_RDFBASED + "> .\n" + "OPTIONAL {?test <" + TEST_CONCLUSION + "> ?conclusion .}\n" + "OPTIONAL {?test <" + TEST_NONCONCLUSION + "> ?nonentailed .}\n" + "}");
    final TupleQueryResult queryResult = query.evaluate();
    while (queryResult.hasNext()) {
        final BindingSet bindings = queryResult.next();
        final Value uri = bindings.getValue("test");
        if (testURIs.contains(uri)) {
            OwlTest test;
            if (tests.containsKey(uri)) {
                test = tests.get(uri);
            } else {
                test = new OwlTest();
                test.uri = uri;
                test.name = bindings.getValue("name").stringValue();
                test.description = bindings.getValue("description").stringValue();
                test.premise = bindings.getValue("graph").stringValue();
                if (bindings.hasBinding("conclusion")) {
                    test.compareTo = bindings.getValue("conclusion").stringValue();
                }
                if (bindings.hasBinding("nonentailed")) {
                    test.compareTo = bindings.getValue("nonentailed").stringValue();
                }
                tests.put(uri, test);
            }
            test.types.add(bindings.getValue("testType").stringValue());
        }
    }
    for (final OwlTest test : tests.values()) {
        if (test.compareTo != null) {
            final RDFXMLParser parser = new RDFXMLParser();
            parser.setRDFHandler(test);
            parser.parse(new StringReader(test.compareTo), "");
        }
    }
    queryResult.close();
    return tests.values();
}
Also used : BindingSet(org.openrdf.query.BindingSet) HashMap(java.util.HashMap) RDFXMLParser(org.openrdf.rio.rdfxml.RDFXMLParser) Value(org.openrdf.model.Value) StringReader(java.io.StringReader) TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 2 with RDFXMLParser

use of org.openrdf.rio.rdfxml.RDFXMLParser in project backstage by zepheira.

the class DataLoadingUtilities method loadDataFromStream.

public static void loadDataFromStream(InputStream stream, String sourceURL, String lang, Sail sail) throws Exception {
    RepoSailTuple rs = createMemoryRepository(null);
    Repository r = rs.repository;
    lang = lang.toLowerCase();
    if ("exhibit/json".equals(lang)) {
        Properties properties = new Properties();
        BabelReader reader = new ExhibitJsonReader();
        try {
            if (reader.takesReader()) {
                InputStreamReader isr = new InputStreamReader(stream);
                reader.read(isr, sail, properties, Locale.getDefault());
            } else {
                reader.read(stream, sail, properties, Locale.getDefault());
            }
        } finally {
            stream.close();
        }
    } else {
        RDFParser parser = null;
        if ("rdfxml".equals(lang)) {
            parser = new RDFXMLParser(r.getValueFactory());
        } else if ("n3".equals(lang) || "turtle".equals(lang)) {
            parser = new TurtleParser(r.getValueFactory());
        } else if ("ntriples".equals(lang)) {
            parser = new NTriplesParser(r.getValueFactory());
        }
        try {
            SailConnection c = null;
            try {
                c = sail.getConnection();
                BNodeConverterStatementHandler handler = new BNodeConverterStatementHandler(c);
                parser.setRDFHandler(handler);
                parser.setParseErrorListener(new LoggingParseErrorListener(sourceURL));
                parser.setVerifyData(false);
                parser.setStopAtFirstError(false);
                parser.parse(stream, sourceURL);
                c.commit();
                _logger.info("Read " + handler.m_count + " statements from '" + sourceURL + "'");
            } catch (RepositoryException e) {
                if (c != null)
                    c.rollback();
            } finally {
                if (c != null)
                    c.close();
            }
        } catch (Exception e) {
            throw new ModelReadFromFileException("Failed to read data from '" + sourceURL + "'", e);
        } finally {
            stream.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RDFXMLParser(org.openrdf.rio.rdfxml.RDFXMLParser) NTriplesParser(org.openrdf.rio.ntriples.NTriplesParser) RepositoryException(org.openrdf.repository.RepositoryException) Properties(java.util.Properties) RDFParser(org.openrdf.rio.RDFParser) SailException(org.openrdf.sail.SailException) RepositoryException(org.openrdf.repository.RepositoryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TurtleParser(org.openrdf.rio.turtle.TurtleParser) SailRepository(org.openrdf.repository.sail.SailRepository) Repository(org.openrdf.repository.Repository) BabelReader(edu.mit.simile.babel.BabelReader) SailConnection(org.openrdf.sail.SailConnection) ExhibitJsonReader(edu.mit.simile.babel.exhibit.ExhibitJsonReader)

Aggregations

RDFXMLParser (org.openrdf.rio.rdfxml.RDFXMLParser)2 BabelReader (edu.mit.simile.babel.BabelReader)1 ExhibitJsonReader (edu.mit.simile.babel.exhibit.ExhibitJsonReader)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Value (org.openrdf.model.Value)1 BindingSet (org.openrdf.query.BindingSet)1 TupleQuery (org.openrdf.query.TupleQuery)1 TupleQueryResult (org.openrdf.query.TupleQueryResult)1 Repository (org.openrdf.repository.Repository)1 RepositoryException (org.openrdf.repository.RepositoryException)1 SailRepository (org.openrdf.repository.sail.SailRepository)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1 RDFParser (org.openrdf.rio.RDFParser)1 NTriplesParser (org.openrdf.rio.ntriples.NTriplesParser)1 TurtleParser (org.openrdf.rio.turtle.TurtleParser)1