0

I would like to add a weight to a relationship using Embedded Neo4j Java API.

For example : A knows B very well so their relationship should be weighted 5. On the other hand, A knows C very little so their relationship should be weighted 1.

How can I do that?

PS : I have already tried the example here : http://neo4j.com/docs/stable/tutorials-java-embedded-graph-algo.html but it doesn't recognize th functions createNode( "name", "A", "x", 0d, "y", 0d ) and createRelationship( nodeA, nodeC, "length", 2d ).

This is the code :

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

    }

}

It should give this result :

enter image description here

It says that these following functions are not defined :

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

Thanks!

user1885868
  • 1,063
  • 3
  • 17
  • 31

3 Answers3

1

As said Ryan, Java compiler doesn't detect the methods createNode() and createRelationship() that you use.

This is a way to create the Nodes, and works for me:

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}

For relationships, only show you how to add properties:

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

Depending of your Neo4j version, you should see how create Nodes and Relationships. Last, I have you create nodes and relationships, and without commit them, you create the PathFinder. I recommend you persist the nodes before querying the graph as good practice.

Rigoberto Peña
  • 380
  • 1
  • 10
0

These methods aren't defined as methods in class Test1, but rather on GraphDatabaseService. So, you need to do db.createNode() and db.createRelationship(). That should work for you.

Ryan Boyd
  • 2,978
  • 1
  • 21
  • 19
  • Can you define how it didn't work? What was the specific error? – Ryan Boyd Feb 24 '15 at 17:06
  • It is just as you said, `These methods aren't defined as methods in class Test1` – user1885868 Feb 24 '15 at 17:08
  • sorry, I meant: what is the error you get when you try db.createNode() and db.createRelationship. The methods are defined in anything implementing the GraphDatabaseService interface: http://neo4j.com/docs/stable/javadocs/org/neo4j/graphdb/GraphDatabaseService.html – Ryan Boyd Feb 24 '15 at 17:13
0

The methods createNode() and createRelationship() are not defined in neo4j java API as you are using these.

adas
  • 1