Search in sources :

Example 6 with DefinitionSet

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

the class ObserverBuilderControlTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ServiceCallback<Node<View<Object>, Edge>> callback = (ServiceCallback<Node<View<Object>, Edge>>) (invocationOnMock.getArguments()[2]);
            view = new ViewImpl<>(new Object(), new BoundsImpl(new BoundImpl(0.0, 0.0), new BoundImpl(10.0, 20.0)));
            Node<View<Object>, Edge> item = new NodeImpl<>("UUID");
            item.setContent(view);
            callback.onSuccess(item);
            return null;
        }
    }).when(clientFactoryServices).newElement(anyString(), anyString(), any(ServiceCallback.class));
    when(graphBoundsIndexer.setRootUUID(anyString())).thenReturn(graphBoundsIndexer);
    AdapterManager adapters = mock(AdapterManager.class);
    DefinitionAdapter definitionAdapter = mock(DefinitionAdapter.class);
    DefinitionSetRuleAdapter rulesAdapter = mock(DefinitionSetRuleAdapter.class);
    when(definitionAdapter.getId(any())).thenReturn("Object");
    when(rulesAdapter.getRuleSet(any())).thenReturn(mock(RuleSet.class));
    when(adapters.forDefinition()).thenReturn(definitionAdapter);
    when(adapters.forRules()).thenReturn(rulesAdapter);
    when(clientDefinitionManager.adapters()).thenReturn(adapters);
    when(clientDefinitionManager.definitionSets()).thenReturn(mock(TypeDefinitionSetRegistry.class));
    when(canvasCommandFactory.addNode(any(Node.class), anyString())).thenAnswer(new Answer<Command>() {

        @Override
        public Command answer(InvocationOnMock invocationOnMock) {
            Node node = (Node) invocationOnMock.getArguments()[0];
            String uid = (String) invocationOnMock.getArguments()[1];
            return new AddCanvasNodeCommand(node, uid);
        }
    });
    when(canvasCommandFactory.updatePosition(any(Node.class), any(Point2D.class))).thenAnswer(new Answer<Command>() {

        @Override
        public Command answer(InvocationOnMock invocationOnMock) {
            Node node = (Node) invocationOnMock.getArguments()[0];
            Point2D location = (Point2D) invocationOnMock.getArguments()[1];
            return new UpdateElementPositionCommand(node, location);
        }
    });
    when(canvasCommandFactory.draw()).thenReturn(mock(CanvasCommand.class));
    ShapeSet shapeSet = mock(ShapeSet.class);
    ShapeFactory shapeFactory = mock(ShapeFactory.class);
    when(shapeFactory.newShape(any())).thenReturn(mock(ElementShape.class));
    when(shapeSet.getShapeFactory()).thenReturn(shapeFactory);
    when(shapeManager.getShapeSet(anyString())).thenReturn(shapeSet);
    when(shapeManager.getDefaultShapeSet(anyString())).thenReturn(shapeSet);
    tested = new ObserverBuilderControl(clientDefinitionManager, clientFactoryServices, graphUtils, ruleManager, canvasCommandFactory, graphBoundsIndexer, canvasLayoutUtils, mock(Event.class));
    Diagram diagram = mock(Diagram.class);
    Metadata metadata = mock(Metadata.class);
    when(metadata.getCanvasRootUUID()).thenReturn("ID");
    when(diagram.getMetadata()).thenReturn(metadata);
    MutableIndex index = mock(MutableIndex.class);
    Graph graph = mock(Graph.class);
    DefinitionSet graphContent = mock(DefinitionSet.class);
    when(graphContent.getBounds()).thenReturn(new BoundsImpl(new BoundImpl(10d, 10d), new BoundImpl(100d, 100d)));
    when(graph.getContent()).thenReturn(graphContent);
    when(index.getGraph()).thenReturn(graph);
    when(graphIndexBuilder.build(any(Graph.class))).thenReturn(index);
    canvasHandler = new CanvasHandlerImpl(clientDefinitionManager, canvasCommandFactory, clientFactoryServices, ruleManager, graphUtils, graphIndexBuilder, shapeManager, mock(TextPropertyProviderFactory.class), mock(Event.class), null, null, null);
    canvasHandler.handle(mock(AbstractCanvas.class));
    canvasHandler.draw(diagram, mock(ParameterizedCommand.class));
    when(diagram.getGraph()).thenReturn(graph);
    when(graph.nodes()).thenReturn(Collections.emptyList());
    CanvasCommandManager commandManager = mock(CanvasCommandManager.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Command command = (Command) invocationOnMock.getArguments()[1];
            command.execute(invocationOnMock.getArguments()[0]);
            return null;
        }
    }).when(commandManager).execute(any(), any(Command.class));
    when(commandManagerProvider.getCommandManager()).thenReturn(commandManager);
    tested.enable(canvasHandler);
    tested.setCommandManagerProvider(commandManagerProvider);
}
Also used : TypeDefinitionSetRegistry(org.kie.workbench.common.stunner.core.registry.definition.TypeDefinitionSetRegistry) Node(org.kie.workbench.common.stunner.core.graph.Node) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) ShapeSet(org.kie.workbench.common.stunner.core.client.ShapeSet) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) ShapeFactory(org.kie.workbench.common.stunner.core.client.shape.factory.ShapeFactory) ObserverBuilderControl(org.kie.workbench.common.stunner.core.client.canvas.controls.builder.impl.ObserverBuilderControl) Matchers.anyString(org.mockito.Matchers.anyString) DefinitionAdapter(org.kie.workbench.common.stunner.core.definition.adapter.DefinitionAdapter) ServiceCallback(org.kie.workbench.common.stunner.core.client.service.ServiceCallback) AdapterManager(org.kie.workbench.common.stunner.core.definition.adapter.AdapterManager) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) ElementShape(org.kie.workbench.common.stunner.core.client.shape.ElementShape) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) CanvasCommand(org.kie.workbench.common.stunner.core.client.command.CanvasCommand) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) MutableIndex(org.kie.workbench.common.stunner.core.graph.processing.index.MutableIndex) CanvasHandlerImpl(org.kie.workbench.common.stunner.core.client.canvas.CanvasHandlerImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Graph(org.kie.workbench.common.stunner.core.graph.Graph) CanvasCommand(org.kie.workbench.common.stunner.core.client.command.CanvasCommand) AddCanvasNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddCanvasNodeCommand) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Command(org.kie.workbench.common.stunner.core.command.Command) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CanvasCommandManager(org.kie.workbench.common.stunner.core.client.command.CanvasCommandManager) AddCanvasNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddCanvasNodeCommand) DefinitionSetRuleAdapter(org.kie.workbench.common.stunner.core.definition.adapter.DefinitionSetRuleAdapter) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Edge(org.kie.workbench.common.stunner.core.graph.Edge) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Before(org.junit.Before)

