use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad in project timbuctoo by HuygensING.
the class SummaryPropDataRetriever method createSummaryProperty.
public Optional<TypedValue> createSummaryProperty(SubjectReference source, DataSet dataSet) {
QuadStore quadStore = dataSet.getQuadStore();
final Optional<TypedValue> localConfiguredSummaryProp = getQuad(quadStore, source.getSubjectUri(), RdfConstants.RDF_TYPE).flatMap(collection -> getQuad(quadStore, collection.getObject(), summaryPropConfigPredicate)).flatMap(userConfigured -> {
try {
SummaryProp summaryProp = OBJECT_MAPPER.readValue(userConfigured.getObject(), SummaryProp.class);
return getQuad(summaryProp.getPath(), source.getSubjectUri(), quadStore).map(quad -> createTypedValue(quad, dataSet));
} catch (IOException e) {
LOG.error("Cannot parse SummaryProp: '{}'", userConfigured.getObject());
}
return Optional.empty();
});
if (localConfiguredSummaryProp.isPresent()) {
return localConfiguredSummaryProp;
} else {
// fallback to default summary props
for (SummaryProp prop : defaultProperties) {
Optional<CursorQuad> quad = getQuad(prop.getPath(), source.getSubjectUri(), quadStore);
if (quad.isPresent()) {
return Optional.of(createTypedValue(quad.get(), dataSet));
}
}
return Optional.empty();
}
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad in project timbuctoo by HuygensING.
the class JsonProvenanceToRdfPatch method collectReplacements.
public static Map<String, List<CursorQuad>> collectReplacements(JsonNode activity, QuadStore quadStore) {
Map<String, List<CursorQuad>> toReplace = new HashMap<>();
for (JsonNode revision : activity.get(PROV_GENERATES)) {
final JsonNode replacements = revision.get(TIM_REPLACEMENTS);
if (replacements != null) {
String entity = revision.get(PROV_SPECIALIZATION_OF).get(0).get("@id").asText();
List<CursorQuad> quads = new ArrayList<>();
toReplace.put(revision.get("@id").asText(), quads);
for (JsonNode replacement : replacements) {
if (replacement.has(TIM_PREDICATE)) {
final String predicate = replacement.get(TIM_PREDICATE).get(0).get("@value").asText();
try (Stream<CursorQuad> source = quadStore.getQuads(entity, predicate, Direction.OUT, "")) {
source.forEach(quads::add);
}
}
}
}
}
return toReplace;
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad in project timbuctoo by HuygensING.
the class JsonProvenanceToRdfPatch method sendQuads.
@Override
public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatus, DataSet dataSet) throws LogStorageFailedException {
for (JsonNode revision : activity.get(PROV_GENERATES)) {
final String entityUri = revision.get(PROV_SPECIALIZATION_OF).get(0).get("@id").asText();
final String revisionUri = revision.get("@id").asText();
String wasRevisionOf = null;
if (revision.get(PROV_REVISION_OF) != null) {
wasRevisionOf = revision.get(PROV_REVISION_OF).get(0).get("@id").asText();
}
generateRevisionInfo(saver, revisionUri, entityUri, wasRevisionOf);
generatePatch(saver, revision.get(TIM_ADDITIONS), entityUri, true);
for (CursorQuad quad : toReplace.getOrDefault(revisionUri, new ArrayList<>())) {
saver.delQuad(quad.getSubject(), quad.getPredicate(), quad.getObject(), quad.getValuetype().orElse(null), quad.getLanguage().orElse(null), null);
}
generatePatch(saver, revision.get(TIM_REPLACEMENTS), entityUri, true);
generatePatch(saver, revision.get(TIM_DELETIONS), entityUri, false);
}
try {
final HashMap map = OBJECT_MAPPER.treeToValue(activity, HashMap.class);
final RDFDataset dataset = (RDFDataset) JsonLdProcessor.toRDF(map);
for (String graphName : dataset.graphNames()) {
for (RDFDataset.Quad quad : dataset.getQuads(graphName)) {
saver.onQuad(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad.getObject().getValue(), quad.getObject().isLiteral() ? quad.getObject().getDatatype() : null, quad.getObject().getLanguage(), quad.getGraph() == null ? null : quad.getGraph().getValue());
}
}
} catch (JsonProcessingException | JsonLdError e) {
throw new LogStorageFailedException(e);
}
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad in project timbuctoo by HuygensING.
the class ViewConfigFetcher method get.
@Override
public Object get(DataFetchingEnvironment env) {
SubjectReference source = env.getSource();
final DataSet dataSet = source.getDataSet();
final QuadStore qs = dataSet.getQuadStore();
final Map<String, Type> schema = dataSet.getSchemaStore().getStableTypes();
final TypeNameStore typeNameStore = dataSet.getTypeNameStore();
try (Stream<CursorQuad> quads = qs.getQuads(source.getSubjectUri(), HAS_VIEW_CONFIG, Direction.OUT, "")) {
return quads.findFirst().flatMap(q -> {
try {
return Optional.ofNullable(objectMapper.readValue(q.getObject(), List.class));
} catch (IOException e) {
LOG.error("view config is not a valid JSON object", e);
return Optional.empty();
}
}).orElseGet(() -> makeDefaultViewConfig(source.getSubjectUri(), schema, typeNameStore));
}
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad in project timbuctoo by HuygensING.
the class SummaryPropDataRetrieverTest method createSummaryPropertyIgnoresValueTypesIfThePathIsLongerThanOne.
@Test
public void createSummaryPropertyIgnoresValueTypesIfThePathIsLongerThanOne() {
List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/path", "http://example.org/path2"));
SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
QuadStore quadStore = mock(QuadStore.class);
CursorQuad foundQuad1 = quadWithObject("http://example.org/objectFound1", Optional.of(RdfConstants.STRING));
CursorQuad foundQuad2 = quadWithObject("http://example.org/objectFound2", Optional.empty());
given(quadStore.getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "")).willReturn(Stream.of(foundQuad1, foundQuad2));
CursorQuad quadThatShouldNotBeFound = quadWithObject("http://example.org/objectThatShouldNotBeRetrieved", Optional.empty());
given(quadStore.getQuads("http://example.org/objectFound1", "http://example.org/path2", Direction.OUT, "")).willReturn(Stream.of(quadThatShouldNotBeFound));
CursorQuad quadWithObjectUriOfPath2 = quadWithObject("http://example.org/objectOfPath2", Optional.empty());
given(quadStore.getQuads("http://example.org/objectFound2", "http://example.org/path2", Direction.OUT, "")).willReturn(Stream.of(quadWithObjectUriOfPath2));
Optional<TypedValue> summaryProperty = instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
assertThat(summaryProperty, is(present()));
assertThat(summaryProperty.get(), hasProperty("value", is("http://example.org/objectOfPath2")));
verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "");
verify(quadStore).getQuads("http://example.org/objectFound2", "http://example.org/path2", Direction.OUT, "");
}
Aggregations