use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.
the class AtomicQueryTest method testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly.
@Test
public void testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String childString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
String parentString = "match ($x, $y) isa is-located-in; get;";
QueryBuilder qb = graph.graql().infer(false);
GetQuery childQuery = qb.parse(childString);
GetQuery parentQuery = qb.parse(parentString);
Set<Answer> answers = childQuery.stream().collect(toSet());
Set<Answer> fullAnswers = parentQuery.stream().collect(toSet());
Atom childAtom = ReasonerQueries.atomic(conjunction(childQuery.match().admin().getPattern()), graph).getAtom();
Atom parentAtom = ReasonerQueries.atomic(conjunction(parentQuery.match().admin().getPattern()), graph).getAtom();
MultiUnifier multiUnifier = childAtom.getMultiUnifier(childAtom, UnifierType.RULE);
Set<Answer> permutedAnswers = answers.stream().flatMap(a -> multiUnifier.stream().map(a::unify)).collect(Collectors.toSet());
MultiUnifier multiUnifier2 = childAtom.getMultiUnifier(parentAtom, UnifierType.RULE);
Set<Answer> permutedAnswers2 = answers.stream().flatMap(a -> multiUnifier2.stream().map(a::unify)).collect(Collectors.toSet());
assertEquals(fullAnswers, permutedAnswers2);
assertEquals(answers, permutedAnswers);
}
use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.
the class AtomicTest method testUnification_ParentHasFewerRelationPlayers.
@Test
public void testUnification_ParentHasFewerRelationPlayers() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
String childString = "{(subRole1: $y, subRole2: $x) isa binary;}";
String parentString = "{(subRole1: $x) isa binary;}";
String parentString2 = "{(subRole2: $y) isa binary;}";
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction(childString, graph), graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction(parentString, graph), graph);
ReasonerAtomicQuery parentQuery2 = ReasonerQueries.atomic(conjunction(parentString2, graph), graph);
Atom childAtom = childQuery.getAtom();
Atom parentAtom = parentQuery.getAtom();
Atom parentAtom2 = parentQuery2.getAtom();
List<Answer> childAnswers = childQuery.getQuery().execute();
List<Answer> parentAnswers = parentQuery.getQuery().execute();
List<Answer> parentAnswers2 = parentQuery2.getQuery().execute();
Unifier unifier = childAtom.getUnifier(parentAtom);
Unifier unifier2 = childAtom.getUnifier(parentAtom2);
assertCollectionsEqual(parentAnswers, childAnswers.stream().map(a -> a.unify(unifier)).map(a -> a.project(parentQuery.getVarNames())).distinct().collect(Collectors.toList()));
assertCollectionsEqual(parentAnswers2, childAnswers.stream().map(a -> a.unify(unifier2)).map(a -> a.project(parentQuery2.getVarNames())).distinct().collect(Collectors.toList()));
}
use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.
the class PatternImplTest method testCNFToDNF.
@Test
public void testCNFToDNF() {
Conjunction cnf = conjunction(disjunction(x, y, z), disjunction(a, b, c));
Set<Conjunction<VarPatternAdmin>> dnf = set(conjunction(x, a), conjunction(x, b), conjunction(x, c), conjunction(y, a), conjunction(y, b), conjunction(y, c), conjunction(z, a), conjunction(z, b), conjunction(z, c));
assertHasDNF(dnf, cnf);
}
use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.
the class TypeInferenceQueryTest method typedAnswers.
private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
List<Answer> answers = new ArrayList<>();
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
for (Type type : possibleTypes) {
GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
}
return answers;
}
use of ai.grakn.graql.admin.Conjunction 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();
}
}
Aggregations