Search in sources :

Example 1 with ValuePredicate

use of ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate in project grakn by graknlabs.

the class InferenceRule method propagateConstraints.

/**
 * @param parentAtom atom containing constraints (parent)
 * @param unifier unifier unifying parent with the rule
 * @return rule with propagated constraints from parent
 */
public InferenceRule propagateConstraints(Atom parentAtom, Unifier unifier) {
    if (!parentAtom.isRelation() && !parentAtom.isResource())
        return this;
    Atom headAtom = head.getAtom();
    Set<Atomic> bodyAtoms = new HashSet<>(body.getAtoms());
    // transfer value predicates
    parentAtom.getPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).forEach(bodyAtoms::add);
    // if head is a resource merge vps into head
    if (headAtom.isResource() && ((ResourceAtom) headAtom).getMultiPredicate().isEmpty()) {
        ResourceAtom resourceHead = (ResourceAtom) headAtom;
        Set<ValuePredicate> innerVps = parentAtom.getInnerPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).peek(bodyAtoms::add).collect(toSet());
        headAtom = ResourceAtom.create(headAtom.getPattern(), headAtom.getPredicateVariable(), resourceHead.getRelationVariable(), resourceHead.getTypeId(), innerVps, headAtom.getParentQuery());
    }
    Set<TypeAtom> unifiedTypes = parentAtom.getTypeConstraints().flatMap(type -> type.unify(unifier).stream()).collect(toSet());
    // set rule body types to sub types of combined query+rule types
    Set<TypeAtom> ruleTypes = body.getAtoms(TypeAtom.class).filter(t -> !t.isRelation()).collect(toSet());
    Set<TypeAtom> allTypes = Sets.union(unifiedTypes, ruleTypes);
    allTypes.stream().filter(ta -> {
        SchemaConcept schemaConcept = ta.getSchemaConcept();
        SchemaConcept subType = allTypes.stream().map(Atom::getSchemaConcept).filter(Objects::nonNull).filter(t -> ReasonerUtils.supers(t).contains(schemaConcept)).findFirst().orElse(null);
        return schemaConcept == null || subType == null;
    }).forEach(t -> bodyAtoms.add(t.copy(body)));
    return new InferenceRule(ReasonerQueries.atomic(headAtom), ReasonerQueries.create(bodyAtoms, tx), ruleId, tx);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 2 with ValuePredicate

use of ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate in project grakn by graknlabs.

the class ResourceAtom method isRuleApplicableViaAtom.

@Override
public boolean isRuleApplicableViaAtom(Atom ruleAtom) {
    // findbugs complains about cast without it
    if (!(ruleAtom instanceof ResourceAtom))
        return false;
    ResourceAtom childAtom = (ResourceAtom) ruleAtom;
    ReasonerQueryImpl childQuery = (ReasonerQueryImpl) childAtom.getParentQuery();
    // check type bindings compatiblity
    Type parentType = this.getParentQuery().getVarTypeMap().get(this.getVarName());
    Type childType = childQuery.getVarTypeMap().get(childAtom.getVarName());
    if (parentType != null && childType != null && areDisjointTypes(parentType, childType) || !childQuery.isTypeRoleCompatible(ruleAtom.getVarName(), parentType))
        return false;
    // check value predicate compatibility
    if (childAtom.getMultiPredicate().isEmpty() || getMultiPredicate().isEmpty())
        return true;
    for (ValuePredicate childPredicate : childAtom.getMultiPredicate()) {
        Iterator<ValuePredicate> parentIt = getMultiPredicate().iterator();
        boolean predicateCompatible = false;
        while (parentIt.hasNext() && !predicateCompatible) {
            ValuePredicate parentPredicate = parentIt.next();
            predicateCompatible = parentPredicate.isCompatibleWith(childPredicate);
        }
        if (!predicateCompatible)
            return false;
    }
    return true;
}
Also used : Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 3 with ValuePredicate

use of ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate in project grakn by graknlabs.

the class HasAttributeProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // NB: HasAttributeProperty always has (type) label specified
    Var varName = var.var().asUserDefined();
    Var relationVariable = relationship().var();
    Var attributeVariable = attribute().var().asUserDefined();
    Set<ValuePredicate> predicates = getValuePredicates(attributeVariable, attribute(), vars, parent);
    IsaProperty isaProp = attribute().getProperties(IsaProperty.class).findFirst().orElse(null);
    VarPatternAdmin typeVar = isaProp != null ? isaProp.type() : null;
    IdPredicate predicate = typeVar != null ? getIdPredicate(attributeVariable, typeVar, vars, parent) : null;
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    // add resource atom
    VarPatternAdmin resVar = relationVariable.isUserDefinedName() ? varName.has(type(), attributeVariable, relationVariable).admin() : varName.has(type(), attributeVariable).admin();
    return ResourceAtom.create(resVar, attributeVariable, relationVariable, predicateId, predicates, parent);
}
Also used : ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ConceptId(ai.grakn.concept.ConceptId)

Example 4 with ValuePredicate

use of ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate in project grakn by graknlabs.

the class RelationshipAtom method getRelationPlayerMappings.

/**
 * @param parentAtom reference atom defining the mapping
 * @param matchType type of match to be performed
 * @return set of possible COMPLETE mappings between this (child) and parent relation players
 */
