Search in sources :

Example 16 with Predicate

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate in project timbuctoo by HuygensING.

the class RdfDescriptionSaver method addValue.

@Override
public void addValue(String subject, String predicate, String value, String dataType, String graph) throws RdfProcessingFailedException {
    try {
        if (Objects.equals(subject, baseUri) && isDescriptionPredicate(predicate)) {
            ValueFactory vf = SimpleValueFactory.getInstance();
            model.add(vf.createIRI(subject), vf.createIRI(predicate), vf.createLiteral(value));
        }
    } catch (Exception e) {
        throw new RdfProcessingFailedException(e);
    }
}
Also used : ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 17 with Predicate

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate in project timbuctoo by HuygensING.

the class BdbSchemaStore method finish.

@Override
public void finish() {
    LOG.info("Finished processing entities");
    importStatus.setStatus("Finished processing entities");
    // Step 3: Add type information to inverse predicates
    for (Map.Entry<String, Type> typeEntry : types.entrySet()) {
        Type type = typeEntry.getValue();
        String typeName = typeEntry.getKey();
        for (Predicate predicate : type.getPredicates()) {
            predicate.finish();
            if (predicate.getDirection() == Direction.IN) {
                continue;
            }
            for (String referenceType : predicate.getReferenceTypes().keySet()) {
                try {
                    types.get(referenceType).getPredicate(predicate.getName(), // There must be an inverse for each outward predicate
                    Direction.IN).incReferenceType(type.getName(), 1);
                } catch (Exception e) {
                    String cause = "Referenced type " + referenceType + " not found";
                    try {
                        if (types.containsKey(referenceType)) {
                            cause = "type does not have the inverse predicate " + predicate.getName();
                            if (types.get(referenceType).getPredicate(predicate.getName(), Direction.IN) != null) {
                                cause = "Something failed during addreferencetype(" + typeName + ")";
                            }
                        }
                        LOG.error("Error during inverse generation (ignored): " + cause, e);
                        importStatus.addError("Error during inverse generation (ignored): " + cause, e);
                    } catch (Exception e2) {
                        LOG.error("Error during inverse generation " + cause, e);
                        importStatus.addError("Error during inverse generation " + cause, e);
                        LOG.error("Error during recovery generation ", e2);
                        importStatus.addError("Error during recovery generation ", e2);
                    }
                }
            }
        }
    }
    try {
        try {
            String serializedValue = objectMapper.writeValueAsString(types);
            dataStore.setValue(serializedValue);
            dataStore.commit();
            stableTypes = readTypes(serializedValue);
        } catch (IOException | DatabaseWriteException e) {
            throw new SchemaUpdateException(e);
        }
    } catch (SchemaUpdateException e) {
        e.printStackTrace();
    }
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) ChangeType(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) IOException(java.io.IOException) SchemaUpdateException(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaUpdateException) HashMap(java.util.HashMap) Map(java.util.Map) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) IOException(java.io.IOException) SchemaUpdateException(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaUpdateException) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Example 18 with Predicate

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate in project timbuctoo by HuygensING.

the class BdbSchemaStore method updatePredicateType.

public void updatePredicateType(Type type, CursorQuad quad, boolean inc, ChangeFetcher changeFetcher) {
    final Predicate predicate = type.getOrCreatePredicate(quad.getPredicate(), quad.getDirection());
    if (quad.getValuetype().isPresent()) {
        predicate.incValueType(quad.getValuetype().get(), inc ? 1 : -1);
    } else {
        try (Stream<CursorQuad> typeQs = changeFetcher.getPredicates(quad.getObject(), RDF_TYPE, OUT, !inc, true, inc)) {
            boolean[] hadType = new boolean[] { false };
            typeQs.forEach(typeQ -> {
                hadType[0] = true;
                predicate.incReferenceType(typeQ.getObject(), inc ? 1 : -1);
            });
            if (!hadType[0]) {
                predicate.incReferenceType(UNKNOWN, inc ? 1 : -1);
            }
        }
    }
}
Also used : CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Example 19 with Predicate

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate in project timbuctoo by HuygensING.

