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;
}
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;
}
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);
}
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);
}
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"));
}
Aggregations