private Set<List<Pair<RelationPlayer, RelationPlayer>>> getRelationPlayerMappings(RelationshipAtom parentAtom, UnifierComparison matchType) {
    Multimap<Role, RelationPlayer> childRoleRPMap = this.getRoleRelationPlayerMap();
    Map<Var, Type> childVarTypeMap = this.getParentQuery().getVarTypeMap();
    Map<Var, Type> parentVarTypeMap = parentAtom.getParentQuery().getVarTypeMap();
    // establish compatible castings for each parent casting
    List<Set<Pair<RelationPlayer, RelationPlayer>>> compatibleMappingsPerParentRP = new ArrayList<>();
    ReasonerQueryImpl childQuery = (ReasonerQueryImpl) getParentQuery();
    Set<Role> childRoles = childRoleRPMap.keySet();
    parentAtom.getRelationPlayers().stream().filter(prp -> prp.getRole().isPresent()).forEach(prp -> {
        VarPatternAdmin parentRolePattern = prp.getRole().orElse(null);
        if (parentRolePattern == null) {
            throw GraqlQueryException.rolePatternAbsent(this);
        }
        Label parentRoleLabel = parentRolePattern.getTypeLabel().orElse(null);
        if (parentRoleLabel != null) {
            Var parentRolePlayer = prp.getRolePlayer().var();
            Type parentType = parentVarTypeMap.get(parentRolePlayer);
            Set<Role> compatibleRoles = compatibleRoles(tx().getSchemaConcept(parentRoleLabel), parentType, childRoles);
            List<RelationPlayer> compatibleRelationPlayers = new ArrayList<>();
            compatibleRoles.stream().filter(childRoleRPMap::containsKey).forEach(role -> childRoleRPMap.get(role).stream().filter(crp -> {
                Var childVar = crp.getRolePlayer().var();
                Type childType = childVarTypeMap.get(childVar);
                return matchType.typePlayability(childQuery, childVar, parentType) && matchType.typeCompatibility(parentType, childType);
            }).filter(crp -> {
                IdPredicate parentId = parentAtom.getIdPredicate(prp.getRolePlayer().var());
                IdPredicate childId = this.getIdPredicate(crp.getRolePlayer().var());
                return matchType.atomicCompatibility(parentId, childId);
            }).filter(crp -> {
                ValuePredicate parentVP = parentAtom.getPredicate(prp.getRolePlayer().var(), ValuePredicate.class);
                ValuePredicate childVP = this.getPredicate(crp.getRolePlayer().var(), ValuePredicate.class);
                return matchType.atomicCompatibility(parentVP, childVP);
            }).forEach(compatibleRelationPlayers::add));
            if (!compatibleRelationPlayers.isEmpty()) {
                compatibleMappingsPerParentRP.add(compatibleRelationPlayers.stream().map(crp -> new Pair<>(crp, prp)).collect(Collectors.toSet()));
            }
        } else {
            compatibleMappingsPerParentRP.add(getRelationPlayers().stream().map(crp -> new Pair<>(crp, prp)).collect(Collectors.toSet()));
        }
    });
    return Sets.cartesianProduct(compatibleMappingsPerParentRP).stream().filter(list -> !list.isEmpty()).filter(list -> {
        List<RelationPlayer> listChildRps = list.stream().map(Pair::getKey).collect(Collectors.toList());
        // NB: this preserves cardinality instead of removing all occuring instances which is what we want
        return ReasonerUtils.subtract(listChildRps, this.getRelationPlayers()).isEmpty();
    }).filter(list -> {
        List<RelationPlayer> listParentRps = list.stream().map(Pair::getValue).collect(Collectors.toList());
        return listParentRps.containsAll(parentAtom.getRelationPlayers());
    }).collect(toSet());
}
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) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ArrayList(java.util.ArrayList) Label(ai.grakn.concept.Label) Role(ai.grakn.concept.Role) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 5 with ValuePredicate

use of ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate in project grakn by graknlabs.

the class ResourceAtom method hasMultiPredicateEquivalentWith.

private boolean hasMultiPredicateEquivalentWith(ResourceAtom atom) {
    if (this.getMultiPredicate().size() != atom.getMultiPredicate().size())
        return false;
    for (ValuePredicate vp : getMultiPredicate()) {
        Iterator<ValuePredicate> objIt = atom.getMultiPredicate().iterator();
        boolean predicateHasEquivalent = false;
        while (objIt.hasNext() && !predicateHasEquivalent) {
            predicateHasEquivalent = vp.isAlphaEquivalent(objIt.next());
        }
        if (!predicateHasEquivalent)
            return false;
    }
    return true;
}
Also used : ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)

Aggregations

ValuePredicate (ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate)5 ConceptId (ai.grakn.concept.ConceptId)3 Var (ai.grakn.graql.Var)3 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)3 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)3 Rule (ai.grakn.concept.Rule)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 Type (ai.grakn.concept.Type)2 Answer (ai.grakn.graql.admin.Answer)2 Atomic (ai.grakn.graql.admin.Atomic)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 Unifier (ai.grakn.graql.admin.Unifier)2 UnifierType (ai.grakn.graql.internal.reasoner.UnifierType)2 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)2 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)2 ReasonerUtils (ai.grakn.graql.internal.reasoner.utils.ReasonerUtils)2 Sets (com.google.common.collect.Sets)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2