Search in sources :

Example 1 with RDFFormat

use of org.openrdf.rio.RDFFormat in project nanopub-server by tkuhn.

the class LoadFiles method checkFilesToLoad.

private void checkFilesToLoad() {
    logger.info("Check whether there are files to load...");
    for (File f : loadDir.listFiles()) {
        stillAlive();
        if (f.isDirectory())
            continue;
        logger.info("Try to load file: " + f);
        try {
            final File processingFile = new File(processingDir, f.getName());
            f.renameTo(processingFile);
            RDFFormat format = Rio.getParserFormatForFileName(processingFile.getName());
            MultiNanopubRdfHandler.process(format, processingFile, new NanopubHandler() {

                @Override
                public void handleNanopub(Nanopub np) {
                    if (!ourPattern.matchesUri(np.getUri().toString()))
                        return;
                    try {
                        db.loadNanopub(np);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    stillAlive();
                }
            });
            processingFile.renameTo(new File(doneDir, f.getName()));
            logger.info("File loaded: " + processingFile);
        } catch (Exception ex) {
            logger.error("Failed to load file: " + f, ex);
        }
    }
}
Also used : NanopubHandler(org.nanopub.MultiNanopubRdfHandler.NanopubHandler) Nanopub(org.nanopub.Nanopub) File(java.io.File) RDFFormat(org.openrdf.rio.RDFFormat)

Example 2 with RDFFormat

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

the class KafkaLoadStatements method fromFile.

@Override
public void fromFile(final Path statementsPath, final String visibilities) throws RyaStreamsException {
    requireNonNull(statementsPath);
    requireNonNull(visibilities);
    if (!statementsPath.toFile().exists()) {
        throw new RyaStreamsException("Could not load statements at path '" + statementsPath + "' because that " + "does not exist. Make sure you've entered the correct path.");
    }
    // Create an RDF Parser whose format is derived from the statementPath's file extension.
    final RDFFormat format = RDFFormat.forFileName(statementsPath.getFileName().toString());
    final RDFParser parser = Rio.createParser(format);
    // Set a handler that writes the statements to the specified kafka topic.
    parser.setRDFHandler(new RDFHandlerBase() {

        @Override
        public void startRDF() throws RDFHandlerException {
            log.trace("Starting loading statements.");
        }

        @Override
        public void handleStatement(final Statement stmnt) throws RDFHandlerException {
            final VisibilityStatement visiStatement = new VisibilityStatement(stmnt, visibilities);
            producer.send(new ProducerRecord<>(topic, visiStatement));
        }

        @Override
        public void endRDF() throws RDFHandlerException {
            producer.flush();
            log.trace("Done.");
        }
    });
    // Do the parse and load.
    try {
        parser.parse(Files.newInputStream(statementsPath), "");
    } catch (RDFParseException | RDFHandlerException | IOException e) {
        throw new RyaStreamsException("Could not load the RDF file's Statements into Rya Streams.", e);
    }
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) Statement(org.openrdf.model.Statement) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) IOException(java.io.IOException) RDFParser(org.openrdf.rio.RDFParser) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Example 3 with RDFFormat

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

the class ConformanceTest method run.

