Search in sources :

Example 16 with Change

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change in project timbuctoo by HuygensING.

the class ProvenanceChangeLogTest method getProvenanceForListObjectField.

@Test
public void getProvenanceForListObjectField() throws Exception {
    CustomProvenance customProvenance = CustomProvenance.getCustomProvenance(ImmutableMap.of("fields", Lists.newArrayList(ImmutableMap.of("uri", NAME_URI, "isList", true, "object", ImmutableMap.of("type", TYPE_URI, "fields", Lists.newArrayList(ImmutableMap.of("uri", FIRST_NAME_URI, "isList", false, "valueType", STRING)))))));
    when(dataSet.getCustomProvenance()).thenReturn(customProvenance);
    String value1 = "value1";
    Map<Object, Object> name1 = Maps.newHashMap();
    name1.put("uri", SUBJECT_NAME_1);
    name1.put(FIRST_NAME_FIELD, createPropertyInput(value1));
    Map<Object, Object> name2 = Maps.newHashMap();
    name2.put("uri", SUBJECT_NAME_2);
    String value2 = "value2";
    name2.put(FIRST_NAME_FIELD, createPropertyInput(value2));
    Map<Object, Object> provenance = Maps.newHashMap();
    provenance.put(NAME_FIELD, newArrayList(name1, name2));
    Map<Object, Object> entity = Maps.newHashMap();
    entity.put("provenance", provenance);
    ProvenanceChangeLog instance = new ProvenanceChangeLog(entity);
    List<Change> provChanges = instance.getProvenance(dataSet, SUBJECT).collect(toList());
    assertThat(provChanges.size(), is(6));
    assertThat(provChanges.get(0), is(likeChange().withSubject(SUBJECT).withPredicate(NAME_URI).withValues(new Value(SUBJECT_NAME_1, null)).oldValuesIsEmpty()));
    assertThat(provChanges.get(1), is(likeChange().withSubject(SUBJECT_NAME_1).withPredicate(RDF_TYPE).withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
    assertThat(provChanges.get(2), is(likeChange().withSubject(SUBJECT_NAME_1).withPredicate(FIRST_NAME_URI).withValues(new Value(value1, STRING)).oldValuesIsEmpty()));
    assertThat(provChanges.get(3), is(likeChange().withSubject(SUBJECT).withPredicate(NAME_URI).withValues(new Value(SUBJECT_NAME_2, null)).oldValuesIsEmpty()));
    assertThat(provChanges.get(4), is(likeChange().withSubject(SUBJECT_NAME_2).withPredicate(RDF_TYPE).withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
    assertThat(provChanges.get(5), is(likeChange().withSubject(SUBJECT_NAME_2).withPredicate(FIRST_NAME_URI).withValues(new Value(value2, STRING)).oldValuesIsEmpty()));
}
Also used : Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) ProvenanceChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.ProvenanceChangeLog) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) CustomProvenance(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance) Test(org.junit.Test)

Example 17 with Change

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change in project timbuctoo by HuygensING.

the class GraphQlToRdfPatch method addData.

private void addData(RdfPatchSerializer saver, DataSet dataSet) throws LogStorageFailedException {
    try (Stream<Change> deletions = changeLog.getDeletions(dataSet)) {
        for (Iterator<Change> changes = deletions.iterator(); changes.hasNext(); ) {
            Change change = changes.next();
            String predicate = change.getPredicate();
            try (Stream<Value> oldValues = change.getOldValues()) {
                for (Iterator<Value> iterator = oldValues.iterator(); iterator.hasNext(); ) {
                    Value value = iterator.next();
                    saver.addDelQuad(false, subjectUri, predicate, value.getRawValue(), value.getType(), null, graphUri);
                }
            }
        }
    }
    try (Stream<Change> replacements = changeLog.getReplacements(dataSet)) {
        for (Iterator<Change> changes = replacements.iterator(); changes.hasNext(); ) {
            Change change = changes.next();
            String predicate = change.getPredicate();
            try (Stream<Value> oldValues = change.getOldValues()) {
                for (Iterator<Value> iterator = oldValues.iterator(); iterator.hasNext(); ) {
                    Value value = iterator.next();
                    saver.addDelQuad(false, subjectUri, predicate, value.getRawValue(), value.getType(), null, graphUri);
                }
            }
            for (Value value : change.getValues()) {
                saver.addDelQuad(true, subjectUri, predicate, value.getRawValue(), value.getType(), null, graphUri);
            }
        }
    }
    try (Stream<Change> additions = changeLog.getAdditions(dataSet)) {
        for (Iterator<Change> changes = additions.iterator(); changes.hasNext(); ) {
            Change change = changes.next();
            for (Value value : change.getValues()) {
                saver.addDelQuad(true, subjectUri, change.getPredicate(), value.getRawValue(), value.getType(), null, graphUri);
            }
        }
    }
}
Also used : Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)

