use of ai.grakn.concept.Label in project grakn by graknlabs.
the class RolePlayerFragmentSetTest method whenLabelDoesNotReferToARelationType_DoNotApplyRelationTypeOptimisation.
@Test
public void whenLabelDoesNotReferToARelationType_DoNotApplyRelationTypeOptimisation() {
Label movie = Label.of("movie");
Collection<EquivalentFragmentSet> fragmentSets = Sets.newHashSet(EquivalentFragmentSets.rolePlayer(null, a, b, c, null), EquivalentFragmentSets.isa(null, a, d, true), EquivalentFragmentSets.label(null, d, ImmutableSet.of(movie)));
Collection<EquivalentFragmentSet> expected = Sets.newHashSet(fragmentSets);
RolePlayerFragmentSet.RELATION_TYPE_OPTIMISATION.apply(fragmentSets, sampleKB.tx());
assertEquals(expected, fragmentSets);
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class RolePlayerFragmentSetTest method whenRoleIsNotInGraph_DoNotApplyRoleOptimisation.
@Test
public void whenRoleIsNotInGraph_DoNotApplyRoleOptimisation() {
Label magician = Label.of("magician");
Collection<EquivalentFragmentSet> fragmentSets = Sets.newHashSet(EquivalentFragmentSets.rolePlayer(null, a, b, c, d), EquivalentFragmentSets.label(null, d, ImmutableSet.of(magician)));
Collection<EquivalentFragmentSet> expected = Sets.newHashSet(fragmentSets);
RolePlayerFragmentSet.ROLE_OPTIMISATION.apply(fragmentSets, sampleKB.tx());
assertEquals(expected, fragmentSets);
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class SampleKBLoaderTest method whenCreatingGraphWithPreLoader_EnsureGraphContainsPreLoadedEntities.
@Test
public void whenCreatingGraphWithPreLoader_EnsureGraphContainsPreLoadedEntities() {
Set<Label> labels = new HashSet<>(Arrays.asList(Label.of("1"), Label.of("2"), Label.of("3")));
Consumer<GraknTx> preLoader = graph -> labels.forEach(graph::putEntityType);
SampleKBLoader loader = SampleKBLoader.preLoad(preLoader);
try (GraknTx graph = loader.tx()) {
Set<Label> foundLabels = graph.admin().getMetaEntityType().subs().map(Type::getLabel).collect(Collectors.toSet());
assertTrue(foundLabels.containsAll(labels));
}
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class ReasoningTests method whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults.
@Test
public void whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults() {
QueryBuilder withInference = testSet14.tx().graql().infer(true);
QueryBuilder withoutInference = testSet14.tx().graql().infer(false);
VarPattern owner = label(HAS_OWNER.getLabel("resource"));
VarPattern value = label(HAS_VALUE.getLabel("resource"));
VarPattern hasRes = label(HAS.getLabel("resource"));
Function<QueryBuilder, GetQuery> query = qb -> qb.match(var().rel(owner, "x").rel(value, "y").isa(hasRes), // This pattern is added only to encourage reasoning to activate
var("a").has("resource", var("b"))).get();
Set<Answer> resultsWithoutInference = query.apply(withoutInference).stream().collect(toSet());
Set<Answer> resultsWithInference = query.apply(withInference).stream().collect(toSet());
assertThat(resultsWithoutInference, not(empty()));
assertThat(Sets.difference(resultsWithoutInference, resultsWithInference), empty());
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class GreedyTraversalPlan method inferRelationshipTypes.
private static void inferRelationshipTypes(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
Map<Var, Type> labelVarTypeMap = getLabelVarTypeMap(tx, allFragments);
if (labelVarTypeMap.isEmpty())
return;
Multimap<Var, Type> instanceVarTypeMap = getInstanceVarTypeMap(allFragments, labelVarTypeMap);
Multimap<Var, Var> relationshipRolePlayerMap = getRelationshipRolePlayerMap(allFragments, instanceVarTypeMap);
if (relationshipRolePlayerMap.isEmpty())
return;
// for each type, get all possible relationship type it could be in
Multimap<Type, RelationshipType> relationshipMap = HashMultimap.create();
labelVarTypeMap.values().stream().distinct().forEach(type -> addAllPossibleRelationships(relationshipMap, type));
// inferred labels should be kept separately, even if they are already in allFragments set
Map<Label, Var> inferredLabels = new HashMap<>();
relationshipRolePlayerMap.asMap().forEach((relationshipVar, rolePlayerVars) -> {
Set<Type> possibleRelationshipTypes = rolePlayerVars.stream().filter(instanceVarTypeMap::containsKey).map(rolePlayer -> getAllPossibleRelationshipTypes(instanceVarTypeMap.get(rolePlayer), relationshipMap)).reduce(Sets::intersection).orElse(Collections.emptySet());
// TODO: if possibleRelationshipTypes here is empty, the query will not match any data
if (possibleRelationshipTypes.size() == 1) {
Type relationshipType = possibleRelationshipTypes.iterator().next();
Label label = relationshipType.getLabel();
// add label fragment if this label has not been inferred
if (!inferredLabels.containsKey(label)) {
Var labelVar = var();
inferredLabels.put(label, labelVar);
Fragment labelFragment = Fragments.label(LabelProperty.of(label), labelVar, ImmutableSet.of(label));
allFragments.add(labelFragment);
}
// finally, add inferred isa fragments
Var labelVar = inferredLabels.get(label);
IsaProperty isaProperty = IsaProperty.of(labelVar.admin());
EquivalentFragmentSet isaEquivalentFragmentSet = EquivalentFragmentSets.isa(isaProperty, relationshipVar, labelVar, relationshipType.isImplicit());
allFragments.addAll(isaEquivalentFragmentSet.fragments());
}
});
}
Aggregations