Search in sources :

Example 6 with RuleSet

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

the class CanvasLayoutUtils method canContain.

private boolean canContain(final CanvasHandler canvasHandler, final Node container, final Node candidate) {
    boolean canContain = true;
    NodeContainmentContext containmentContext = RuleContextBuilder.GraphContexts.containment(canvasHandler.getDiagram().getGraph(), container, candidate);
    String definitionSetId = canvasHandler.getDiagram().getMetadata().getDefinitionSetId();
    Object definitionSet = definitionManager.definitionSets().getDefinitionSetById(definitionSetId);
    RuleSet ruleSet = definitionManager.adapters().forRules().getRuleSet(definitionSet);
    RuleViolations violations = ruleManager.evaluate(ruleSet, containmentContext);
    if (violations.violations(Violation.Type.ERROR).iterator().hasNext()) {
        canContain = false;
    }
    return canContain;
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) NodeContainmentContext(org.kie.workbench.common.stunner.core.rule.context.NodeContainmentContext) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations)

Example 7 with RuleSet

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

the class CommonLookups method getAllowedTargetDefinitions.

/**
 * Returns the allowed definition identifiers that can be used as target node for the given source node and
 * the given edge (connector) identifier.
 * <p>
 * TODO: Handle several result pages.
 */
@SuppressWarnings("unchecked")
public <T> Set<Object> getAllowedTargetDefinitions(final String defSetId, final Graph<?, ? extends Node> graph, final Node<? extends Definition<T>, ? extends Edge> sourceNode, final String edgeId, final int page, final int pageSize) {
    final Set<Object> result = new LinkedHashSet<>();
    if (null != defSetId && null != graph && null != sourceNode && null != edgeId) {
        final T definition = sourceNode.getContent().getDefinition();
        final RuleSet ruleSet = getRuleSet(defSetId);
        log(Level.FINEST, "*** Checking the target definitions allowed " + "for [" + definition + "] and using the " + "connector [" + edgeId + "] ***");
        // Check outgoing connectors cardinality for the source node ( plus the new one to be added ).
        final int outConnectorsCount = countOutgoingEdges(sourceNode, edgeId);
        log(Level.FINEST, "The source node has  " + outConnectorsCount + "] outgoing connections.");
        final RuleViolations oev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(sourceNode.getLabels(), edgeId, outConnectorsCount, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD)));
        final boolean oeCardinalityAllowed = pass(oev);
        log(Level.FINEST, "Outgoing edge cardinality rules evaluation " + "result = [" + oeCardinalityAllowed + "]");
        if (oeCardinalityAllowed) {
            // Obtain allowed target roles that pass connection rules.
            final Set<String> allowedConnectionRoles = getConnectionRulesAllowedTargets(defSetId, definition, edgeId, page, pageSize);
            log(Level.FINEST, "Allowed target roles that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
            if (null != allowedConnectionRoles) {
                // Obtain a first set of candidate Defintiion identifiers.
                final Set<String> allowedDefinitions = getDefinitions(defSetId, allowedConnectionRoles);
                log(Level.FINEST, "Allowed target definitions that pass connection rules " + "ARE [" + allowedConnectionRoles + "]");
                if (null != allowedDefinitions) {
                    final Map<String, Integer> graphLabelCount = GraphUtils.getLabelsCount(graph, allowedConnectionRoles);
                    final int inConnectorsCount = countIncomingEdges(sourceNode, edgeId);
                    allowedDefinitions.stream().forEach(defId -> {
                        final Object targetDefinition = createDefinition(defId);
                        if (null != targetDefinition) {
                            final Set<String> targetDefinitionRoles = getDefinitionManager().adapters().forDefinition().getLabels(targetDefinition);
                            // Check cardinality for each of the roles for this potential target node.
                            final boolean hasCardinalityViolations = targetDefinitionRoles.stream().filter(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)));
                                return !pass(violations);
                            }).findFirst().isPresent();
                            log(Level.FINEST, "Cardinality rules evaluation " + "result = [" + hasCardinalityViolations + "]");
                            if (!hasCardinalityViolations) {
                                // Check incoming connector cardinality for each the target node.
                                final RuleViolations iev = ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(Collections.singleton(defId), edgeId, inConnectorsCount, EdgeCardinalityContext.Direction.INCOMING, Optional.of(CardinalityContext.Operation.ADD)));
                                final boolean ieCardinalityAllowed = pass(iev);
                                log(Level.FINEST, "Incoming edge cardinality rules evaluation " + "result = [" + ieCardinalityAllowed + "]");
                                if (ieCardinalityAllowed) {
                                    // This potential node can be used as target one, as it passes all rule checks.
                                    result.add(targetDefinition);
                                }
                            }
                        }
                    });
                    return result;
                }
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) RuleViolations(org.kie.workbench.common.stunner.core.rule.RuleViolations)

Example 8 with RuleSet

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

the class CommonLookups method getAllowedConnectors.

/**
 * Returns the allowed edge definition identifiers that can be added as outgoing edges for the given source node.
 */
