簡體   English   中英

使用neo4j嵌入式Java API為關系增加權重

[英]Add weight to a relationship using neo4j embedded java api

我想使用嵌入式Neo4j Java API為關系添加權重。

例如: A非常了解B因此他們的關系應加權5 另一方面, AC A了解很少,因此應將其關系加權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 );

謝謝!

正如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的版本,您應該了解如何創建節點和關系。 最后,我要創建節點和關系,並且不提交它們就創建路徑查找器。 建議您在查詢圖形之前先保留節點,這是一種很好的做法。

這些方法未在類Test1定義為方法,而是在GraphDatabaseServiceGraphDatabaseService 因此,您需要執行db.createNode()db.createRelationship() 那應該為您工作。

在neo4j java API中未定義方法createNode()和createRelationship(),因為您正在使用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM