Search in sources :

Example 31 with Entity

use of nl.knaw.huygens.timbuctoo.rdf.Entity in project timbuctoo by HuygensING.

the class PersonNameVariantTripleProcessor method processAssertion.

@Override
protected void processAssertion(String vreName, String subject, String predicate, String object) {
    final Optional<Entity> subjectEntity = database.findEntity(vreName, subject);
    final Optional<Entity> objectEntity = database.findEntity(vreName, object);
    if (!subjectEntity.isPresent()) {
        LOG.error("Entity with rdf uri '{}' not found", subject);
        return;
    }
    if (!objectEntity.isPresent()) {
        LOG.error("Entity with rdf uri '{}' not found", object);
    }
    final Optional<String> subjectRawNames = subjectEntity.get().getPropertyValue(NAMES_PROPERTY_NAME);
    if (subjectRawNames.isPresent()) {
        final Optional<String> objectRawNames = objectEntity.get().getPropertyValue(NAMES_PROPERTY_NAME);
        if (objectRawNames.isPresent()) {
            try {
                final UriBearingPersonNames mergedNames = mergeNames(subjectRawNames.get(), objectRawNames.get());
                objectEntity.get().addProperty(NAMES_PROPERTY_NAME, objectMapper.writeValueAsString(mergedNames), NAMES_TYPE_ID);
            } catch (IOException e) {
                LOG.error("Failed to read/write personNames json", e);
                return;
            }
        } else {
            objectEntity.get().addProperty(NAMES_PROPERTY_NAME, subjectRawNames.get(), NAMES_TYPE_ID);
        }
    }
    database.purgeEntity(vreName, subjectEntity.get());
}
Also used : Entity(nl.knaw.huygens.timbuctoo.rdf.Entity) UriBearingPersonNames(nl.knaw.huygens.timbuctoo.rdf.UriBearingPersonNames) IOException(java.io.IOException)

Example 32 with Entity

use of nl.knaw.huygens.timbuctoo.rdf.Entity in project timbuctoo by HuygensING.

the class PersonNamesTripleProcessor method processAssertion.

@Override
protected void processAssertion(String vreName, String subject, String predicate, String lexicalValue, String typeUri) {
    LOG.debug("Process PersonNames triple for subject '{}' with value '{}'", subject, lexicalValue);
    Entity entity = database.findOrCreateEntity(vreName, subject);
    String propertyName = getLocalName(predicate);
    Optional<String> propertyValue = entity.getPropertyValue(propertyName);
    try {
        PersonName personName = objectMapper.readValue(lexicalValue, PersonName.class);
        PersonNames personNames = getPersonNames(objectMapper, propertyValue);
        personNames.list.add(personName);
        String names = objectMapper.writeValueAsString(personNames);
        // Because the person names is wrapped in a person names type, the type changes
        entity.addProperty(propertyName, names, NAMES_TYPE_ID);
    } catch (IOException e) {
        LOG.error("Could not convert '{}' to PersonName", lexicalValue);
    }
}
Also used : PersonNames(nl.knaw.huygens.timbuctoo.model.PersonNames) Entity(nl.knaw.huygens.timbuctoo.rdf.Entity) PersonName(nl.knaw.huygens.timbuctoo.model.PersonName) IOException(java.io.IOException)

Example 33 with Entity

use of nl.knaw.huygens.timbuctoo.rdf.Entity in project timbuctoo by HuygensING.

the class RelationTripleProcessor method processRetraction.

@Override
protected void processRetraction(String vreName, String subject, String predicate, String object) {
    final Entity subjectEntity = database.findOrCreateEntity(vreName, subject);
    final RelationType relationType = database.findOrCreateRelationType(predicate, getLocalName(predicate));
    final Entity objectEntity = database.findOrCreateEntity(vreName, object);
    subjectEntity.removeRelation(relationType, objectEntity);
}
Also used : Entity(nl.knaw.huygens.timbuctoo.rdf.Entity) RelationType(nl.knaw.huygens.timbuctoo.rdf.RelationType)

Example 34 with Entity

use of nl.knaw.huygens.timbuctoo.rdf.Entity in project timbuctoo by HuygensING.

the class RelationTripleProcessor method processAssertion.

