Search in sources :

Example 71 with Concept

use of ai.grakn.concept.Concept 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)

Example 72 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class QueryAnswerStream method joinWithInverse.

/**
 * lazy stream join with fast lookup from inverse answer map
 * @param stream left stream operand
 * @param stream2 right stream operand
 * @param stream2InverseMap inverse map of right operand from cache
 * @param joinVars intersection on variables of two streams
 * @return joined stream
 */
static Stream<Answer> joinWithInverse(Stream<Answer> stream, Stream<Answer> stream2, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap, ImmutableSet<Var> joinVars) {
    if (joinVars.isEmpty()) {
        LazyAnswerIterator l2 = new LazyAnswerIterator(stream2);
        return stream.flatMap(a1 -> l2.stream().map(a -> a.merge(a1)));
    }
    return stream.flatMap(a1 -> {
        Iterator<Var> vit = joinVars.iterator();
        Set<Answer> matchAnswers = findMatchingAnswers(a1, stream2InverseMap, vit.next());
        while (vit.hasNext()) {
            matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(a1, stream2InverseMap, vit.next()));
        }
        return matchAnswers.stream().map(a -> a.merge(a1));
    });
}
Also used : Pair(ai.grakn.graql.internal.reasoner.utils.Pair) ImmutableSet(com.google.common.collect.ImmutableSet) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Answer(ai.grakn.graql.admin.Answer) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Stream(java.util.stream.Stream) Map(java.util.Map) Var(ai.grakn.graql.Var) Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) LazyAnswerIterator(ai.grakn.graql.internal.reasoner.iterator.LazyAnswerIterator)

Example 73 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ConceptTest method whenAConceptIsNotDeleted_CallingIsDeletedReturnsFalse.

@Test
public void whenAConceptIsNotDeleted_CallingIsDeletedReturnsFalse() {
    Concept stillAlive = tx.putEntityType("still-alive");
    assertFalse(stillAlive.isDeleted());
}
Also used : Concept(ai.grakn.concept.Concept) Test(org.junit.Test)

Example 74 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ConceptTest method whenCastingToCorrectType_ReturnCorrectType.

@Test
public void whenCastingToCorrectType_ReturnCorrectType() {
    Concept concept = tx.putEntityType("Test");
    assertTrue("Concept is not of type [" + EntityType.class.getName() + "]", concept.isEntityType());
    EntityType type = concept.asEntityType();
    assertEquals(type, concept);
}
Also used : Concept(ai.grakn.concept.Concept) EntityType(ai.grakn.concept.EntityType) Test(org.junit.Test)

Example 75 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ConceptTest method whenComparingConcepts_EnsureEqualityIsBasedOnConceptID.

@Test
public void whenComparingConcepts_EnsureEqualityIsBasedOnConceptID() {
    Concept v1_1 = tx.putEntityType("Value_1");
    Concept v1_2 = tx.getEntityType("Value_1");
    Concept v1_3 = tx.putEntityType("Value_1");
    Concept v2_1 = tx.putEntityType("Value_2");
    assertEquals(v1_1, v1_2);
    assertNotEquals(v1_1, v2_1);
    assertNotEquals(v1_1.getId(), v2_1.getId());
    HashSet<Concept> concepts = new HashSet<>();
    concepts.add(v1_1);
    concepts.add(v1_2);
    concepts.add(v1_3);
    assertEquals(1, concepts.size());
    concepts.add(v2_1);
    assertEquals(2, concepts.size());
}
Also used : Concept(ai.grakn.concept.Concept) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Concept (ai.grakn.concept.Concept)91 Test (org.junit.Test)56 ConceptId (ai.grakn.concept.ConceptId)26 GraknTx (ai.grakn.GraknTx)25 Answer (ai.grakn.graql.admin.Answer)25 SchemaConcept (ai.grakn.concept.SchemaConcept)19 Label (ai.grakn.concept.Label)18 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)18 QueryBuilder (ai.grakn.graql.QueryBuilder)17 Var (ai.grakn.graql.Var)15 Set (java.util.Set)15 Role (ai.grakn.concept.Role)14 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 HashSet (java.util.HashSet)13 List (java.util.List)12 AttributeType (ai.grakn.concept.AttributeType)11 EntityType (ai.grakn.concept.EntityType)11 Schema (ai.grakn.util.Schema)10 Collectors (java.util.stream.Collectors)10 Stream (java.util.stream.Stream)10