use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class QueryAnswerStream method knownFilterWithInverse.
static boolean knownFilterWithInverse(Answer answer, Map<Pair<Var, Concept>, Set<Answer>> stream2InverseMap) {
Iterator<Map.Entry<Var, Concept>> eit = answer.entrySet().iterator();
Map.Entry<Var, Concept> entry = eit.next();
Set<Answer> matchAnswers = findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap);
while (eit.hasNext()) {
entry = eit.next();
matchAnswers = Sets.intersection(matchAnswers, findMatchingAnswers(entry.getKey(), entry.getValue(), stream2InverseMap));
}
for (Answer knownAnswer : matchAnswers) {
if (knownAnswer.entrySet().containsAll(answer.entrySet())) {
return false;
}
}
return true;
}
use of ai.grakn.concept.Concept 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.concept.Concept 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.concept.Concept in project grakn by graknlabs.
the class MigratorTestUtils method getResources.
public static Stream<Attribute> getResources(GraknTx graph, Thing thing, Label label) {
Role roleOwner = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(label));
Role roleOther = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(label));
Stream<Relationship> relations = thing.relationships(roleOwner);
return relations.flatMap(r -> r.rolePlayers(roleOther)).map(Concept::asAttribute);
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class QueryOperationExecutor method get.
/**
* Return a {@link Concept} for a given {@link Var}.
*
* <p>
* This method is expected to be called from implementations of
* {@link VarPropertyInternal#insert(Var)}, provided they return the given {@link Var} in the
* response to {@link PropertyExecutor#requiredVars()}.
* </p>
*/
public Concept get(Var var) {
var = equivalentVars.componentOf(var);
assert var != null;
@Nullable Concept concept = concepts.get(var);
if (concept == null) {
@Nullable ConceptBuilder builder = conceptBuilders.remove(var);
if (builder != null) {
concept = buildConcept(var, builder);
}
}
if (concept != null) {
return concept;
}
LOG.debug("Could not build concept for {}\nconcepts = {}\nconceptBuilders = {}", var, concepts, conceptBuilders);
throw GraqlQueryException.insertUndefinedVariable(printableRepresentation(var));
}
Aggregations