Announcement Announcement Module
Collapse
No announcement yet.
neo4j @GraphTraversal issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • neo4j @GraphTraversal issues

    Is there a working example of @GraphTraversal in action? The examples that I could find included declarations for @GraphTraversal but nowhere in the code or the test cases was the graph actually traversed. I'm using 2.0 RC1 and I am using the Repository model instead of populating the graph using the template object.

    When I try on my own I am getting a NullPointer exception in my tests because the Iterable isn't being initialized, if I initialize the Iterable myself it isn't getting populated. I am trying to create a Portfolio object which has edges to Projects or other Portfolios, and I want to use traversal to get an Iterable for all Project objects attached to the Portfolio, or linked Portfolios (recursively).

    @NodeEntity
    public class Portfolio {
    ...
    //Build a relationship to itself to allow for sub-portfolios
    @RelatedTo(type = "SUBPORTFOLIO", elementClass = Portfolio.class, direction = Direction.BOTH)
    private Set<Portfolio> portfolios;

    //Build a relationship for projects. This is an outgoing relationship, so projects are attached to porfolios
    @RelatedTo(type = "CONTAINS", elementClass = Project.class, direction = Direction.OUTGOING)
    private Set<Project> projects;
    ...
    @GraphTraversal(traversal = ProjectTraversalBuilder.class,elementClass = Project.class)
    private Iterable<Project> allProjects;
    ...
    public Iterable<Project> getAllProjects() {
    return allProjects;
    }
    ...
    }

    @NodeEntity
    public class Project {
    ...
    //Build an incoming relationship with the Portfolios, this will be populated
    //by spring-data and provides read-only access from this direction
    @RelatedToVia(type = "CONTAINS", direction = Direction.INCOMING)
    Iterable<Portfolio> portfolios;
    ...
    }
    Last edited by secooper; Nov 29th, 2011, 11:06 AM.

  • #2
    For the simple mapping (non-aspectj) you have to annotate @GraphTraversals or @Query with @Fetch to be eagerly loaded.

    We're still experimenting with this approach so your feedback is very valuable.

    Cheers

    Michael

    Comment


    • #3
      I added @Fetch, but now it is telling me that the classes are incorrect... I have Portfolios with Portfolios('SUBPORTFOLIO') and Projects('CONTAINS'). My traversal builder is following the 'SUBPORTFOLIO' edges which lead to Portfolios. But I want Projects, how do I traverse the 'SUBPORTFOLIO' edges and 'CONTAINS' edges to return all Project objects which are either linked off of the main Portfolio or off of SUBPORTFOLIO edge'd Portfolios?

      //Here is my current traversal description
      @Override
      public TraversalDescription build(Object o, Neo4jPersistentProperty npp, String... strings) {
      return new TraversalDescriptionImpl()
      .breadthFirst()
      .relationships(DynamicRelationshipType.withName("S UBPORTFOLIO"));
      }

      Comment


      • #4
        Why is @Fetch required? Is there a way to programmaticly trigger this fetch?

        Comment


        • #5
          I think I can get it to follow both the "SUBPORTFOLIO" and "CONTAINS" edges by adding an additional .relationships(DynamicRelationshipType.withName("C ONTAINS")) to the relationship chain, but I'm still running into the problem that it is trying to return Portfolio objects when I only want Project objects back. Is there a way to filter the results prior to insertion into the Iterable to throw out the intermediate Nodes which don't match the specified class?

          Comment


          • #6
            I added an Evaluator to the chain but now the Iterable for the traversal is no longer being populated

            I am trying to follow the SUBPORTFOLIO and CONTAINS edges and only return the Project nodes which are found by following the CONTAINS edges.


            @Override
            public TraversalDescription build(Object o, Neo4jPersistentProperty npp, String... strings) {
            return new TraversalDescriptionImpl()
            .breadthFirst()
            .relationships(DynamicRelationshipType.withName("S UBPORTFOLIO"))
            .relationships(DynamicRelationshipType.withName("C ONTAINS"))
            .evaluator(new Evaluator() {
            @Override
            public Evaluation evaluate(Path path) {
            if (path.lastRelationship() == null)
            return Evaluation.EXCLUDE_AND_CONTINUE;

            if ("SUBPORTFOLIO".equals(path.lastRelationship().get Type().name()))
            return Evaluation.EXCLUDE_AND_CONTINUE;

            return Evaluation.INCLUDE_AND_CONTINUE;
            }
            });
            }

            Comment


            • #7
              I added @Fetch, but now it is telling me that the classes are incorrect...
              What exactly did it complain about?

              Did you run the traversal using just the Neo4j Core API ?
              Code:
              Node portfolio=template.getPersistentState(portfolio);
              TraversalDescription desc = Traversal.description().breadthFirst()
                .relationships(Direction.OUTGOING, DynamicRelationshipType.withName("SUBPORTFOLIO"))
                .relationships(Direction.OUTGOING, DynamicRelationshipType.withName("CONTAINS"));
              for (Node project : desc.traverse(portfolio).nodes()) {
                 println project
              }

              Comment


              • #8
                It should be possible to do:

                @GraphTraversal(traversal = ProjectTraversalBuilder.class,elementClass = Project.class)
                Iterable<Project> projects;

                If it doesn't it's a bug.

                Do you have the code around somewhere?

                Comment


                • #9
                  Had to add @Fetch to get the traversal to kick off.

                  Originally posted by MichaelHunger View Post
                  It should be possible to do:

                  @GraphTraversal(traversal = ProjectTraversalBuilder.class,elementClass = Project.class)
                  Iterable<Project> projects;

                  If it doesn't it's a bug.

                  Do you have the code around somewhere?

                  Comment


                  • #10
                    I was getting an error because it was returning both Portfolio and Project nodes which have different property sets, adding in the Evaluator to EXCLUDE_AND_CONTINUE when it landed on a Portfolio worked after configuring AspectJ.

                    Originally posted by MichaelHunger View Post
                    What exactly did it complain about?

                    Did you run the traversal using just the Neo4j Core API ?
                    Code:
                    Node portfolio=template.getPersistentState(portfolio);
                    TraversalDescription desc = Traversal.description().breadthFirst()
                      .relationships(Direction.OUTGOING, DynamicRelationshipType.withName("SUBPORTFOLIO"))
                      .relationships(Direction.OUTGOING, DynamicRelationshipType.withName("CONTAINS"));
                    for (Node project : desc.traverse(portfolio).nodes()) {
                       println project
                    }

                    Comment


                    • #11
                      So it works now for you?

                      Great. Which part of the docs should be updated (and how) to assure this issue doesn't occur again?

                      Comment

                      Working...
                      X