use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class AtomicTest method testUnification_IndirectRoles.
@Test
public void testUnification_IndirectRoles() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
VarPatternAdmin basePattern = var().rel(var("role1").label("subRole1"), var("y1")).rel(var("role2").label("subSubRole2"), var("y2")).isa("binary").admin();
ReasonerAtomicQuery baseQuery = ReasonerQueries.atomic(Patterns.conjunction(Sets.newHashSet(basePattern)), graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction("{($r1: $x1, $r2: $x2) isa binary;" + "$r1 label 'subRole1';" + "$r2 label 'subSubRole2';}", graph), graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction("{($R1: $x, $R2: $y) isa binary;" + "$R1 label 'subRole1';" + "$R2 label 'subSubRole2';}", graph), graph);
exactUnification(parentQuery, childQuery, true, true);
exactUnification(baseQuery, parentQuery, true, true);
exactUnification(baseQuery, childQuery, true, true);
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class InsertQueryPropertyTest method anInsertQueryWithoutAnIsaProperty_CannotBeInserted.
@Ignore("Currently no error message is returned when trying to insert an empty set of propoerties. I am not entirely sure this is correct")
@Property
public void anInsertQueryWithoutAnIsaProperty_CannotBeInserted(@Open GraknTx tx, @Size(max = 5) Set<VarProperty> properties) {
boolean containsIsa = properties.stream().anyMatch(IsaProperty.class::isInstance);
assumeFalse(containsIsa);
VarPatternAdmin pattern = Patterns.varPattern(Graql.var("x"), properties);
exception.expect(GraqlQueryException.class);
tx.graql().insert(pattern).execute();
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipAtom method getRoleVarMap.
/**
* @return map containing roleType - (rolePlayer var - rolePlayer type) pairs
*/
@Memoized
public Multimap<Role, Var> getRoleVarMap() {
ImmutableMultimap.Builder<Role, Var> builder = ImmutableMultimap.builder();
GraknTx graph = getParentQuery().tx();
getRelationPlayers().forEach(c -> {
Var varName = c.getRolePlayer().var();
VarPatternAdmin rolePattern = c.getRole().orElse(null);
if (rolePattern != null) {
// try directly
Label typeLabel = rolePattern.getTypeLabel().orElse(null);
Role role = typeLabel != null ? graph.getRole(typeLabel.getValue()) : null;
// try indirectly
if (role == null && rolePattern.var().isUserDefinedName()) {
IdPredicate rolePredicate = getIdPredicate(rolePattern.var());
if (rolePredicate != null) {
Role r = graph.getConcept(rolePredicate.getPredicate());
if (r == null)
throw GraqlQueryException.idNotFound(rolePredicate.getPredicate());
role = r;
}
}
if (role != null)
builder.put(role, varName);
}
});
return builder.build();
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipAtom method getRoleRelationPlayerMap.
private Multimap<Role, RelationPlayer> getRoleRelationPlayerMap() {
Multimap<Role, RelationPlayer> roleRelationPlayerMap = ArrayListMultimap.create();
Multimap<Role, Var> roleVarMap = getRoleVarMap();
List<RelationPlayer> relationPlayers = getRelationPlayers();
roleVarMap.asMap().forEach((role, value) -> {
Label roleLabel = role.getLabel();
relationPlayers.stream().filter(rp -> rp.getRole().isPresent()).forEach(rp -> {
VarPatternAdmin roleTypeVar = rp.getRole().orElse(null);
Label rl = roleTypeVar != null ? roleTypeVar.getTypeLabel().orElse(null) : null;
if (roleLabel != null && roleLabel.equals(rl)) {
roleRelationPlayerMap.put(role, rp);
}
});
});
return roleRelationPlayerMap;
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipAtom method rewriteWithVariableRoles.
/**
* if any {@link Role} variable of the parent is user defined rewrite ALL {@link Role} variables to user defined (otherwise unification is problematic)
* @param parentAtom parent atom that triggers rewrite
* @return new relation atom with user defined {@link Role} variables if necessary or this
*/
private RelationshipAtom rewriteWithVariableRoles(Atom parentAtom) {
if (!parentAtom.requiresRoleExpansion())
return this;
VarPattern relVar = getPattern().admin().getProperty(IsaProperty.class).map(prop -> getVarName().isa(prop.type())).orElse(getVarName());
for (RelationPlayer rp : getRelationPlayers()) {
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
if (rolePattern != null) {
Var roleVar = rolePattern.var();
Label roleLabel = rolePattern.getTypeLabel().orElse(null);
relVar = relVar.rel(roleVar.asUserDefined().label(roleLabel), rp.getRolePlayer());
} else {
relVar = relVar.rel(rp.getRolePlayer());
}
}
return create(relVar.admin(), getPredicateVariable(), getTypeId(), getParentQuery());
}
Aggregations