@Override
protected void processAssertion(String vreName, String subject, String predicate, String object) {
    final Entity subjectEntity = database.findOrCreateEntity(vreName, subject);
    final RelationType relationType = database.findOrCreateRelationType(predicate, getLocalName(predicate));
    final Entity objectEntity = database.findOrCreateEntity(vreName, object);
    if (relationType.isInverted()) {
        final Relation relation = objectEntity.addRelation(relationType, subjectEntity);
        relation.setCommonVreProperties(vreName);
    } else {
        final Relation relation = subjectEntity.addRelation(relationType, objectEntity);
        relation.setCommonVreProperties(vreName);
    }
}
Also used : Entity(nl.knaw.huygens.timbuctoo.rdf.Entity) Relation(nl.knaw.huygens.timbuctoo.rdf.Relation) RelationType(nl.knaw.huygens.timbuctoo.rdf.RelationType)

Example 35 with Entity

use of nl.knaw.huygens.timbuctoo.rdf.Entity in project timbuctoo by HuygensING.

the class SameAsTripleProcessor method processAssertion.

@Override
protected void processAssertion(String vreName, String subject, String predicate, String object) {
    if (StringUtils.isBlank(object)) {
        return;
    }
    Optional<Entity> objectEntityOpt = database.findEntity(vreName, object);
    Optional<Entity> subjectEntityOpt = database.findEntity(vreName, subject);
    if (objectEntityOpt.isPresent() && subjectEntityOpt.isPresent()) {
        final Entity objectEntity = objectEntityOpt.get();
        final Entity subjectEntity = subjectEntityOpt.get();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Merging object entity into subject entity: {} <-- {}", subjectEntity.getProperties(), objectEntity.getProperties());
        }
        // First make sure all the edges are copied (especially edges pointing to collections only the object is part of)
        database.copyEdgesFromObjectIntoSubject(subjectEntity, objectEntity);
        // Reload subject entity with any new collection added to it from the object (vertex must still be present here)
        final Entity reloadedSubjectEntity = database.findEntity(vreName, subject).get();
        // Merge the properties of the object entity into the reloaded subject entity via Entity model
        mergeEntityProperties(reloadedSubjectEntity, objectEntity);
        if (LOG.isDebugEnabled()) {
            final Entity finalSubject = database.findEntity(vreName, subject).get();
            LOG.debug("Final subject properties: {}", finalSubject.getProperties());
        }
        // purge the object entity from the database and index
        database.purgeEntity(vreName, objectEntity);
        // add the object uri as a synonym to the subject entity
        database.addRdfSynonym(vreName, subjectEntity, object);
    } else if (objectEntityOpt.isPresent()) {
        database.addRdfSynonym(vreName, objectEntityOpt.get(), subject);
    } else if (subjectEntityOpt.isPresent()) {
        database.addRdfSynonym(vreName, subjectEntityOpt.get(), object);
    } else {
        Entity entity = database.findOrCreateEntity(vreName, object);
        database.addRdfSynonym(vreName, entity, subject);
    }
}
Also used : Entity(nl.knaw.huygens.timbuctoo.rdf.Entity)

Aggregations

Entity (nl.knaw.huygens.timbuctoo.rdf.Entity)35 Database (nl.knaw.huygens.timbuctoo.rdf.Database)23 Test (org.junit.Test)22 IOException (java.io.IOException)19 CreateEntity (nl.knaw.huygens.timbuctoo.core.dto.CreateEntity)18 ReadEntity (nl.knaw.huygens.timbuctoo.core.dto.ReadEntity)18 UpdateEntity (nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)17 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)17 Lists (com.google.common.collect.Lists)17 URI (java.net.URI)17 Instant (java.time.Instant)17 ArrayList (java.util.ArrayList)17 List (java.util.List)17 Optional (java.util.Optional)17 UUID (java.util.UUID)17 Collectors.toList (java.util.stream.Collectors.toList)17 Try (javaslang.control.Try)17 OptionalPresentMatcher.present (nl.knaw.huygens.hamcrest.OptionalPresentMatcher.present)17 AlreadyUpdatedException (nl.knaw.huygens.timbuctoo.core.AlreadyUpdatedException)17