2015-02-24 24 views
0

我想使用嵌入式Neo4j Java API为关系添加权重。使用neo4j嵌入式java API添加权重

例如:A知道B非常好,所以他们的关系应该加权5。另一方面,A知道C很少,所以他们的关系应加权1

我该怎么做?

PS:我已经在这里试过这个例子:http://neo4j.com/docs/stable/tutorials-java-embedded-graph-algo.html,但它不能识别第createNode("name", "A", "x", 0d, "y", 0d)createRelationship(nodeA, nodeC, "length", 2d)的第n个功能。

这是代码:

package com.neo4j.test.test1; 

import org.neo4j.graphalgo.CommonEvaluators; 
import org.neo4j.graphalgo.EstimateEvaluator; 
import org.neo4j.graphalgo.GraphAlgoFactory; 
import org.neo4j.graphalgo.PathFinder; 
import org.neo4j.graphalgo.WeightedPath; 
import org.neo4j.graphdb.GraphDatabaseService; 
import org.neo4j.graphdb.Node; 
import org.neo4j.graphdb.PathExpanders; 
import org.neo4j.graphdb.Relationship; 
import org.neo4j.graphdb.Transaction; 
import org.neo4j.graphdb.factory.GraphDatabaseFactory; 

import com.neo4j.test.labels.NodeLabels; 
import com.neo4j.test.labels.TypeRelation; 

public class Test1 { 

    public static void main(String[] args) { 

     GraphDatabaseFactory dbFactory = new GraphDatabaseFactory(); 
     GraphDatabaseService db = dbFactory.newEmbeddedDatabase("C:\\Zakaria\\NeoTests\\Test2"); 

     try (Transaction tx = db.beginTx()) { 

      Node nodeA = createNode("name", "A", "x", 0d, "y", 0d); 
      Node nodeB = createNode("name", "B", "x", 7d, "y", 0d); 
      Node nodeC = createNode("name", "C", "x", 2d, "y", 1d); 
      Relationship relAB = createRelationship(nodeA, nodeC, "length", 2d); 
      Relationship relBC = createRelationship(nodeC, nodeB, "length", 3d); 
      Relationship relAC = createRelationship(nodeA, nodeB, "length", 10d); 

      EstimateEvaluator<Double> estimateEvaluator = new EstimateEvaluator<Double>() 
      { 
       @Override 
       public Double getCost(final Node node, final Node goal) 
       { 
        double dx = (Double) node.getProperty("x") - (Double) goal.getProperty("x"); 
        double dy = (Double) node.getProperty("y") - (Double) goal.getProperty("y"); 
        double result = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); 
        return result; 
       } 
      }; 
      PathFinder<WeightedPath> astar = GraphAlgoFactory.aStar(
        PathExpanders.allTypesAndDirections(), 
        CommonEvaluators.doubleCostEvaluator("length"), estimateEvaluator); 
      WeightedPath path = astar.findSinglePath(nodeA, nodeB); 

      tx.success(); 

     } 

     System.out.println("Done!"); 

    } 

} 

它应该给这样的结果:

enter image description here

它说,以下这些功能都没有定义:

Node nodeA = createNode("name", "A", "x", 0d, "y", 0d); 
Node nodeB = createNode("name", "B", "x", 7d, "y", 0d); 
Node nodeC = createNode("name", "C", "x", 2d, "y", 1d); 
Relationship relAB = createRelationship(nodeA, nodeC, "length", 2d); 
Relationship relBC = createRelationship(nodeC, nodeB, "length", 3d); 
Relationship relAC = createRelationship(nodeA, nodeB, "length", 10d); 

谢谢!

+1

请发布您尝试过的代码,以及它产生的任何错误消息。 – amphetamachine 2015-02-24 16:24:46

+0

@amphetamachine请检查更新! – user1885868 2015-02-24 16:36:14

回答

1

正如Ryan所说,Java编译器没有检测到您使用的方法createNode()createRelationship()

这是创建节点的方式,对我的作品:

try (Transaction Tx = gdbs.beginTx(){ 
    Node nodo = gdbs.createNode(); 
    nodo.addLabel(p); // if you have Labels 
    nodo.setProperty("property1", someValue); 
    Tx.success(); 
    Tx.close(); 
} catch (Exception e){//do something} 

对于关系,只能说明你如何添加属性:

relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS); 
relationship.setProperty("message", "brave Neo4j "); 

根据你的Neo4j的版本,你应该看看如何创建节点和关系。最后,我要创建节点和关系,并且不提交它们,您将创建路径查找器。我建议你在查询图表之前坚持这些节点是很好的做法。

0

这些方法没有被定义为类Test1中的方法,而是GraphDatabaseService中的方法。所以,你需要做db.createNode()db.createRelationship()。这应该适合你。

+0

我已经试过,但没有工作 – user1885868 2015-02-24 17:00:06

+0

你可以定义它是如何工作的吗?具体的错误是什么? – 2015-02-24 17:06:15

+0

正如你所说,'这些方法没有被定义为Test1类中的方法' – user1885868 2015-02-24 17:08:18

0

在您使用这些方法时,createNode()和createRelationship()方法未在neo4j java API中定义。