Skip to content

Commit

Permalink
feat(reasoner): add config in UdfMng (#391)
Browse files Browse the repository at this point in the history
Co-authored-by: FishJoy <[email protected]>
  • Loading branch information
wangsff and fishjoy authored Nov 6, 2024
1 parent 37514d9 commit 84b9209
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -53,6 +58,12 @@ public boolean containsAny(String toCheckedStr, String[] keywords) {
*/
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 84b9209

Please sign in to comment.