Search in sources :

Example 36 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class AtomicTest method testUnification_IndirectRoles.

@Test
public void testUnification_IndirectRoles() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).isa("binary").admin();
    ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2) isa binary;" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y) isa binary;" + "$R1 label 'subRole1';" + "$R2 label 'subSubRole2';}", graph), graph);
    exactUnification(parentQuery, childQuery, true, true);
    exactUnification(baseQuery, parentQuery, true, true);
    exactUnification(baseQuery, childQuery, true, true);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 37 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class InsertQueryPropertyTest method anInsertQueryWithoutAnIsaProperty_CannotBeInserted.

@Ignore("Currently no error message is returned when trying to insert an empty set of propoerties. I am not entirely sure this is correct")
@Property
public void anInsertQueryWithoutAnIsaProperty_CannotBeInserted(@Open GraknTx tx, @Size(max = 5) Set<VarProperty> properties) {
    boolean containsIsa = properties.stream().anyMatch(IsaProperty.class::isInstance);
    assumeFalse(containsIsa);
    VarPatternAdmin pattern = Patterns.varPattern(Graql.var("x"), properties);
    exception.expect(GraqlQueryException.class);
    tx.graql().insert(pattern).execute();
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Ignore(org.junit.Ignore) Property(com.pholser.junit.quickcheck.Property) VarProperty(ai.grakn.graql.admin.VarProperty) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty)

Example 38 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class RelationshipAtom method getRoleVarMap.

/**
 * @return map containing roleType - (rolePlayer var - rolePlayer type) pairs
 */
@Memoized
public Multimap<Role, Var> getRoleVarMap() {
    ImmutableMultimap.Builder<Role, Var> builder = ImmutableMultimap.builder();
    GraknTx graph = getParentQuery().tx();
    getRelationPlayers().forEach(c -> {
        Var varName = c.getRolePlayer().var();
        VarPatternAdmin rolePattern = c.getRole().orElse(null);
        if (rolePattern != null) {
            // try directly
            Label typeLabel = rolePattern.getTypeLabel().orElse(null);
            Role role = typeLabel != null ? graph.getRole(typeLabel.getValue()) : null;
            // try indirectly
            if (role == null && rolePattern.var().isUserDefinedName()) {
                IdPredicate rolePredicate = getIdPredicate(rolePattern.var());
                if (rolePredicate != null) {
                    Role r = graph.getConcept(rolePredicate.getPredicate());
                    if (r == null)
                        throw GraqlQueryException.idNotFound(rolePredicate.getPredicate());
                    role = r;
                }
            }
            if (role != null)
                builder.put(role, varName);
        }
    });
    return builder.build();
}
Also used : Role(ai.grakn.concept.Role) GraknTx(ai.grakn.GraknTx) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Memoized(com.google.auto.value.extension.memoized.Memoized)

Example 39 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class RelationshipAtom method getRoleRelationPlayerMap.

private Multimap<Role, RelationPlayer> getRoleRelationPlayerMap() {
    Multimap<Role, RelationPlayer> roleRelationPlayerMap = ArrayListMultimap.create();
    Multimap<Role, Var> roleVarMap = getRoleVarMap();
    List<RelationPlayer> relationPlayers = getRelationPlayers();
    roleVarMap.asMap().forEach((role, value) -> {
        Label roleLabel = role.getLabel();
        relationPlayers.stream().filter(rp -> rp.getRole().isPresent()).forEach(rp -> {
            VarPatternAdmin roleTypeVar = rp.getRole().orElse(null);
            Label rl = roleTypeVar != null ? roleTypeVar.getTypeLabel().orElse(null) : null;
            if (roleLabel != null && roleLabel.equals(rl)) {
                roleRelationPlayerMap.put(role, rp);
            }
        });
    });
    return roleRelationPlayerMap;
}
Also used : Role(ai.grakn.concept.Role) 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) Label(ai.grakn.concept.Label) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Example 40 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin 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

VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)55 Test (org.junit.Test)20 Var (ai.grakn.graql.Var)17 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)16 Unifier (ai.grakn.graql.admin.Unifier)15 ConceptId (ai.grakn.concept.ConceptId)13 Label (ai.grakn.concept.Label)13 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)13 Role (ai.grakn.concept.Role)11 GraknTx (ai.grakn.GraknTx)10 VarProperty (ai.grakn.graql.admin.VarProperty)10 Collection (java.util.Collection)10 Set (java.util.Set)10 Stream (java.util.stream.Stream)10 SchemaConcept (ai.grakn.concept.SchemaConcept)9 GraqlQueryException (ai.grakn.exception.GraqlQueryException)9 VarPattern (ai.grakn.graql.VarPattern)9 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)9 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)9