Search in sources :

Example 1 with Statement

use of org.openrdf.model.Statement in project gocd by gocd.

the class SesameGraph method dumpTriplesNotInContext.

private void dumpTriplesNotInContext(Writer writer) {
    try {
        writer.append("Statements not in any context: \n");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    RDFXMLWriter xmlWriter = new RDFXMLWriter(writer);
    xmlWriter.startRDF();
    try {
        RepositoryResult<Statement> result = conn.getStatements(null, null, null, false);
        while (result.hasNext()) {
            Statement statement = result.next();
            if (statement.getContext() == null) {
                xmlWriter.handleStatement(statement);
            }
        }
    } catch (RepositoryException | RDFHandlerException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            xmlWriter.endRDF();
        } catch (RDFHandlerException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) RDFXMLWriter(org.openrdf.rio.rdfxml.RDFXMLWriter) Statement(org.openrdf.model.Statement) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException)

Example 2 with Statement

use of org.openrdf.model.Statement in project wikidata-query-rdf by wikimedia.

the class StatementHelper method basicEntity.

/**
 * Construct statements about a basic entity.
 */
public static List<Statement> basicEntity(WikibaseUris uris, String id) {
    Literal version = new LiteralImpl("a revision number I promise");
    List<Statement> statements = new ArrayList<>();
    String entityDataUri = uris.entityData() + id;
    // EntityData is all munged onto Entity
    statement(statements, entityDataUri, SchemaDotOrg.ABOUT, id);
    statement(statements, entityDataUri, SchemaDotOrg.VERSION, version);
    statement(statements, entityDataUri, SchemaDotOrg.DATE_MODIFIED, new LiteralImpl("a date I promise"));
    return statements;
}
Also used : IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Statement(org.openrdf.model.Statement) Literal(org.openrdf.model.Literal) ArrayList(java.util.ArrayList)

Example 3 with Statement

use of org.openrdf.model.Statement in project wikidata-query-rdf by wikimedia.

the class RdfRepository method syncFromChanges.

/**
 * Sync repository from changes list.
 * @param changes List of changes.
 * @return Number of triples modified.
 */
public int syncFromChanges(Collection<Change> changes, boolean verifyResult) {
    if (changes.isEmpty()) {
        // no changes, we're done
        return 0;
    }
    UpdateBuilder b = new UpdateBuilder(msyncBody);
    b.bindUri("schema:about", SchemaDotOrg.ABOUT);
    b.bindUri("prov:wasDerivedFrom", Provenance.WAS_DERIVED_FROM);
    b.bind("uris.value", uris.value());
    b.bind("uris.statement", uris.statement());
    Set<String> entityIds = newHashSetWithExpectedSize(changes.size());
    List<Statement> insertStatements = new ArrayList<>();
    List<Statement> entityStatements = new ArrayList<>();
    List<Statement> statementStatements = new ArrayList<>();
    Set<Statement> aboutStatements = new HashSet<>();
    Set<String> valueSet = new HashSet<>();
    for (final Change change : changes) {
        if (change.getStatements() == null) {
            // broken change, probably failed retrieval
            continue;
        }
        entityIds.add(change.entityId());
        insertStatements.addAll(change.getStatements());
        classifyStatements(change.getStatements(), change.entityId(), entityStatements, statementStatements, aboutStatements);
        valueSet.addAll(change.getCleanupList());
    }
    if (entityIds.isEmpty()) {
        // If we've got no IDs, this means all change retrieval failed
        log.debug("Got no valid changes, we're done");
        return 0;
    }
    b.bindUris("entityList", entityIds, uris.entity());
    b.bindStatements("insertStatements", insertStatements);
    b.bindValues("entityStatements", entityStatements);
    b.bindValues("statementStatements", statementStatements);
    b.bindValues("aboutStatements", aboutStatements);
    if (!valueSet.isEmpty()) {
        UpdateBuilder cleanup = new UpdateBuilder(cleanUnused);
        cleanup.bindUris("values", valueSet);
        b.bind("cleanupQuery", cleanup.toString());
    } else {
        b.bind("cleanupQuery", "");
    }
    long start = System.currentTimeMillis();
    int modified = execute("update", UPDATE_COUNT_RESPONSE, b.toString());
    log.debug("Update query took {} millis and modified {} statements", System.currentTimeMillis() - start, modified);
    if (verifyResult) {
        try {
            verifyStatements(entityIds, insertStatements);
        } catch (QueryEvaluationException e) {
            throw new FatalException("Can't load verify results: " + e, e);
        }
    }
    return modified;
}
Also used : FatalException(org.wikidata.query.rdf.tool.exception.FatalException) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) Change(org.wikidata.query.rdf.tool.change.Change) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) HashSet(java.util.HashSet)

Example 4 with Statement

use of org.openrdf.model.Statement in project wikidata-query-rdf by wikimedia.

the class RdfRepositoryIntegrationTest method referenceWithExpandedValueChanged.

