Search in sources :

Example 1 with AttributeTypeImpl

use of ai.grakn.kb.internal.concept.AttributeTypeImpl in project grakn by graknlabs.

the class PostProcessingTest method whenMergingDuplicateResourceEdges_EnsureNoDuplicatesRemain.

@Test
public void whenMergingDuplicateResourceEdges_EnsureNoDuplicatesRemain() {
    AttributeTypeImpl<String> resourceType = (AttributeTypeImpl<String>) tx.putAttributeType("My Sad Attribute", AttributeType.DataType.STRING);
    EntityType entityType = tx.putEntityType("My Happy EntityType").attribute(resourceType);
    RelationshipType relationshipType = tx.putRelationshipType("My Miserable RelationshipType").attribute(resourceType);
    Entity entity = entityType.addEntity();
    Relationship relationship = relationshipType.addRelationship();
    AttributeImpl<?> r1dup1 = createFakeResource(resourceType, "1");
    AttributeImpl<?> r1dup2 = createFakeResource(resourceType, "1");
    AttributeImpl<?> r1dup3 = createFakeResource(resourceType, "1");
    AttributeImpl<?> r2dup1 = createFakeResource(resourceType, "2");
    AttributeImpl<?> r2dup2 = createFakeResource(resourceType, "2");
    AttributeImpl<?> r2dup3 = createFakeResource(resourceType, "2");
    entity.attribute(r1dup1);
    entity.attribute(r1dup2);
    entity.attribute(r1dup3);
    relationship.attribute(r1dup1);
    relationship.attribute(r1dup2);
    relationship.attribute(r1dup3);
    entity.attribute(r2dup1);
    // Check everything is broken
    // Entities Too Many Resources
    assertEquals(4, entity.attributes().count());
    assertEquals(3, relationship.attributes().count());
    // There are too many resources
    assertEquals(6, tx.admin().getMetaAttributeType().instances().count());
    // Now fix everything for resource 1
    tx.fixDuplicateResources(r1dup1.getIndex(), new HashSet<>(Arrays.asList(r1dup1.getId(), r1dup2.getId(), r1dup3.getId())));
    // Check resource one has been sorted out
    assertEquals(2, entity.attributes().count());
    assertEquals(2, entity.attributes().count());
    // 4 because we still have 2 dups on r2
    assertEquals(4, tx.admin().getMetaAttributeType().instances().count());
    // Now fix everything for resource 2
    tx.fixDuplicateResources(r2dup1.getIndex(), new HashSet<>(Arrays.asList(r2dup1.getId(), r2dup2.getId(), r2dup3.getId())));
    // Check resource one has been sorted out
    assertEquals(2, entity.attributes().count());
    assertEquals(2, entity.attributes().count());
    // 4 because we still have 2 dups on r2
    assertEquals(2, tx.admin().getMetaAttributeType().instances().count());
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) AttributeTypeImpl(ai.grakn.kb.internal.concept.AttributeTypeImpl) Test(org.junit.Test)

Example 2 with AttributeTypeImpl

use of ai.grakn.kb.internal.concept.AttributeTypeImpl in project grakn by graknlabs.

the class PostProcessingTest method whenMergingDuplicateResourcesWithRelations_EnsureSingleResourceRemainsAndNoDuplicateRelationsAreCreated.

@Test
public void whenMergingDuplicateResourcesWithRelations_EnsureSingleResourceRemainsAndNoDuplicateRelationsAreCreated() {
    Role roleEntity = tx.putRole("Entity Role");
    Role roleResource = tx.putRole("Attribute Role");
    RelationshipType relationshipType = tx.putRelationshipType("Relationship Type").relates(roleEntity).relates(roleResource);
    AttributeTypeImpl<String> resourceType = (AttributeTypeImpl<String>) tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING).plays(roleResource);
    EntityType entityType = tx.putEntityType("Entity Type").plays(roleEntity).attribute(resourceType);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Entity e3 = entityType.addEntity();
    // Create fake resources
    Set<ConceptId> resourceIds = new HashSet<>();
    AttributeImpl<?> r1 = createFakeResource(resourceType, "1");
    AttributeImpl<?> r11 = createFakeResource(resourceType, "1");
    AttributeImpl<?> r111 = createFakeResource(resourceType, "1");
    resourceIds.add(r1.getId());
    resourceIds.add(r11.getId());
    resourceIds.add(r111.getId());
    // Give resources some relationships
    addReifiedRelation(roleEntity, roleResource, relationshipType, e1, r1);
    // When merging this relation should not be absorbed
    addReifiedRelation(roleEntity, roleResource, relationshipType, e1, r11);
    // Absorb
    addReifiedRelation(roleEntity, roleResource, relationshipType, e2, r11);
    // Don't Absorb
    addEdgeRelation(e2, r111);
    // Absorb
    addEdgeRelation(e3, r111);
    // Check everything is broken
    assertEquals(3, resourceType.instances().count());
    assertEquals(1, r1.relationships().count());
    assertEquals(2, r11.relationships().count());
    assertEquals(1, r1.relationships().count());
    assertEquals(4, tx.getTinkerTraversal().V().hasLabel(Schema.BaseType.RELATIONSHIP.name()).toList().size());
    assertEquals(2, tx.getTinkerTraversal().E().hasLabel(Schema.EdgeLabel.ATTRIBUTE.getLabel()).toList().size());
    r1.relationships().forEach(rel -> assertTrue(rel.rolePlayers().anyMatch(thing -> thing.equals(e1))));
    // Now fix everything
    tx.fixDuplicateResources(r1.getIndex(), resourceIds);
    // Check everything is in order
    assertEquals(1, resourceType.instances().count());
    // Get back the surviving resource
    Attribute<String> foundR1 = null;
    for (Attribute<String> attribute : resourceType.instances().collect(toSet())) {
        if (attribute.getValue().equals("1")) {
            foundR1 = attribute;
            break;
        }
    }
    assertNotNull(foundR1);
    assertThat(foundR1.ownerInstances().collect(toSet()), containsInAnyOrder(e1, e2, e3));
    assertEquals(6, tx.admin().getMetaRelationType().instances().count());
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) AttributeTypeImpl(ai.grakn.kb.internal.concept.AttributeTypeImpl) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Entity (ai.grakn.concept.Entity)2 EntityType (ai.grakn.concept.EntityType)2 RelationshipType (ai.grakn.concept.RelationshipType)2 AttributeTypeImpl (ai.grakn.kb.internal.concept.AttributeTypeImpl)2 Test (org.junit.Test)2 ConceptId (ai.grakn.concept.ConceptId)1 Relationship (ai.grakn.concept.Relationship)1 Role (ai.grakn.concept.Role)1 HashSet (java.util.HashSet)1