Example 18 with Change

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change in project timbuctoo by HuygensING.

the class GraphQlToRdfPatch method addPlan.

private void addPlan(RdfPatchSerializer saver, DataSet dataSet, String newRevision, String association) throws LogStorageFailedException {
    String planUri = dataSetObjectUri(dataSet, "plan");
    saver.addDelQuad(true, association, PROV_HAD_PLAN, planUri, null, null, graphUri);
    saver.addDelQuad(true, planUri, RDF_TYPE, PROV_PLAN, null, null, graphUri);
    // fill plan
    // add additions
    boolean additionsPresent;
    try (Stream<Change> additions = changeLog.getAdditions(dataSet)) {
        additionsPresent = additions.findAny().isPresent();
    }
    if (additionsPresent) {
        try (Stream<Change> additions = changeLog.getAdditions(dataSet)) {
            String actionPluralUri = dataSetObjectUri(dataSet, "additions");
            saver.addDelQuad(true, planUri, timPredicate("additions"), actionPluralUri, null, null, graphUri);
            saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Additions"), null, null, graphUri);
            for (Iterator<Change> changes = additions.iterator(); changes.hasNext(); ) {
                Change change = changes.next();
                String changeUri = dataSetObjectUri(dataSet, "addition");
                saver.addDelQuad(true, actionPluralUri, timPredicate("hasAddition"), changeUri, null, null, graphUri);
                saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Addition"), null, null, graphUri);
                String predicate = change.getPredicate();
                saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
                saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
                String prefValueUri = null;
                for (Value value : change.getValues()) {
                    String uri = dataSetObjectUri(dataSet, "value");
                    createValue(saver, changeUri, prefValueUri, value, uri);
                    prefValueUri = uri;
                }
            }
        }
    }
    // add deletions
    boolean deletionsPresent;
    try (Stream<Change> deletions = changeLog.getDeletions(dataSet)) {
        deletionsPresent = deletions.findAny().isPresent();
    }
    if (deletionsPresent) {
        try (Stream<Change> deletions = changeLog.getDeletions(dataSet)) {
            String actionPluralUri = dataSetObjectUri(dataSet, "deletions");
            saver.addDelQuad(true, planUri, timPredicate("deletions"), actionPluralUri, null, null, graphUri);
            saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Deletions"), null, null, graphUri);
            for (Iterator<Change> changes = deletions.iterator(); changes.hasNext(); ) {
                Change change = changes.next();
                String changeUri = dataSetObjectUri(dataSet, "deletion");
                saver.addDelQuad(true, actionPluralUri, timPredicate("hasDeletion"), changeUri, null, null, graphUri);
                saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Deletion"), null, null, graphUri);
                String predicate = change.getPredicate();
                saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
                saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
                String prefValueUri = null;
                try (Stream<Value> oldValues = change.getOldValues()) {
                    for (Iterator<Value> values = oldValues.iterator(); values.hasNext(); ) {
                        Value value = values.next();
                        String uri = dataSetObjectUri(dataSet, "value");
                        createValue(saver, changeUri, prefValueUri, value, uri);
                        prefValueUri = uri;
                    }
                }
            }
        }
    }
    // add replacements
    boolean replacementsPresent;
    try (Stream<Change> replacements = changeLog.getReplacements(dataSet)) {
        replacementsPresent = replacements.findAny().isPresent();
    }
    if (replacementsPresent) {
        try (Stream<Change> replacements = changeLog.getReplacements(dataSet)) {
            String actionPluralUri = dataSetObjectUri(dataSet, "replacements");
            saver.addDelQuad(true, planUri, timPredicate("replacements"), actionPluralUri, null, null, graphUri);
            saver.addDelQuad(true, actionPluralUri, RDF_TYPE, timType("Replacements"), null, null, graphUri);
            for (Iterator<Change> changes = replacements.iterator(); changes.hasNext(); ) {
                Change change = changes.next();
                String changeUri = dataSetObjectUri(dataSet, "replacement");
                saver.addDelQuad(true, actionPluralUri, timPredicate("hasReplacement"), changeUri, null, null, graphUri);
                saver.addDelQuad(true, changeUri, RDF_TYPE, timType("Replacement"), null, null, graphUri);
                String predicate = change.getPredicate();
                saver.addDelQuad(true, changeUri, timPredicate("hasKey"), predicate, null, null, graphUri);
                saver.addDelQuad(true, predicate, RDF_TYPE, timType("ChangeKey"), null, null, graphUri);
                String prefValueUri = null;
                for (Value value : change.getValues()) {
                    String uri = dataSetObjectUri(dataSet, "value");
                    createValue(saver, changeUri, prefValueUri, value, uri);
                    prefValueUri = uri;
                }
                try (Stream<Value> oldValues = change.getOldValues()) {
                    for (Iterator<Value> values = oldValues.iterator(); values.hasNext(); ) {
                        Value value = values.next();
                        String uri = dataSetObjectUri(dataSet, "oldValue");
                        createOldValue(saver, changeUri, prefValueUri, value, uri);
                        prefValueUri = uri;
                    }
                }
            }
        }
    }
    // add custom provenance
    if (changeLog.getProvenance(dataSet, subjectUri).findAny().isPresent()) {
        String customProvUri = dataSetObjectUri(dataSet, "customProv");
        saver.addDelQuad(true, planUri, timPredicate("hasCustomProv"), customProvUri, null, null, graphUri);
        saver.addDelQuad(true, customProvUri, RDF_TYPE, timType("CustomProv"), null, null, graphUri);
        Stream<Change> customProvChanges = changeLog.getProvenance(dataSet, newRevision, customProvUri);
        for (Iterator<Change> changes = customProvChanges.iterator(); changes.hasNext(); ) {
            Change change = changes.next();
            for (Value value : change.getValues()) {
                saver.addDelQuad(true, change.getSubject(), change.getPredicate(), value.getRawValue(), value.getType(), null, graphUri);
            }
        }
    }
}
Also used : Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)

