Search in sources :

Example 56 with Node

use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.

the class DMNMarshallerTest method setup.

@Before
public void setup() throws Exception {
    // Graph utils.
    when(definitionManager.adapters()).thenReturn(adapterManager);
    when(adapterManager.registry()).thenReturn(adapterRegistry);
    definitionUtils = new DefinitionUtils(definitionManager, applicationFactoryManager);
    testScopeModelFactory = new TestScopeModelFactory(new DMNDefinitionSet.DMNDefinitionSetBuilder().build());
    // Definition manager.
    final BackendDefinitionAdapter definitionAdapter = new BackendDefinitionAdapter(definitionUtils);
    final BackendDefinitionSetAdapter definitionSetAdapter = new BackendDefinitionSetAdapter(definitionAdapter);
    final BackendPropertySetAdapter propertySetAdapter = new BackendPropertySetAdapter();
    final BackendPropertyAdapter propertyAdapter = new BackendPropertyAdapter();
    when(adapterManager.forDefinitionSet()).thenReturn(definitionSetAdapter);
    when(adapterManager.forDefinition()).thenReturn(definitionAdapter);
    when(adapterManager.forPropertySet()).thenReturn(propertySetAdapter);
    when(adapterManager.forProperty()).thenReturn(propertyAdapter);
    when(adapterRegistry.getDefinitionSetAdapter(any(Class.class))).thenReturn(definitionSetAdapter);
    when(adapterRegistry.getDefinitionAdapter(any(Class.class))).thenReturn(definitionAdapter);
    when(adapterRegistry.getPropertySetAdapter(any(Class.class))).thenReturn(propertySetAdapter);
    when(adapterRegistry.getPropertyAdapter(any(Class.class))).thenReturn(propertyAdapter);
    commandManager = new GraphCommandManagerImpl(null, null, null);
    commandFactory = new GraphCommandFactory();
    connectionEdgeFactory = new EdgeFactoryImpl(definitionManager);
    viewNodeFactory = new NodeFactoryImpl(definitionUtils);
    dmnGraphFactory = new GraphFactoryImpl(definitionManager);
    doAnswer(invocationOnMock -> {
        String id = (String) invocationOnMock.getArguments()[0];
        return testScopeModelFactory.build(id);
    }).when(applicationFactoryManager).newDefinition(anyString());
    doAnswer(invocationOnMock -> {
        String uuid = (String) invocationOnMock.getArguments()[0];
        String id = (String) invocationOnMock.getArguments()[1];
        if (DMNDefinitionSet.class.getName().equals(id)) {
            // Emulate DMNGraphFactoryImpl, that adds a DMNDiagram to new Graphs
            // Please note this is different from the stunner jbpm test which this dmn test is based on
            Graph graph = (Graph) dmnGraphFactory.build(uuid, DMN_DEF_SET_ID);
            DMNDiagram model = new DMNDiagram.DMNDiagramBuilder().build();
            Node node = viewNodeFactory.build(uuid, model);
            graph.addNode(node);
            return graph;
        }
        Object model = testScopeModelFactory.accepts(id) ? testScopeModelFactory.build(id) : null;
        if (null != model) {
            Class<? extends ElementFactory> element = BackendDefinitionAdapter.getGraphFactory(model.getClass());
            if (element.isAssignableFrom(NodeFactory.class)) {
                Node node = viewNodeFactory.build(uuid, model);
                return node;
            } else if (element.isAssignableFrom(EdgeFactory.class)) {
                Edge edge = connectionEdgeFactory.build(uuid, model);
                return edge;
            }
        }
        return null;
    }).when(applicationFactoryManager).newElement(anyString(), anyString());
    doAnswer(invocationOnMock -> {
        String uuid = (String) invocationOnMock.getArguments()[0];
        Class type = (Class) invocationOnMock.getArguments()[1];
        String id = BindableAdapterUtils.getGenericClassName(type);
        if (DMNDefinitionSet.class.equals(type)) {
            Graph graph = (Graph) dmnGraphFactory.build(uuid, DMN_DEF_SET_ID);
            return graph;
        }
        Object model = testScopeModelFactory.accepts(id) ? testScopeModelFactory.build(id) : null;
        if (null != model) {
            Class<? extends ElementFactory> element = BackendDefinitionAdapter.getGraphFactory(model.getClass());
            if (element.isAssignableFrom(NodeFactory.class)) {
                Node node = viewNodeFactory.build(uuid, model);
                return node;
            } else if (element.isAssignableFrom(EdgeFactory.class)) {
                Edge edge = connectionEdgeFactory.build(uuid, model);
                return edge;
            }
        }
        return null;
    }).when(applicationFactoryManager).newElement(anyString(), any(Class.class));
    doAnswer(invocationOnMock -> {
        String uuid = (String) invocationOnMock.getArguments()[0];
        String defSetId = (String) invocationOnMock.getArguments()[1];
        final Graph graph = (Graph) applicationFactoryManager.newElement(uuid, defSetId);
        final DiagramImpl result = new DiagramImpl(uuid, new MetadataImpl.MetadataImplBuilder(defSetId).build());
        result.setGraph(graph);
        return result;
    }).when(applicationFactoryManager).newDiagram(anyString(), anyString(), any(Metadata.class));
    MappingContextSingleton.loadDynamicMarshallers();
}
Also used : BackendPropertyAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertyAdapter) NodeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.NodeFactoryImpl) BackendDefinitionAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionAdapter) Node(org.kie.workbench.common.stunner.core.graph.Node) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) GraphFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.GraphFactoryImpl) Matchers.anyString(org.mockito.Matchers.anyString) BackendDefinitionSetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionSetAdapter) BackendPropertySetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertySetAdapter) DMNDiagram(org.kie.workbench.common.dmn.api.definition.v1_1.DMNDiagram) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) GraphCommandManagerImpl(org.kie.workbench.common.stunner.core.graph.command.GraphCommandManagerImpl) GraphCommandFactory(org.kie.workbench.common.stunner.core.graph.command.impl.GraphCommandFactory) TestScopeModelFactory(org.kie.workbench.common.stunner.backend.definition.factory.TestScopeModelFactory) Graph(org.kie.workbench.common.stunner.core.graph.Graph) EdgeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.EdgeFactoryImpl) DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) EdgeFactory(org.kie.workbench.common.stunner.core.factory.graph.EdgeFactory) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Before(org.junit.Before)

