Search in sources :

Example 16 with Answer

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

the class OntologicalQueryTest method allInstancesOfMetaEntity.

/**
 * meta concepts *
 */
@Test
public void allInstancesOfMetaEntity() {
    GraknTx tx = testContext.tx();
    QueryBuilder qb = tx.graql().infer(true);
    long noOfEntities = tx.admin().getMetaEntityType().instances().count();
    String queryString = "match $x isa entity;get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), noOfEntities);
}
Also used : GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 17 with Answer

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

the class ReasonerTest method testReasoningWithQueryContainingLimit.

@Test
public void testReasoningWithQueryContainingLimit() {
    GraknTx graph = nonMaterialisedGeoKB.tx();
    String limitQueryString = "match (geo-entity: $x, entity-location: $y)isa is-located-in;limit 5; get;";
    String queryString = "match (geo-entity: $x, entity-location: $y)isa is-located-in; get;";
    QueryBuilder iqb = graph.graql().infer(true);
    GetQuery limitQuery = iqb.parse(limitQueryString);
    GetQuery query = iqb.parse(queryString);
    List<Answer> limitedAnswers = limitQuery.execute();
    List<Answer> answers = query.execute();
    assertEquals(limitedAnswers.size(), 5);
    assertTrue(answers.size() > limitedAnswers.size());
    assertTrue(answers.containsAll(limitedAnswers));
}
Also used : GraknTx(ai.grakn.GraknTx) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 18 with Answer

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

the class MatchBenchmark method match.

@Benchmark
public void match() {
    Match match = graph.graql().match(var("x").isa(BENCHMARK_ENTITY_TYPE).has(BENCHMARK_ATTRIBUTE_TYPE, "0"));
    GetQuery answers = match.get();
    Optional<Answer> first = answers.stream().findFirst();
    first.get();
}
Also used : Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) Match(ai.grakn.graql.Match) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 19 with Answer

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

the class RelationshipAtom method materialise.

@Override
public Stream<Answer> materialise() {
    RelationshipType relationType = getSchemaConcept().asRelationshipType();
    Multimap<Role, Var> roleVarMap = getRoleVarMap();
    Answer substitution = getParentQuery().getSubstitution();
    Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
    roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
    Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
    return Stream.of(substitution.merge(relationSub));
}
Also used : Role(ai.grakn.concept.Role) 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) 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) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType)

Example 20 with Answer

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

the class InferenceRule method subGoal.

/**
 * @param parentAtom atom to which this rule is applied
 * @param ruleUnifier unifier with parent state
 * @param parent parent state
 * @param visitedSubGoals set of visited sub goals
 * @param cache query cache
 * @return resolution subGoal formed from this rule
 */
public ResolutionState subGoal(Atom parentAtom, Unifier ruleUnifier, QueryStateBase parent, Set<ReasonerAtomicQuery> visitedSubGoals, QueryCache<ReasonerAtomicQuery> cache) {
    Unifier ruleUnifierInverse = ruleUnifier.inverse();
    // delta' = theta . thetaP . delta
    Answer partialSubPrime = parentAtom.getParentQuery().getSubstitution().unify(ruleUnifierInverse);
    return new RuleState(this.propagateConstraints(parentAtom, ruleUnifierInverse), partialSubPrime, ruleUnifier, parent, visitedSubGoals, cache);
}
Also used : Answer(ai.grakn.graql.admin.Answer) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Aggregations

Answer (ai.grakn.graql.admin.Answer)193 Test (org.junit.Test)164 QueryBuilder (ai.grakn.graql.QueryBuilder)104 GetQuery (ai.grakn.graql.GetQuery)69 GraknTx (ai.grakn.GraknTx)47 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)37 Concept (ai.grakn.concept.Concept)32 Var (ai.grakn.graql.Var)21 Set (java.util.Set)21 Unifier (ai.grakn.graql.admin.Unifier)19 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)18 Collectors (java.util.stream.Collectors)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 Sets (com.google.common.collect.Sets)16 List (java.util.List)16 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)15 Stream (java.util.stream.Stream)15 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)14 Pair (ai.grakn.graql.internal.reasoner.utils.Pair)12