@Override
public int run(final String[] args) throws Exception {
    // Validate command
    if (args.length < 1 || args.length > 2) {
        System.out.println("Usage:\n");
        System.out.println("\tConformanceTest [configuration options] " + "<test-file> <temp-dir>\n");
        System.out.println("to load test data from an RDF file " + "(configuration property " + MRUtils.FORMAT_PROP + " specifies the format, default RDF/XML); or\n");
        System.out.println("\tConformanceTest [configuration options] <temp-dir>\n");
        System.out.println("to load test data from a Rya instance (specified " + "using standard configuration properties).\n");
        System.out.println("For each test given, run the reasoner over the " + "premise ontology using a temporary Mini Accumulo instance " + "at <temp-dir>, then report conformance results.");
        System.exit(1);
    }
    final Set<Value> conformanceTestURIs = new HashSet<>();
    Collection<OwlTest> conformanceTests = new LinkedList<>();
    final List<OwlTest> successes = new LinkedList<>();
    final List<OwlTest> failures = new LinkedList<>();
    final Configuration conf = getConf();
    Repository repo;
    File workingDir;
    // If tests are in a file, stick them in a repository for querying
    if (args.length == 2) {
        workingDir = new File(PathUtils.clean(args[1]));
        RDFFormat inputFormat = RDFFormat.RDFXML;
        final String formatString = conf.get(MRUtils.FORMAT_PROP);
        if (formatString != null) {
            inputFormat = RDFFormat.valueOf(formatString);
        }
        repo = new SailRepository(new MemoryStore());
        repo.initialize();
        final RepositoryConnection conn = repo.getConnection();
        FileInputStream fileInput = new FileInputStream(PathUtils.clean(args[0]));
        conn.add(fileInput, "", inputFormat);
        fileInput.close();
        conn.close();
    } else // Otherwise, get a Rya repository
    {
        workingDir = new File(PathUtils.clean(args[0]));
        repo = MRReasoningUtils.getRepository(conf);
        repo.initialize();
    }
    // Query for the tests we're interested in
    final RepositoryConnection conn = repo.getConnection();
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_INCONSISTENCY));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_CONSISTENCY));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_ENTAILMENT));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_NONENTAILMENT));
    conformanceTests = getTests(conn, conformanceTestURIs);
    conn.close();
    repo.shutDown();
    // Set up a MiniAccumulo cluster and set up conf to connect to it
    final String username = "root";
    final String password = "root";
    final MiniAccumuloCluster mini = new MiniAccumuloCluster(workingDir, password);
    mini.start();
    conf.set(MRUtils.AC_INSTANCE_PROP, mini.getInstanceName());
    conf.set(MRUtils.AC_ZK_PROP, mini.getZooKeepers());
    conf.set(MRUtils.AC_USERNAME_PROP, username);
    conf.set(MRUtils.AC_PWD_PROP, password);
    conf.setBoolean(MRUtils.AC_MOCK_PROP, false);
    conf.set(MRUtils.TABLE_PREFIX_PROPERTY, "temp_");
    // Run the conformance tests
    int result;
    for (final OwlTest test : conformanceTests) {
        System.out.println(test.uri);
        result = runTest(conf, args, test);
        if (result != 0) {
            return result;
        }
        if (test.success) {
            successes.add(test);
            System.out.println("(SUCCESS)");
        } else {
            failures.add(test);
            System.out.println("(FAIL)");
        }
    }
    mini.stop();
    System.out.println("\n" + successes.size() + " successful tests:");
    for (final OwlTest test : successes) {
        System.out.println("\t[SUCCESS] " + test.type() + " " + test.name);
    }
    System.out.println("\n" + failures.size() + " failed tests:");
    for (final OwlTest test : failures) {
        System.out.println("\t[FAIL] " + test.type() + " " + test.name);
        System.out.println("\t\t(" + test.description + ")");
        for (final Statement triple : test.error) {
            if (test.types.contains(TEST_ENTAILMENT)) {
                System.out.println("\t\tExpected: " + triple);
            } else if (test.types.contains(TEST_NONENTAILMENT)) {
                System.out.println("\t\tUnexpected: " + triple);
            }
        }
    }
    return 0;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Configuration(org.apache.hadoop.conf.Configuration) SailRepository(org.openrdf.repository.sail.SailRepository) Statement(org.openrdf.model.Statement) MiniAccumuloCluster(org.apache.accumulo.minicluster.MiniAccumuloCluster) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) MemoryStore(org.openrdf.sail.memory.MemoryStore) SailRepository(org.openrdf.repository.sail.SailRepository) Repository(org.openrdf.repository.Repository) Value(org.openrdf.model.Value) File(java.io.File) HashSet(java.util.HashSet) RDFFormat(org.openrdf.rio.RDFFormat)

Example 4 with RDFFormat

use of org.openrdf.rio.RDFFormat in project stanbol by apache.

the class RdfResourceImporter method importResource.

