Search in sources :

Example 26 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class RelatesProperty method define.

@Override
public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException {
    Var roleVar = role().var();
    PropertyExecutor.Method relatesMethod = executor -> {
        Role role = executor.get(roleVar).asRole();
        executor.get(var).asRelationshipType().relates(role);
    };
    PropertyExecutor relatesExecutor = PropertyExecutor.builder(relatesMethod).requires(var, roleVar).build();
    // This allows users to skip stating `$roleVar sub role` when they say `$var relates $roleVar`
    PropertyExecutor.Method isRoleMethod = executor -> executor.builder(roleVar).isRole();
    PropertyExecutor isRoleExecutor = PropertyExecutor.builder(isRoleMethod).produces(roleVar).build();
    VarPatternAdmin superRoleVarPattern = superRole();
    if (superRoleVarPattern != null) {
        Var superRoleVar = superRoleVarPattern.var();
        PropertyExecutor.Method subMethod = executor -> {
            Role superRole = executor.get(superRoleVar).asRole();
            executor.builder(roleVar).sub(superRole);
        };
        PropertyExecutor subExecutor = PropertyExecutor.builder(subMethod).requires(superRoleVar).produces(roleVar).build();
        return ImmutableSet.of(relatesExecutor, isRoleExecutor, subExecutor);
    } else {
        return ImmutableSet.of(relatesExecutor, isRoleExecutor);
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Role(ai.grakn.concept.Role) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) EquivalentFragmentSets.sub(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.sub) EquivalentFragmentSets.relates(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.relates) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) RelationshipType(ai.grakn.concept.RelationshipType) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) RelatesAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelatesAtom) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) Nullable(javax.annotation.Nullable) Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var)

Example 27 with Var

use of ai.grakn.graql.Var 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 28 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class ReasonerQueryImpl method getRoleSubstitution.

public Answer getRoleSubstitution() {
    Map<Var, Concept> roleSub = new HashMap<>();
    getAtoms(RelationshipAtom.class).flatMap(RelationshipAtom::getRolePredicates).forEach(p -> {
        Concept concept = tx().getConcept(p.getPredicate());
        if (concept == null)
            throw GraqlQueryException.idNotFound(p.getPredicate());
        roleSub.put(p.getVarName(), concept);
    });
    return new QueryAnswer(roleSub);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var)

Example 29 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class AtomicState method materialisedAnswer.

private Answer materialisedAnswer(Answer baseAnswer, InferenceRule rule, Unifier unifier) {
    Answer answer = baseAnswer;
    ReasonerAtomicQuery query = getQuery();
    QueryCache<ReasonerAtomicQuery> cache = getCache();
    ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(query, answer);
    ReasonerAtomicQuery ruleHead = ReasonerQueries.atomic(rule.getHead(), answer);
    Set<Var> queryVars = query.getVarNames().size() < ruleHead.getVarNames().size() ? unifier.keySet() : ruleHead.getVarNames();
    boolean queryEquivalentToHead = subbedQuery.isEquivalent(ruleHead);
    // check if the specific answer to ruleHead already in cache/db
    Answer headAnswer = cache.getAnswer(ruleHead, answer).project(queryVars).unify(unifier);
    // if not and query different than rule head do the same with the query
    Answer queryAnswer = headAnswer.isEmpty() && queryEquivalentToHead ? cache.getAnswer(query, answer) : new QueryAnswer();
    // ensure no duplicates created - only materialise answer if it doesn't exist in the db
    if (headAnswer.isEmpty() && queryAnswer.isEmpty()) {
        Answer materialisedSub = ruleHead.materialise(answer).findFirst().orElse(null);
        if (!queryEquivalentToHead)
            cache.recordAnswer(ruleHead, materialisedSub);
        answer = materialisedSub.project(queryVars).unify(unifier);
    } else {
        answer = headAnswer.isEmpty() ? queryAnswer : headAnswer;
    }
    if (answer.isEmpty())
        return answer;
    return answer.merge(query.getSubstitution()).explain(new RuleExplanation(query, rule));
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 30 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class DefineQueryTest method whenExecutingADefineQuery_ResultContainsAllInsertedVars.

@Test
public void whenExecutingADefineQuery_ResultContainsAllInsertedVars() {
    Var type = var("type");
    Var type2 = var("type2");
    // Note that two variables refer to the same type. They should both be in the result
    DefineQuery query = qb.define(type.label("my-type").sub("entity"), type2.label("my-type"));
    Answer result = query.execute();
    assertThat(result.vars(), containsInAnyOrder(type, type2));
    assertEquals(result.get(type), result.get(type2));
}
Also used : Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) DefineQuery(ai.grakn.graql.DefineQuery) Test(org.junit.Test)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17