Example 7 with DefinitionSet

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

the class AbstractVFSDiagramService method getDiagramByPath.

@SuppressWarnings("unchecked")
public D getDiagramByPath(final org.uberfire.backend.vfs.Path file) {
    if (accepts(file)) {
        DefinitionSetService services = getServiceByPath(file);
        if (null != services) {
            final String defSetId = getDefinitionSetId(services);
            final String name = parseFileName(file, services);
            // Check if any metadata definition exist.
            M metadata = obtainMetadata(services, file, defSetId, name);
            if (null == metadata) {
                metadata = (M) buildMetadataInstance(file, defSetId, name);
            }
            metadata.setPath(file);
            // Parse and load the diagram raw data.
            final InputStream is = loadPath(file);
            try {
                Graph<DefinitionSet, ?> graph = services.getDiagramMarshaller().unmarshall(metadata, is);
                DiagramFactory<M, ?> factory = factoryManager.registry().getDiagramFactory(graph.getContent().getDefinition(), getMetadataType());
                return (D) factory.build(name, metadata, graph);
            } catch (java.io.IOException e) {
                LOG.error("Cannot unmarshall diagram for diagram's path [" + file + "]", e);
                return null;
            }
        }
    }
    throw new UnsupportedOperationException("Diagram format not supported [" + file + "]");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) DefinitionSetService(org.kie.workbench.common.stunner.core.definition.service.DefinitionSetService)

Example 8 with DefinitionSet

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

the class BPMN2JsonParser method initialize.

/*
        ****************************************************************
        *               Custom Stunner logic
        ****************************************************************
     */