@Override
public ResourceState importResource(InputStream is, String resourceName) throws IOException {
    log.info("> importing {}:", resourceName);
    RDFFormat rdfFormat = Rio.getParserFormatForFileName(resourceName);
    if (rdfFormat == null) {
        log.info("  ... unable to detect RDF format for {}", resourceName);
        log.info("  ... resource '{}' will not be imported", resourceName);
        return ResourceState.IGNORED;
    } else {
        RepositoryConnection con = null;
        try {
            con = repository.getConnection();
            con.begin();
            con.add(new InputStreamReader(is, UTF8), baseUri, rdfFormat, contexts);
            con.commit();
            return ResourceState.LOADED;
        } catch (RDFParseException e) {
            log.error("  ... unable to parser RDF file " + resourceName + " (format: " + rdfFormat + ")", e);
            return ResourceState.ERROR;
        } catch (RepositoryException e) {
            throw new IllegalArgumentException("Repository Exception while " + resourceName + "!", e);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (RepositoryException e1) {
                /* ignore */
                }
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputStreamReader(java.io.InputStreamReader) RepositoryException(org.openrdf.repository.RepositoryException) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Example 5 with RDFFormat

use of org.openrdf.rio.RDFFormat in project stanbol by apache.

the class RdfIndexingSource method loadRepositoryConfig.

/**
 * @param repoConfigFile
 * @return
 */
private RepositoryConfig loadRepositoryConfig(File repoConfigFile) {
    Repository configRepo = new SailRepository(new MemoryStore());
    RepositoryConnection con = null;
    try {
        configRepo.initialize();
        con = configRepo.getConnection();
        // We need to load the configuration into a context
        org.openrdf.model.URI configContext = con.getValueFactory().createURI("urn:stanbol.entityhub:indexing.source.sesame:config.context");
        RDFFormat format = Rio.getParserFormatForFileName(repoConfigFile.getName());
        try {
            con.add(new InputStreamReader(new FileInputStream(repoConfigFile), Charset.forName("UTF-8")), baseUri, format, configContext);
        } catch (RDFParseException e) {
            throw new IllegalArgumentException("Unable to parsed '" + repoConfigFile + "' using RDF format '" + format + "'!", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to access '" + repoConfigFile + "'!", e);
        }
        con.commit();
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to load '" + repoConfigFile + "' to inmemory Sail!", e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException e) {
            /* ignore */
            }
        }
    }
    Set<String> repoNames;
    RepositoryConfig repoConfig;
    try {
        repoNames = RepositoryConfigUtil.getRepositoryIDs(configRepo);
        if (repoNames.size() == 1) {
            repoConfig = RepositoryConfigUtil.getRepositoryConfig(configRepo, repoNames.iterator().next());
            repoConfig.validate();
        } else if (repoNames.size() > 1) {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' MUST only contain a single repository configuration!");
        } else {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' DOES NOT contain a repository configuration!");
        }
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to read RepositoryConfiguration form the " + "in-memory Sail!", e);
    } catch (RepositoryConfigException e) {
        throw new IllegalArgumentException("Repository Configuration in '" + repoConfigFile + "is not valid!", e);
    } finally {
        try {
            configRepo.shutDown();
        } catch (RepositoryException e) {
        /* ignore */
        }
    }
    if (repoConfig.getRepositoryImplConfig() == null) {
        throw new IllegalArgumentException("Missing RepositoryImpl config for " + "config " + repoConfig.getID() + " of file " + repoConfigFile + "!");
    }
    return repoConfig;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryConfig(org.openrdf.repository.config.RepositoryConfig) InputStreamReader(java.io.InputStreamReader) SailRepository(org.openrdf.repository.sail.SailRepository) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) RepositoryConfigException(org.openrdf.repository.config.RepositoryConfigException) FileInputStream(java.io.FileInputStream) MemoryStore(org.openrdf.sail.memory.MemoryStore) Repository(org.openrdf.repository.Repository) SailRepository(org.openrdf.repository.sail.SailRepository) URI(org.openrdf.model.URI) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Aggregations

RDFFormat (org.openrdf.rio.RDFFormat)15 IOException (java.io.IOException)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 Statement (org.openrdf.model.Statement)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RDFParseException (org.openrdf.rio.RDFParseException)3 RDFParser (org.openrdf.rio.RDFParser)3 InputStreamReader (java.io.InputStreamReader)2 LinkedList (java.util.LinkedList)2 Configuration (org.apache.hadoop.conf.Configuration)2 Nanopub (org.nanopub.Nanopub)2 Repository (org.openrdf.repository.Repository)2 RepositoryException (org.openrdf.repository.RepositoryException)2 SailRepository (org.openrdf.repository.sail.SailRepository)2 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)2 RDFHandlerException (org.openrdf.rio.RDFHandlerException)2 MemoryStore (org.openrdf.sail.memory.MemoryStore)2 InputStream (java.io.InputStream)1