gpt4 book ai didi

java - 使用 neo4j 嵌入式 java api 为关系添加权重

转载 作者:行者123 更新时间:2023-12-01 11:52:24 24 4
gpt4 key购买 nike

我想使用嵌入式 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!");

}

}

它应该给出这个结果:

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

谢谢!

最佳答案

正如 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。我建议您在查询图表之前保留节点,这是一种很好的做法。

关于java - 使用 neo4j 嵌入式 java api 为关系添加权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28700982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com