use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class TailRecursionKB method buildExtensionalDB.
private void buildExtensionalDB(GraknTx graph, int n, int m) {
Role qfrom = graph.getRole("Q-from");
Role qto = graph.getRole("Q-to");
EntityType aEntity = graph.getEntityType("a-entity");
EntityType bEntity = graph.getEntityType("b-entity");
RelationshipType q = graph.getRelationshipType("Q");
putEntityWithResource(graph, "a0", aEntity, key);
for (int i = 1; i <= m + 1; i++) {
for (int j = 1; j <= n; j++) {
putEntityWithResource(graph, "b" + i + "," + j, bEntity, key);
}
}
for (int j = 1; j <= n; j++) {
q.addRelationship().addRolePlayer(qfrom, getInstance(graph, "a0")).addRolePlayer(qto, getInstance(graph, "b1" + "," + j));
for (int i = 1; i <= m; i++) {
q.addRelationship().addRolePlayer(qfrom, getInstance(graph, "b" + i + "," + j)).addRolePlayer(qto, getInstance(graph, "b" + (i + 1) + "," + j));
}
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class TxCacheTest method whenClosingTransaction_EnsureTransactionCacheIsEmpty.
@Test
public void whenClosingTransaction_EnsureTransactionCacheIsEmpty() {
TxCache cache = tx.txCache();
// Load some sample data
AttributeType<String> attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
Role role1 = tx.putRole("role 1");
Role role2 = tx.putRole("role 2");
EntityType entityType = tx.putEntityType("My Type").plays(role1).plays(role2).attribute(attributeType);
RelationshipType relationshipType = tx.putRelationshipType("My Relationship Type").relates(role1).relates(role2);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
Attribute<String> r1 = attributeType.putAttribute("test");
e1.attribute(r1);
relationshipType.addRelationship().addRolePlayer(role1, e1).addRolePlayer(role2, e2);
// Check the caches are not empty
assertThat(cache.getConceptCache().keySet(), not(empty()));
assertThat(cache.getSchemaConceptCache().keySet(), not(empty()));
assertThat(cache.getLabelCache().keySet(), not(empty()));
assertThat(cache.getShardingCount().keySet(), not(empty()));
assertThat(cache.getModifiedCastings(), not(empty()));
// Close the transaction
tx.commit();
// Check the caches are empty
assertThat(cache.getConceptCache().keySet(), empty());
assertThat(cache.getSchemaConceptCache().keySet(), empty());
assertThat(cache.getLabelCache().keySet(), empty());
assertThat(cache.getShardingCount().keySet(), empty());
assertThat(cache.getModifiedThings(), empty());
assertThat(cache.getModifiedRoles(), empty());
assertThat(cache.getModifiedRelationshipTypes(), empty());
assertThat(cache.getModifiedRules(), empty());
assertThat(cache.getModifiedCastings(), empty());
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class RelationshipAtom method materialise.
@Override
public Stream<Answer> materialise() {
RelationshipType relationType = getSchemaConcept().asRelationshipType();
Multimap<Role, Var> roleVarMap = getRoleVarMap();
Answer substitution = getParentQuery().getSubstitution();
Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
return Stream.of(substitution.merge(relationSub));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class StatisticsTest method addSchemaAndEntities.
private void addSchemaAndEntities() throws InvalidKBException {
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
EntityType entityType1 = tx.putEntityType(thing);
EntityType entityType2 = tx.putEntityType(anotherThing);
Entity entity1 = entityType1.addEntity();
Entity entity2 = entityType1.addEntity();
Entity entity3 = entityType1.addEntity();
Entity entity4 = entityType2.addEntity();
entityId1 = entity1.getId();
entityId2 = entity2.getId();
entityId3 = entity3.getId();
entityId4 = entity4.getId();
Role relation1 = tx.putRole("relation1");
Role relation2 = tx.putRole("relation2");
entityType1.plays(relation1).plays(relation2);
entityType2.plays(relation1).plays(relation2);
RelationshipType related = tx.putRelationshipType("related").relates(relation1).relates(relation2);
related.addRelationship().addRolePlayer(relation1, entity1).addRolePlayer(relation2, entity2);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity3);
related.addRelationship().addRolePlayer(relation1, entity2).addRolePlayer(relation2, entity4);
AttributeType<Double> attribute1 = tx.putAttributeType(resourceType1, AttributeType.DataType.DOUBLE);
AttributeType<Long> attribute2 = tx.putAttributeType(resourceType2, AttributeType.DataType.LONG);
AttributeType<Long> attribute3 = tx.putAttributeType(resourceType3, AttributeType.DataType.LONG);
AttributeType<String> attribute4 = tx.putAttributeType(resourceType4, AttributeType.DataType.STRING);
AttributeType<Long> attribute5 = tx.putAttributeType(resourceType5, AttributeType.DataType.LONG);
AttributeType<Double> attribute6 = tx.putAttributeType(resourceType6, AttributeType.DataType.DOUBLE);
AttributeType<Double> attribute7 = tx.putAttributeType(resourceType7, AttributeType.DataType.DOUBLE);
entityType1.attribute(attribute1);
entityType1.attribute(attribute2);
entityType1.attribute(attribute3);
entityType1.attribute(attribute4);
entityType1.attribute(attribute5);
entityType1.attribute(attribute6);
entityType1.attribute(attribute7);
entityType2.attribute(attribute1);
entityType2.attribute(attribute2);
entityType2.attribute(attribute3);
entityType2.attribute(attribute4);
entityType2.attribute(attribute5);
entityType2.attribute(attribute6);
entityType2.attribute(attribute7);
tx.commit();
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class StatisticsTest method addResourceRelations.
private void addResourceRelations() throws InvalidKBException {
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
Entity entity1 = graph.getConcept(entityId1);
Entity entity2 = graph.getConcept(entityId2);
Entity entity3 = graph.getConcept(entityId3);
Entity entity4 = graph.getConcept(entityId4);
Role resourceOwner1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType1)));
Role resourceOwner2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType2)));
Role resourceOwner3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType3)));
Role resourceOwner4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType4)));
Role resourceOwner5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType5)));
Role resourceOwner6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(Label.of(resourceType6)));
Role resourceValue1 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType1)));
Role resourceValue2 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType2)));
Role resourceValue3 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType3)));
Role resourceValue4 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType4)));
Role resourceValue5 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType5)));
Role resourceValue6 = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(Label.of(resourceType6)));
RelationshipType relationshipType1 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType1)));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.2));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity1).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.5));
relationshipType1.addRelationship().addRolePlayer(resourceOwner1, entity3).addRolePlayer(resourceValue1, graph.<Double>getAttributeType(resourceType1).putAttribute(1.8));
RelationshipType relationshipType2 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType2)));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(4L));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity1).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(-1L));
relationshipType2.addRelationship().addRolePlayer(resourceOwner2, entity4).addRolePlayer(resourceValue2, graph.<Long>getAttributeType(resourceType2).putAttribute(0L));
graph.<Long>getAttributeType(resourceType3).putAttribute(100L);
RelationshipType relationshipType5 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType5)));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity1).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity2).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
relationshipType5.addRelationship().addRolePlayer(resourceOwner5, entity4).addRolePlayer(resourceValue5, graph.<Long>getAttributeType(resourceType5).putAttribute(-7L));
RelationshipType relationshipType6 = graph.getSchemaConcept(Schema.ImplicitType.HAS.getLabel(Label.of(resourceType6)));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity1).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity2).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
relationshipType6.addRelationship().addRolePlayer(resourceOwner6, entity4).addRolePlayer(resourceValue6, graph.<Double>getAttributeType(resourceType6).putAttribute(7.5));
// some resources in, but not connect them to any instances
graph.<Double>getAttributeType(resourceType1).putAttribute(2.8);
graph.<Long>getAttributeType(resourceType2).putAttribute(-5L);
graph.<Long>getAttributeType(resourceType5).putAttribute(10L);
graph.<Double>getAttributeType(resourceType6).putAttribute(0.8);
graph.commit();
}
}
Aggregations