use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class DegreeTest method testOneRolePlayerMultipleRoles.
@Test
public void testOneRolePlayerMultipleRoles() throws InvalidKBException {
Role pet = tx.putRole("pet");
Role owner = tx.putRole("owner");
Role breeder = tx.putRole("breeder");
RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner).relates(breeder);
EntityType person = tx.putEntityType("person").plays(owner).plays(breeder);
EntityType animal = tx.putEntityType("animal").plays(pet);
// make one person breeder and owner
Entity coco = animal.addEntity();
Entity dave = person.addEntity();
mansBestFriend.addRelationship().addRolePlayer(pet, coco).addRolePlayer(owner, dave).addRolePlayer(breeder, dave);
Map<Long, Set<String>> referenceDegrees = new HashMap<>();
referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue()));
referenceDegrees.put(2L, Collections.singleton(dave.getId().getValue()));
tx.commit();
try (GraknTx graph = session.open(GraknTxType.READ)) {
Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().execute();
assertEquals(referenceDegrees, degrees);
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class DegreeTest method testDegreeTwoAttributes.
@Test
public void testDegreeTwoAttributes() throws InvalidKBException {
// create a simple graph
Role pet = tx.putRole("pet");
Role owner = tx.putRole("owner");
RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner);
EntityType person = tx.putEntityType("person").plays(owner);
EntityType animal = tx.putEntityType("animal").plays(pet);
AttributeType<String> name = tx.putAttributeType("name", AttributeType.DataType.STRING);
AttributeType<String> altName = tx.putAttributeType("alternate-name", AttributeType.DataType.STRING);
animal.attribute(name).attribute(altName);
// add data to the graph
Entity coco = animal.addEntity();
Entity dave = person.addEntity();
Attribute coconut = name.putAttribute("coconut");
Attribute stinky = altName.putAttribute("stinky");
mansBestFriend.addRelationship().addRolePlayer(owner, dave).addRolePlayer(pet, coco);
coco.attribute(coconut).attribute(stinky);
// manually compute the degree for small graph
Map<Long, Set<String>> subgraphReferenceDegrees = new HashMap<>();
subgraphReferenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue()));
// manually compute degree for almost full graph
Map<Long, Set<String>> almostFullReferenceDegrees = new HashMap<>();
almostFullReferenceDegrees.put(2L, Sets.newHashSet(coco.getId().getValue()));
almostFullReferenceDegrees.put(1L, Sets.newHashSet(dave.getId().getValue(), coconut.getId().getValue()));
// manually compute degrees
Map<Long, Set<String>> fullReferenceDegrees = new HashMap<>();
fullReferenceDegrees.put(3L, Sets.newHashSet(coco.getId().getValue()));
fullReferenceDegrees.put(1L, Sets.newHashSet(dave.getId().getValue(), coconut.getId().getValue(), stinky.getId().getValue()));
tx.commit();
try (GraknTx graph = session.open(GraknTxType.READ)) {
// create a subgraph excluding attributes and their relationship
HashSet<Label> subGraphTypes = Sets.newHashSet(Label.of("animal"), Label.of("person"), Label.of("mans-best-friend"));
Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().in(subGraphTypes).execute();
assertEquals(subgraphReferenceDegrees, degrees);
// create a subgraph excluding one attribute type only
HashSet<Label> almostFullTypes = Sets.newHashSet(Label.of("animal"), Label.of("person"), Label.of("mans-best-friend"), Label.of("@has-name"), Label.of("name"));
degrees = graph.graql().compute().centrality().usingDegree().in(almostFullTypes).execute();
assertEquals(almostFullReferenceDegrees, degrees);
// full graph
degrees = graph.graql().compute().centrality().usingDegree().of().execute();
assertEquals(fullReferenceDegrees, degrees);
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class DegreeTest method testDegreeMissingRolePlayer.
@Test
public void testDegreeMissingRolePlayer() {
Role pet = tx.putRole("pet");
Role owner = tx.putRole("owner");
Role breeder = tx.putRole("breeder");
RelationshipType mansBestFriend = tx.putRelationshipType("mans-best-friend").relates(pet).relates(owner).relates(breeder);
EntityType person = tx.putEntityType("person").plays(owner).plays(breeder);
EntityType animal = tx.putEntityType("animal").plays(pet);
// make one person breeder and owner
Entity coco = animal.addEntity();
Entity dave = person.addEntity();
mansBestFriend.addRelationship().addRolePlayer(pet, coco).addRolePlayer(owner, dave);
// manual degrees
Map<Long, Set<String>> referenceDegrees = new HashMap<>();
referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue()));
tx.commit();
try (GraknTx graph = session.open(GraknTxType.READ)) {
Map<Long, Set<String>> degrees = graph.graql().compute().centrality().usingDegree().execute();
assertEquals(referenceDegrees, degrees);
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class KCoreTest method testOnGraphWithTwoEntitiesAndTwoRelationships.
@Test
public void testOnGraphWithTwoEntitiesAndTwoRelationships() {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType = graph.putEntityType(thing);
Entity entity1 = entityType.addEntity();
Entity entity2 = entityType.addEntity();
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType.plays(role1).plays(role2);
graph.putRelationshipType(related).relates(role1).relates(role2).addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
entityType.plays(role3).plays(role4);
graph.putRelationshipType(veryRelated).relates(role3).relates(role4).addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity2);
Map<String, Set<String>> result = graph.graql().compute().cluster().usingKCore().kValue(2L).execute();
assertTrue(result.isEmpty());
}
}
use of ai.grakn.concept.EntityType in project grakn by graknlabs.
the class KCoreTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = graph.putEntityType(thing);
EntityType entityType2 = graph.putEntityType(anotherThing);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
entityType1.plays(role1).plays(role2).plays(role3).plays(role4);
entityType2.plays(role1).plays(role2).plays(role3).plays(role4);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType1.addEntity();
relationshipType1.addRelationship().addRolePlayer(role1, entity1).addRolePlayer(role2, entity2);
relationshipType1.addRelationship().addRolePlayer(role1, entity2).addRolePlayer(role2, entity3);
relationshipType1.addRelationship().addRolePlayer(role1, entity3).addRolePlayer(role2, entity4);
relationshipType1.addRelationship().addRolePlayer(role1, entity4).addRolePlayer(role2, entity1);
relationshipType2.addRelationship().addRolePlayer(role3, entity1).addRolePlayer(role4, entity3);
relationshipType2.addRelationship().addRolePlayer(role3, entity2).addRolePlayer(role4, entity4);
entityId1 = entity1.getId();
entityId2 = entity2.getId();
entityId3 = entity3.getId();
entityId4 = entity4.getId();
graph.commit();
}
}
Aggregations