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);
}
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));
}
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));
}
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(""));
}
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();
}
Aggregations