Search in sources :

Example 1 with CanContain

use of org.kie.workbench.common.stunner.core.rule.impl.CanContain 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 2 with CanContain

use of org.kie.workbench.common.stunner.core.rule.impl.CanContain in project kie-wb-common by kiegroup.

the class RuleManagerImplTest method setup.

@Before
public void setup() throws Exception {
    rule1 = new CanContain("r1", "r1-cId", new HashSet<String>(2) {

        {
            add("role1");
            add("role2");
        }
    });
    rule2 = new CanContain("r2", "r2-cId", new HashSet<String>(2) {

        {
            add("role1");
            add("role2");
        }
    });
    ruleEvaluationContext = RuleContextBuilder.DomainContexts.containment(Collections.singleton("r1-cId"), new HashSet<String>(1) {

        {
            add("r2-cId");
        }
    });
    ruleViolation1 = new RuleViolationImpl("error - v1");
    ruleViolation2 = new RuleViolationImpl("error - v2");
    ruleViolation3 = new RuleViolationImpl("error - v3");
    ruleViolations1 = new DefaultRuleViolations().addViolation(ruleViolation1);
    ruleViolations2 = new DefaultRuleViolations().addViolation(ruleViolation2);
    ruleViolations3 = new DefaultRuleViolations().addViolation(ruleViolation3);
    ruleExtension = new RuleExtension("re1", "cId").setHandlerType(RuleExtensionHandler.class);
    ruleSet = new RuleSetImpl("rs1", new ArrayList<Rule>(2) {

        {
            add(rule1);
            add(rule2);
            add(ruleExtension);
        }
    });
    handlers.add(handler1);
    handlers.add(handler2);
    when(registryFactory.newRuleHandlerRegistry()).thenReturn(registry);
    when(registry.getHandlersByContext(any(Class.class))).thenReturn(handlers);
    when(registry.getExtensionHandler(eq(RuleExtensionHandler.class))).thenReturn(extensionHandler);
    when(handler1.getRuleType()).thenReturn(CanContain.class);
    when(handler1.getContextType()).thenReturn(ContainmentContext.class);
    when(handler2.getRuleType()).thenReturn(CanContain.class);
    when(handler2.getContextType()).thenReturn(ContainmentContext.class);
    when(extensionHandler.getRuleType()).thenReturn(RuleExtension.class);
    when(extensionHandler.getContextType()).thenReturn(ContainmentContext.class);
    tested = new RuleManagerImpl(registryFactory);
}
Also used : CanContain(org.kie.workbench.common.stunner.core.rule.impl.CanContain) RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) RuleExtensionHandler(org.kie.workbench.common.stunner.core.rule.ext.RuleExtensionHandler) ArrayList(java.util.ArrayList) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) HashSet(java.util.HashSet) RuleExtension(org.kie.workbench.common.stunner.core.rule.ext.RuleExtension) Before(org.junit.Before)

Aggregations

CanContain (org.kie.workbench.common.stunner.core.rule.impl.CanContain)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 RuleExtension (org.kie.workbench.common.stunner.core.rule.ext.RuleExtension)1 RuleExtensionHandler (org.kie.workbench.common.stunner.core.rule.ext.RuleExtensionHandler)1 CanConnect (org.kie.workbench.common.stunner.core.rule.impl.CanConnect)1 EdgeOccurrences (org.kie.workbench.common.stunner.core.rule.impl.EdgeOccurrences)1 Occurrences (org.kie.workbench.common.stunner.core.rule.impl.Occurrences)1 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)1 RuleViolationImpl (org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl)1