use of org.kie.workbench.common.stunner.core.rule.impl.CanConnect in project kie-wb-common by kiegroup.
the class CommonLookups method getConnectionRulesAllowedEdges.
/**
* Returns the allowed edge identifiers that satisfy connection rules for the given
* source definition.
* @oaram sourceDefinition The domain model object ( not a graph element ).
*/
private <T> Set<String> getConnectionRulesAllowedEdges(final String defSetId, final T sourceDefinition, final int page, final int pageSize) {
final List<Rule> rules = lookupConnectionRules(defSetId, sourceDefinition, null, page, pageSize);
if (null != rules && !rules.isEmpty()) {
final Set<String> result = new LinkedHashSet<>();
for (final Rule rule : rules) {
final CanConnect cr = (CanConnect) rule;
final String edgeId = cr.getRole();
result.add(edgeId);
}
return result;
}
return null;
}
use of org.kie.workbench.common.stunner.core.rule.impl.CanConnect in project kie-wb-common by kiegroup.
the class RuleLookupManagerImpl method matches.
@Override
protected boolean matches(final String key, final String value, final Rule rule) {
switch(key) {
case TYPE:
return CONTAINMENT.equals(value) && (rule instanceof CanContain) || CONNECTION.equals(value) && (rule instanceof CanConnect) || CARDINALITY.equals(value) && (rule instanceof Occurrences) || EDGECARDINALITY.equals(value) && (rule instanceof EdgeOccurrences);
case ROLES:
try {
// Permitted roles on containment rules.
final CanContain cr = (CanContain) rule;
final Set<String> rolesSet = toSet(value);
if (null != rolesSet) {
return isIntersect(cr.getAllowedRoles(), rolesSet);
}
} catch (final ClassCastException e) {
return false;
}
return true;
case ID:
String _id = null;
if (rule instanceof CanContain) {
final CanContain er = (CanContain) rule;
_id = er.getRole();
} else if (rule instanceof CanConnect) {
final CanConnect er = (CanConnect) rule;
_id = er.getRole();
}
return _id != null && _id.equals(value);
case ROLE:
if (rule instanceof EdgeOccurrences) {
final EdgeOccurrences er = (EdgeOccurrences) rule;
return (er.getRole().equals(value));
}
return false;
case ROLE_IN:
if (rule instanceof EdgeOccurrences) {
final EdgeOccurrences er = (EdgeOccurrences) rule;
final Set<String> set = toSet(value);
if (null != set && !set.isEmpty()) {
for (final String s : set) {
if (er.getRole().equals(value)) {
return true;
}
}
}
}
return false;
case EDGE_TYPE:
try {
final EdgeOccurrences er = (EdgeOccurrences) rule;
return er.getDirection().equals(INCOMING.equals(value) ? EdgeCardinalityContext.Direction.INCOMING : EdgeCardinalityContext.Direction.OUTGOING);
} catch (final ClassCastException e) {
return false;
}
case FROM:
case TO:
// Connection rules.
try {
final CanConnect cr = (CanConnect) rule;
final Set<String> fromSet = toSet(value);
Set<String> ruleSet = getRoles(cr.getPermittedConnections(), FROM.equals(key));
if (null != fromSet) {
return isIntersect(fromSet, ruleSet);
}
} catch (final Exception e) {
return false;
}
}
throw new UnsupportedOperationException("Cannot filter rules by key [" + key + "]");
}
use of org.kie.workbench.common.stunner.core.rule.impl.CanConnect in project kie-wb-common by kiegroup.
the class CommonLookups method getConnectionRulesAllowedTargets.
/**
* Returns the allowed ROLES that satisfy connection rules for a given source
* definition ( domain model object, not a node ).and the given edge (connector) identifier.
* <p>
* TODO: Handle several result pages.
*/
private <T> Set<String> getConnectionRulesAllowedTargets(final String defSetId, final T sourceDefinition, final String edgeId, final int page, final int pageSize) {
final List<Rule> rules = lookupConnectionRules(defSetId, sourceDefinition, edgeId, page, pageSize);
if (null != rules && !rules.isEmpty()) {
final Set<String> result = new LinkedHashSet<>();
final Set<String> sourceDefLabels = getDefinitionLabels(sourceDefinition);
for (final Rule rule : rules) {
final CanConnect cr = (CanConnect) rule;
final List<CanConnect.PermittedConnection> connections = cr.getPermittedConnections();
if (null != connections && !connections.isEmpty()) {
for (final CanConnect.PermittedConnection connection : connections) {
if (sourceDefLabels != null && sourceDefLabels.contains(connection.getStartRole())) {
result.add(connection.getEndRole());
}
}
}
}
return result;
}
return null;
}
use of org.kie.workbench.common.stunner.core.rule.impl.CanConnect in project kie-wb-common by kiegroup.
the class ConnectionEvaluationHandler method evaluate.
@Override
public RuleViolations evaluate(final CanConnect rule, final ConnectionContext context) {
final List<CanConnect.PermittedConnection> permittedConnections = rule.getPermittedConnections();
final String currentConnectorRole = context.getConnectorRole();
final Set<String> incomingLabels = context.getTargetRoles().orElse(Collections.emptySet());
final Set<String> outgoingLabels = context.getSourceRoles().orElse(Collections.emptySet());
final DefaultRuleViolations results = new DefaultRuleViolations();
final Set<Pair<String, String>> couples = new LinkedHashSet<>();
for (CanConnect.PermittedConnection pc : permittedConnections) {
final boolean startMatch = outgoingLabels.contains(pc.getStartRole());
final boolean endMatch = startMatch && incomingLabels.contains(pc.getEndRole());
if (endMatch) {
return results;
}
couples.add(new Pair<>(pc.getStartRole(), pc.getEndRole()));
}
results.addViolation(new ConnectionRuleViolation(currentConnectorRole, serializeAllowedConnections(couples)));
return results;
}
use of org.kie.workbench.common.stunner.core.rule.impl.CanConnect in project kie-wb-common by kiegroup.
the class GraphConnectionEvaluationHandler method evaluate.
@Override
public RuleViolations evaluate(final CanConnect rule, final GraphConnectionContext context) {
final Edge<? extends View<?>, ? extends Node> connector = context.getConnector();
final Node<? extends View<?>, ? extends Edge> source = context.getSource().orElse(null);
final Node<? extends View<?>, ? extends Edge> target = context.getTarget().orElse(null);
if (source == null || target == null) {
return new DefaultRuleViolations();
}
final Set<String> edgeLabels = evalUtils.getLabels(connector);
final Optional<Set<String>> sourceLabels = Optional.of(evalUtils.getLabels(source));
final Optional<Set<String>> targetLabels = Optional.of(evalUtils.getLabels(target));
final DefaultRuleViolations result = new DefaultRuleViolations();
edgeLabels.stream().filter(pr -> rule.getRole().equals(pr)).forEach(pr -> result.addViolations(connectionEvaluationHandler.evaluate(rule, RuleContextBuilder.DomainContexts.connection(pr, sourceLabels, targetLabels))));
return GraphEvaluationHandlerUtils.addViolationsSourceUUID(connector.getUUID(), result);
}
Aggregations