Search in sources :

Example 1 with ReasonerQuery

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

the class ReasonerAtomicQuery method getMultiUnifier.

/**
 * @throws IllegalArgumentException if passed a {@link ReasonerQuery} that is not a {@link ReasonerAtomicQuery}.
 */
@Override
public MultiUnifier getMultiUnifier(ReasonerQuery p, UnifierComparison unifierType) {
    if (p == this)
        return new MultiUnifierImpl();
    Preconditions.checkArgument(p instanceof ReasonerAtomicQuery);
    ReasonerAtomicQuery parent = (ReasonerAtomicQuery) p;
    MultiUnifier multiUnifier = this.getAtom().getMultiUnifier(parent.getAtom(), unifierType);
    Set<TypeAtom> childTypes = this.getAtom().getTypeConstraints().collect(Collectors.toSet());
    if (childTypes.isEmpty())
        return multiUnifier;
    // get corresponding type unifiers
    Set<TypeAtom> parentTypes = parent.getAtom().getTypeConstraints().collect(Collectors.toSet());
    if (multiUnifier.isEmpty())
        return new MultiUnifierImpl(typeUnifier(childTypes, parentTypes, new UnifierImpl()));
    Set<Unifier> unifiers = multiUnifier.unifiers().stream().map(unifier -> typeUnifier(childTypes, parentTypes, unifier)).collect(Collectors.toSet());
    return new MultiUnifierImpl(unifiers);
}
Also used : QueryAnswerStream.entityTypeFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.entityTypeFilter) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Concept(ai.grakn.concept.Concept) LoggerFactory(org.slf4j.LoggerFactory) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) ReasonerQueryIterator(ai.grakn.graql.internal.reasoner.iterator.ReasonerQueryIterator) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) QueryAnswerStream.knownFilterWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.knownFilterWithInverse) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Preconditions(com.google.common.base.Preconditions) AtomicStateProducer(ai.grakn.graql.internal.reasoner.state.AtomicStateProducer) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Example 2 with ReasonerQuery

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

the class ValidateGlobalRules method combinedRuleQuery.

private static ReasonerQuery combinedRuleQuery(GraknTx graph, Rule rule) {
    ReasonerQuery bodyQuery = rule.getWhen().admin().getDisjunctiveNormalForm().getPatterns().iterator().next().toReasonerQuery(graph);
    ReasonerQuery headQuery = rule.getThen().admin().getDisjunctiveNormalForm().getPatterns().iterator().next().toReasonerQuery(graph);
    return headQuery.conjunction(bodyQuery);
}
Also used : ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery)

Example 3 with ReasonerQuery

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

the class MatchInfer method stream.

@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
    // If the tx is not embedded, treat it like there is no transaction
    // TODO: this is dodgy - when queries don't contain transactions this can be fixed
    EmbeddedGraknTx<?> tx = optionalOr(optionalGraph, inner.tx().filter(t -> t instanceof EmbeddedGraknTx).map(t -> (EmbeddedGraknTx<?>) t)).orElseThrow(GraqlQueryException::noTx);
    if (!RuleUtils.hasRules(tx))
        return inner.stream(optionalGraph);
    validatePattern(tx);
    try {
        Iterator<Conjunction<VarPatternAdmin>> conjIt = getPattern().getDisjunctiveNormalForm().getPatterns().iterator();
        Conjunction<VarPatternAdmin> conj = conjIt.next();
        ReasonerQuery conjQuery = ReasonerQueries.create(conj, tx);
        conjQuery.checkValid();
        Stream<Answer> answerStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
        while (conjIt.hasNext()) {
            conj = conjIt.next();
            conjQuery = ReasonerQueries.create(conj, tx);
            Stream<Answer> localStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
            answerStream = Stream.concat(answerStream, localStream);
        }
        return answerStream.map(result -> result.project(getSelectedNames()));
    } catch (GraqlQueryException e) {
        System.err.println(e.getMessage());
        return Stream.empty();
    }
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) Answer(ai.grakn.graql.admin.Answer) CommonUtil.optionalOr(ai.grakn.util.CommonUtil.optionalOr) Match(ai.grakn.graql.Match) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) Answer(ai.grakn.graql.admin.Answer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Conjunction(ai.grakn.graql.admin.Conjunction) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery)

Example 4 with ReasonerQuery

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

the class ExplanationTest method explanationConsistentWithAnswer.

private boolean explanationConsistentWithAnswer(Answer ans) {
    ReasonerQuery query = ans.getExplanation().getQuery();
    Set<Var> vars = query != null ? query.getVarNames() : new HashSet<>();
    return vars.containsAll(ans.map().keySet());
}
Also used : Var(ai.grakn.graql.Var) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery)

Example 5 with ReasonerQuery

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

the class RelationshipProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // set varName as user defined if reified
    // reified if contains more properties than the RelationshipProperty itself and potential IsaProperty
    boolean isReified = var.getProperties().filter(prop -> !RelationshipProperty.class.isInstance(prop)).anyMatch(prop -> !AbstractIsaProperty.class.isInstance(prop));
    VarPattern relVar = isReified ? var.var().asUserDefined() : var.var();
    for (RelationPlayer rp : relationPlayers()) {
        VarPattern rolePattern = rp.getRole().orElse(null);
        VarPattern rolePlayer = rp.getRolePlayer();
        if (rolePattern != null) {
            Var roleVar = rolePattern.admin().var();
            // look for indirect role definitions
            IdPredicate roleId = getUserDefinedIdPredicate(roleVar, vars, parent);
            if (roleId != null) {
                Concept concept = parent.tx().getConcept(roleId.getPredicate());
                if (concept != null) {
                    if (concept.isRole()) {
                        Label roleLabel = concept.asSchemaConcept().getLabel();
                        rolePattern = roleVar.label(roleLabel);
                    } else {
                        throw GraqlQueryException.nonRoleIdAssignedToRoleVariable(var);
                    }
                }
            }
            relVar = relVar.rel(rolePattern, rolePlayer);
        } else
            relVar = relVar.rel(rolePlayer);
    }
    // isa part
    AbstractIsaProperty isaProp = var.getProperty(AbstractIsaProperty.class).orElse(null);
    IdPredicate predicate = null;
    // if no isa property present generate type variable
    Var typeVariable = isaProp != null ? isaProp.type().var() : Graql.var();
    // Isa present
    if (isaProp != null) {
        VarPatternAdmin isaVar = isaProp.type();
        Label label = isaVar.getTypeLabel().orElse(null);
        if (label != null) {
            predicate = IdPredicate.create(typeVariable, label, parent);
        } else {
            typeVariable = isaVar.var();
            predicate = getUserDefinedIdPredicate(typeVariable, vars, parent);
        }
    }
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    relVar = isaProp instanceof DirectIsaProperty ? relVar.directIsa(typeVariable.asUserDefined()) : relVar.isa(typeVariable.asUserDefined());
    return RelationshipAtom.create(relVar.admin(), typeVariable, predicateId, parent);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Role(ai.grakn.concept.Role) EquivalentFragmentSets.rolePlayer(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.rolePlayer) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Graql(ai.grakn.graql.Graql) HashSet(java.util.HashSet) UniqueVarProperty(ai.grakn.graql.admin.UniqueVarProperty) Label(ai.grakn.concept.Label) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) QueryOperationExecutor(ai.grakn.graql.internal.query.runner.QueryOperationExecutor) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) VarPattern(ai.grakn.graql.VarPattern) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Aggregations

ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 HashSet (java.util.HashSet)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 Var (ai.grakn.graql.Var)3 Atomic (ai.grakn.graql.admin.Atomic)3 Conjunction (ai.grakn.graql.admin.Conjunction)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 GraknTx (ai.grakn.GraknTx)2 Concept (ai.grakn.concept.Concept)2 Label (ai.grakn.concept.Label)2 Relationship (ai.grakn.concept.Relationship)2 Role (ai.grakn.concept.Role)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 Thing (ai.grakn.concept.Thing)2 Answer (ai.grakn.graql.admin.Answer)2 CommonUtil (ai.grakn.util.CommonUtil)2 Collection (java.util.Collection)2 Set (java.util.Set)2