Search in sources :

Example 46 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class AtomicQueryTest method testWhenCopying_TheCopyIsAlphaEquivalent.

@Test
public void testWhenCopying_TheCopyIsAlphaEquivalent() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{($x, $y) isa is-located-in;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    ReasonerAtomicQuery atomicQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery copy = ReasonerQueries.atomic(atomicQuery);
    assertEquals(atomicQuery, copy);
    assertEquals(atomicQuery.hashCode(), copy.hashCode());
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 47 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.

// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;($x1, $x2) isa binary;}";
    String patternString2 = "{$y1 isa twoRoleEntity;($y1, $y2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 48 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsRoles_ParentRepeatsRPs.

@Test
public void testExactUnification_TernaryRelation_ParentRepeatsRoles_ParentRepeatsRPs() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{(role1: $x, role1: $x, role2: $y) isa ternary;}";
    String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
    String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
    String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
    String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
    Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
    Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
    Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
    Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
    Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
    ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
    ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
    MultiUnifier emptyUnifier = childQuery.getMultiUnifier(parentQuery);
    MultiUnifier emptyUnifier2 = childQuery2.getMultiUnifier(parentQuery);
    assertTrue(emptyUnifier.isEmpty());
    assertTrue(emptyUnifier2.isEmpty());
    Unifier unifier = childQuery3.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
    assertTrue(unifier.containsAll(correctUnifier));
    Unifier unifier2 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("y")));
    assertTrue(unifier2.containsAll(correctUnifier2));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 49 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class QueryTest method testWhenReifyingRelation_ExtraAtomIsCreatedWithUserDefinedName.

@Test
public void testWhenReifyingRelation_ExtraAtomIsCreatedWithUserDefinedName() {
    EmbeddedGraknTx<?> graph = geoKB.tx();
    String patternString = "{(geo-entity: $x, entity-location: $y) isa is-located-in;}";
    String patternString2 = "{($x, $y) relates geo-entity;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerQueryImpl query = ReasonerQueries.create(pattern, graph);
    ReasonerQueryImpl query2 = ReasonerQueries.create(pattern2, graph);
    assertEquals(query.getAtoms(RelationshipAtom.class).findFirst().orElse(null).isUserDefined(), false);
    assertEquals(query2.getAtoms(RelationshipAtom.class).findFirst().orElse(null).isUserDefined(), true);
    assertEquals(query.getAtoms().size(), 1);
    assertEquals(query2.getAtoms().size(), 2);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Test(org.junit.Test)

Example 50 with VarPatternAdmin

use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.

the class RelationshipProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // set varName as user defined if reified
    // reified if contains more properties than the RelationshipProperty itself and potential IsaProperty
    boolean isReified = var.getProperties().filter(prop -> !RelationshipProperty.class.isInstance(prop)).anyMatch(prop -> !AbstractIsaProperty.class.isInstance(prop));
    VarPattern relVar = isReified ? var.var().asUserDefined() : var.var();
    for (RelationPlayer rp : relationPlayers()) {
        VarPattern rolePattern = rp.getRole().orElse(null);
        VarPattern rolePlayer = rp.getRolePlayer();
        if (rolePattern != null) {
            Var roleVar = rolePattern.admin().var();
            // look for indirect role definitions
            IdPredicate roleId = getUserDefinedIdPredicate(roleVar, vars, parent);
            if (roleId != null) {
                Concept concept = parent.tx().getConcept(roleId.getPredicate());
                if (concept != null) {
                    if (concept.isRole()) {
                        Label roleLabel = concept.asSchemaConcept().getLabel();
                        rolePattern = roleVar.label(roleLabel);
                    } else {
                        throw GraqlQueryException.nonRoleIdAssignedToRoleVariable(var);
                    }
                }
            }
            relVar = relVar.rel(rolePattern, rolePlayer);
        } else
            relVar = relVar.rel(rolePlayer);
    }
    // isa part
    AbstractIsaProperty isaProp = var.getProperty(AbstractIsaProperty.class).orElse(null);
    IdPredicate predicate = null;
    // if no isa property present generate type variable
    Var typeVariable = isaProp != null ? isaProp.type().var() : Graql.var();
    // Isa present
    if (isaProp != null) {
        VarPatternAdmin isaVar = isaProp.type();
        Label label = isaVar.getTypeLabel().orElse(null);
        if (label != null) {
            predicate = IdPredicate.create(typeVariable, label, parent);
        } else {
            typeVariable = isaVar.var();
            predicate = getUserDefinedIdPredicate(typeVariable, vars, parent);
        }
    }
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    relVar = isaProp instanceof DirectIsaProperty ? relVar.directIsa(typeVariable.asUserDefined()) : relVar.isa(typeVariable.asUserDefined());
    return RelationshipAtom.create(relVar.admin(), typeVariable, predicateId, parent);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Role(ai.grakn.concept.Role) EquivalentFragmentSets.rolePlayer(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.rolePlayer) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Graql(ai.grakn.graql.Graql) HashSet(java.util.HashSet) UniqueVarProperty(ai.grakn.graql.admin.UniqueVarProperty) Label(ai.grakn.concept.Label) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) QueryOperationExecutor(ai.grakn.graql.internal.query.runner.QueryOperationExecutor) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) VarPattern(ai.grakn.graql.VarPattern) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Aggregations

VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)55 Test (org.junit.Test)20 Var (ai.grakn.graql.Var)17 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)16 Unifier (ai.grakn.graql.admin.Unifier)15 ConceptId (ai.grakn.concept.ConceptId)13 Label (ai.grakn.concept.Label)13 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)13 Role (ai.grakn.concept.Role)11 GraknTx (ai.grakn.GraknTx)10 VarProperty (ai.grakn.graql.admin.VarProperty)10 Collection (java.util.Collection)10 Set (java.util.Set)10 Stream (java.util.stream.Stream)10 SchemaConcept (ai.grakn.concept.SchemaConcept)9 GraqlQueryException (ai.grakn.exception.GraqlQueryException)9 VarPattern (ai.grakn.graql.VarPattern)9 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)9 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)9