在图形模型中,我有一个名为 timeCategory
的顶点,其中包含子节点 year-vertices
、month-vertices
和 day-vertices
。我可以用 IS_YEAR
来识别 edge-label
的年份,用 IS_MONTH
来识别月份,等等。
我想执行一个查询,返回给定年份范围内的所有顶点,类似于以下代码:
GremlinPipeline yearPipe = new GremlinPipeline(timeCategory).out("IS_YEAR").property("year")
.filter(new PipeFunction<Integer, Boolean>() {
public Boolean compute(Integer i)
{
return i < 2013 && i >= 2011;
}
});
for(Object v : yearPipe) {
//v is an Integer, but i need the vertex
System.out.println(v);
}
...这非常适合打印所有属性值(年份为整数),但我需要的是所有 VERTICES 的列表,其中年份属性在给定范围内。
我的第二个问题是关于从管道中的值创建交集。
例如:我有三个管道,一个年管道、一个月管道和一个日管道,这些管道包含标识年、月或日的顶点。
是否有可能将所有传出顶点都放到一个特殊的日期...
半代码:allVertices2012-01-01 = yearpipe.out() AND monthpipe.out() AND daypipe.out
当然,我可以在一些 for-each-loops 中做一些比较,但我想知道是否有一个(简单的)gremlin - 方法可以做得更好。
最佳答案
您的问题有两种解决方案。这是在 Gremlin-Groovy 中:
timeCategory.out('IS_YEAR').filter{year = it.getProperty('year'); year < 2013 && year >= 2011}
-或-
timeCategory.out('IS_YEAR').property('year').filter{it < 2013 && it >= 2011}.back(1)
这是你在 Gremlin-Java 代码中的答案(对于上面的第二种表示):
GremlinPipeline yearPipe = new GremlinPipeline(timeCategory).out("IS_YEAR").property("year")
.filter(new PipeFunction<Integer, Boolean>() {
public Boolean compute(Integer i)
{
if(i<2013 && i>=2011)
return true;
else
return false;
}
}).back(1);
关于date - 如何在 Java 中使用 GremlinPipeline 和 PipeFunctions 进行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068657/