Example 57 with Node

use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.

the class DMNMarshallerTest method test_function_java_WB_model.

@Test
public void test_function_java_WB_model() throws IOException {
    final DMNMarshaller m = new DMNMarshaller(new XMLEncoderDiagramMetadataMarshaller(), applicationFactoryManager);
    @SuppressWarnings("unchecked") final Graph<?, Node<?, ?>> g = m.unmarshall(null, this.getClass().getResourceAsStream("/DROOLS-2372.dmn"));
    final Stream<Node<?, ?>> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(g.nodes().iterator(), Spliterator.ORDERED), false);
    final Optional<Decision> wbDecision = stream.filter(n -> n.getContent() instanceof ViewImpl).map(n -> (ViewImpl) n.getContent()).filter(n -> n.getDefinition() instanceof Decision).map(n -> (Decision) n.getDefinition()).findFirst();
    wbDecision.ifPresent(d -> {
        assertTrue(d.getExpression() instanceof FunctionDefinition);
        final FunctionDefinition wbFunction = (FunctionDefinition) d.getExpression();
        // This is what the WB expects
        assertTrue(wbFunction.getAdditionalAttributes().containsKey(FunctionDefinition.KIND_QNAME));
        assertEquals("J", wbFunction.getAdditionalAttributes().get(FunctionDefinition.KIND_QNAME));
    });
    final DMNRuntime runtime = roundTripUnmarshalMarshalThenUnmarshalDMN(this.getClass().getResourceAsStream("/DROOLS-2372.dmn"));
    final DMNModel dmnModel = runtime.getModels().get(0);
    final DecisionNode dmnDecision = dmnModel.getDecisions().iterator().next();
    assertTrue(dmnDecision.getDecision().getExpression() instanceof org.kie.dmn.model.v1_1.FunctionDefinition);
    final org.kie.dmn.model.v1_1.FunctionDefinition dmnFunction = (org.kie.dmn.model.v1_1.FunctionDefinition) dmnDecision.getDecision().getExpression();
    assertTrue(dmnFunction.getAdditionalAttributes().containsKey(org.kie.dmn.model.v1_1.FunctionDefinition.KIND_QNAME));
    assertEquals("J", dmnFunction.getAdditionalAttributes().get(org.kie.dmn.model.v1_1.FunctionDefinition.KIND_QNAME));
}
Also used : Arrays(java.util.Arrays) DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) AuthorityRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.AuthorityRequirement) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) Spliterators(java.util.Spliterators) Edge(org.kie.workbench.common.stunner.core.graph.Edge) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.v1_1.KnowledgeSource) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) BackendDefinitionSetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionSetAdapter) EdgeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.EdgeFactoryImpl) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) Bounds(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.org.omg.spec.CMMN_20151109_DC.Bounds) DecisionEvaluationStatus(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus) DMNDiagram(org.kie.workbench.common.dmn.api.definition.v1_1.DMNDiagram) Mockito.doAnswer(org.mockito.Mockito.doAnswer) GraphFactory(org.kie.workbench.common.stunner.core.factory.graph.GraphFactory) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) StringInputStream(org.apache.tools.ant.filters.StringInputStream) NodeFactory(org.kie.workbench.common.stunner.core.factory.graph.NodeFactory) InputData(org.kie.workbench.common.dmn.api.definition.v1_1.InputData) Collection(java.util.Collection) BackendPropertySetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertySetAdapter) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) EdgeFactory(org.kie.workbench.common.stunner.core.factory.graph.EdgeFactory) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.v1_1.BusinessKnowledgeModel) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) Definitions(org.kie.dmn.model.v1_1.Definitions) Context(org.kie.workbench.common.dmn.api.definition.v1_1.Context) DDExtensionsRegister(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DDExtensionsRegister) Assert.assertFalse(org.junit.Assert.assertFalse) DMNContext(org.kie.dmn.api.core.DMNContext) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1_1.DMNMarshallerFactory) MetadataImpl(org.kie.workbench.common.stunner.core.diagram.MetadataImpl) Optional(java.util.Optional) MappingContextSingleton(org.jboss.errai.marshalling.server.MappingContextSingleton) KieHelper(org.kie.dmn.core.util.KieHelper) DMNShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNShape) Color(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.org.omg.spec.CMMN_20151109_DC.Color) Spliterator(java.util.Spliterator) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision) Node(org.kie.workbench.common.stunner.core.graph.Node) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) DMNStyle(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNStyle) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.v1_1.FunctionDefinition) Mock(org.mockito.Mock) GraphCommandManager(org.kie.workbench.common.stunner.core.graph.command.GraphCommandManager) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) GraphCommandManagerImpl(org.kie.workbench.common.stunner.core.graph.command.GraphCommandManagerImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Matchers.anyString(org.mockito.Matchers.anyString) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BackendDefinitionAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionAdapter) KieServices(org.kie.api.KieServices) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) StreamSupport(java.util.stream.StreamSupport) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InformationRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.InformationRequirement) GraphFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.GraphFactoryImpl) AdapterManager(org.kie.workbench.common.stunner.core.definition.adapter.AdapterManager) Before(org.junit.Before) BindableAdapterUtils(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils) NodeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.NodeFactoryImpl) TextAnnotation(org.kie.workbench.common.dmn.api.definition.v1_1.TextAnnotation) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) Assert.assertNotNull(org.junit.Assert.assertNotNull) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AdapterRegistry(org.kie.workbench.common.stunner.core.registry.definition.AdapterRegistry) Mockito.when(org.mockito.Mockito.when) UUID(org.uberfire.commons.uuid.UUID) Association(org.kie.workbench.common.dmn.api.definition.v1_1.Association) TestScopeModelFactory(org.kie.workbench.common.stunner.backend.definition.factory.TestScopeModelFactory) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) Consumer(java.util.function.Consumer) GraphCommandFactory(org.kie.workbench.common.stunner.core.graph.command.impl.GraphCommandFactory) Graph(org.kie.workbench.common.stunner.core.graph.Graph) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) BackendFactoryManager(org.kie.workbench.common.stunner.core.backend.BackendFactoryManager) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.KnowledgeRequirement) BackendPropertyAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertyAdapter) ElementFactory(org.kie.workbench.common.stunner.core.factory.graph.ElementFactory) RuleManager(org.kie.workbench.common.stunner.core.rule.RuleManager) Assert(org.junit.Assert) BeanManager(javax.enterprise.inject.spi.BeanManager) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Node(org.kie.workbench.common.stunner.core.graph.Node) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.v1_1.FunctionDefinition) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 58 with Node

