【发布时间】:2021-08-01 02:42:20
【问题描述】:
我正在尝试以间隔遍历图表。每条边都有一个存储间隔的属性(“间隔”)。我正在使用withSack 将区间的交叉点传播到下一步。如果没有交叉点,则遍历应该停止。
例如:
V1 e1 V2 e2 V3 e3 V4
O----------------------O----------------------O----------------------O
^
[[1,3],[5,7]] [[4,6]] [[7,9]]
e1.interval e2.interval e3.interval
如果我以区间 [2,8] 从 V1 开始遍历,我希望它返回
V1: [[2,3],[5,7]]
V2: [[5,6]]
注意 V3 和 V4 不包括在内,因为 e2 上的相交区间在 6 处停止。
我正在使用 Tinkerpop Java API,为此,我定义了一个返回区间交集的方法,并尝试使用withSack(Lambda.biFunction(...))。该函数有带花括号的while循环({}),我认为它会导致gremlin服务器的脚本引擎出现问题。我得到的例外是:
Script28.groovy: 1: expecting '}', found 'return' @ line 1, column 520.
get(j).get(1)) i++; else j++;}return int
我将函数作为字符串传递给(Lambda.biFunction(...)),如下所示:
"x, y -> " +
"List<List<Long>> intersections = new ArrayList();" +
"if (x.isEmpty() || y.isEmpty()) return new ArrayList<>();" +
"int i = 0, j = 0;" +
"while (i < x.size() && j < y.size()) {" +
"long low = Math.max(x.get(i).get(0), y.get(j).get(0));" +
"long high = Math.min(x.get(i).get(1), y.get(j).get(1));" +
"if (low <= high) intersections.add(Arrays.asList(low, high));" +
"if (x.get(i).get(1) < y.get(j).get(1)) i++; else j++;" +
"}" +
"return intersections;";
为了可读性,我也放了原始函数:
public List<List<Long>> intersections(List<List<Long>> x, List<List<Long>> y) {
List<List<Long>> intersections = new ArrayList();
if (x.isEmpty() || y.isEmpty()) {
return new ArrayList<>();
}
int i = 0, j = 0;
while (i < x.size() && j < y.size()) {
long low = Math.max(x.get(i).get(0), y.get(j).get(0));
long high = Math.min(x.get(i).get(1), y.get(j).get(1));
if (low <= high) {
intersections.add(Arrays.asList(low, high));
}
if (x.get(i).get(1) < y.get(j).get(1)) {
i++;
} else {
j++;
}
}
return intersections;
}
我有两个问题:
- 如何将这样一个复杂的 lambda 函数传递给 gremlin 服务器?
- 有没有更好的方法来实现这一点?
【问题讨论】:
标签: gremlin graph-databases tinkerpop tinkerpop3 gremlin-server