@SuppressWarnings("unchecked")
private void initialize(final ContextualParser.Context parsingContext) {
    Graph graph = diagram.getGraph();
    final Map<String, EdgeParser> edgeParsers = new HashMap<>();
    new ChildrenTraverseProcessorImpl(new TreeWalkTraverseProcessorImpl()).traverse(graph, new AbstractChildrenTraverseCallback<Node<View, Edge>, Edge<Child, Node>>() {

        final Stack<NodeParser> parsers = new Stack<NodeParser>();

        NodeParser currentParser = null;

        @Override
        public void startGraphTraversal(Graph<DefinitionSet, Node<View, Edge>> graph) {
            super.startGraphTraversal(graph);
        }

        @Override
        public boolean startNodeTraversal(final List<Node<View, Edge>> parents, final Node<View, Edge> node) {
            super.startNodeTraversal(parents, node);
            onNodeTraversal(node);
            return true;
        }

        @Override
        public void startNodeTraversal(final Node<View, Edge> node) {
            super.startNodeTraversal(node);
            onNodeTraversal(node);
        }

        private void onNodeTraversal(final Node node) {
            NodeParser p = new NodeParser("", node);
            if (null != currentParser) {
                parsers.peek().addChild(p);
            } else {
                BPMN2JsonParser.this.rootParser = p;
            }
            currentParser = p;
            List<Edge> outEdges = node.getOutEdges();
            if (null != outEdges && !outEdges.isEmpty()) {
                for (Edge edge : outEdges) {
                    // Only add the edges with view connector types into the resulting structure to generate the bpmn definition.
                    if (edge.getContent() instanceof ViewConnector && !edgeParsers.containsKey(edge.getUUID())) {
                        edgeParsers.put(edge.getUUID(), new EdgeParser("", (Edge) edge));
                    }
                }
            }
        }

        @Override
        public void startEdgeTraversal(Edge<Child, Node> edge) {
            super.startEdgeTraversal(edge);
            parsers.push(currentParser);
        }

        @Override
        public void endEdgeTraversal(Edge<Child, Node> edge) {
            super.endEdgeTraversal(edge);
            currentParser = parsers.pop();
        }

        @Override
        public void endGraphTraversal() {
            super.endGraphTraversal();
        }
    });
    // In oryx format, all edges are added into the main BPMNDiagram node.
    if (null != rootParser && !edgeParsers.isEmpty()) {
        for (EdgeParser edgeParser : edgeParsers.values()) {
            rootParser.addChild(edgeParser);
        }
    }
    // Initialize all the element parsers added in the tree.
    BPMN2JsonParser.this.rootParser.initialize(parsingContext);
    System.out.println("End of children and view traverse");
}
Also used : ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) HashMap(java.util.HashMap) Node(org.kie.workbench.common.stunner.core.graph.Node) View(org.kie.workbench.common.stunner.core.graph.content.view.View) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) Stack(java.util.Stack) Graph(org.kie.workbench.common.stunner.core.graph.Graph) ChildrenTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ChildrenTraverseProcessorImpl) List(java.util.List) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child)

Example 9 with DefinitionSet

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

the class Unmarshalling method unmarshall.

public static Diagram<Graph, Metadata> unmarshall(DiagramMarshaller tested, InputStream is) throws Exception {
    Metadata metadata = new MetadataImpl.MetadataImplBuilder(BindableAdapterUtils.getDefinitionSetId(BPMNDefinitionSet.class)).build();
    DiagramImpl diagram = new DiagramImpl(UUID.uuid(), metadata);
    Graph<DefinitionSet, Node> graph = tested.unmarshall(metadata, is);
    diagram.setGraph(graph);
    return diagram;
}
Also used : MetadataImpl(org.kie.workbench.common.stunner.core.diagram.MetadataImpl) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) BPMNDefinitionSet(org.kie.workbench.common.stunner.bpmn.BPMNDefinitionSet) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)

Example 10 with DefinitionSet

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

the class BPMNGraphGenerator method createGraph.

public Graph<DefinitionSet, Node> createGraph() {
    Graph<DefinitionSet, Node> graph = (Graph<DefinitionSet, Node>) factoryManager.newElement(UUID.uuid(), diagramDefinitionSetClass);
    // TODO: Where are the BPMN diagram bounds in the Oryx json structure? Exist?
    if (null == graph.getContent().getBounds()) {
        graph.getContent().setBounds(new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(BPMNGraphFactory.GRAPH_DEFAULT_WIDTH, BPMNGraphFactory.GRAPH_DEFAULT_HEIGHT)));
    }
    builderContext.init(graph).execute(builderContext.getCommandFactory().clearGraph());
    NodeObjectBuilder diagramBuilder = getDiagramBuilder(builderContext);
    if (diagramBuilder == null) {
        throw new RuntimeException("No diagrams found!");
    }
    Node<View<BPMNDefinition>, Edge> diagramNode = (Node<View<BPMNDefinition>, Edge>) diagramBuilder.build(builderContext);
    graph.addNode(diagramNode);
    logBuilders();
    return graph;
}
Also used : Graph(org.kie.workbench.common.stunner.core.graph.Graph) TreeNode(com.fasterxml.jackson.core.TreeNode) Node(org.kie.workbench.common.stunner.core.graph.Node) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) BPMNDefinition(org.kie.workbench.common.stunner.bpmn.definition.BPMNDefinition) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Aggregations

DefinitionSet (org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet)15 Node (org.kie.workbench.common.stunner.core.graph.Node)11 Graph (org.kie.workbench.common.stunner.core.graph.Graph)6 Test (org.junit.Test)5 Edge (org.kie.workbench.common.stunner.core.graph.Edge)5 View (org.kie.workbench.common.stunner.core.graph.content.view.View)5 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)4 RuleSet (org.kie.workbench.common.stunner.core.rule.RuleSet)4 TestingGraphInstanceBuilder (org.kie.workbench.common.stunner.core.TestingGraphInstanceBuilder)3 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)3 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)3 TreeWalkTraverseProcessorImpl (org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl)3 Command (org.kie.workbench.common.stunner.core.command.Command)2 Diagram (org.kie.workbench.common.stunner.core.diagram.Diagram)2 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)2 TreeNode (com.fasterxml.jackson.core.TreeNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1