Search in sources :

Example 36 with Thing

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

the class MigratorTestUtils method assertRelationBetweenInstancesExists.

public static void assertRelationBetweenInstancesExists(GraknTx graph, Thing thing1, Thing thing2, Label relation) {
    RelationshipType relationshipType = graph.getSchemaConcept(relation);
    Role role1 = thing1.plays().filter(r -> r.relationshipTypes().anyMatch(rel -> rel.equals(relationshipType))).findFirst().get();
    assertTrue(thing1.relationships(role1).anyMatch(rel -> rel.rolePlayers().anyMatch(r -> r.equals(thing2))));
}
Also used : Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) RelationshipType(ai.grakn.concept.RelationshipType)

Example 37 with Thing

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

the class ValidatorTest method whenDeletingRelations_EnsureGraphRemainsValid.

@Test
public void whenDeletingRelations_EnsureGraphRemainsValid() throws InvalidKBException {
    // schema
    EntityType person = tx.putEntityType("person");
    EntityType movie = tx.putEntityType("movie");
    RelationshipType cast = tx.putRelationshipType("cast");
    Role feature = tx.putRole("feature");
    Role actor = tx.putRole("actor");
    cast.relates(feature).relates(actor);
    person.plays(actor);
    movie.plays(feature);
    // add a single movie
    Thing godfather = movie.addEntity();
    // add many random actors
    int n = 100;
    for (int i = 0; i < n; i++) {
        Thing newPerson = person.addEntity();
        cast.addRelationship().addRolePlayer(actor, newPerson).addRolePlayer(feature, godfather);
    }
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    // now try to delete all assertions and then the movie
    godfather = tx.getEntityType("movie").instances().iterator().next();
    Collection<Relationship> assertions = godfather.relationships().collect(Collectors.toSet());
    Set<ConceptId> assertionIds = new HashSet<>();
    for (Relationship a : assertions) {
        assertionIds.add(a.getId());
        a.delete();
    }
    godfather.delete();
    tx.commit();
    tx = EmbeddedGraknSession.create(tx.keyspace(), Grakn.IN_MEMORY).open(GraknTxType.WRITE);
    assertionIds.forEach(id -> assertNull(tx.getConcept(id)));
    // assert the movie is gone
    assertNull(tx.getEntityType("godfather"));
}
Also used : EntityType(ai.grakn.concept.EntityType) Role(ai.grakn.concept.Role) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) ConceptId(ai.grakn.concept.ConceptId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 38 with Thing

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

the class ValidatorTest method whenARoleInARelationIsPlayedTwice_TheGraphIsValid.

@Test
public void whenARoleInARelationIsPlayedTwice_TheGraphIsValid() {
    Role role1 = tx.putRole("role-1");
    Role role2 = tx.putRole("role-2");
    RelationshipType relationshipType = tx.putRelationshipType("my-relationship").relates(role1).relates(role2);
    EntityType entityType = tx.putEntityType("my-entity").plays(role1);
    Thing thing1 = entityType.addEntity();
    Thing thing2 = entityType.addEntity();
    Relationship relationship = relationshipType.addRelationship();
    relationship.addRolePlayer(role1, thing1);
    relationship.addRolePlayer(role1, thing2);
    assertThat(relationship.rolePlayers(role1).collect(toSet()), hasItems(thing1, thing2));
    tx.commit();
}
Also used : Role(ai.grakn.concept.Role) EntityType(ai.grakn.concept.EntityType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 39 with Thing

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

the class RelationshipProperty method addRoleplayer.

/**
 * Add a roleplayer to the given {@link Relationship}
 * @param relationship the concept representing the {@link Relationship}
 * @param relationPlayer a casting between a role type and role player
 */
private void addRoleplayer(QueryOperationExecutor executor, Relationship relationship, RelationPlayer relationPlayer) {
    VarPatternAdmin roleVar = getRole(relationPlayer);
    Role role = executor.get(roleVar.var()).asRole();
    Thing roleplayer = executor.get(relationPlayer.getRolePlayer().var()).asThing();
    relationship.addRolePlayer(role, roleplayer);
}
Also used : Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Thing(ai.grakn.concept.Thing)

Example 40 with Thing

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

the class GraknKeyspaceStoreImpl method deleteKeyspace.

@Override
public boolean deleteKeyspace(Keyspace keyspace) {
    if (keyspace.equals(SYSTEM_KB_KEYSPACE)) {
        return false;
    }
    try (EmbeddedGraknTx<?> tx = session.tx(GraknTxType.WRITE)) {
        AttributeType<String> keyspaceName = tx.getSchemaConcept(KEYSPACE_RESOURCE);
        Attribute<String> attribute = keyspaceName.getAttribute(keyspace.getValue());
        if (attribute == null)
            return false;
        Thing thing = attribute.owner();
        if (thing != null)
            thing.delete();
        attribute.delete();
        existingKeyspaces.remove(keyspace);
        tx.commitSubmitNoLogs();
    }
    return true;
}
Also used : Thing(ai.grakn.concept.Thing)

Aggregations

Thing (ai.grakn.concept.Thing)47 Role (ai.grakn.concept.Role)30 Test (org.junit.Test)29 RelationshipType (ai.grakn.concept.RelationshipType)17 EntityType (ai.grakn.concept.EntityType)14 Relationship (ai.grakn.concept.Relationship)12 HashSet (java.util.HashSet)9 Set (java.util.Set)9 GraknTx (ai.grakn.GraknTx)7 Attribute (ai.grakn.concept.Attribute)7 ConceptId (ai.grakn.concept.ConceptId)7 AttributeType (ai.grakn.concept.AttributeType)6 Concept (ai.grakn.concept.Concept)6 Entity (ai.grakn.concept.Entity)6 Collectors (java.util.stream.Collectors)5 Label (ai.grakn.concept.Label)4 Schema (ai.grakn.util.Schema)4 Collection (java.util.Collection)4 Map (java.util.Map)4 GraknSession (ai.grakn.GraknSession)3