feat(reasoner): add config in UdfMng (#391)

Co-authored-by: FishJoy <chengqiang.cq@antgroup.com>
This commit is contained in:
wangshaofei 2024-11-06 11:50:24 +08:00 committed by GitHub
parent 37514d94f5
commit 84b9209ed9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View File

@ -85,4 +85,7 @@ public class Constants {
Sets.newHashSet("isA", "locateAt");
public static final String CONCEPT_EDGE_EXPAND_FUNC_NAME = "concept_edge_expand";
/** allow throw exception in udf */
public static final String ALLOW_UDF_EXCEPTION = "allowUDFThrowException";
}

View File

@ -13,11 +13,16 @@
package com.antgroup.openspg.reasoner.udf.builtin.udf;
import com.antgroup.openspg.reasoner.common.constants.Constants;
import com.antgroup.openspg.reasoner.udf.impl.UdfMngImpl;
import com.antgroup.openspg.reasoner.udf.model.UdfDefine;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
public class ContainsAny {
public ContainsAny() {}
/**
* Contains any function. Return true if the first element of the input array (a string) contains
* one or more of the remaining elements, otherwise return false. Such as: 1."adfvg", ["as, "gv"]
@ -53,6 +58,12 @@ public class ContainsAny {
*/
@UdfDefine(name = "contains_any", compatibleName = "contains")
public boolean contains(String toCheckedStr, String keyword) {
Map<String, Object> configMap = UdfMngImpl.sceneConfigMap.get();
Boolean allowUDFThrowException =
(Boolean) configMap.getOrDefault(Constants.ALLOW_UDF_EXCEPTION, false);
if ((toCheckedStr == null || keyword == null) && allowUDFThrowException) {
throw new RuntimeException("contains_any arguments is null");
}
if (StringUtils.isEmpty(toCheckedStr) || null == keyword) {
return false;
}

View File

@ -54,6 +54,9 @@ public class UdfMngImpl implements UdfMng {
private static volatile UdfMngImpl instance = null;
public static ThreadLocal<Map<String, Object>> sceneConfigMap =
ThreadLocal.withInitial(HashMap::new);
/** 单例 */
public static UdfMngImpl getInstance() {
if (null == instance) {