use of ai.grakn.concept.Entity in project grakn by graknlabs.
the class DegreeTest method testDegreeTernaryRelationships.
@Test
public void testDegreeTernaryRelationships() throws InvalidKBException {
// make relationship
Role productionWithCast = tx.putRole("production-with-cast");
Role actor = tx.putRole("actor");
Role characterBeingPlayed = tx.putRole("character-being-played");
RelationshipType hasCast = tx.putRelationshipType("has-cast").relates(productionWithCast).relates(actor).relates(characterBeingPlayed);
EntityType movie = tx.putEntityType("movie").plays(productionWithCast);
EntityType person = tx.putEntityType("person").plays(actor);
EntityType character = tx.putEntityType("character").plays(characterBeingPlayed);
Entity godfather = movie.addEntity();
Entity marlonBrando = person.addEntity();
Entity donVitoCorleone = character.addEntity();
hasCast.addRelationship().addRolePlayer(productionWithCast, godfather).addRolePlayer(actor, marlonBrando).addRolePlayer(characterBeingPlayed, donVitoCorleone);
Map<Long, Set<String>> referenceDegrees = new HashMap<>();
referenceDegrees.put(1L, Sets.newHashSet(godfather.getId().getValue(), marlonBrando.getId().getValue(), donVitoCorleone.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.Entity 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.Entity 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.Entity 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.Entity 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());
}
}
Aggregations