use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class DegreeTest method testRelationshipPlaysARole.
@Test
public void testRelationshipPlaysARole() throws InvalidKBException {
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);
Role ownership = tx.putRole("ownership");
Role ownershipResource = tx.putRole("ownership-resource");
RelationshipType hasOwnershipResource = tx.putRelationshipType("has-ownership-resource").relates(ownership).relates(ownershipResource);
AttributeType<String> startDate = tx.putAttributeType("start-date", AttributeType.DataType.STRING);
startDate.plays(ownershipResource);
mansBestFriend.plays(ownership);
// add instances
Entity coco = animal.addEntity();
Entity dave = person.addEntity();
Relationship daveOwnsCoco = mansBestFriend.addRelationship().addRolePlayer(owner, dave).addRolePlayer(pet, coco);
Attribute aStartDate = startDate.putAttribute("01/01/01");
hasOwnershipResource.addRelationship().addRolePlayer(ownershipResource, aStartDate).addRolePlayer(ownership, daveOwnsCoco);
Map<Long, Set<String>> referenceDegrees = new HashMap<>();
referenceDegrees.put(1L, Sets.newHashSet(coco.getId().getValue(), dave.getId().getValue(), aStartDate.getId().getValue(), daveOwnsCoco.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.RelationshipType 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.RelationshipType 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.RelationshipType 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.RelationshipType 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);
}
}
Aggregations