使用 neo4j embedded java api 为关系增加权重

Add weight to a relationship using neo4j embedded java api

我想使用嵌入式 Neo4j 为关系增加权重 Java API.

例如:A 非常了解 B 所以他们的关系应该加权 5。另一方面,AC知之甚少,所以他们的关系应该加权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 ).

这是代码:

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!");

    }

}

它应该给出这个结果:

它说没有定义以下函数:

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 );

谢谢!

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

正如 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 版本,您应该了解如何创建节点和关系。最后,我让您创建节点和关系,并且在不提交它们的情况下创建 PathFinder。我建议您在查询图形之前保留节点,这是一种很好的做法。

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