Search in sources :

Example 21 with VarPattern

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());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 22 with VarPattern

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);
}
Also used : Answer(ai.grakn.graql.admin.Answer) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 23 with VarPattern

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));
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder)

Example 24 with VarPattern

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);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Label(ai.grakn.concept.Label) VarPattern(ai.grakn.graql.VarPattern)

Example 25 with VarPattern

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());
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) VarPattern(ai.grakn.graql.VarPattern) Label(ai.grakn.concept.Label) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Aggregations

VarPattern (ai.grakn.graql.VarPattern)46 Test (org.junit.Test)28 Var (ai.grakn.graql.Var)12 Pattern (ai.grakn.graql.Pattern)11 Answer (ai.grakn.graql.admin.Answer)9 GraknTx (ai.grakn.GraknTx)8 Role (ai.grakn.concept.Role)8 Label (ai.grakn.concept.Label)7 RelationshipType (ai.grakn.concept.RelationshipType)7 Graql (ai.grakn.graql.Graql)7 Set (java.util.Set)7 ConceptId (ai.grakn.concept.ConceptId)6 EntityType (ai.grakn.concept.EntityType)6 List (java.util.List)6 Collectors.toSet (java.util.stream.Collectors.toSet)6 SchemaConcept (ai.grakn.concept.SchemaConcept)5 GraqlQueryException (ai.grakn.exception.GraqlQueryException)5 QueryBuilder (ai.grakn.graql.QueryBuilder)5 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5