Search in sources :

Example 36 with RuleViolations

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

the class ElementCardinalityEvaluationHandlerTest method testMax1Ok.

@Test
@SuppressWarnings("unchecked")
public void testMax1Ok() {
    final Map<String, Integer> count = new HashMap<String, Integer>(2) {

        {
            put(CANDIDATE_ROLE1, 0);
            put(CANDIDATE_ROLE2, 0);
        }
    };
    doReturn(count).when(tested).countLabels(any(Graph.class), anySet());
    when(context.getCandidate()).thenReturn(Optional.of(candidate));
    when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.ADD));
    final RuleViolations violations = tested.evaluate(RULE_MAX_1, context);
    assertNotNull(violations);
    assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext());
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) HashMap(java.util.HashMap) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Test(org.junit.Test)

Example 37 with RuleViolations

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

the class ElementCardinalityEvaluationHandlerTest method testMin1DeleteOk.

@Test
@SuppressWarnings("unchecked")
public void testMin1DeleteOk() {
    final Map<String, Integer> count = new HashMap<String, Integer>(1) {

        {
            put(CANDIDATE_ROLE1, 2);
        }
    };
    doReturn(count).when(tested).countLabels(any(Graph.class), anySet());
    when(context.getCandidate()).thenReturn(Optional.of(candidate));
    when(context.getOperation()).thenReturn(Optional.of(CardinalityContext.Operation.DELETE));
    final RuleViolations violations = tested.evaluate(RULE_MIN_1, context);
    assertNotNull(violations);
    assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext());
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) HashMap(java.util.HashMap) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Test(org.junit.Test)

Example 38 with RuleViolations

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

the class NodeDockingEvaluationHandlerTest method testEvaluateSuccess.

@Test
@SuppressWarnings("unchecked")
public void testEvaluateSuccess() {
    when(context.getCandidate()).thenReturn(candidate);
    final RuleViolations violations = tested.evaluate(RULE, context);
    assertNotNull(violations);
    assertFalse(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext());
}
Also used : RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Test(org.junit.Test)

Example 39 with RuleViolations

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

the class NodeDockingEvaluationHandlerTest method testEvaluateFailed.

@Test
@SuppressWarnings("unchecked")
public void testEvaluateFailed() {
    when(context.getCandidate()).thenReturn(candidate);
    final RuleViolations violations = tested.evaluate(RULE_INVALID, context);
    assertNotNull(violations);
    assertTrue(violations.violations(RuleViolation.Type.ERROR).iterator().hasNext());
}
Also used : RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) Test(org.junit.Test)

Example 40 with RuleViolations

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

the class AbstractElementBuilderControl method allows.

@Override
@SuppressWarnings("unchecked")
public boolean allows(final ElementBuildRequest<AbstractCanvasHandler> request) {
    final double x = request.getX();
    final double y = request.getY();
    final Object definition = request.getDefinition();
    final Node<View<?>, Edge> parent = getParent(x, y);
    final Set<String> labels = clientDefinitionManager.adapters().forDefinition().getLabels(definition);
    final RuleSet ruleSet = canvasHandler.getRuleSet();
    // Check containment rules.
    if (null != parent) {
        final Object parentDef = parent.getContent().getDefinition();
        final Set<String> parentLabels = clientDefinitionManager.adapters().forDefinition().getLabels(parentDef);
        final RuleViolations containmentViolations = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.containment(parentLabels, labels));
        if (!isValid(containmentViolations)) {
            return false;
        }
    }
    // Check cardinality rules.
    final Map<String, Integer> graphLabelCount = GraphUtils.getLabelsCount(canvasHandler.getDiagram().getGraph(), labels);
    final DefaultRuleViolations cardinalityViolations = new DefaultRuleViolations();
    labels.forEach(role -> {
        final Integer roleCount = Optional.ofNullable(graphLabelCount.get(role)).orElse(0);
        final RuleViolations violations = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.cardinality(Collections.singleton(role), roleCount, Optional.of(CardinalityContext.Operation.ADD)));
        cardinalityViolations.addViolations(violations);
    });
    labels.stream().forEach(role -> {
    });
    return isValid(cardinalityViolations);
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations) DefaultRuleViolations(org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Aggregations

RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)76 Test (org.junit.Test)68 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)20 DefaultRuleViolations (org.kie.workbench.common.stunner.core.rule.violations.DefaultRuleViolations)19 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)16 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)14 Edge (org.kie.workbench.common.stunner.core.graph.Edge)13 Graph (org.kie.workbench.common.stunner.core.graph.Graph)13 Node (org.kie.workbench.common.stunner.core.graph.Node)12 HashMap (java.util.HashMap)11 ContainmentRuleViolation (org.kie.workbench.common.stunner.core.rule.violations.ContainmentRuleViolation)11 HashSet (java.util.HashSet)8 List (java.util.List)7 Collection (java.util.Collection)3 AbstractGraphDefinitionTypesTest (org.kie.workbench.common.stunner.core.AbstractGraphDefinitionTypesTest)3 NodeImpl (org.kie.workbench.common.stunner.core.graph.impl.NodeImpl)3 TreeTraverseCallback (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeTraverseCallback)3 ArrayList (java.util.ArrayList)2 View (org.kie.workbench.common.stunner.core.graph.content.view.View)2 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)2