use of org.eclipse.rdf4j.rio.WriterConfig in project molgenis by molgenis.
the class EntityModelWriterTest method testCreateStatementForAttribute.
@ParameterizedTest
@MethodSource("createStatementForAttributeProvider")
void testCreateStatementForAttribute(AttributeType attributeType, Object value, Consumer<Entity> consumer, String fragment) {
when(objectEntity.getEntityType()).thenReturn(entityType);
when(entityType.getId()).thenReturn("fdp_Catalog");
when(objectEntity.getIdValue()).thenReturn("attributeName");
when(objectEntity.get("attributeName")).thenReturn(value);
consumer.accept(objectEntity);
when(entityType.getAtomicAttributes()).thenReturn(List.of(attribute));
when(attribute.getName()).thenReturn("attributeName");
when(attribute.getDataType()).thenReturn(attributeType);
Multimap<Relation, LabeledResource> tags = ImmutableMultimap.of(isAssociatedWith, labeledResource);
when(labeledResource.getIri()).thenReturn("http://example.org/iri");
when(tagService.getTagsForAttribute(entityType, attribute)).thenReturn(tags);
Model result = writer.createRdfModel(objectEntity);
StringWriter writer = new StringWriter();
Rio.write(result, writer, TURTLE, new WriterConfig().set(INLINE_BLANK_NODES, true));
assertThat(writer.toString()).contains(fragment);
}
use of org.eclipse.rdf4j.rio.WriterConfig in project incubator-rya by apache.
the class QueryResultsOutputUtil method toBindingSetJSONFile.
/**
* Writes the results of a {@link QueryResultStream} to the output stream as JSON until the
* shutdown signal is set.
*
* @param out - The stream the JSON will be written to. (not null)
* @param query - The parsed SPARQL Query whose results are being output. This
* object is used to figure out which bindings may appear. (not null)
* @param resultsStream - The results stream that will be polled for results to
* write to {@code out}. (not null)
* @param shutdownSignal - Setting this signal will cause the thread that
* is processing this function to finish and leave. (not null)
* @throws TupleQueryResultHandlerException A problem was encountered while
* writing the JSON to the output stream.
* @throws IllegalStateException The {@code resultsStream} is closed.
* @throws RyaStreamsException Could not fetch the next set of results.
*/
public static void toBindingSetJSONFile(final OutputStream out, final TupleExpr query, final QueryResultStream<VisibilityBindingSet> resultsStream, final AtomicBoolean shutdownSignal) throws TupleQueryResultHandlerException, IllegalStateException, RyaStreamsException {
requireNonNull(out);
requireNonNull(query);
requireNonNull(resultsStream);
requireNonNull(shutdownSignal);
// Create a writer that does not pretty print.
final SPARQLResultsJSONWriter writer = new SPARQLResultsJSONWriter(out);
final WriterConfig config = writer.getWriterConfig();
config.set(BasicWriterSettings.PRETTY_PRINT, false);
// Start the JSON and enumerate the possible binding names.
writer.startQueryResult(Lists.newArrayList(query.getBindingNames()));
while (!shutdownSignal.get()) {
for (final VisibilityBindingSet result : resultsStream.poll(1000)) {
writer.handleSolution(result);
}
}
writer.endQueryResult();
}
use of org.eclipse.rdf4j.rio.WriterConfig in project molgenis by molgenis.
the class EntityModelWriterTest method testCreateRfdModelMREF.
@Test
void testCreateRfdModelMREF() {
when(objectEntity.getEntityType()).thenReturn(entityType);
when(entityType.getId()).thenReturn("fdp_Catalog");
when(objectEntity.getIdValue()).thenReturn("attributeName");
when(objectEntity.get("attributeName")).thenReturn(refEntity);
when(objectEntity.getEntities("attributeName")).thenReturn(List.of(refEntity));
when(refEntity.getEntityType()).thenReturn(refEntityType);
when(refEntityType.getAttributeNames()).thenReturn(List.of("IRI"));
when(refEntity.getString("IRI")).thenReturn("http://example.org/refEntity");
when(entityType.getAtomicAttributes()).thenReturn(List.of(attribute));
when(attribute.getName()).thenReturn("attributeName");
when(attribute.getDataType()).thenReturn(MREF);
when(tagService.getTagsForAttribute(entityType, attribute)).thenReturn(ImmutableMultimap.of(isAssociatedWith, labeledResource));
when(labeledResource.getIri()).thenReturn("http://example.org/relation");
Model result = writer.createRdfModel(objectEntity);
assertEquals(1, result.size());
StringWriter writer = new StringWriter();
Rio.write(result, writer, TURTLE, new WriterConfig().set(INLINE_BLANK_NODES, true));
assertThat(writer.toString()).contains("<http://example.org/relation> <http://example.org/refEntity>");
}
use of org.eclipse.rdf4j.rio.WriterConfig in project molgenis by molgenis.
the class EntityModelWriterTest method testCreateRfdModelForEntity.
@Test
void testCreateRfdModelForEntity() {
List<Attribute> attributeList = singletonList(attribute);
when(objectEntity.getEntityType()).thenReturn(entityType);
when(entityType.getId()).thenReturn("fdp_Catalog");
when(objectEntity.getIdValue()).thenReturn("catalogId");
when(objectEntity.get("attributeName1")).thenReturn("value1");
when(objectEntity.getString("attributeName1")).thenReturn("value1");
when(entityType.getAtomicAttributes()).thenReturn(attributeList);
when(attribute.getName()).thenReturn("attributeName1");
when(attribute.getDataType()).thenReturn(STRING);
when(tagService.getTagsForEntity(entityType)).thenReturn(List.of(entityTag, entityTag2));
when(entityTag.getRelation()).thenReturn(isAssociatedWith);
when(entityTag.getObject()).thenReturn(labeledResource);
when(labeledResource.getIri()).thenReturn(DCAT_RESOURCE);
when(entityTag2.getRelation()).thenReturn(isAssociatedWith);
when(entityTag2.getObject()).thenReturn(labeledResource2);
when(labeledResource2.getIri()).thenReturn(R3D_REPOSITORY);
LabeledResource tag1 = new LabeledResource("http://IRI1.nl", "tag1Label");
Multimap<Relation, LabeledResource> tags = ImmutableMultimap.of(isAssociatedWith, tag1);
when(tagService.getTagsForAttribute(entityType, attribute)).thenReturn(tags);
Model result = writer.createRdfModel(objectEntity);
assertEquals(9, result.size());
StringWriter writer = new StringWriter();
Rio.write(result, writer, TURTLE, new WriterConfig().set(INLINE_BLANK_NODES, true));
assertThat(writer.toString()).contains("<http://IRI1.nl> \"value1\";").contains("fdp:metadataIdentifier [ a datacite:Identifier;\n" + " dct:identifier <http://example.org/api/fdp/fdp_Catalog/catalogId>\n" + " ]").contains("r3d:repositoryIdentifier [ a datacite:Identifier;\n" + " dct:identifier <http://example.org/api/fdp/fdp_Catalog/catalogId>\n" + " ]");
}
use of org.eclipse.rdf4j.rio.WriterConfig in project molgenis by molgenis.
the class EntityModelWriterTest method testCreateRfdModelWithCustomIRI.
@Test
void testCreateRfdModelWithCustomIRI() {
List<Attribute> attributeList = singletonList(attribute);
when(objectEntity.getEntityType()).thenReturn(entityType);
when(entityType.getId()).thenReturn("fdp_Catalog");
when(objectEntity.getString("IRI")).thenReturn("http://purl.org/example/catalog_id");
when(entityType.getAttributeNames()).thenReturn(singletonList("IRI"));
when(entityType.getAtomicAttributes()).thenReturn(attributeList);
when(attribute.getName()).thenReturn("IRI");
when(tagService.getTagsForEntity(entityType)).thenReturn(List.of(entityTag, entityTag2));
when(entityTag.getRelation()).thenReturn(isAssociatedWith);
when(entityTag.getObject()).thenReturn(labeledResource);
when(labeledResource.getIri()).thenReturn(DCAT_RESOURCE);
Model result = writer.createRdfModel(objectEntity);
assertEquals(4, result.size());
StringWriter writer = new StringWriter();
Rio.write(result, writer, TURTLE, new WriterConfig().set(INLINE_BLANK_NODES, true));
assertThat(writer.toString()).contains("<http://purl.org/example/catalog_id> a dcat:Resource").contains("fdp:metadataIdentifier [ a datacite:Identifier;\n" + " dct:identifier <http://purl.org/example/catalog_id>\n" + " ]");
}
Aggregations