Search in sources :

Example 1 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class AtomicQueryTest method testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred.

@Test
public void testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
    EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
    QueryBuilder qb = graph.graql().infer(false);
    Concept firstEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity1; get;").execute()).get("x");
    Concept secondEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity2; get;").execute()).get("x");
    ReasonerAtomicQuery relationQuery = ReasonerQueries.atomic(conjunction("{" + "$r (role1: $x, role2: $y);" + "$x id " + firstEntity.getId().getValue() + ";" + "$y id " + secondEntity.getId().getValue() + ";" + "}", graph), graph);
    assertEquals(relationQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("r").asRelationship().isInferred(), true);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryBuilder(ai.grakn.graql.QueryBuilder) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 2 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_BinaryRelationWithSubs.

/**
 * ##################################
 *
 *     Unification Tests
 *
 * ##################################
 */
@Test
public void testExactUnification_BinaryRelationWithSubs() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    Concept x1 = getConceptByResourceValue(graph, "x1");
    Concept x2 = getConceptByResourceValue(graph, "x2");
    String basePatternString = "{($x1, $x2) isa binary;}";
    String basePatternString2 = "{($y1, $y2) isa binary;}";
    ReasonerAtomicQuery xbaseQuery = ReasonerQueries.atomic(conjunction(basePatternString, graph), graph);
    ReasonerAtomicQuery ybaseQuery = ReasonerQueries.atomic(conjunction(basePatternString2, graph), graph);
    Answer xAnswer = new QueryAnswer(ImmutableMap.of(var("x1"), x1, var("x2"), x2));
    Answer flippedXAnswer = new QueryAnswer(ImmutableMap.of(var("x1"), x2, var("x2"), x1));
    Answer yAnswer = new QueryAnswer(ImmutableMap.of(var("y1"), x1, var("y2"), x2));
    Answer flippedYAnswer = new QueryAnswer(ImmutableMap.of(var("y1"), x2, var("y2"), x1));
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(xbaseQuery, xAnswer);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(xbaseQuery, flippedXAnswer);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("x1"), var("x2"), var("x2"), var("x1")));
    assertTrue(unifier.containsAll(correctUnifier));
    ReasonerAtomicQuery yChildQuery = ReasonerQueries.atomic(ybaseQuery, yAnswer);
    ReasonerAtomicQuery yChildQuery2 = ReasonerQueries.atomic(ybaseQuery, flippedYAnswer);
    Unifier unifier2 = yChildQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier2.containsAll(correctUnifier2));
    Unifier unifier3 = yChildQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier3 = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x1")));
    assertTrue(unifier3.containsAll(correctUnifier3));
}
Also used : Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 3 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer 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 4 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class RelationshipAtom method toString.

@Override
public String toString() {
    String typeString = getSchemaConcept() != null ? getSchemaConcept().getLabel().getValue() : "{" + inferPossibleTypes(new QueryAnswer()).stream().map(rt -> rt.getLabel().getValue()).collect(Collectors.joining(", ")) + "}";
    String relationString = (isUserDefined() ? getVarName() + " " : "") + typeString + (isDirect() ? "!" : "") + getRelationPlayers().toString();
    return relationString + getPredicates(Predicate.class).map(Predicate::toString).collect(Collectors.joining(""));
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate)

Example 5 with QueryAnswer

use of ai.grakn.graql.internal.query.QueryAnswer in project grakn by graknlabs.

the class RelationshipAtom method isRuleApplicableViaAtom.

@Override
public boolean isRuleApplicableViaAtom(Atom ruleAtom) {
    if (ruleAtom.isResource())
        return isRuleApplicableViaAtom(ruleAtom.toRelationshipAtom());
    // findbugs complains about cast without it
    if (!(ruleAtom instanceof RelationshipAtom))
        return false;
    RelationshipAtom headAtom = (RelationshipAtom) ruleAtom;
    RelationshipAtom atomWithType = this.addType(headAtom.getSchemaConcept()).inferRoles(new QueryAnswer());
    // rule head atom is applicable if it is unifiable
    return headAtom.getRelationPlayers().size() >= atomWithType.getRelationPlayers().size() && !headAtom.getRelationPlayerMappings(atomWithType).isEmpty();
}
Also used : QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer)

Aggregations

QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)29 Answer (ai.grakn.graql.admin.Answer)15 Concept (ai.grakn.concept.Concept)13 Test (org.junit.Test)13 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)9 Var (ai.grakn.graql.Var)8 GetQuery (ai.grakn.graql.GetQuery)6 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ConceptId (ai.grakn.concept.ConceptId)4 Type (ai.grakn.concept.Type)4 Unifier (ai.grakn.graql.admin.Unifier)4 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)4 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)4 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 List (java.util.List)3 Set (java.util.Set)3