Search in sources :

Example 96 with Entity

use of ai.grakn.concept.Entity in project grakn by graknlabs.

the class RelationshipTest method whenAddingDuplicateRelationsWithDifferentKeys_EnsureTheyCanBeCommitted.

@Test
public void whenAddingDuplicateRelationsWithDifferentKeys_EnsureTheyCanBeCommitted() {
    Role role1 = tx.putRole("dark");
    Role role2 = tx.putRole("souls");
    AttributeType<Long> attributeType = tx.putAttributeType("Death Number", AttributeType.DataType.LONG);
    RelationshipType relationshipType = tx.putRelationshipType("Dark Souls").relates(role1).relates(role2).key(attributeType);
    EntityType entityType = tx.putEntityType("Dead Guys").plays(role1).plays(role2);
    Entity e1 = entityType.addEntity();
    Entity e2 = entityType.addEntity();
    Attribute<Long> r1 = attributeType.putAttribute(1000000L);
    Attribute<Long> r2 = attributeType.putAttribute(2000000L);
    Relationship rel1 = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
    Relationship rel2 = relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
    // Set the keys and commit. Without this step it should fail
    rel1.attribute(r1);
    rel2.attribute(r2);
    tx.commit();
    tx = session.open(GraknTxType.WRITE);
    assertThat(tx.admin().getMetaRelationType().instances().collect(toSet()), Matchers.hasItem(rel1));
    assertThat(tx.admin().getMetaRelationType().instances().collect(toSet()), Matchers.hasItem(rel2));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 97 with Entity

use of ai.grakn.concept.Entity 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 98 with Entity

use of ai.grakn.concept.Entity 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)

Example 99 with Entity

use of ai.grakn.concept.Entity in project grakn by graknlabs.

the class RelationshipTest method whenDeletingFinalInstanceOfRelation_RelationIsDeleted.

@Test
public void whenDeletingFinalInstanceOfRelation_RelationIsDeleted() {
    Role roleA = tx.putRole("RoleA");
    Role roleB = tx.putRole("RoleB");
    Role roleC = tx.putRole("RoleC");
    RelationshipType relation = tx.putRelationshipType("relation type").relates(roleA).relates(roleB).relates(roleC);
    EntityType type = tx.putEntityType("concept type").plays(roleA).plays(roleB).plays(roleC);
    Entity a = type.addEntity();
    Entity b = type.addEntity();
    Entity c = type.addEntity();
    ConceptId relationId = relation.addRelationship().addRolePlayer(roleA, a).addRolePlayer(roleB, b).addRolePlayer(roleC, c).getId();
    a.delete();
    assertNotNull(tx.getConcept(relationId));
    b.delete();
    assertNotNull(tx.getConcept(relationId));
    c.delete();
    assertNull(tx.getConcept(relationId));
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) RelationshipType(ai.grakn.concept.RelationshipType) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Aggregations

Entity (ai.grakn.concept.Entity)99 Test (org.junit.Test)81 EntityType (ai.grakn.concept.EntityType)74 Role (ai.grakn.concept.Role)53 RelationshipType (ai.grakn.concept.RelationshipType)50 GraknTx (ai.grakn.GraknTx)44 Attribute (ai.grakn.concept.Attribute)18 Set (java.util.Set)16 Relationship (ai.grakn.concept.Relationship)14 Label (ai.grakn.concept.Label)11 ConceptId (ai.grakn.concept.ConceptId)10 HashSet (java.util.HashSet)10 ArrayList (java.util.ArrayList)8 AttributeType (ai.grakn.concept.AttributeType)7 List (java.util.List)7 GraknSession (ai.grakn.GraknSession)6 Before (org.junit.Before)6 GraknTxType (ai.grakn.GraknTxType)5 Concept (ai.grakn.concept.Concept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5