feat(reasoner): support get_value udf (#196)

This commit is contained in:
Donghai 2024-04-09 17:20:24 +08:00 committed by GitHub
parent b169228efb
commit 98be8a8282
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

View File

@ -133,18 +133,20 @@ public class RuleRunnerTest {
Map<String, Object> context = new HashMap<>(); Map<String, Object> context = new HashMap<>();
context.put("A", 1000); context.put("A", 1000);
context.put("B", 100); context.put("B", 100);
context.put("A/B*C", 200);
boolean rst = boolean rst =
RuleRunner.getInstance() RuleRunner.getInstance()
.check( .check(
context, context,
Lists.newArrayList( Lists.newArrayList(
"R0 = get_value(\"A/B*C\") == 200 && get_value() == null && get_value(\"B\") == 100",
"R1 = A + B == 1100", "R1 = A + B == 1100",
"R2 = A > B", "R2 = A > B",
"R3 = A >= B", "R3 = A >= B",
"R4 = B < A", "R4 = B < A",
"R5 = B <= A", "R5 = B <= A",
"R6 = A != B", "R6 = A != B",
"R1 && R2 && R3 && R4 && R5 && R6"), "R0 && R1 && R2 && R3 && R4 && R5 && R6"),
""); "");
Assert.assertTrue(rst); Assert.assertTrue(rst);
} }

View File

@ -20,6 +20,7 @@ import com.antgroup.openspg.reasoner.udf.UdfMngFactory;
import com.antgroup.openspg.reasoner.udf.model.RuntimeUdfMeta; import com.antgroup.openspg.reasoner.udf.model.RuntimeUdfMeta;
import com.antgroup.openspg.reasoner.udf.model.UdfOperatorTypeEnum; import com.antgroup.openspg.reasoner.udf.model.UdfOperatorTypeEnum;
import com.antgroup.openspg.reasoner.udf.rule.op.OperatorEqualsLessMore; import com.antgroup.openspg.reasoner.udf.rule.op.OperatorEqualsLessMore;
import com.antgroup.openspg.reasoner.udf.rule.op.OperatorGetValue;
import com.antgroup.openspg.reasoner.udf.rule.op.OperatorIn; import com.antgroup.openspg.reasoner.udf.rule.op.OperatorIn;
import com.antgroup.openspg.reasoner.udf.rule.op.OperatorLike; import com.antgroup.openspg.reasoner.udf.rule.op.OperatorLike;
import com.antgroup.openspg.reasoner.udf.rule.op.OperatorMultiDiv; import com.antgroup.openspg.reasoner.udf.rule.op.OperatorMultiDiv;
@ -179,6 +180,7 @@ public class RuleRunner {
EXPRESS_RUNNER.setShortCircuit(true); EXPRESS_RUNNER.setShortCircuit(true);
registerUdf(); registerUdf();
overrideOperator(); overrideOperator();
EXPRESS_RUNNER.addFunction("get_value", new OperatorGetValue());
} }
/** register all udfs */ /** register all udfs */

View File

@ -0,0 +1,42 @@
/*
* Copyright 2023 OpenSPG Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
*/
package com.antgroup.openspg.reasoner.udf.rule.op;
import com.ql.util.express.ArraySwap;
import com.ql.util.express.InstructionSetContext;
import com.ql.util.express.OperateData;
import com.ql.util.express.config.QLExpressRunStrategy;
import com.ql.util.express.instruction.OperateDataCacheManager;
import com.ql.util.express.instruction.op.OperatorBase;
public class OperatorGetValue extends OperatorBase {
@Override
public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
Object[] parameters = new Object[list.length];
for (int i = 0; i < list.length; ++i) {
if (list.get(i) == null && QLExpressRunStrategy.isAvoidNullPointer()) {
parameters[i] = null;
} else {
parameters[i] = list.get(i).getObject(parent);
}
}
if (parameters.length <= 0) {
return OperateDataCacheManager.fetchOperateData(null, Object.class);
}
String key = String.valueOf(parameters[0]);
Object result = parent.getParent().get(key);
return OperateDataCacheManager.fetchOperateData(result, Object.class);
}
}