在另一个方法中调用带参数的方法

时间:2013-02-18 18:02:35

标签: java

我需要在另一个方法中调用带参数的方法。这是我的代码:

    public void createNodesAndRelationships(List<String[]> all, GraphDatabaseService      graphDb) {

    Node firstNode = null;
    Node secondNode = null;
    Relationship relationship;



    //register the shutdown msg
    registerShutdownHook(graphDb);
    Index<Node> nodeIndex;
    for (int i = 0; i < all.size(); i++) {
        Transaction tx = graphDb.beginTx();
        long id, id2;
        try {
            //convert IPs to long numbers format
            long ip1 = Long.parseLong(all.get(i)[0]);
            long ip2 = Long.parseLong(all.get(i)[1]);

            //check if src higher than dest
            //if yes reorder src < dest
            //this is used make relationships from lower IP to higher ones
            if (ip1 > ip2) {
                long ip3 = ip1;
                ip1 = ip2;
                ip2 = ip3;
            }
            //LuceneIndexService finder = new LuceneIndexService(graphDb);
            //firstNode = finder.getSingleNode("IP", all.get(i)[0]);
            //firstNode = null;
            nodeIndex = graphDb.index().forNodes("users");

            //get node if exists or create it
            firstNode = getOrCreateNode(graphDb, "IP", ip1);
            if (firstNode == null) {
                firstNode = graphDb.createNode();
                firstNode.setProperty("IP", ip1);
                nodeIndex.add(firstNode, "IP", ip1);
                Node node = graph.addNode("Ip");
                node.setProperty("IP",ip1);
            }
            //secondNode = finder.getSingleNode("IP", all.get(i)[1]);
            //secondNode = null;
            secondNode = getOrCreateNode(graphDb, "IP", ip2);
            if (secondNode == null) {
                secondNode = graphDb.createNode();
                secondNode.setProperty("IP", ip2);
                nodeIndex.add(secondNode, "IP", ip2);
                Node node2 = graph.addNode("Ip");
                node2.setProperty("IP",ip2);
            }

            //get relationships of one IP (src)
            Iterable<Relationship> rels = firstNode.getRelationships(RelTypes.COMMUNICATE);
            Iterator it = rels.iterator();

            //set relationship still not found
            boolean found = false;
            while (it.hasNext() && found == false) {
                Relationship rel = (Relationship) it.next();

                //get dest node from relationship
                Node otherNode = rel.getOtherNode(firstNode);

                //if dest IP == relationship dest
                //then update relationship weight
                if (otherNode == secondNode) {
                    int weight = Integer.parseInt(all.get(i)[2]);
                    int relWeight = Integer.parseInt(rel.getProperty("weight").toString());
                    int finalWeight = weight + relWeight;
                    rel.setProperty("weight", finalWeight);
                    System.out.println("index: " + i + " srcID: " + firstNode.getId() + " src:" + firstNode.getProperty("IP") + " dest: " + secondNode.getProperty("IP") + " weight: " + rel.getProperty("weight"));
                     Node rell = graph.addEdge("IP","IP",null);
                      rell.setProperty("IP",finalWeight);
                    //relationship found
                    found = true;
                }

            }

            //relationship not found
            if (found == false) {

                //create new relationship
                relationship = firstNode.createRelationshipTo(secondNode, RelTypes.COMMUNICATE);
                relationship.setProperty("weight", all.get(i)[2]);
                 Node rell = graph.addEdge("weight","IP","IP");
                     rell.setProperty("IP",RelTypes.COMMUNICATE);
                System.out.println("index: " + i + " srcID: " + firstNode.getId() + " src:" + firstNode.getProperty("IP") + " dest: " + secondNode.getProperty("IP") + " weight: " + relationship.getProperty("weight"));
            }



            tx.success();
        } finally {
            tx.finish();
            id = firstNode.getId();
            Node testnode = graphDb.getNodeById(id);
            id2 = secondNode.getId();
            Node testnode2 = graphDb.getNodeById(id2);
            System.out.println(testnode.getId() + " " + testnode2.getId());
        }
    }
}

public void run() {

     contentPane = new JPanel();
    contentPane.setBackground(Color.DARK_GRAY);
    contentPane.setBorder(new EmptyBorder(0,0,0,0));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    JPanel panelSettings = new JPanel();
    panelSettings.setBorder(new EmptyBorder(0, 8, 0, 8));
  //  panelSettings.setBackground(SystemColor.activeCaption);
    panelSettings.setLayout(new FlowLayout());
     panelSettings.add(b1);
    contentPane.add(panelSettings, BorderLayout.WEST);

    viewer.enableAutoLayout();
    view = viewer.addDefaultView(false);

    contentPane.add(view, BorderLayout.CENTER);

    setSize(800, 600);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

问题是如何在方法run()中调用方法createNodesAndRelationships(...)?我需要使用Node node = graph.addNode(&#34; Ip&#34;)和node.setProperty(&#34; IP&#34;,ip1)来绘制图形。谢谢

1 个答案:

答案 0 :(得分:0)

将此registerShutdownHook(graphDb);移至您创建EmbeddedGraphDatabase

的地方

然后只用你的数组和db作为参数调用方法。