Skip to content

Commit

Permalink
fix(reasoner): fix pattern schema extra (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
royzhao authored Sep 5, 2024
1 parent 5822cbc commit 579be8b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ object PatternOps {
val typeSet = conn.relTypes
val targetSet = pattern.getNode(conn.target).typeNames
val spoSet = sourceSet.flatMap(s =>
typeSet.flatMap(p => targetSet.map(o => new SPO(s, p, o).toString)))
typeSet.flatMap(p => targetSet.map(o => {
if (conn.direction == Direction.OUT) {
new SPO(s, p, o).toString
} else {
new SPO(o, p, s).toString
}
})))
typeMap.put(conn.alias, spoSet)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,4 +1077,38 @@ private void doTest20() {
Assert.assertEquals("root", result.get(0)[0]);
Assert.assertEquals("100", result.get(0)[1]);
}

@Test
public void test21() {
FileMutex.runTestWithMutex(this::doTest21);
}

private void doTest21() {
String dsl =
"\n"
+ "GraphStructure {\n"
+ " A [FilmStar] \n"
+ " B [Film,__start__='true'] \n"
+ " C [Film] \n"
+ "\n"
+ " B->A [starOfFilm, __optional__='true'] as BA \n"
+ " C->A [starOfFilm, __optional__='true'] as CA\n"
+ "\n"
+ "}\n"
+ "Rule {\n"
+ " R2: exist(BA) and BA.joinTs >=100\n"
+ " R3: exist(CA) and CA.joinTs >=100\n"
+ "}\n"
+ "Action {\n"
+ " get(\n"
+ " A.id,\n"
+ " B.id,\n"
+ " BA.joinTs,\n"
+ " C.id,\n"
+ " CA.joinTs\n"
+ " ) \n"
+ "}";
List<String[]> result = runTestResult(dsl);
Assert.assertEquals(11, result.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void testTransitiveWithComputeAcc() {
+ " A->B [holdShare] repeat(1,10) as e\n"
+ "}\n"
+ "Rule {\n"
+ "totalRate = e.edges().reduce((x,y) => y.rate * x, 1)"
+ "totalRate = e.edges().reduce((x,y) => y.rate * x, 1.0)\n"
+ " R1(\"只保留最长的路径\"): group(A).keep_longest_path(e)\n"
+ "}\n"
+ "Action {\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public void testRepeatDsl() {
dSet.add(row[3]);
}
Assert.assertTrue(dSet.contains("D_333"));
Assert.assertTrue(dSet.contains(null));
Assert.assertTrue(dSet.contains("null"));

dataGraphStr =
"Graph {\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.antgroup.openspg.reasoner.udf.rule.RuleRunner;
import com.antgroup.openspg.reasoner.utils.RunnerUtil;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -60,8 +61,6 @@ public Object[] toRow(KgGraph<IVertexId> path) {

if (null == selectValue) {
row[i] = null;
} else if (this.forceOutputString) {
row[i] = String.valueOf(selectValue);
} else {
FieldType type = FieldType.fromKgType(fieldType);
if (FieldType.STRING.equals(type)) {
Expand All @@ -88,10 +87,30 @@ public Object[] toRow(KgGraph<IVertexId> path) {
row[i] = selectValue;
}
}
row[i] = convert2OutputFormat(row[i]);
}
return row;
}

private Object convert2OutputFormat(Object obj) {
if (!this.forceOutputString) {
return obj;
}
if (obj == null) {
return "null";
}

if (obj instanceof Double || obj instanceof Float) {
// 将Double或Float转换为BigDecimal
BigDecimal bd = new BigDecimal(obj.toString());
// 使用toPlainString以避免科学计数法
return bd.toPlainString();
} else {
// 对于其他类型,直接调用toString方法
return obj.toString();
}
}

public static Object getSelectValue(
String alias, String propertyName, Map<String, Object> context) {
if (Constants.PROPERTY_JSON_KEY.equals(propertyName)) {
Expand Down

0 comments on commit 579be8b

Please sign in to comment.