Search in sources :

Example 1 with IsaAtomBase

use of ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase in project grakn by graknlabs.

the class ReasonerQueryImpl method getVarTypeMap.

private Map<Var, Type> getVarTypeMap(Stream<IsaAtomBase> isas) {
    HashMap<Var, Type> map = new HashMap<>();
    isas.map(at -> new Pair<>(at.getVarName(), at.getSchemaConcept())).filter(p -> Objects.nonNull(p.getValue())).filter(p -> p.getValue().isType()).forEach(p -> {
        Var var = p.getKey();
        Type newType = p.getValue().asType();
        Type type = map.get(var);
        if (type == null)
            map.put(var, newType);
        else {
            boolean isSubType = type.subs().anyMatch(t -> t.equals(newType));
            if (isSubType)
                map.put(var, newType);
        }
    });
    return map;
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) Type(ai.grakn.concept.Type) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) Pair(ai.grakn.graql.internal.reasoner.utils.Pair)

Example 2 with IsaAtomBase

use of ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase in project grakn by graknlabs.

the class ReasonerQueryImpl method getSubstitution.

/**
 * @return substitution obtained from all id predicates (including internal) in the query
 */
public Answer getSubstitution() {
    if (substitution == null) {
        Set<Var> varNames = getVarNames();
        Set<IdPredicate> predicates = getAtoms(IsaAtomBase.class).map(IsaAtomBase::getTypePredicate).filter(Objects::nonNull).filter(p -> varNames.contains(p.getVarName())).collect(Collectors.toSet());
        getAtoms(IdPredicate.class).forEach(predicates::add);
        HashMap<Var, Concept> answerMap = new HashMap<>();
        predicates.forEach(p -> {
            Concept concept = tx().getConcept(p.getPredicate());
            if (concept == null)
                throw GraqlQueryException.idNotFound(p.getPredicate());
            answerMap.put(p.getVarName(), concept);
        });
        substitution = new QueryAnswer(answerMap);
    }
    return substitution;
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) LoggerFactory(org.slf4j.LoggerFactory) Type(ai.grakn.concept.Type) ResolutionIterator(ai.grakn.graql.internal.reasoner.ResolutionIterator) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) QueryAnswerStream.join(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.join) Map(java.util.Map) IsaAtom(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtom) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ConceptId(ai.grakn.concept.ConceptId) Graql.var(ai.grakn.graql.Graql.var) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConjunctiveState(ai.grakn.graql.internal.reasoner.state.ConjunctiveState) QueryAnswerStream.joinWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.joinWithInverse) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) AtomicBase(ai.grakn.graql.internal.reasoner.atom.AtomicBase) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) CumulativeState(ai.grakn.graql.internal.reasoner.state.CumulativeState) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) Iterators(com.google.common.collect.Iterators) QueryAnswerStream.nonEqualsFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.nonEqualsFilter) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) LinkedList(java.util.LinkedList) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) IsaAtomBase(ai.grakn.graql.internal.reasoner.atom.binary.IsaAtomBase) GetQuery(ai.grakn.graql.GetQuery) AtomicFactory(ai.grakn.graql.internal.reasoner.atom.AtomicFactory) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResolutionPlan(ai.grakn.graql.internal.reasoner.plan.ResolutionPlan) Collections(java.util.Collections) Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) Objects(java.util.Objects)

Aggregations

Concept (ai.grakn.concept.Concept)2 ConceptId (ai.grakn.concept.ConceptId)2 Type (ai.grakn.concept.Type)2 GraqlQueryException (ai.grakn.exception.GraqlQueryException)2 GetQuery (ai.grakn.graql.GetQuery)2 Graql.var (ai.grakn.graql.Graql.var)2 Var (ai.grakn.graql.Var)2 Answer (ai.grakn.graql.admin.Answer)2 Atomic (ai.grakn.graql.admin.Atomic)2 Conjunction (ai.grakn.graql.admin.Conjunction)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)2 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)2 Unifier (ai.grakn.graql.admin.Unifier)2 UnifierComparison (ai.grakn.graql.admin.UnifierComparison)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 Patterns (ai.grakn.graql.internal.pattern.Patterns)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)2 ResolutionIterator (ai.grakn.graql.internal.reasoner.ResolutionIterator)2