use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingRelationshipTypes_GetTheExpectedResult.
@Test
public void whenCallingRelationshipTypes_GetTheExpectedResult() {
RelationshipType a = RemoteConcepts.createRelationshipType(tx, A);
RelationshipType b = RemoteConcepts.createRelationshipType(tx, B);
RelationshipType c = RemoteConcepts.createRelationshipType(tx, C);
mockConceptMethod(ConceptMethods.GET_RELATIONSHIP_TYPES_THAT_RELATE_ROLE, Stream.of(a, b, c));
assertThat(role.relationshipTypes().collect(toSet()), containsInAnyOrder(a, b, c));
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class ThingImpl method attributeRelationship.
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) {
Schema.ImplicitType has = Schema.ImplicitType.HAS;
Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE;
Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER;
// Is this attribute a key to me?
if (type().keys().anyMatch(rt -> rt.equals(attribute.type()))) {
has = Schema.ImplicitType.KEY;
hasValue = Schema.ImplicitType.KEY_VALUE;
hasOwner = Schema.ImplicitType.KEY_OWNER;
}
Label label = attribute.type().getLabel();
RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label));
Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label));
Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label));
if (hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().plays().noneMatch(play -> play.equals(hasAttributeOwner))) {
throw GraknTxOperationException.hasNotAllowed(this, attribute);
}
EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE);
if (isInferred)
attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true);
return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue);
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class BenchmarkIT method loadRandomisedRelationInstances.
private void loadRandomisedRelationInstances(String entityLabel, String fromRoleLabel, String toRoleLabel, String relationLabel, int N, GraknSession session, GraknClient graknClient, Keyspace keyspace) {
try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
GraknTx tx = session.open(GraknTxType.READ);
Var entityVar = var().asUserDefined();
ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
assertEquals(instances.length, N);
Role fromRole = tx.getRole(fromRoleLabel);
Role toRole = tx.getRole(toRoleLabel);
RelationshipType relationType = tx.getRelationshipType(relationLabel);
Random rand = new Random();
Multimap<Integer, Integer> assignmentMap = HashMultimap.create();
for (int i = 0; i < N; i++) {
int from = rand.nextInt(N - 1);
int to = rand.nextInt(N - 1);
while (to == from && assignmentMap.get(from).contains(to)) to = rand.nextInt(N - 1);
Var fromRolePlayer = Graql.var();
Var toRolePlayer = Graql.var();
Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(relationType.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[from])).and(toRolePlayer.asUserDefined().id(instances[to]));
loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
}
tx.close();
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class BenchmarkIT method loadRuleChainData.
private void loadRuleChainData(int N) {
final GraknClient graknClient = GraknClient.of(engine.uri());
String entityLabel = "genericEntity";
String attributeLabel = "index";
String baseRelationLabel = "relation1";
String genericRelationLabel = "relation";
String fromRoleLabel = "fromRole";
String toRoleLabel = "toRole";
// load ontology
try (GraknTx tx = session.open(GraknTxType.WRITE)) {
Role fromRole = tx.putRole(fromRoleLabel);
Role toRole = tx.putRole(toRoleLabel);
AttributeType<String> index = tx.putAttributeType(attributeLabel, AttributeType.DataType.STRING);
tx.putEntityType(entityLabel).plays(fromRole).plays(toRole).attribute(index);
// define N relation types
for (int i = 1; i <= N; i++) {
tx.putRelationshipType(genericRelationLabel + i).relates(fromRole).relates(toRole);
}
// define N rules
for (int i = 2; i <= N; i++) {
Var fromVar = Graql.var().asUserDefined();
Var intermedVar = Graql.var().asUserDefined();
Var toVar = Graql.var().asUserDefined();
VarPattern rulePattern = Graql.label("rule" + i).when(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), intermedVar).isa(baseRelationLabel), Graql.var().rel(Graql.label(fromRole.getLabel()), intermedVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa(genericRelationLabel + i)));
tx.graql().define(rulePattern).execute();
}
tx.commit();
}
// insert N + 1 entities
loadEntities(entityLabel, N + 1, graknClient, keyspace);
// load initial relation instances
try (BatchExecutorClient loader = BatchExecutorClient.newBuilder().taskClient(graknClient).build()) {
try (GraknTx tx = session.open(GraknTxType.READ)) {
Var entityVar = var().asUserDefined();
ConceptId[] instances = tx.graql().match(entityVar.isa(entityLabel)).get().execute().stream().map(ans -> ans.get(entityVar).getId()).toArray(ConceptId[]::new);
RelationshipType baseRelation = tx.getRelationshipType(baseRelationLabel);
Role fromRole = tx.getRole(fromRoleLabel);
Role toRole = tx.getRole(toRoleLabel);
loader.add(Graql.insert(Graql.var().asUserDefined().has(attributeLabel, "first").id(instances[0]).admin().varPatterns()), keyspace).subscribe();
for (int i = 1; i < instances.length; i++) {
Var fromRolePlayer = Graql.var();
Var toRolePlayer = Graql.var();
Pattern relationInsert = Graql.var().rel(Graql.label(fromRole.getLabel()), fromRolePlayer).rel(Graql.label(toRole.getLabel()), toRolePlayer).isa(Graql.label(baseRelation.getLabel())).and(fromRolePlayer.asUserDefined().id(instances[i - 1])).and(toRolePlayer.asUserDefined().id(instances[i]));
loader.add(Graql.insert(relationInsert.admin().varPatterns()), keyspace).subscribe();
Pattern resourceInsert = Graql.var().asUserDefined().has(attributeLabel, String.valueOf(i)).id(instances[i]);
loader.add(Graql.insert(resourceInsert.admin().varPatterns()), keyspace).subscribe();
}
}
}
}
use of ai.grakn.concept.RelationshipType in project grakn by graknlabs.
the class MigratorTestUtils method getProperties.
public static Collection<Thing> getProperties(GraknTx graph, Thing thing, String label) {
RelationshipType relation = graph.getRelationshipType(label);
Set<Thing> things = new HashSet<>();
relation.instances().filter(i -> i.rolePlayers().anyMatch(t -> t.equals(thing))).forEach(i -> i.rolePlayers().forEach(things::add));
things.remove(thing);
return things;
}
Aggregations