use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class BenchmarkTests method nonRecursiveChainOfRules.
/**
* Executes a scalability test defined in terms of the number of rules in the system. Creates a simple rule chain:
*
* R_i(x, y) := R_{i-1}(x, y); i e [1, N]
*
* with a single initial relation instance R_0(a ,b)
*/
@Test
public void nonRecursiveChainOfRules() {
final int N = 200;
LOG.debug(new Object() {
}.getClass().getEnclosingMethod().getName());
GraknSession graknSession = sessionContext.newSession();
// NB: loading data here as defining it as KB and using graql api leads to circular dependencies
try (GraknTx tx = graknSession.open(GraknTxType.WRITE)) {
Role fromRole = tx.putRole("fromRole");
Role toRole = tx.putRole("toRole");
RelationshipType relation0 = tx.putRelationshipType("relation0").relates(fromRole).relates(toRole);
for (int i = 1; i <= N; i++) {
tx.putRelationshipType("relation" + i).relates(fromRole).relates(toRole);
}
EntityType genericEntity = tx.putEntityType("genericEntity").plays(fromRole).plays(toRole);
Entity fromEntity = genericEntity.addEntity();
Entity toEntity = genericEntity.addEntity();
relation0.addRelationship().addRolePlayer(fromRole, fromEntity).addRolePlayer(toRole, toEntity);
for (int i = 1; i <= N; i++) {
Var fromVar = 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()), toVar).isa("relation" + (i - 1)))).then(Graql.and(Graql.var().rel(Graql.label(fromRole.getLabel()), fromVar).rel(Graql.label(toRole.getLabel()), toVar).isa("relation" + i)));
tx.graql().define(rulePattern).execute();
}
tx.commit();
}
try (GraknTx tx = graknSession.open(GraknTxType.READ)) {
final long limit = 1;
String queryPattern = "(fromRole: $x, toRole: $y) isa relation" + N + ";";
String queryString = "match " + queryPattern + " get;";
String limitedQueryString = "match " + queryPattern + "limit " + limit + ";" + "get;";
assertEquals(executeQuery(queryString, tx, "full").size(), limit);
assertEquals(executeQuery(limitedQueryString, tx, "limit").size(), limit);
}
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class PatternTest method testVarEquals.
@Test
public void testVarEquals() {
VarPattern var1;
VarPattern var2;
var1 = var();
var2 = var();
assertTrue(var1.equals(var1));
assertFalse(var1.equals(var2));
var1 = var("x");
var2 = var("y");
assertTrue(var1.equals(var1));
assertFalse(var1.equals(var2));
var1 = var("x").isa("movie");
var2 = var("x").isa("movie");
assertTrue(var1.equals(var2));
var1 = var("x").isa("movie").has("title", "abc");
var2 = var("x").has("title", "abc").isa("movie");
assertTrue(var1.equals(var2));
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class InstanceMapper method map.
/**
* Map a {@link Attribute} to a var IF it is not attached in a has relation to another instance
* @param attribute {@link Attribute} to be mapped
* @return var patterns representing the given instance
*/
private static VarPattern map(Attribute attribute) {
if (isHasResourceResource(attribute)) {
return var();
}
VarPattern var = base(attribute);
var = var.val(attribute.getValue());
return var;
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class SchemaConceptMapper method formatBase.
/**
* Create a var with the information underlying all Types
* @param schemaConcept type to be mapped
* @return {@link VarPattern} containing basic information about the given type
*/
private static VarPattern formatBase(SchemaConcept schemaConcept) {
VarPattern var = var().label(schemaConcept.getLabel());
SchemaConcept superType = schemaConcept.sup();
if (schemaConcept.sup() != null) {
var = var.sub(Graql.label(superType.getLabel()));
}
if (schemaConcept.isType()) {
Type type = schemaConcept.asType();
var = plays(var, type);
var = isAbstract(var, type);
}
return var;
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class QueryVisitor method visitPropHas.
@Override
public UnaryOperator<VarPattern> visitPropHas(GraqlParser.PropHasContext ctx) {
Label type = visitLabel(ctx.label());
VarPattern relation = Optional.ofNullable(ctx.relation).map(this::getVariable).orElseGet(Graql::var);
VarPattern resource = Optional.ofNullable(ctx.resource).map(this::getVariable).orElseGet(Graql::var);
if (ctx.predicate() != null) {
resource = resource.val(visitPredicate(ctx.predicate()));
}
VarPattern finalResource = resource;
return var -> var.has(type, finalResource, relation);
}
Aggregations