Search in sources :

Example 51 with Label

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);
}
Also used : Label(ai.grakn.concept.Label) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) Test(org.junit.Test)

Example 52 with Label

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);
}
Also used : Label(ai.grakn.concept.Label) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) Test(org.junit.Test)

Example 53 with Label

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));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) Set(java.util.Set) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) Assert.assertTrue(org.junit.Assert.assertTrue) Type(ai.grakn.concept.Type) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SessionContext(ai.grakn.test.rule.SessionContext) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) GraknTx(ai.grakn.GraknTx) GraknTxTinker(ai.grakn.kb.internal.GraknTxTinker) ClassRule(org.junit.ClassRule) Collectors.toSet(java.util.stream.Collectors.toSet) Assert.assertEquals(org.junit.Assert.assertEquals) GraknTx(ai.grakn.GraknTx) Label(ai.grakn.concept.Label) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 54 with Label

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());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 55 with Label

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());
        }
    });
}
Also used : HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) RelationshipType(ai.grakn.concept.RelationshipType) Label(ai.grakn.concept.Label) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType)

Aggregations

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10