@Test
public void referenceWithExpandedValueChanged() throws QueryEvaluationException {
    referenceWithExpandedValue();
    String statementUri = uris.statement() + "someotheruuid";
    String referenceUri = uris.reference() + "andanotheruri";
    String valueUri = uris.value() + "someuuid2";
    cleanupList.add(valueUri);
    cleanupList.add(referenceUri);
    List<Statement> george = new ArrayList<>();
    statement(george, "Q23", "P509", statementUri);
    statement(george, statementUri, Provenance.WAS_DERIVED_FROM, referenceUri);
    statement(george, referenceUri, uris.property(PropertyType.REFERENCE_VALUE) + "P509", valueUri);
    statement(george, valueUri, Ontology.Time.VALUE, new LiteralImpl("dog"));
    statement(george, valueUri, Ontology.Time.CALENDAR_MODEL, new LiteralImpl("animals"));
    rdfRepository.sync("Q23", george, cleanupList);
    assertTrue(rdfRepository.ask(Provenance.prefix(Ontology.prefix(uris.prefixes(new StringBuilder()))).append("ASK { wd:Q23 p:P509 [ prov:wasDerivedFrom [ prv:P509 [ ontology:timeValue \"dog\" ] ] ] }").toString()));
    assertTrue(rdfRepository.ask(Provenance.prefix(Ontology.prefix(uris.prefixes(new StringBuilder()))).append("ASK { wd:Q23 p:P509 [ prov:wasDerivedFrom [ prv:P509 [ ontology:timeCalendarModel \"animals\" ] ] ] }").toString()));
    assertFalse(rdfRepository.ask(Provenance.prefix(Ontology.prefix(uris.prefixes(new StringBuilder()))).append("ASK { wd:Q23 p:P509 [ prov:wasDerivedFrom [ prv:P509 [ ontology:timeTime \"cat\" ] ] ] }").toString()));
    // We've unlinked enwiki
    assertFalse(rdfRepository.ask(Provenance.prefix(Ontology.prefix(uris.prefixes(new StringBuilder()))).append("ASK { wd:Q23 p:P509 [ prov:wasDerivedFrom [ pr:P143 [ p:P509 wd:Q328 ] ] ] }").toString()));
    assertTrue(rdfRepository.ask(Provenance.prefix(uris.prefixes(new StringBuilder())).append("ASK { wd:Q328 p:P509 wd:Q328 }").toString()));
}
Also used : IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 5 with Statement

use of org.openrdf.model.Statement in project wikidata-query-rdf by wikimedia.

the class RdfRepositoryIntegrationTest method expandedStatementWithExpandedValueChanged.

@Test
public void expandedStatementWithExpandedValueChanged() throws QueryEvaluationException {
    expandedStatementWithExpandedValue();
    String statementUri = uris.statement() + "someotheruuid";
    String valueUri = uris.value() + "newuuid";
    cleanupList.add(valueUri);
    List<Statement> george = new ArrayList<>();
    statement(george, "Q23", "P509", statementUri);
    statement(george, statementUri, uris.property(PropertyType.STATEMENT_VALUE) + "P509", valueUri);
    statement(george, valueUri, Ontology.Time.VALUE, new LiteralImpl("dog"));
    statement(george, valueUri, Ontology.Time.CALENDAR_MODEL, new LiteralImpl("animals"));
    Collection<String> cleanupList = new ArrayList<String>();
    cleanupList.add(valueUri);
    cleanupList.add(uris.value() + "someuuid");
    rdfRepository.sync("Q23", george);
    assertTrue(rdfRepository.ask(Ontology.prefix(uris.prefixes(new StringBuilder())).append("ASK { wd:Q23 p:P509 [ psv:P509 [ ontology:timeValue \"dog\" ] ] }").toString()));
    assertTrue(rdfRepository.ask(Ontology.prefix(uris.prefixes(new StringBuilder())).append("ASK { wd:Q23 p:P509 [ psv:P509 [ ontology:timeCalendarModel \"animals\" ] ] }").toString()));
    assertFalse(rdfRepository.ask(Ontology.prefix(uris.prefixes(new StringBuilder())).append("ASK { wd:Q23 p:P509 [ psv:P509 [ ontology:timeValue \"cat\" ] ] }").toString()));
}
Also used : IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Statement(org.openrdf.model.Statement) ArrayList(java.util.ArrayList) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

Statement (org.openrdf.model.Statement)359 Test (org.junit.Test)209 ValueFactory (org.openrdf.model.ValueFactory)114 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)106 URI (org.openrdf.model.URI)96 HashSet (java.util.HashSet)88 Resource (org.openrdf.model.Resource)69 Value (org.openrdf.model.Value)67 BindingSet (org.openrdf.query.BindingSet)60 RyaStatement (org.apache.rya.api.domain.RyaStatement)56 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)54 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)53 MapBindingSet (org.openrdf.query.impl.MapBindingSet)43 ArrayList (java.util.ArrayList)41 LiteralImpl (org.openrdf.model.impl.LiteralImpl)40 StatementImpl (org.openrdf.model.impl.StatementImpl)40 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)36 URIImpl (org.openrdf.model.impl.URIImpl)30 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24