Search in sources :

Example 46 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class JSONLDInternalTripleCallback method triple.

private void triple(String s, String p, String value, String datatype, String language, String graph) {
    if (s == null || p == null || value == null) {
        // TODO: i don't know what to do here!!!!
        return;
    }
    final Resource subject = createResource(s);
    final IRI predicate = vf.createIRI(p);
    final IRI datatypeURI = datatype == null ? null : vf.createIRI(datatype);
    Value object;
    try {
        object = RDFParserHelper.createLiteral(value, language, datatypeURI, getParserConfig(), getParserErrorListener(), getValueFactory());
    } catch (final RDFParseException e) {
        throw new RuntimeException(e);
    }
    Statement result;
    if (graph == null) {
        result = vf.createStatement(subject, predicate, object);
    } else {
        result = vf.createStatement(subject, predicate, object, createResource(graph));
    }
    if (handler != null) {
        try {
            handler.handleStatement(result);
        } catch (final RDFHandlerException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 47 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class BufferedGroupingRDFHandler method processBuffer.

/*
	 * not synchronized, assumes calling method has obtained a lock on bufferLock
	 */
private void processBuffer() throws RDFHandlerException {
    // primary grouping per context.
    for (Resource context : contexts) {
        Set<Resource> subjects = GraphUtil.getSubjects(bufferedStatements, null, null, context);
        for (Resource subject : subjects) {
            Set<IRI> processedPredicates = new HashSet<IRI>();
            // give rdf:type preference over other predicates.
            Iterator<Statement> typeStatements = bufferedStatements.match(subject, RDF.TYPE, null, context);
            while (typeStatements.hasNext()) {
                Statement typeStatement = typeStatements.next();
                super.handleStatement(typeStatement);
            }
            processedPredicates.add(RDF.TYPE);
            // retrieve other statement from this context with the same
            // subject, and output them grouped by predicate
            Iterator<Statement> subjectStatements = bufferedStatements.match(subject, null, null, context);
            while (subjectStatements.hasNext()) {
                Statement subjectStatement = subjectStatements.next();
                IRI predicate = subjectStatement.getPredicate();
                if (!processedPredicates.contains(predicate)) {
                    Iterator<Statement> toWrite = bufferedStatements.match(subject, predicate, null, context);
                    while (toWrite.hasNext()) {
                        Statement toWriteSt = toWrite.next();
                        super.handleStatement(toWriteSt);
                    }
                    processedPredicates.add(predicate);
                }
            }
        }
    }
    bufferedStatements.clear();
    contexts.clear();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) HashSet(java.util.HashSet)

Example 48 with Resource

use of org.eclipse.rdf4j.model.Resource in project opentheso by miledrousset.

the class WriteRdfFileTest method write4.

@Test
public void write4() {
    // We'll use a ModelBuilder to create two named graphs, one containing data about
    // picasso, the other about Van Gogh.
    ModelBuilder builder = new ModelBuilder();
    builder.setNamespace("ex", "http://example.org/");
    // in named graph 1, we add info about Picasso
    builder.namedGraph("ex:namedGraph1").subject("ex:Picasso").add(RDF.TYPE, "type1").add(FOAF.FIRST_NAME, "Pablo");
    // in named graph2, we add info about Van Gogh.
    builder.namedGraph("ex:namedGraph2").subject("ex:VanGogh").add(RDF.TYPE, "type2").add(FOAF.FIRST_NAME, "Vincent");
    // We're done building, create our Model
    Model model = builder.build();
    // each named graph is stored as a separate context in our Model
    for (Resource context : model.contexts()) {
        System.out.println("Named graph " + context + " contains: ");
        // write _only_ the statemements in the current named graph to the console, in N-Triples format
        Rio.write(model.filter(null, null, null, context), System.out, RDFFormat.NTRIPLES);
        System.out.println();
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : ModelBuilder(org.eclipse.rdf4j.model.util.ModelBuilder) Model(org.eclipse.rdf4j.model.Model) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Resource(org.eclipse.rdf4j.model.Resource) Test(org.junit.Test)

Example 49 with Resource

use of org.eclipse.rdf4j.model.Resource in project molgenis by molgenis.

the class EntityModelWriter method addEntityToModel.

public void addEntityToModel(String subjectIRI, Entity objectEntity, Model model) {
    Resource subject = valueFactory.createIRI(subjectIRI);
    EntityType entityType = objectEntity.getEntityType();
    addStatementsForAttributeTags(objectEntity, model, subject, entityType);
    addStatementsForEntityTags(model, subject, entityType);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Resource(org.eclipse.rdf4j.model.Resource) LabeledResource(org.molgenis.data.semantic.LabeledResource)

Example 50 with Resource

use of org.eclipse.rdf4j.model.Resource in project molgenis by molgenis.

the class EntityModelWriterTest method testAddStatementsForEntityType.

@Test
public void testAddStatementsForEntityType() {
    Model model = new LinkedHashModel();
    Resource subject = valueFactory.createIRI("http://example.org/subject");
    LabeledResource object = new LabeledResource("http://example.org/object", "object");
    LabeledResource codeSystem = new LabeledResource("ex:object");
    SemanticTag<EntityType, LabeledResource, LabeledResource> tag = new SemanticTag<>("tagId", entityType, Relation.isAssociatedWith, object, codeSystem);
    when(tagService.getTagsForEntity(entityType)).thenReturn(singletonList(tag));
    writer.addStatementsForEntityTags(model, subject, entityType);
    Statement statement = valueFactory.createStatement(subject, TYPE, valueFactory.createIRI("http://example.org/object"));
    assertEquals(newArrayList(model), singletonList(statement));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) LabeledResource(org.molgenis.data.semantic.LabeledResource) Statement(org.eclipse.rdf4j.model.Statement) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Resource(org.eclipse.rdf4j.model.Resource) LabeledResource(org.molgenis.data.semantic.LabeledResource) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) SemanticTag(org.molgenis.data.semantic.SemanticTag) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4