示例#1
0
  /**
   * Creates a KGraph, represented by a parent node that contains the actual nodes and edges of the
   * graph.
   *
   * @return a parent node that contains graph elements
   */
  private static KNode createGraph() {
    // create parent node
    KNode parentNode = KimlUtil.createInitializedNode();

    // create child nodes
    KNode childNode1 = KimlUtil.createInitializedNode();
    // This automatically adds the child to the list of its parent's children.
    childNode1.setParent(parentNode);
    KLabel nodeLabel1 = KimlUtil.createInitializedLabel(childNode1);
    nodeLabel1.setText("node1");
    KNode childNode2 = KimlUtil.createInitializedNode();
    childNode2.setParent(parentNode);
    KLabel nodeLabel2 = KimlUtil.createInitializedLabel(childNode2);
    nodeLabel2.setText("node2");

    // create ports (optional)
    KPort port1 = KimlUtil.createInitializedPort();
    // This automatically adds the port to the node's list of ports.
    port1.setNode(childNode1);
    KPort port2 = KimlUtil.createInitializedPort();
    port2.setNode(childNode2);

    // create edges
    KEdge edge1 = KimlUtil.createInitializedEdge();
    // This automatically adds the edge to the node's list of outgoing edges.
    edge1.setSource(childNode1);
    // This automatically adds the edge to the node's list of incoming edges.
    edge1.setTarget(childNode2);
    // As our ports do not distinguish between incoming and outgoing edges,
    // the edges must be added manually to their list of edges.
    edge1.setSourcePort(port1);
    port1.getEdges().add(edge1);
    edge1.setTargetPort(port2);
    port2.getEdges().add(edge1);

    return parentNode;
  }