use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class ReasoningTests method whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults.
@Test
public void whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults() {
QueryBuilder withInference = testSet14.tx().graql().infer(true);
QueryBuilder withoutInference = testSet14.tx().graql().infer(false);
VarPattern owner = label(HAS_OWNER.getLabel("resource"));
VarPattern value = label(HAS_VALUE.getLabel("resource"));
VarPattern hasRes = label(HAS.getLabel("resource"));
Function<QueryBuilder, GetQuery> query = qb -> qb.match(var().rel(owner, "x").rel(value, "y").isa(hasRes), // This pattern is added only to encourage reasoning to activate
var("a").has("resource", var("b"))).get();
Set<Answer> resultsWithoutInference = query.apply(withoutInference).stream().collect(toSet());
Set<Answer> resultsWithInference = query.apply(withInference).stream().collect(toSet());
assertThat(resultsWithoutInference, not(empty()));
assertThat(Sets.difference(resultsWithoutInference, resultsWithInference), empty());
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class GeoInferenceTest method testTransitiveQuery_Closure_variableSpecificRoles.
@Test
public void testTransitiveQuery_Closure_variableSpecificRoles() {
QueryBuilder iqb = geoKB.tx().graql().infer(true);
VarPattern rolePattern = var().rel(var("r1").label("geo-entity"), var("x")).rel(var("r2").label("entity-location"), var("y"));
List<Answer> answers = iqb.match(rolePattern).get().execute();
List<Answer> answers2 = iqb.materialise(true).match(rolePattern).get().execute();
answers.forEach(ans -> assertEquals(ans.size(), 4));
assertEquals(answers.size(), 51);
answers2.forEach(ans -> assertEquals(ans.size(), 4));
assertCollectionsEqual(answers, answers2);
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class ReasoningTests method ternaryNaryRelationWithVariableRoles.
private void ternaryNaryRelationWithVariableRoles(String label, int conceptDOF) {
GraknTx graph = testSet29.tx();
QueryBuilder qb = graph.graql().infer(true);
final int arity = (int) graph.getRelationshipType(label).relates().count();
VarPattern resourcePattern = var("a1").has("name", "a");
// This query generalises all roles but the first one.
VarPattern pattern = var().rel("role1", "a1");
for (int i = 2; i <= arity; i++) pattern = pattern.rel(var("r" + i), "a" + i);
pattern = pattern.isa(label);
List<Answer> answers = qb.match(pattern.and(resourcePattern)).get().execute();
assertEquals(answers.size(), answerCombinations(arity - 1, conceptDOF));
// We get extra conceptDOF degrees of freedom by removing the resource constraint on $a1 and the set is symmetric.
List<Answer> answers2 = qb.match(pattern).get().execute();
assertEquals(answers2.size(), answerCombinations(arity - 1, conceptDOF) * conceptDOF);
// The general case of mapping all available Rps
VarPattern generalPattern = var();
for (int i = 1; i <= arity; i++) generalPattern = generalPattern.rel(var("r" + i), "a" + i);
generalPattern = generalPattern.isa(label);
List<Answer> answers3 = qb.match(generalPattern).get().execute();
assertEquals(answers3.size(), answerCombinations(arity, conceptDOF));
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class HasAttributeTypeProperty method of.
/**
* @throws GraqlQueryException if no label is specified on {@code resourceType}
*/
public static HasAttributeTypeProperty of(VarPatternAdmin resourceType, boolean required) {
Label resourceLabel = resourceType.getTypeLabel().orElseThrow(() -> GraqlQueryException.noLabelSpecifiedForHas(resourceType));
VarPattern role = Graql.label(Schema.MetaSchema.ROLE.getLabel());
VarPatternAdmin ownerRole = var().sub(role).admin();
VarPatternAdmin valueRole = var().sub(role).admin();
VarPattern relationType = var().sub(Graql.label(Schema.MetaSchema.RELATIONSHIP.getLabel()));
// If a key, limit only to the implicit key type
if (required) {
ownerRole = ownerRole.label(KEY_OWNER.getLabel(resourceLabel)).admin();
valueRole = valueRole.label(KEY_VALUE.getLabel(resourceLabel)).admin();
relationType = relationType.label(KEY.getLabel(resourceLabel));
}
VarPatternAdmin relationOwner = relationType.relates(ownerRole).admin();
VarPatternAdmin relationValue = relationType.admin().var().relates(valueRole).admin();
return new AutoValue_HasAttributeTypeProperty(resourceType, ownerRole, valueRole, relationOwner, relationValue, required);
}
use of ai.grakn.graql.VarPattern in project grakn by graknlabs.
the class RelationshipAtom method rewriteWithVariableRoles.
/**
* if any {@link Role} variable of the parent is user defined rewrite ALL {@link Role} variables to user defined (otherwise unification is problematic)
* @param parentAtom parent atom that triggers rewrite
* @return new relation atom with user defined {@link Role} variables if necessary or this
*/
private RelationshipAtom rewriteWithVariableRoles(Atom parentAtom) {
if (!parentAtom.requiresRoleExpansion())
return this;
VarPattern relVar = getPattern().admin().getProperty(IsaProperty.class).map(prop -> getVarName().isa(prop.type())).orElse(getVarName());
for (RelationPlayer rp : getRelationPlayers()) {
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
if (rolePattern != null) {
Var roleVar = rolePattern.var();
Label roleLabel = rolePattern.getTypeLabel().orElse(null);
relVar = relVar.rel(roleVar.asUserDefined().label(roleLabel), rp.getRolePlayer());
} else {
relVar = relVar.rel(rp.getRolePlayer());
}
}
return create(relVar.admin(), getPredicateVariable(), getTypeId(), getParentQuery());
}
Aggregations