Search in sources :

Example 31 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method store.

protected final Iterable<Representation> store(Iterable<Representation> representations, boolean allowCreate) throws IllegalArgumentException, YardException {
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        ArrayList<Representation> added = new ArrayList<Representation>();
        for (Representation representation : representations) {
            if (representation != null) {
                // reassign
                Representation stored = store(con, representation, allowCreate, false);
                // to check if the store was successful
                if (stored != null) {
                    added.add(stored);
                } else {
                    // can only be the case if allowCreate==false (update was called)
                    log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!", representation.getId(), getId()));
                }
            }
        // ignore null values in the parsed Iterable!
        }
        con.commit();
        return added;
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove parsed Representations", e);
    } catch (IllegalArgumentException e) {
        try {
            // to avoid Exception logs in case store(..) throws an Exception
            // in the case allowCreate and canNotCreateIsError do not allow
            // the store operation
            con.rollback();
        } catch (RepositoryException ignore) {
        }
        throw e;
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ArrayList(java.util.ArrayList) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException)

Example 32 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method remove.

@Override
public void remove(String id) throws YardException, IllegalArgumentException {
    if (id == null) {
        throw new IllegalArgumentException("The parsed Representation id MUST NOT be NULL!");
    }
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        remove(con, sesameFactory.createURI(id));
        con.commit();
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove for Representation " + id, e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RepositoryException(org.openrdf.repository.RepositoryException)

Example 33 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameContextTest method initYard.

@BeforeClass
public static final void initYard() throws RepositoryException {
    repo.initialize();
    // create the graphs in Clerezza
    // init the ClerezzaYards for the created Clerezza graphs
    SesameYardConfig yard1config = new SesameYardConfig("context 1 yard");
    yard1config.setName("Yard over context 1");
    yard1config.setContextEnabled(true);
    yard1config.setContexts(new String[] { CONTEXT1.stringValue() });
    yard1 = new SesameYard(repo, yard1config);
    SesameYardConfig yard2config = new SesameYardConfig("context 2 yard");
    yard2config.setName("Yard over context 2");
    yard2config.setContextEnabled(true);
    yard2config.setContexts(new String[] { CONTEXT2.stringValue() });
    yard2 = new SesameYard(repo, yard2config);
    SesameYardConfig unionYardConfig = new SesameYardConfig("union yard");
    unionYardConfig.setName("Union Yard");
    unionYard = new SesameYard(repo, unionYardConfig);
    yards = Arrays.asList(yard1, yard2, unionYard);
    // add the test data (to the Repository to also test pre-existing data)
    RepositoryConnection con = repo.getConnection();
    con.begin();
    URI entity1 = sesameFactory.createURI("http://www.test.org/entity1");
    con.add(entity1, rdfType, skosConcept, CONTEXT1);
    con.add(entity1, skosPrefLabel, sesameFactory.createLiteral("test context one", EN), CONTEXT1);
    con.add(entity1, skosPrefLabel, sesameFactory.createLiteral("Test Context Eins", DE), CONTEXT1);
    expectedEntities.put(entity1, Arrays.asList(yard1, unionYard));
    URI entity2 = sesameFactory.createURI("http://www.test.org/entity2");
    con.add(entity2, rdfType, skosConcept, CONTEXT2);
    con.add(entity2, skosPrefLabel, sesameFactory.createLiteral("test context two", EN), CONTEXT2);
    con.add(entity2, skosPrefLabel, sesameFactory.createLiteral("Test Context Zwei", DE), CONTEXT2);
    expectedEntities.put(entity2, Arrays.asList(yard2, unionYard));
    con.commit();
    con.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) SesameYardConfig(org.apache.stanbol.entityhub.yard.sesame.SesameYardConfig) URI(org.openrdf.model.URI) SesameYard(org.apache.stanbol.entityhub.yard.sesame.SesameYard) BeforeClass(org.junit.BeforeClass)

Example 34 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 35 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3