public <T> Set<String> getAllowedConnectors(final String defSetId, final Node<? extends Definition<T>, Edge> sourceNode, final int page, final int pageSize) {
    final Set<String> result = new LinkedHashSet<>();
    if (null != defSetId && null != sourceNode) {
        final T definition = sourceNode.getContent().getDefinition();
        final Set<String> connectionAllowedEdges = getConnectionRulesAllowedEdges(defSetId, definition, page, pageSize);
        if (null != connectionAllowedEdges && !connectionAllowedEdges.isEmpty()) {
            final RuleSet ruleSet = getRuleSet(defSetId);
            connectionAllowedEdges.stream().forEach(allowedEdgeId -> {
                final int edgeCount = countOutgoingEdges(sourceNode, allowedEdgeId);
                final boolean oeCardinalityAllowed = getDefinitionLabels(definition).stream().filter(role -> pass(ruleManager.evaluate(ruleSet, RuleContextBuilder.DomainContexts.edgeCardinality(sourceNode.getLabels(), allowedEdgeId, edgeCount, EdgeCardinalityContext.Direction.OUTGOING, Optional.of(CardinalityContext.Operation.ADD))))).findAny().isPresent();
                log(Level.FINEST, "Outgoing edge cardinality rules evaluation - Result = [" + oeCardinalityAllowed + "]");
                if (oeCardinalityAllowed) {
                    result.add(allowedEdgeId);
                }
            });
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet)

Example 9 with RuleSet

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

the class TestingGraphMockHandler method init.

@SuppressWarnings("unchecked")
private TestingGraphMockHandler init() {
    MockitoAnnotations.initMocks(this);
    this.graphFactory = new GraphFactoryImpl(definitionManager);
    this.nodeFactory = new NodeFactoryImpl(definitionUtils);
    this.edgeFactory = new EdgeFactoryImpl(definitionManager);
    this.commandFactory = new GraphCommandFactory();
    this.graph = graphFactory.build(GRAPH_UUID, DEF_SET_ID);
    when(definitionUtils.getDefinitionManager()).thenReturn(definitionManager);
    when(definitionManager.definitionSets()).thenReturn(definitionSetRegistry);
    when(definitionManager.adapters()).thenReturn(adapterManager);
    when(adapterManager.registry()).thenReturn(adapterRegistry);
    when(adapterManager.forDefinition()).thenReturn(definitionAdapter);
    when(adapterManager.forProperty()).thenReturn(propertyAdapter);
    when(adapterManager.forRules()).thenReturn(ruleAdapter);
    when(adapterRegistry.getDefinitionAdapter(any(Class.class))).thenReturn(definitionAdapter);
    when(adapterRegistry.getPropertyAdapter(any(Class.class))).thenReturn(propertyAdapter);
    when(graphCommandExecutionContext.getDefinitionManager()).thenReturn(definitionManager);
    when(graphCommandExecutionContext.getFactoryManager()).thenReturn(factoryManager);
    when(graphCommandExecutionContext.getRuleManager()).thenReturn(ruleManager);
    when(graphCommandExecutionContext.getGraphIndex()).thenReturn(graphIndex);
    when(graphCommandExecutionContext.getRuleSet()).thenReturn(ruleSet);
    when(graphIndex.getGraph()).thenReturn(graph);
    when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(EMPTY_VIOLATIONS);
    return this;
}
Also used : GraphCommandFactory(org.kie.workbench.common.stunner.core.graph.command.impl.GraphCommandFactory) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) NodeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.NodeFactoryImpl) EdgeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.EdgeFactoryImpl) GraphFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.GraphFactoryImpl) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)

Example 10 with RuleSet

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

the class AbstractGraphCommandTest method init.

@SuppressWarnings("unchecked")
public void init(final double width, final double height) {
    MockitoAnnotations.initMocks(this);
    Bounds bounds = mockBounds(0, 0, width, height);
    when(graphContent.getBounds()).thenReturn(bounds);
    when(graph.getUUID()).thenReturn(GRAPH_UUID);
    when(graph.getContent()).thenReturn(graphContent);
    when(graph.nodes()).thenReturn(graphNodes);
    when(definitionManager.adapters()).thenReturn(adapterManager);
    when(adapterManager.registry()).thenReturn(adapterRegistry);
    when(adapterManager.forDefinition()).thenReturn(definitionAdapter);
    when(adapterManager.forProperty()).thenReturn(propertyAdapter);
    when(adapterRegistry.getDefinitionAdapter(any(Class.class))).thenReturn(definitionAdapter);
    when(adapterRegistry.getPropertyAdapter(any(Class.class))).thenReturn(propertyAdapter);
    when(graphCommandExecutionContext.getDefinitionManager()).thenReturn(definitionManager);
    when(graphCommandExecutionContext.getFactoryManager()).thenReturn(factoryManager);
    when(graphCommandExecutionContext.getRuleManager()).thenReturn(ruleManager);
    when(graphCommandExecutionContext.getGraphIndex()).thenReturn(graphIndex);
    when(graphCommandExecutionContext.getRuleSet()).thenReturn(ruleSet);
    when(graphIndex.getGraph()).thenReturn(graph);
    when(ruleManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(EMPTY_VIOLATIONS);
}
Also used : RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)

Aggregations

RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)10 Node (org.kie.workbench.common.stunner.core.graph.Node)4 DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)4 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)4 RuleViolations (org.kie.workbench.common.stunner.core.rule.RuleViolations)4 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 TestingGraphInstanceBuilder (org.kie.workbench.common.stunner.core.TestingGraphInstanceBuilder)3 RuleManager (org.kie.workbench.common.stunner.core.rule.RuleManager)3 Edge (org.kie.workbench.common.stunner.core.graph.Edge)2 View (org.kie.workbench.common.stunner.core.graph.content.view.View)2 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)2 EmptyConnectionViolation (org.kie.workbench.common.stunner.core.rule.violations.EmptyConnectionViolation)2 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Stack (java.util.Stack)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Logger (java.util.logging.Logger)1