使用具有自定义节点和边的持久布局

时间:2014-08-21 21:13:56

标签: java layout nodes jung

我使用Jung库创建图表。我想使用持久布局每次生成相同的图形。

我复制粘贴了这个工作代码: http://jung.sourceforge.net/site/jung-samples/xref/edu/uci/ics/jung/samples/PersistentLayoutDemo.html

我改变的只是正在创建的图表。我创建的图表有自定义节点和边。但是现在当我试图恢复时我得到了(即时使用eclipse):

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at helloworld.main.main(main.java:161)

当我在eclipse中访问问题视图时(我之前在控制台)有2个错误:

1)令牌上的语法错误&#34 ;;",{此令牌后的预期

这是一行:

BufferedReader reader = new BufferedReader(new FileReader("Topology2.csv"));

2)语法错误,插入"}"完成Block

这是一行:

PersistentLayout<nodes,Edges> persistentLayout;

这是我的代码:

  public class main {

  /**
  * the graph
  */public static nodes find(String a, ArrayList<nodes> b) {

    for (int i = 0; i < b.size(); i++) {

        if (a.compareTo(b.get(i).name) == 0){
            return b.get(i);
        }

    }
    //System.out.println("Couldnt find node:  " + a);
    return b.get(0);
}



  /**
  * the name of the file where the layout is saved
   */
  String fileName;

  /**
  * the visual component and renderer for the graph
   */
  VisualizationViewer<nodes,Edges> vv;

  PersistentLayout<nodes,Edges> persistentLayout;

 /**
   * create an instance of a simple graph with controls to
   * demo the persistence and zoom features.
  * 
   * @param fileName where to save/restore the graph positions
   */
 public main(final String fileName)  throws Exception {
     Graph<nodes, Edges> g = new SparseMultigraph<nodes, Edges>();
        ArrayList<nodes> araylist = new ArrayList<nodes>();
        String line = null;
        BufferedReader reader = new BufferedReader(new FileReader("Topology2.csv"));
        while ((line = reader.readLine()) != null) {
            nodes newNode = new nodes (line);
            araylist.add(newNode);
        }
        reader.close();
        BufferedReader reader2 = new BufferedReader(new FileReader("Topology.csv"));
        int counter = 0;

        while ((line = reader2.readLine()) != null) {
            counter++;
            String[] a = line.split(",");
            int number = a.length;
            //System.out.println("length: " + number);
            nodes origin =  find(a[0],araylist);
            nodes prev = find(a[1], araylist);
            if (number >= 3) {
                nodes next = find(a[2], araylist);
                g.addEdge(new Edges(counter,origin,next),origin, next);
            }
            g.addEdge(new Edges(counter,origin,prev),origin, prev);
            counter++;
        }
        reader2.close();
     this.fileName = fileName;

     // create a simple graph for the demo
     persistentLayout = 
         new PersistentLayoutImpl<nodes,Edges>(new FRLayout<nodes,Edges>(g));

      vv = new VisualizationViewer<nodes,Edges>(persistentLayout);

     // add my listener for ToolTips
     vv.setVertexToolTipTran sformer(new ToStringLabeller());
     DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
      vv.setGraphMouse(gm);
      // create a frome to hold the graph
      final JFrame frame = new JFrame();
      frame.getContentPane().add(new GraphZoomScrollPane(vv));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // create a control panel and buttons for demo
      // functions
      JPanel p = new JPanel();

      JButton persist = new JButton("Save Layout");
     // saves the graph vertex positions to a file
      persist.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              try {
                 persistentLayout.persist(fileName);
              } catch (IOException e1) {
                  System.err.println("got "+e1);
            }
         }
      });
      p.add(persist);

      JButton restore = new JButton("Restore Layout");
    // restores the graph vertex positions from a file
    // if new vertices were added since the last 'persist',
     // they will be placed at random locations
     restore.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
  //                PersistentLayout<nodes,Edges> pl = (PersistentLayout<nodes,Edges>)        vv.getGraphLayout();
             try {
                 persistentLayout.restore(fileName);
             } catch (Exception e1) {
                 e1.printStackTrace();
            }
         }
     });
     p.add(restore);
    p.add(gm.getModeComboBox());

     frame.getContentPane().add(p, BorderLayout.SOUTH);
     frame.pack();//setSize(600, 600);
     frame.setVisible(true);
 }

 /**
  * a driver for this demo
  * @param args should hold the filename for the persistence demo
 * @throws Exception 
  */
 public static void main(String[] args) throws Exception {

    String filename;
     if (args.length >= 1)
         filename = args[0];
     else
         filename = "PersistentLayoutDemo.out";
     new main(filename);
 }
 }

自定义边缘代码:

public class Edges {

int id;
nodes start;
nodes end;

public Edges(int id_, nodes a, nodes b) {
id = id_;
start = a;
end = b;

}

}

自定义节点代码:

package helloworld; 
import java.io.Serializable;

public class nodes  {
String name = "";
nodes next = null;
nodes prev = null;
boolean alarm = false;
String alarmLevel = "";
String alarmCaption = "";
String alarmInfo = "";

public nodes(String na) {
    name = na;

}
public nodes(String na, String n, String p, boolean a, String lvl, String cap, String         info) {
    name = na;
    next = new nodes();
    prev = new nodes();
    next.name = n;
    prev.name = p;
    alarm = a;
    alarmLevel = lvl;
    alarmCaption = cap;
    alarmInfo = info;
}
public nodes() {
    // TODO Auto-generated constructor stub
}
}

1 个答案:

答案 0 :(得分:0)

在节点构造函数中,您要分配next.name但从不初始化next,即null。这至少是你问题的一部分。

旁注:你的边缘类引用了两个节点对象,没有明显的原因,我也不确定节点类中的prev和next是什么。

您需要弄清楚您的数据模型应该是什么,并且请记住Graph类正在跟踪节点/边连接。