Example 19 with Change

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change in project timbuctoo by HuygensING.

the class ChangeLog method getProvenanceChanges.

protected Stream<Change> getProvenanceChanges(DataSet dataSet, Graph graph, String[] subjects, CustomProvenance provenance, Map<String, JsonNode> values) {
    TypeNameStore typeNameStore = dataSet.getTypeNameStore();
    Stream<Change> customProv = provenance.getFields().stream().filter(field -> field.getValueType() != null).flatMap(field -> {
        String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
        return Stream.of(subjects).map(subject -> new Change(graph, subject, field.getUri(), getValues(dataSet, values.get(graphQlPred)), Stream.empty()));
    });
    Stream<Change> customProvNested = provenance.getFields().stream().filter(field -> field.getObject() != null).flatMap(field -> {
        String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
        JsonNode objectValues = values.get(graphQlPred);
        if (objectValues.isArray()) {
            Spliterator<JsonNode> spliterator = Spliterators.spliteratorUnknownSize(objectValues.iterator(), Spliterator.ORDERED);
            return StreamSupport.stream(spliterator, false).flatMap(newObjectValues -> getChangesForProvObject(dataSet, newObjectValues, graph, subjects, field));
        }
        return getChangesForProvObject(dataSet, objectValues, graph, subjects, field);
    });
    return Stream.concat(customProv, customProvNested);
}
Also used : Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) RdfConstants(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants) Spliterators(java.util.Spliterators) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Collectors(java.util.stream.Collectors) List(java.util.List) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Map(java.util.Map) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) Spliterator(java.util.Spliterator) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore) JsonNode(com.fasterxml.jackson.databind.JsonNode) Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)

Example 20 with Change

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change in project timbuctoo by HuygensING.

the class ChangeLog method getChangesForProvObject.

private Stream<Change> getChangesForProvObject(DataSet dataSet, JsonNode objectValues, Graph graph, String[] subjects, CustomProvenance.CustomProvenanceValueFieldInput field) {
    String newSubject;
    if (objectValues.get("uri") != null) {
        newSubject = objectValues.get("uri").asText();
    } else {
        TypeNameStore typeNameStore = dataSet.getTypeNameStore();
        String typeName = typeNameStore.makeGraphQlnameForPredicate(field.getObject().getType(), Direction.OUT, field.isList());
        newSubject = RdfConstants.dataSetObjectUri(dataSet, typeName);
    }
    return Stream.concat(Stream.concat(Stream.of(subjects).map(subject -> new Change(graph, subject, field.getUri(), new Change.Value(newSubject, null))), Stream.of(new Change(graph, newSubject, RdfConstants.RDF_TYPE, new Change.Value(field.getObject().getType(), null)))), getProvenanceChanges(dataSet, graph, new String[] { newSubject }, field.getObject(), OBJECT_MAPPER.convertValue(objectValues, new TypeReference<>() {
    })));
}
Also used : Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)

Aggregations

Test (org.junit.Test)36 Graph (nl.knaw.huygens.timbuctoo.v5.util.Graph)33 ChangeMatcher.likeChange (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange)32 Value (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)24 EditMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.EditMutationChangeLog)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 TypeNameStore (nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)5 Change (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change)5 CustomProvenance (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CustomProvenance)5 ProvenanceChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.ProvenanceChangeLog)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 List (java.util.List)4 Map (java.util.Map)4 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)4 Stream (java.util.stream.Stream)3 CreateMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog)3 DeleteMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.DeleteMutationChangeLog)3 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2