Search in sources :

Example 1 with WriterConfig

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);
}
Also used : Relation(org.molgenis.data.semantic.Relation) LabeledResource(org.molgenis.data.semantic.LabeledResource) StringWriter(java.io.StringWriter) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) WriterConfig(org.eclipse.rdf4j.rio.WriterConfig) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with WriterConfig

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();
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) SPARQLResultsJSONWriter(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONWriter) WriterConfig(org.eclipse.rdf4j.rio.WriterConfig)

Example 3 with WriterConfig

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>");
}
Also used : StringWriter(java.io.StringWriter) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) WriterConfig(org.eclipse.rdf4j.rio.WriterConfig) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with WriterConfig

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" + "    ]");
}
Also used : Relation(org.molgenis.data.semantic.Relation) LabeledResource(org.molgenis.data.semantic.LabeledResource) StringWriter(java.io.StringWriter) Attribute(org.molgenis.data.meta.model.Attribute) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) WriterConfig(org.eclipse.rdf4j.rio.WriterConfig) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with WriterConfig

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" + "    ]");
}
Also used : StringWriter(java.io.StringWriter) Attribute(org.molgenis.data.meta.model.Attribute) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) WriterConfig(org.eclipse.rdf4j.rio.WriterConfig) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

WriterConfig (org.eclipse.rdf4j.rio.WriterConfig)8 StringWriter (java.io.StringWriter)7 Model (org.eclipse.rdf4j.model.Model)7 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 Test (org.junit.jupiter.api.Test)6 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 Attribute (org.molgenis.data.meta.model.Attribute)3 LabeledResource (org.molgenis.data.semantic.LabeledResource)3 Relation (org.molgenis.data.semantic.Relation)3 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)1 SPARQLResultsJSONWriter (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONWriter)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Entity (org.molgenis.data.Entity)1 EntityType (org.molgenis.data.meta.model.EntityType)1