Search in sources :

Example 1 with CanConnect

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Rule(org.kie.workbench.common.stunner.core.rule.Rule) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect)

Example 2 with CanConnect

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 + "]");
}
Also used : CanContain(org.kie.workbench.common.stunner.core.rule.impl.CanContain) EdgeOccurrences(org.kie.workbench.common.stunner.core.rule.impl.EdgeOccurrences) EdgeOccurrences(org.kie.workbench.common.stunner.core.rule.impl.EdgeOccurrences) Occurrences(org.kie.workbench.common.stunner.core.rule.impl.Occurrences) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect)

Example 3 with CanConnect

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Rule(org.kie.workbench.common.stunner.core.rule.Rule) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect)

Example 4 with CanConnect

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect) ConnectionRuleViolation(org.kie.workbench.common.stunner.core.rule.violations.ConnectionRuleViolation) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) Pair(org.uberfire.commons.data.Pair)

Example 5 with CanConnect

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);
}
Also used : RuleEvaluationHandler(org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Set(java.util.Set) CanConnect(org.kie.workbench.common.stunner.core.rule.impl.CanConnect) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphConnectionContext(org.kie.workbench.common.stunner.core.rule.context.GraphConnectionContext) Inject(javax.inject.Inject) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Optional(java.util.Optional) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Node(org.kie.workbench.common.stunner.core.graph.Node) Set(java.util.Set) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)

Aggregations

CanConnect (org.kie.workbench.common.stunner.core.rule.impl.CanConnect)8 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 RuleSetImpl (org.kie.workbench.common.stunner.core.rule.RuleSetImpl)3 Rule (org.kie.workbench.common.stunner.core.rule.Rule)2 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)2 Optional (java.util.Optional)1 Set (java.util.Set)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 DefinitionManager (org.kie.workbench.common.stunner.core.api.DefinitionManager)1 Edge (org.kie.workbench.common.stunner.core.graph.Edge)1 Node (org.kie.workbench.common.stunner.core.graph.Node)1 View (org.kie.workbench.common.stunner.core.graph.content.view.View)1 RuleEvaluationHandler (org.kie.workbench.common.stunner.core.rule.RuleEvaluationHandler)1 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)1 GraphConnectionContext (org.kie.workbench.common.stunner.core.rule.context.GraphConnectionContext)1 RuleContextBuilder (org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder)1 CanContain (org.kie.workbench.common.stunner.core.rule.impl.CanContain)1