use of ai.grakn.graql.VarPattern 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.VarPattern in project grakn by graknlabs.
the class RelationshipProperty method mapToAtom.
@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
// set varName as user defined if reified
// reified if contains more properties than the RelationshipProperty itself and potential IsaProperty
boolean isReified = var.getProperties().filter(prop -> !RelationshipProperty.class.isInstance(prop)).anyMatch(prop -> !AbstractIsaProperty.class.isInstance(prop));
VarPattern relVar = isReified ? var.var().asUserDefined() : var.var();
for (RelationPlayer rp : relationPlayers()) {
VarPattern rolePattern = rp.getRole().orElse(null);
VarPattern rolePlayer = rp.getRolePlayer();
if (rolePattern != null) {
Var roleVar = rolePattern.admin().var();
// look for indirect role definitions
IdPredicate roleId = getUserDefinedIdPredicate(roleVar, vars, parent);
if (roleId != null) {
Concept concept = parent.tx().getConcept(roleId.getPredicate());
if (concept != null) {
if (concept.isRole()) {
Label roleLabel = concept.asSchemaConcept().getLabel();
rolePattern = roleVar.label(roleLabel);
} else {
throw GraqlQueryException.nonRoleIdAssignedToRoleVariable(var);
}
}
}
relVar = relVar.rel(rolePattern, rolePlayer);
} else
relVar = relVar.rel(rolePlayer);
}
// isa part
AbstractIsaProperty isaProp = var.getProperty(AbstractIsaProperty.class).orElse(null);
IdPredicate predicate = null;
// if no isa property present generate type variable
Var typeVariable = isaProp != null ? isaProp.type().var() : Graql.var();
// Isa present
if (isaProp != null) {
VarPatternAdmin isaVar = isaProp.type();
Label label = isaVar.getTypeLabel().orElse(null);
if (label != null) {
predicate = IdPredicate.create(typeVariable, label, parent);
} else {
typeVariable = isaVar.var();
predicate = getUserDefinedIdPredicate(typeVariable, vars, parent);
}
}
ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
relVar = isaProp instanceof DirectIsaProperty ? relVar.directIsa(typeVariable.asUserDefined()) : relVar.isa(typeVariable.asUserDefined());
return RelationshipAtom.create(relVar.admin(), typeVariable, predicateId, parent);
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class DefineQueryTest method testHas.
@Test
public void testHas() {
String resourceType = "a-new-resource-type";
qb.define(label("a-new-type").sub("entity").has(resourceType), label(resourceType).sub(Schema.MetaSchema.ATTRIBUTE.getLabel().getValue()).datatype(AttributeType.DataType.STRING), label("an-unconnected-resource-type").sub(Schema.MetaSchema.ATTRIBUTE.getLabel().getValue()).datatype(AttributeType.DataType.LONG)).execute();
// Make sure a-new-type can have the given resource type, but not other resource types
assertExists(qb, label("a-new-type").sub("entity").has(resourceType));
assertNotExists(qb, label("a-new-type").has("title"));
assertNotExists(qb, label("movie").has(resourceType));
assertNotExists(qb, label("a-new-type").has("an-unconnected-resource-type"));
VarPattern hasResource = Graql.label(HAS.getLabel(resourceType));
VarPattern hasResourceOwner = Graql.label(HAS_OWNER.getLabel(resourceType));
VarPattern hasResourceValue = Graql.label(HAS_VALUE.getLabel(resourceType));
// Make sure the expected ontology elements are created
assertExists(qb, hasResource.sub(RELATIONSHIP.getLabel().getValue()));
assertExists(qb, hasResourceOwner.sub(ROLE.getLabel().getValue()));
assertExists(qb, hasResourceValue.sub(ROLE.getLabel().getValue()));
assertExists(qb, hasResource.relates(hasResourceOwner));
assertExists(qb, hasResource.relates(hasResourceValue));
assertExists(qb, label("a-new-type").plays(hasResourceOwner));
assertExists(qb, label(resourceType).plays(hasResourceValue));
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class DefineQueryTest method testKey.
@Test
public void testKey() {
String resourceType = "a-new-resource-type";
qb.define(label("a-new-type").sub("entity").key(resourceType), label(resourceType).sub(Schema.MetaSchema.ATTRIBUTE.getLabel().getValue()).datatype(AttributeType.DataType.STRING)).execute();
// Make sure a-new-type can have the given resource type as a key or otherwise
assertExists(qb, label("a-new-type").sub("entity").key(resourceType));
assertExists(qb, label("a-new-type").sub("entity").has(resourceType));
assertNotExists(qb, label("a-new-type").sub("entity").key("title"));
assertNotExists(qb, label("movie").sub("entity").key(resourceType));
VarPattern key = Graql.label(KEY.getLabel(resourceType));
VarPattern keyOwner = Graql.label(KEY_OWNER.getLabel(resourceType));
VarPattern keyValue = Graql.label(KEY_VALUE.getLabel(resourceType));
// Make sure the expected ontology elements are created
assertExists(qb, key.sub(RELATIONSHIP.getLabel().getValue()));
assertExists(qb, keyOwner.sub(ROLE.getLabel().getValue()));
assertExists(qb, keyValue.sub(ROLE.getLabel().getValue()));
assertExists(qb, key.relates(keyOwner));
assertExists(qb, key.relates(keyValue));
assertExists(qb, label("a-new-type").plays(keyOwner));
assertExists(qb, label(resourceType).plays(keyValue));
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class DefineQueryTest method whenDefiningANonRuleWithAThenPattern_Throw.
@Test
public void whenDefiningANonRuleWithAThenPattern_Throw() {
VarPattern rule = label("covfefe").sub(label(ENTITY.getLabel())).then(var("x"));
exception.expect(GraqlQueryException.class);
exception.expectMessage(anyOf(// Either we see "entity" and an unexpected "when"...
allOf(containsString("unexpected property"), containsString("then")), // ...or we see "when" and don't find the expected "then"
containsString(GraqlQueryException.insertNoExpectedProperty("when", rule.admin()).getMessage())));
qb.define(rule).execute();
}
Aggregations