use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.

the class DMNEditBusinessKnowledgeModelToolboxAction method onMouseClick.

@Override
@SuppressWarnings("unchecked")
public ToolboxAction<AbstractCanvasHandler> onMouseClick(final AbstractCanvasHandler canvasHandler, final String uuid, final MouseClickEvent event) {
    // Notice the toolbox factory ensure this action is only being included
    // for BusinessKnowledgeModel definitions, next cast is safe.
    final Node<View<? extends BusinessKnowledgeModel>, Edge> bkmNode = (Node<View<? extends BusinessKnowledgeModel>, Edge>) AbstractToolboxAction.getElement(canvasHandler, uuid).asNode();
    final BusinessKnowledgeModel bkm = bkmNode.getContent().getDefinition();
    editExpressionEvent.fire(new EditExpressionEvent(sessionManager.getCurrentSession(), uuid, bkm.getEncapsulatedLogic(), Optional.of(bkm)));
    return this;
}
Also used : BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.v1_1.BusinessKnowledgeModel) Node(org.kie.workbench.common.stunner.core.graph.Node) EditExpressionEvent(org.kie.workbench.common.dmn.client.events.EditExpressionEvent) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 59 with Node

use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.

the class DMNEditDecisionToolboxAction method onMouseClick.

@Override
@SuppressWarnings("unchecked")
public ToolboxAction<AbstractCanvasHandler> onMouseClick(final AbstractCanvasHandler canvasHandler, final String uuid, final MouseClickEvent event) {
    // Notice the toolbox factory ensure this action is only being included
    // for Decision definitions, next cast is safe.
    final Node<View<? extends Decision>, Edge> decisionNode = (Node<View<? extends Decision>, Edge>) AbstractToolboxAction.getElement(canvasHandler, uuid).asNode();
    final Decision decision = decisionNode.getContent().getDefinition();
    editExpressionEvent.fire(new EditExpressionEvent(sessionManager.getCurrentSession(), uuid, decision, Optional.of(decision)));
    return this;
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) EditExpressionEvent(org.kie.workbench.common.dmn.client.events.EditExpressionEvent) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision)

