use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class PatternTest method testVarPattern.
@Test
public void testVarPattern() {
Pattern x = var("x");
assertTrue(x.admin().isVarPattern());
assertFalse(x.admin().isDisjunction());
assertFalse(x.admin().isConjunction());
assertEquals(x.admin(), x.admin().asVarPattern());
}
use of ai.grakn.graql.Pattern 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.graql.Pattern 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.graql.Pattern in project grakn by graknlabs.
the class ValidateGlobalRules method checkRuleSideInvalid.
/**
* @param graph The graph to query against
* @param rule The rule the pattern was extracted from
* @param side The side from which the pattern was extracted
* @param pattern The pattern from which we will extract the types in the pattern
* @return A list of errors if the pattern refers to any non-existent types in the graph
*/
private static Set<String> checkRuleSideInvalid(GraknTx graph, Rule rule, Schema.VertexProperty side, Pattern pattern) {
Set<String> errors = new HashSet<>();
pattern.admin().varPatterns().stream().flatMap(v -> v.innerVarPatterns().stream()).flatMap(v -> v.getTypeLabels().stream()).forEach(typeLabel -> {
SchemaConcept schemaConcept = graph.getSchemaConcept(typeLabel);
if (schemaConcept == null) {
errors.add(ErrorMessage.VALIDATION_RULE_MISSING_ELEMENTS.getMessage(side, rule.getLabel(), typeLabel));
} else {
if (Schema.VertexProperty.RULE_WHEN.equals(side)) {
if (schemaConcept.isType()) {
RuleImpl.from(rule).addHypothesis(schemaConcept.asType());
}
} else if (Schema.VertexProperty.RULE_THEN.equals(side)) {
if (schemaConcept.isType()) {
RuleImpl.from(rule).addConclusion(schemaConcept.asType());
}
} else {
throw GraknTxOperationException.invalidPropertyUse(rule, side);
}
}
});
return errors;
}
use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class ConceptControllerTest method setUp.
@BeforeClass
public static void setUp() {
when(mockLockProvider.getLock(any())).thenReturn(mockLock);
// Load Silly Sample Data
try (GraknTx tx = factory.tx(keyspace, GraknTxType.WRITE)) {
// Build The Sample KB
ai.grakn.concept.Role role1 = tx.putRole("My Special Role 1");
ai.grakn.concept.Role role2 = tx.putRole("My Special Role 2");
ai.grakn.concept.AttributeType attributeType = tx.putAttributeType("My Attribute Type", ai.grakn.concept.AttributeType.DataType.STRING);
ai.grakn.concept.Attribute attribute1 = attributeType.putAttribute("An attribute 1");
ai.grakn.concept.Attribute attribute2 = attributeType.putAttribute("An attribute 2");
ai.grakn.concept.AttributeType attributeTypeKey = tx.putAttributeType("My Key Attribute Type", ai.grakn.concept.AttributeType.DataType.STRING);
ai.grakn.concept.Attribute key = attributeTypeKey.putAttribute("An attribute Key 1");
ai.grakn.concept.EntityType entityType = tx.putEntityType("My Special Entity Type").plays(role1).plays(role2).attribute(attributeType).key(attributeTypeKey);
ai.grakn.concept.EntityType entityTypeSub = tx.putEntityType("My Special Sub Entity Type").sup(entityType);
entity = entityType.addEntity().attribute(attribute1).attribute(attribute2).attribute(key);
ai.grakn.concept.RelationshipType relationshipType = tx.putRelationshipType("My Relationship Type").relates(role1).relates(role2);
ai.grakn.concept.Relationship relationship = relationshipType.addRelationship().addRolePlayer(role1, entity).addRolePlayer(role2, entity);
Pattern when = tx.graql().parser().parsePattern("$x isa \"My Relationship Type\"");
Pattern then = tx.graql().parser().parsePattern("$x isa \"My Special Entity Type\"");
ai.grakn.concept.Rule rule = tx.putRule("My Special Snowflake of a Rule", when, then);
// Manually Serialise The Concepts
roleWrapper1 = ConceptBuilder.build(role1);
roleWrapper2 = ConceptBuilder.build(role2);
relationshipTypeWrapper = ConceptBuilder.build(relationshipType);
relationshipWrapper = ConceptBuilder.build(relationship);
entityTypeWrapper = ConceptBuilder.build(entityType);
entityTypeSubWrapper = ConceptBuilder.build(entityTypeSub);
entityWrapper = ConceptBuilder.build(entity);
attributeTypeWrapper = ConceptBuilder.build(attributeType);
attributeTypeKeyWrapper = ConceptBuilder.build(attributeTypeKey);
attributeWrapper1 = ConceptBuilder.build(attribute1);
attributeWrapper2 = ConceptBuilder.build(attribute2);
ruleWrapper = ConceptBuilder.build(rule);
tx.commit();
}
}
Aggregations