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);
}
}
}
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();
}
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);
}
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);
}
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));
}
Aggregations