【问题标题】:Neo4j Reordering a pathNeo4j 重新排序路径
【发布时间】:2013-08-20 15:12:03
【问题描述】:

Neo4j 中是否有(可能使用 PathExpanderRelationshipExpander)通过 java 中关系的属性(在我的情况下为时间戳)重新排序遍历所采用的路径?

我搜索了几乎所有的 api 和社区讨论,但找不到提示。

【问题讨论】:

    标签: java graph neo4j graph-theory


    【解决方案1】:

    您可以创建一个路径扩展器,根据属性的值扩展路径,类似这样(假设您想要一个递增的顺序)。

    public class OrderPathExpander implements PathExpander<String> {
    
    private final RelationshipType relationshipType;
    private final Direction direction;
    
    public OrderPathExpander( RelationshipType relationshipType, Direction direction )
    {
        this.relationshipType = relationshipType;
        this.direction = direction;
    }
    @Override
    public Iterable<Relationship> expand(Path path, BranchState<String> state)
    {
        List<Relationship> results = new ArrayList<Relationship>();
        if ( path.length() == 0 ) {
            for ( Relationship r : path.endNode().getRelationships( relationshipType, direction ) )
            {
                    results.add( r );
    
            }
    
        }
        else {
        for ( Relationship r : path.endNode().getRelationships( relationshipType, direction ) )
        {
            if ( r.getProperty("timestamp") >= (path.lastRelationship().getProperty("timestamp"))  )
            {
                results.add( r );
            }
        }
        }
        return results;
    }
    @Override
    public PathExpander<String> reverse()
    {
        return null;
    }
    

    }

    然后在你的遍历中使用你的路径扩展器,

    TraversalDescription td = Traversal.description()
            .breadthFirst()
            .expand(new OrderPathExpander(YourRelationshipType, Direction.INCOMING))
            .evaluator(new Evaluator() {...});
    

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 2019-03-24
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多