the class PredicateMutationRdfPatcher method sendQuads.

@Override
public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatusConsumer, DataSet dataSet) throws LogStorageFailedException {
    final QuadStore quadStore = dataSet.getQuadStore();
    final Map<String, String> foundSubjects = new HashMap<>();
    for (Map.Entry<UUID, PredicateMutation.SubjectFinder> entry : mutation.getSubjectFinders().entrySet()) {
        foundSubjects.put(entry.getKey().toString(), entry.getValue().getSubject(quadStore));
    }
    for (CursorQuad newValue : mutation.getFullRetractions()) {
        final String subject = foundSubjects.getOrDefault(newValue.getSubject(), newValue.getSubject());
        final String predicate = newValue.getPredicate();
        final Direction direction = newValue.getDirection();
        try (Stream<CursorQuad> quads = quadStore.getQuads(subject, predicate, direction, "")) {
            for (CursorQuad oldValue : (Iterable<CursorQuad>) quads::iterator) {
                saver.delQuad(oldValue.getSubject(), oldValue.getPredicate(), oldValue.getObject(), oldValue.getValuetype().orElse(null), oldValue.getLanguage().orElse(null), null);
            }
        }
    }
    for (CursorQuad oldValue : mutation.getRetractions()) {
        saver.delQuad(foundSubjects.getOrDefault(oldValue.getSubject(), oldValue.getSubject()), oldValue.getPredicate(), foundSubjects.getOrDefault(oldValue.getObject(), oldValue.getObject()), oldValue.getValuetype().orElse(null), oldValue.getLanguage().orElse(null), null);
    }
    for (CursorQuad newValue : mutation.getAdditions()) {
        if (newValue.getObject() != null) {
            saver.onQuad(foundSubjects.getOrDefault(newValue.getSubject(), newValue.getSubject()), newValue.getPredicate(), foundSubjects.getOrDefault(newValue.getObject(), newValue.getObject()), newValue.getValuetype().orElse(null), newValue.getLanguage().orElse(null), null);
        }
    }
}
Also used : QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) HashMap(java.util.HashMap) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with Predicate

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate in project timbuctoo by HuygensING.

the class MergeSchemas method mergeSchema.

public Map<String, Type> mergeSchema(Map<String, Type> generatedSchema, Map<String, Type> customSchema) {
    Map<String, Type> mergedSchema = generatedSchema;
    Collection<Predicate> mergedPredicates;
    for (Map.Entry<String, Type> entry : customSchema.entrySet()) {
        mergedPredicates = new HashSet<>();
        if (mergedSchema.get(entry.getKey()) != null) {
            for (Predicate customPredicate : entry.getValue().getPredicates()) {
                if (mergedSchema.get(entry.getKey()).getPredicate(customPredicate.getName(), customPredicate.getDirection()) != null) {
                    Predicate generatedPredicate = mergedSchema.get(entry.getKey()).getPredicate(customPredicate.getName(), customPredicate.getDirection());
                    Predicate mergedPredicate = generatedPredicate.merge(customPredicate);
                    mergedPredicate.setIsExplicit(true);
                    mergedPredicates.add(mergedPredicate);
                } else {
                    mergedPredicates.add(customPredicate);
                }
            }
            mergedSchema.get(entry.getKey()).setPredicates(mergedPredicates);
        } else {
            mergedSchema.put(entry.getKey(), entry.getValue());
        }
    }
    return mergedSchema;
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) Map(java.util.Map) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Aggregations

Predicate (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)16 Test (org.junit.Test)12 RdfProcessingFailedException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)6 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 SAXException (org.xml.sax.SAXException)4 DocumentLoader (com.github.jsonldjava.core.DocumentLoader)3 DatabaseWriteException (nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)3 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)3 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)3 StringValue (graphql.language.StringValue)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ChangeType (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType)2