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))));
}
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"));
}
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();
}
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);
}
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;
}
Aggregations