Example 60 with Node

use of org.kie.workbench.common.stunner.core.graph.Node in project kie-wb-common by kiegroup.

the class DMNFlowActionsToolboxFactory method getActions.

@Override
@SuppressWarnings("unchecked")
public Collection<ToolboxAction<AbstractCanvasHandler>> getActions(final AbstractCanvasHandler canvasHandler, final Element<?> element) {
    final Set<ToolboxAction<AbstractCanvasHandler>> actions = new LinkedHashSet<>();
    final Node<Definition<Object>, Edge> node = (Node<Definition<Object>, Edge>) element;
    final Diagram diagram = canvasHandler.getDiagram();
    final String defSetId = diagram.getMetadata().getDefinitionSetId();
    // Look for the allowed connectors present in the Definition Set.
    final Set<String> allowedConnectorIds = commonLookups.getAllowedConnectors(defSetId, node, 0, 10);
    for (final String allowedConnectorId : allowedConnectorIds) {
        // Append a new action for each connector.
        actions.add(createConnectorActions.get().setEdgeId(allowedConnectorId));
        // Append a new action for each candidate target node (as from the current connector).
        commonLookups.getAllowedTargetDefinitions(defSetId, diagram.getGraph(), node, allowedConnectorId, 0, 10).forEach(def -> actions.add(createNodeActions.get().setEdgeId(allowedConnectorId).setNodeId(getDefinitionId(def))));
    }
    return actions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ToolboxAction(org.kie.workbench.common.stunner.core.client.components.toolbox.actions.ToolboxAction) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram)

Aggregations

Node (org.kie.workbench.common.stunner.core.graph.Node)153 Edge (org.kie.workbench.common.stunner.core.graph.Edge)85 View (org.kie.workbench.common.stunner.core.graph.content.view.View)59 Test (org.junit.Test)38 Graph (org.kie.workbench.common.stunner.core.graph.Graph)32 ViewConnector (org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector)24 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)21 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)17 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)15 Element (org.kie.workbench.common.stunner.core.graph.Element)14 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)13 DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)13 EdgeImpl (org.kie.workbench.common.stunner.core.graph.impl.EdgeImpl)13 Matchers.anyString (org.mockito.Matchers.anyString)13 List (java.util.List)12 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)12 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)12 Before (org.junit.Before)11 Definition (org.kie.workbench.common.stunner.core.graph.content.definition.Definition)11 Child (org.kie.workbench.common.stunner.core.graph.content.relationship.Child)11