Search in sources :

Example 51 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class GreedyTraversalPlan method getRelationshipRolePlayerMap.

private static Multimap<Var, Var> getRelationshipRolePlayerMap(Set<Fragment> allFragments, Multimap<Var, Type> instanceVarTypeMap) {
    // relationship vars and its role player vars
    Multimap<Var, Var> relationshipRolePlayerMap = HashMultimap.create();
    allFragments.stream().filter(OutRolePlayerFragment.class::isInstance).forEach(fragment -> relationshipRolePlayerMap.put(fragment.start(), fragment.end()));
    // find all the relationships requiring type inference
    Iterator<Var> iterator = relationshipRolePlayerMap.keySet().iterator();
    while (iterator.hasNext()) {
        Var relationship = iterator.next();
        // the relation should have at least 2 known role players so we can infer something useful
        if (instanceVarTypeMap.containsKey(relationship) || relationshipRolePlayerMap.get(relationship).size() < 2) {
            iterator.remove();
        } else {
            int numRolePlayersHaveType = 0;
            for (Var rolePlayer : relationshipRolePlayerMap.get(relationship)) {
                if (instanceVarTypeMap.containsKey(rolePlayer)) {
                    numRolePlayersHaveType++;
                }
            }
            if (numRolePlayersHaveType < 2) {
                iterator.remove();
            }
        }
    }
    return relationshipRolePlayerMap;
}
Also used : Var(ai.grakn.graql.Var)

Example 52 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class Fragment method applyTraversal.

/**
 * @param traversal the traversal to extend with this Fragment
 * @param tx     the graph to execute the traversal on
 */
public final GraphTraversal<Vertex, ? extends Element> applyTraversal(GraphTraversal<Vertex, ? extends Element> traversal, EmbeddedGraknTx<?> tx, Collection<Var> vars, @Nullable Var currentVar) {
    if (currentVar != null) {
        if (!currentVar.equals(start())) {
            if (vars.contains(start())) {
                // If the variable name has been visited but the traversal is not at that variable name, select it
                traversal.select(start().name());
            } else {
                // Restart traversal when fragments are disconnected
                traversal.V().as(start().name());
            }
        }
    } else {
        // If the variable name has not been visited yet, remember it and use the 'as' step
        traversal.as(start().name());
    }
    vars.add(start());
    traversal = applyTraversalInner(traversal, tx, vars);
    Var end = end();
    if (end != null) {
        assignVar(traversal, end, vars);
    }
    vars.addAll(vars());
    return traversal;
}
Also used : Var(ai.grakn.graql.Var)

Example 53 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class RelatesProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    Var varName = var.var().asUserDefined();
    VarPatternAdmin roleVar = this.role();
    Var roleVariable = roleVar.var().asUserDefined();
    IdPredicate predicate = getIdPredicate(roleVariable, roleVar, vars, parent);
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    return RelatesAtom.create(varName, roleVariable, predicateId, parent);
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId)

Example 54 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class HasAttributeTypeProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // NB: HasResourceType is a special case and it doesn't allow variables as resource types
    Var varName = var.var().asUserDefined();
    Label label = this.resourceType().getTypeLabel().orElse(null);
    Var predicateVar = var().asUserDefined();
    SchemaConcept schemaConcept = parent.tx().getSchemaConcept(label);
    ConceptId predicateId = schemaConcept != null ? schemaConcept.getId() : null;
    // isa part
    VarPatternAdmin resVar = varName.has(Graql.label(label)).admin();
    return HasAtom.create(resVar, predicateVar, predicateId, parent);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) ConceptId(ai.grakn.concept.ConceptId)

Example 55 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class AtomicTest method testRewriteAndUnification.

@Test
public void testRewriteAndUnification() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String parentString = "{$r (subRole1: $x) isa binary;}";
    Atom parentAtom = ReasonerQueries.atomic(conjunction(parentString, graph), graph).getAtom();
    Var parentVarName = parentAtom.getVarName();
    String childPatternString = "(subRole1: $x, subRole2: $y) isa binary";
    InferenceRule testRule = new InferenceRule(graph.putRule("Checking Rewrite & Unification", graph.graql().parser().parsePattern(childPatternString), graph.graql().parser().parsePattern(childPatternString)), graph).rewrite(parentAtom);
    RelationshipAtom headAtom = (RelationshipAtom) testRule.getHead().getAtom();
    Var headVarName = headAtom.getVarName();
    Unifier unifier = Iterables.getOnlyElement(testRule.getMultiUnifier(parentAtom));
    Unifier correctUnifier = new UnifierImpl(ImmutableMap.of(var("x"), var("x"), headVarName, parentVarName));
    assertTrue(unifier.containsAll(correctUnifier));
    Multimap<Role, Var> roleMap = roleSetMap(headAtom.getRoleVarMap());
    Collection<Var> wifeEntry = roleMap.get(graph.getRole("subRole1"));
    assertEquals(wifeEntry.size(), 1);
    assertEquals(wifeEntry.iterator().next(), var("x"));
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17