308 changed files with 19067 additions and 8277 deletions
@ -1,169 +1,33 @@ |
|||||
package io.sc.engine.rule.client; |
package io.sc.engine.rule.client; |
||||
|
|
||||
|
import io.sc.engine.rule.core.ResourceResult; |
||||
|
|
||||
import java.util.Map; |
import java.util.Map; |
||||
|
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceResult; |
|
||||
|
|
||||
/** |
/** |
||||
* 客户端执行器接口 |
* 客户端执行器接口 |
||||
*/ |
*/ |
||||
public interface Executor { |
public interface Executor { |
||||
/** |
/** |
||||
* 编译资源 |
* 获取资源定义加载器 |
||||
* @param resourceId 资源唯一标识 |
* @return 资源定义加载器 |
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public void compileById(String resourceId) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 编译资源 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public void compileByCode(String resourceCode,Integer version) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param json 需要提交给资源的参数 json 字符串 |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeById(String resourceId,String json) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param json 需要提交给资源的参数 json 字符串 |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeById(String resourceId,String subModelCode,String json) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeById(String resourceId,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeById(String resourceId,String subModelCode,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param json 需要提交给资源的参数 json 字符串 |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCode(String resourceCode,Integer version,String json) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param json 需要提交给资源的参数 json 字符串 |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCode(String resourceCode,Integer version,String subModelCode,String json) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCode(String resourceCode,Integer version,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCode(String resourceCode,Integer version,String subModelCode,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
/** |
|
||||
* 编译资源(测试) |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
*/ |
||||
public void compileByIdForTest(String resourceId) throws Exception; |
public Loader getLoader(); |
||||
|
|
||||
/** |
public void compile(String resourceId) throws Exception; |
||||
* 编译资源(测试) |
public void compile(String resourceCode,Integer version) throws Exception; |
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public void compileByCodeForTest(String resourceCode,Integer version) throws Exception; |
|
||||
|
|
||||
/** |
public ResourceResult execute(String resourceId,Map<String,Object> map) throws Exception; |
||||
* 执行资源(测试) |
public ResourceResult execute(String resourceId, String json) throws Exception; |
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByIdForTest(String resourceId,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
public ResourceResult execute(String resourceId,String subModelCode,Map<String,Object> map) throws Exception; |
||||
* 执行资源(测试) |
public ResourceResult execute(String resourceId,String subModelCode,String json) throws Exception; |
||||
* @param resourceId 资源唯一标识 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByIdForTest(String resourceId,String subModelCode,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 执行资源(测试) |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCodeForTest(String resourceCode,Integer version,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
public ResourceResult execute(String resourceCode,Integer version,Map<String,Object> map) throws Exception; |
||||
* 执行资源(测试) |
public ResourceResult execute(String resourceCode,Integer version,String json) throws Exception; |
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param map 需要提交给资源的参数 map |
|
||||
* @return 执行结果 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceResult executeByCodeForTest(String resourceCode,Integer version,String subModelCode,Map<String,Object> map) throws Exception; |
|
||||
|
|
||||
/** |
public ResourceResult execute(String resourceCode,Integer version,String subModelCode,Map<String,Object> map) throws Exception; |
||||
* 获取资源定义加载器 |
public ResourceResult execute(String resourceCode,Integer version,String subModelCode,String json) throws Exception; |
||||
* @return 资源定义加载器 |
|
||||
*/ |
|
||||
public Loader getLoader(); |
|
||||
} |
} |
||||
|
@ -1,44 +1,12 @@ |
|||||
package io.sc.engine.rule.client; |
package io.sc.engine.rule.client; |
||||
|
|
||||
import io.sc.engine.rule.core.classes.ResourceAbstract; |
import io.sc.engine.rule.core.code.ExecuteUnit; |
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
|
|
||||
/** |
/** |
||||
* 客户端模型定义加载器接口 |
* 客户端模型定义加载器接口 |
||||
*/ |
*/ |
||||
public interface Loader { |
public interface Loader { |
||||
/** |
public ExecuteUnit load(String resourceId) throws Exception; |
||||
* 获取资源定义 |
public ExecuteUnit load(String resourceCode,Integer version) throws Exception; |
||||
* 说明: 通过 ID 加载资源定义时,系统不做缓存 |
|
||||
* @param resourceId 资源唯一标识 |
|
||||
* @return 资源定义 PO 对象 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceWrapper getResourceById(String resourceId) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 获取资源定义 |
|
||||
* 说明: 通过代码和版本加载资源定义时,如果有版本,则缓存,反之,不缓存 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 资源版本 |
|
||||
* @return 资源定义 PO 对象 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public ResourceWrapper getResourceByCode(String resourceCode,Integer version) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 获取所有顶级资源摘要信息 |
|
||||
* @return 所有顶级资源摘要信息 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public List<ResourceAbstract> getAllReleasableResourceAbstract() throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 清除已经加载的模型定义缓存 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public void cleanCache() throws Exception; |
|
||||
} |
} |
||||
|
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import io.sc.engine.rule.core.enums.ParameterType; |
import io.sc.engine.rule.core.enums.ParameterType; |
||||
|
|
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
/** |
/** |
||||
* 模型摘要信息 |
* 模型摘要信息 |
@ -1,8 +1,6 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import io.sc.engine.rule.core.classes.RuleResult; |
|
||||
import io.sc.engine.rule.core.classes.SingleRuleResult; |
|
||||
import io.sc.engine.rule.core.enums.ParameterType; |
import io.sc.engine.rule.core.enums.ParameterType; |
||||
import io.sc.platform.util.ObjectMapperUtil; |
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
|
@ -1,13 +1,11 @@ |
|||||
package io.sc.engine.rule.core.classes; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import java.util.Date; |
|
||||
|
|
||||
import io.sc.engine.rule.core.enums.DeployStatus; |
import io.sc.engine.rule.core.enums.DeployStatus; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
/** |
/** |
||||
* 资源摘要信息 |
* 资源摘要信息 |
||||
* @author wangshaoping |
|
||||
* |
|
||||
*/ |
*/ |
||||
public class ResourceAbstract { |
public class ResourceAbstract { |
||||
private String id; |
private String id; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import io.sc.engine.rule.core.enums.ParameterType; |
import io.sc.engine.rule.core.enums.ParameterType; |
@ -1,9 +1,7 @@ |
|||||
package io.sc.engine.rule.core.classes; |
package io.sc.engine.rule.core; |
||||
|
|
||||
/** |
/** |
||||
* 规则信息 |
* 规则信息 |
||||
* @author wangshaoping |
|
||||
* |
|
||||
*/ |
*/ |
||||
public class Rule { |
public class Rule { |
||||
private boolean isTriggered; //是否触发
|
private boolean isTriggered; //是否触发
|
@ -1,12 +1,10 @@ |
|||||
package io.sc.engine.rule.core.classes; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import io.sc.platform.util.ObjectMapperUtil; |
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
|
||||
/** |
/** |
||||
* 单规则结果值 |
* 单规则结果值 |
||||
* @author wangshaoping |
|
||||
* |
|
||||
*/ |
*/ |
||||
public class SingleRuleResult { |
public class SingleRuleResult { |
||||
private boolean triggered; //是否触发
|
private boolean triggered; //是否触发
|
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
public class ValidateField { |
public class ValidateField { |
||||
private String fieldCode; |
private String fieldCode; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core; |
||||
|
|
||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||
import java.util.List; |
import java.util.List; |
@ -1,29 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code; |
|
||||
|
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper; |
|
||||
|
|
||||
/** |
|
||||
* 代码生成器接口 |
|
||||
* @author wangshaoping |
|
||||
* |
|
||||
*/ |
|
||||
public interface CodeGenerator { |
|
||||
/** |
|
||||
* 生成可执行的源代码 |
|
||||
* @param resourceId 资源ID |
|
||||
* @param wrapper 资源封装器对象 |
|
||||
* @return 源代码封装对象 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public SourceCode generateSourceCodeById(String resourceId,ResourceWrapper wrapper) throws Exception; |
|
||||
|
|
||||
/** |
|
||||
* 生成可执行的源代码 |
|
||||
* @param resourceCode 资源代码 |
|
||||
* @param version 模型版本 |
|
||||
* @param wrapper 资源封装器对象 |
|
||||
* @return 源代码封装对象 |
|
||||
* @throws Exception 违例 |
|
||||
*/ |
|
||||
public SourceCode generateSourceCodeByCode(String resourceCode,Integer version,ResourceWrapper wrapper) throws Exception; |
|
||||
} |
|
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core.code; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonSubTypes; |
import com.fasterxml.jackson.annotation.JsonSubTypes; |
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core.code; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
||||
import io.sc.engine.rule.core.po.lib.Lib; |
import io.sc.engine.rule.core.po.lib.Lib; |
@ -1,10 +1,8 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
package io.sc.engine.rule.core.code; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
||||
import io.sc.engine.rule.core.po.lib.Lib; |
import io.sc.engine.rule.core.po.lib.Lib; |
||||
import io.sc.engine.rule.core.po.model.Model; |
|
||||
import io.sc.engine.rule.core.po.resource.Resource; |
import io.sc.engine.rule.core.po.resource.Resource; |
||||
import io.sc.engine.rule.core.vo.resource.ModelResource; |
|
||||
|
|
||||
import java.util.List; |
import java.util.List; |
||||
|
|
@ -1,7 +1,8 @@ |
|||||
package io.sc.engine.rule.core.code; |
package io.sc.engine.rule.core.code; |
||||
|
|
||||
import io.sc.engine.rule.core.code.impl.support.ExecuteUnit; |
|
||||
|
|
||||
public interface SourceCodeGenerator { |
public interface SourceCodeGenerator { |
||||
public SourceCode generateSourceCode(ExecuteUnit executeUnit) throws Exception; |
public SourceCode generate(ExecuteUnit executeUnit) throws Exception; |
||||
|
public SourceCode generate(String resourceId,ExecuteUnit executeUnit) throws Exception; |
||||
|
public SourceCode generate(String resourceCode,Integer version,ExecuteUnit executeUnit) throws Exception; |
||||
} |
} |
||||
|
@ -0,0 +1,27 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator; |
||||
|
|
||||
|
import io.sc.engine.rule.core.code.*; |
||||
|
|
||||
|
public class GroovySourceCodeGenerator implements SourceCodeGenerator { |
||||
|
@Override |
||||
|
public SourceCode generate(ExecuteUnit executeUnit) throws Exception { |
||||
|
if(executeUnit instanceof ExecuteUnit4Resource){ |
||||
|
ExecuteUnitGenerator generator =new ExecuteUnit4ResourceGenerator((ExecuteUnit4Resource)executeUnit); |
||||
|
return generator.generateSourceCode(); |
||||
|
}else if(executeUnit instanceof ExecuteUnit4Lib){ |
||||
|
ExecuteUnitGenerator generator =new ExecuteUnit4LibGenerator((ExecuteUnit4Lib)executeUnit); |
||||
|
return generator.generateSourceCode(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public SourceCode generate(String resourceId, ExecuteUnit executeUnit) throws Exception { |
||||
|
return generate(executeUnit); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public SourceCode generate(String resourceCode, Integer version, ExecuteUnit executeUnit) throws Exception { |
||||
|
return null; |
||||
|
} |
||||
|
} |
@ -1,9 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.resource.Resource; |
|
||||
|
|
||||
public class ArgumentClassGenerator { |
|
||||
public static String generate(Resource resource) { |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
@ -1,13 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import org.springframework.util.StreamUtils; |
|
||||
|
|
||||
import java.nio.charset.StandardCharsets; |
|
||||
|
|
||||
public class CommonImportGenerator { |
|
||||
private static final String TEMPLATE_CLASSPATH = "/io/sc/engine/rule/core/code/generator/template/Import.tpl"; |
|
||||
|
|
||||
public static String generate() throws Exception{ |
|
||||
return StreamUtils.copyToString(ResourceClassGenerator.class.getResourceAsStream(TEMPLATE_CLASSPATH), StandardCharsets.UTF_8); |
|
||||
} |
|
||||
} |
|
@ -1,11 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.dictionary.Dictionary; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
public class DictionaryClassGenerator { |
|
||||
public static String generate(List<Dictionary> dictionaries) { |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,75 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl; |
||||
|
|
||||
|
import io.sc.engine.rule.core.po.dictionary.Dictionary; |
||||
|
import io.sc.engine.rule.core.po.dictionary.UserDefinedJavaClassField; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.engine.rule.core.util.ValueTypeUtil; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class DictionaryGenerator { |
||||
|
public String generateFields(List<UserDefinedJavaClassField> fields){ |
||||
|
if(!CollectionUtil.hasElements(fields)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(UserDefinedJavaClassField field : fields){ |
||||
|
String code =field.getCode(); |
||||
|
String name =field.getName(); |
||||
|
String valueType =field.getValueType(); |
||||
|
Integer valueTypeVersion =field.getValueTypeVersion(); |
||||
|
String defaultValue =field.getDefaultValue(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName", IdReplacer.fieldName(code)); |
||||
|
variables.put("defaultValue",defaultValue); |
||||
|
if(ValueTypeUtil.isString(valueType)){ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
String tpl ="String ${fieldName} =\"\"\"${defaultValue}\"\"\"; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="String ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
}else{ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName} =${defaultValue}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public String generateInits(List<UserDefinedJavaClassField> fields){ |
||||
|
if(!CollectionUtil.hasElements(fields)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(UserDefinedJavaClassField field : fields){ |
||||
|
String code =field.getCode(); |
||||
|
String name =field.getName(); |
||||
|
String valueCalculation =field.getValueCalculation(); |
||||
|
|
||||
|
if(StringUtils.hasText(valueCalculation)) { |
||||
|
Map<String, Object> variables = new HashMap<>(); |
||||
|
variables.put("name", name); |
||||
|
variables.put("fieldName", IdReplacer.fieldName(code)); |
||||
|
variables.put("expression", GroovyExpressionReplacer.groovy(valueCalculation)); |
||||
|
|
||||
|
String tpl = "${fieldName} =${expression}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl, variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
@ -1,29 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.function.Function; |
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
import org.springframework.util.StreamUtils; |
|
||||
|
|
||||
import java.nio.charset.StandardCharsets; |
|
||||
import java.util.List; |
|
||||
|
|
||||
public class FunctionGenerator { |
|
||||
private static final String TEMPLATE_CLASSPATH = "/io/sc/engine/rule/core/code/generator/template/Function.tpl"; |
|
||||
|
|
||||
public static String generate(List<Function> functions) throws Exception{ |
|
||||
String template =StreamUtils.copyToString(ResourceClassGenerator.class.getResourceAsStream(TEMPLATE_CLASSPATH), StandardCharsets.UTF_8); |
|
||||
StringBuilder sb =new StringBuilder(""); |
|
||||
if(functions!=null && !functions.isEmpty()){ |
|
||||
for(Function function : functions){ |
|
||||
String[] pattern =new String[]{ |
|
||||
"/**", |
|
||||
" * {0}", |
|
||||
" */", |
|
||||
}; |
|
||||
sb.append(StringUtil.format(pattern,function.getDescription())); |
|
||||
sb.append(function.getBody()).append("\n\n"); |
|
||||
} |
|
||||
} |
|
||||
return StringUtil.format(template,sb.toString()); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,256 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl; |
||||
|
|
||||
|
import io.sc.engine.rule.core.enums.ParameterType; |
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.IndicatorValidator; |
||||
|
import io.sc.engine.rule.core.po.lib.validator.*; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.ParameterValidator; |
||||
|
import io.sc.engine.rule.core.po.model.validator.*; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.engine.rule.core.util.ValueTypeUtil; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.math.RoundingMode; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class IndicatorGenerator { |
||||
|
public String generateFields(List<Indicator> indicators){ |
||||
|
if(!CollectionUtil.hasElements(indicators)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Indicator indicator : indicators){ |
||||
|
String code =indicator.getCode(); |
||||
|
String name =indicator.getName(); |
||||
|
String valueType =indicator.getValueType(); |
||||
|
Integer valueTypeVersion =indicator.getValueTypeVersion(); |
||||
|
String defaultValue =indicator.getDefaultValue(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName", IdReplacer.fieldName(code)); |
||||
|
variables.put("defaultValue",defaultValue); |
||||
|
if(ValueTypeUtil.isString(valueType)){ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
String tpl ="String ${fieldName} =\"\"\"${defaultValue}\"\"\"; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="String ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
}else{ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName} =${defaultValue}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateConvertorFromMap(List<Indicator> indicators) { |
||||
|
if (!CollectionUtil.hasElements(indicators)) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(""); |
||||
|
for (Indicator indicator : indicators) { |
||||
|
String code = indicator.getCode(); |
||||
|
String name = indicator.getName(); |
||||
|
String valueType = indicator.getValueType(); |
||||
|
Integer valueTypeVersion = indicator.getValueTypeVersion(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
if (indicator.getValueTypeIsList()) { |
||||
|
String tpl ="arg.${fieldName} =TypeConvertor.getValue(map.get('${code}'),new TypeReference<List<${className}>>(){}); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} else { |
||||
|
String tpl ="arg.${fieldName} =TypeConvertor.getValue(map.get('${code}'),${className}.class); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
if (!ValueTypeUtil.isBase(valueType)) { |
||||
|
String tpl ="if(arg.${fieldName}!=null) { arg.${fieldName}.init(); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateConvertorFromJson(List<Indicator> indicators) { |
||||
|
if (!CollectionUtil.hasElements(indicators)) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(""); |
||||
|
for (Indicator indicator : indicators) { |
||||
|
String code = indicator.getCode(); |
||||
|
String name = indicator.getName(); |
||||
|
String valueType = indicator.getValueType(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
variables.put("name",name); |
||||
|
if (!ValueTypeUtil.isBase(valueType)) { |
||||
|
String tpl ="if(arg.${fieldName}!=null) { arg.${fieldName}.init(); } //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateValidator(List<Indicator> indicators) { |
||||
|
if (!CollectionUtil.hasElements(indicators)) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(""); |
||||
|
for (Indicator indicator : indicators) { |
||||
|
String code =indicator.getCode(); |
||||
|
String name =indicator.getName(); |
||||
|
if(CollectionUtil.hasElements(indicator.getValidators())){ |
||||
|
for(IndicatorValidator validator : indicator.getValidators()){ |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
if(validator instanceof EmptyIndicatorValidator){ |
||||
|
String tpl ="FieldValidator.empty('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof NotEmptyIndicatorValidator){ |
||||
|
String tpl ="FieldValidator.notEmpty('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof TrueIndicatorValidator){ |
||||
|
String tpl ="FieldValidator.trueValue('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof FalseIndicatorValidator){ |
||||
|
String tpl ="FieldValidator.falseValue('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof EmailIndicatorValidator){ |
||||
|
String tpl ="FieldValidator.mail('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof IntegerRangeIndicatorValidator){ |
||||
|
IntegerRangeIndicatorValidator integerRangeIndicatorValidator =(IntegerRangeIndicatorValidator)validator; |
||||
|
variables.put("minInclude",integerRangeIndicatorValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(integerRangeIndicatorValidator.getMinValue())?integerRangeIndicatorValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(integerRangeIndicatorValidator.getMaxValue())?integerRangeIndicatorValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",integerRangeIndicatorValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.integerRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof DecimalRangeIndicatorValidator){ |
||||
|
DecimalRangeIndicatorValidator decimalRangeIndicatorValidator =(DecimalRangeIndicatorValidator)validator; |
||||
|
variables.put("minInclude",decimalRangeIndicatorValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(decimalRangeIndicatorValidator.getMinValue())?decimalRangeIndicatorValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(decimalRangeIndicatorValidator.getMaxValue())?decimalRangeIndicatorValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",decimalRangeIndicatorValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.decimalRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof LengthRangeIndicatorValidator){ |
||||
|
LengthRangeIndicatorValidator lengthRangeIndicatorValidator =(LengthRangeIndicatorValidator)validator; |
||||
|
variables.put("minInclude",lengthRangeIndicatorValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(lengthRangeIndicatorValidator.getMinValue())?lengthRangeIndicatorValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(lengthRangeIndicatorValidator.getMaxValue())?lengthRangeIndicatorValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",lengthRangeIndicatorValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.lengthRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof DateRangeIndicatorValidator){ |
||||
|
DateRangeIndicatorValidator dateRangeIndicatorValidator =(DateRangeIndicatorValidator)validator; |
||||
|
variables.put("minInclude",dateRangeIndicatorValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(dateRangeIndicatorValidator.getMinValue())?"DateUtil.parseDate(\"" + dateRangeIndicatorValidator.getMinValue() + "\",DateUtil.yyyy_MM_dd_HH_mm_ss)":"null"); |
||||
|
variables.put("max",StringUtils.hasText(dateRangeIndicatorValidator.getMaxValue())?"DateUtil.parseDate(\"" + dateRangeIndicatorValidator.getMaxValue() + "\",DateUtil.yyyy_MM_dd_HH_mm_ss)":"null"); |
||||
|
variables.put("maxInclude",dateRangeIndicatorValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.dateRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof PatternIndicatorValidator){ |
||||
|
PatternIndicatorValidator patternIndicatorValidator =(PatternIndicatorValidator)validator; |
||||
|
variables.put("pattern",StringUtils.hasText(patternIndicatorValidator.getPattern())?patternIndicatorValidator.getPattern():"null"); |
||||
|
String tpl ="FieldValidator.dateRange('${code}','${name}',this.${fieldName},'${pattern}',result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateToResult(List<Indicator> indicators) { |
||||
|
if(!CollectionUtil.hasElements(indicators)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Indicator indicator : indicators){ |
||||
|
String type =indicator.getType().toString(); |
||||
|
String code =indicator.getCode(); |
||||
|
String name =indicator.getName(); |
||||
|
String valueType =indicator.getValueType(); |
||||
|
Integer valueScale =indicator.getValueScale(); |
||||
|
RoundingMode valueRoundingMode =indicator.getValueRoundingMode(); |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("type",type); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("valueType",valueType); |
||||
|
variables.put("valueScale",valueScale==null?8:valueScale); |
||||
|
variables.put("valueRoundingMode",valueRoundingMode==null?RoundingMode.HALF_UP:valueRoundingMode); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
|
||||
|
if(ParameterType.INDICATOR.equals(indicator.getType()) || ParameterType.OUT.equals(indicator.getType()) || ParameterType.INTERMEDIATE.equals(indicator.getType())){ |
||||
|
if(ValueTypeUtil.isDecimal(indicator.getValueType())){ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',${valueScale},RoundingMode.${valueRoundingMode},this.${fieldName}==null?null:this.${fieldName}.setScale(${valueScale},RoundingMode.${valueRoundingMode})));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(ValueTypeUtil.isBase(indicator.getValueType())){ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',null,null,this.${fieldName}==null?null:this.${fieldName}));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',null,null,this.${fieldName}==null?null:ObjectMapperUtil.json().writeValueAsString(this.${fieldName})));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateToMap(List<Indicator> indicators) { |
||||
|
if(!CollectionUtil.hasElements(indicators)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Indicator indicator : indicators){ |
||||
|
String code =indicator.getCode(); |
||||
|
String name =indicator.getName(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
|
||||
|
String tpl ="map.put('${code}',this.${fieldName}); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateMergeResult(List<Indicator> indicators) { |
||||
|
if(!CollectionUtil.hasElements(indicators)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Indicator indicator : indicators){ |
||||
|
String code =indicator.getCode(); |
||||
|
String valueType = indicator.getValueType(); |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("valueType",valueType); |
||||
|
if(ValueTypeUtil.isBase(valueType)){ |
||||
|
String tpl ="if(result.exists('${code}')){ this.${code}=TypeConvertor.getValue(result.getValueByParameterCode('${code}'),${valueType}.class); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="if(result.exists('${code}')){ this.${code}=result.getValueByParameterCode('${code}'); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
@ -1,12 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.dictionary.Dictionary; |
|
||||
import io.sc.engine.rule.core.po.lib.Lib; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
public class LibClassGenerator { |
|
||||
public static String generate(List<Lib> libs) { |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
@ -1,57 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.model.Model; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.platform.util.CollectionUtil; |
|
||||
import io.sc.platform.util.FileUtil; |
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
|
|
||||
import java.util.HashMap; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
public class ModelGenerator { |
|
||||
private static final String MODEL_TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/Model.tpl"; |
|
||||
private static final String MODEL_EXECUTE_METHOD_TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/ModelExecuteMethod.tpl"; |
|
||||
|
|
||||
public static String generate(Model model) throws Exception{ |
|
||||
String template = FileUtil.readString(MODEL_TEMPLATE_CLASSPATH); |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
|
|
||||
Map<String,Object> variables =new HashMap<>(); |
|
||||
variables.put("enable",model.getEnable()); |
|
||||
variables.put("name",model.getName()); |
|
||||
variables.put("methodName", CodeReplacer.methodName(model.getCode())); |
|
||||
variables.put("executeMode",model.getExecuteMode().toString()); |
|
||||
variables.put("executeModelMethods",generateExecuteMethods(model)); |
|
||||
variables.put("executeParameterMethods",ParameterGenerator.generateExecuteMethods(model)); |
|
||||
variables.put("modelMethodBodys",generateMethodBodys(model.getChildren())); |
|
||||
variables.put("parameterMethodBodys",ParameterGenerator.generateMethodBodys(model.getAllParameters())); |
|
||||
sb.append(StringUtil.format(template,variables)); |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
|
|
||||
public static String generateExecuteMethods(Model model) throws Exception { |
|
||||
StringBuilder sb =new StringBuilder(""); |
|
||||
if(CollectionUtil.hasElements(model.getChildren())){ |
|
||||
String template = FileUtil.readString(MODEL_EXECUTE_METHOD_TEMPLATE_CLASSPATH); |
|
||||
for(Model subModel : model.getChildren()) { |
|
||||
Map<String,Object> variables =new HashMap<>(); |
|
||||
variables.put("methodName", CodeReplacer.methodName(subModel.getCode())); |
|
||||
variables.put("comments",subModel.getName()); |
|
||||
sb.append(StringUtil.format(template,variables)); |
|
||||
} |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
|
|
||||
public static String generateMethodBodys(List<Model> models) throws Exception { |
|
||||
StringBuilder sb =new StringBuilder(""); |
|
||||
if(CollectionUtil.hasElements(models)){ |
|
||||
for(Model model : models) { |
|
||||
sb.append(generate(model)); |
|
||||
} |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
} |
|
@ -1,15 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
import org.springframework.util.StreamUtils; |
|
||||
|
|
||||
import java.nio.charset.StandardCharsets; |
|
||||
|
|
||||
public class PackageGenerator { |
|
||||
private static final String TEMPLATE_CLASSPATH = "/io/sc/engine/rule/core/code/generator/template/Package.tpl"; |
|
||||
|
|
||||
public static String generate(String packageName) throws Exception{ |
|
||||
String template =StreamUtils.copyToString(ResourceClassGenerator.class.getResourceAsStream(TEMPLATE_CLASSPATH), StandardCharsets.UTF_8); |
|
||||
return StringUtil.format(template,packageName); |
|
||||
} |
|
||||
} |
|
@ -1,60 +1,253 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
package io.sc.engine.rule.core.code.generator.impl; |
||||
|
|
||||
import io.sc.engine.rule.core.po.model.Model; |
import io.sc.engine.rule.core.enums.ParameterType; |
||||
import io.sc.engine.rule.core.po.model.Parameter; |
import io.sc.engine.rule.core.po.model.Parameter; |
||||
import io.sc.engine.rule.core.po.model.parameter.IntermediateParameter; |
import io.sc.engine.rule.core.po.model.ParameterValidator; |
||||
import io.sc.engine.rule.core.po.model.parameter.OutParameter; |
import io.sc.engine.rule.core.po.model.validator.*; |
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.engine.rule.core.util.ValueTypeUtil; |
||||
import io.sc.platform.util.CollectionUtil; |
import io.sc.platform.util.CollectionUtil; |
||||
import io.sc.platform.util.FileUtil; |
|
||||
import io.sc.platform.util.StringUtil; |
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.math.RoundingMode; |
||||
import java.util.HashMap; |
import java.util.HashMap; |
||||
import java.util.List; |
import java.util.List; |
||||
import java.util.Map; |
import java.util.Map; |
||||
|
|
||||
public class ParameterGenerator { |
public class ParameterGenerator { |
||||
private static final String PARAMETER_TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/Parameter.tpl"; |
public String generateFields(List<Parameter> parameters){ |
||||
private static final String PARAMETER_EXECUTE_METHOD_TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/ParameterExecuteMethod.tpl"; |
if(!CollectionUtil.hasElements(parameters)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
public static String generate(Parameter parameter) throws Exception{ |
for(Parameter parameter : parameters){ |
||||
String template = FileUtil.readString(PARAMETER_TEMPLATE_CLASSPATH); |
String code =parameter.getCode(); |
||||
StringBuilder sb =new StringBuilder(); |
String name =parameter.getName(); |
||||
|
String valueType =parameter.getValueType(); |
||||
Map<String,Object> variables =new HashMap<>(); |
Integer valueTypeVersion =parameter.getValueTypeVersion(); |
||||
variables.put("name",parameter.getName()); |
String defaultValue =parameter.getDefaultValue(); |
||||
variables.put("methodName", CodeReplacer.methodName(parameter.getCode())); |
|
||||
variables.put("type", parameter.getType().toString()); |
Map<String,Object> variables =new HashMap<>(); |
||||
sb.append(StringUtil.format(template,variables)); |
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
variables.put("defaultValue",defaultValue); |
||||
|
if(ValueTypeUtil.isString(valueType)){ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
String tpl ="String ${fieldName} =\"\"\"${defaultValue}\"\"\"; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="String ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
}else{ |
||||
|
if(StringUtils.hasText(defaultValue)){ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName} =${defaultValue}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
String tpl ="${className} ${fieldName}; //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
return sb.toString(); |
return sb.toString(); |
||||
} |
} |
||||
|
|
||||
public static String generateExecuteMethods(Model model) throws Exception { |
public static String generateConvertorFromMap(List<Parameter> parameters) { |
||||
StringBuilder sb =new StringBuilder(""); |
if (!CollectionUtil.hasElements(parameters)) { |
||||
if(CollectionUtil.hasElements(model.getParameters())){ |
return null; |
||||
String template = FileUtil.readString(PARAMETER_EXECUTE_METHOD_TEMPLATE_CLASSPATH); |
} |
||||
for(Parameter parameter : model.getParameters()) { |
StringBuilder sb = new StringBuilder(""); |
||||
if(parameter instanceof IntermediateParameter || parameter instanceof OutParameter) { |
for (Parameter parameter : parameters) { |
||||
Map<String, Object> variables = new HashMap<>(); |
String code = parameter.getCode(); |
||||
variables.put("methodName", CodeReplacer.methodName(parameter.getCode())); |
String name = parameter.getName(); |
||||
variables.put("name", parameter.getName()); |
String valueType = parameter.getValueType(); |
||||
variables.put("type", parameter.getType().toString()); |
Integer valueTypeVersion = parameter.getValueTypeVersion(); |
||||
sb.append(StringUtil.format(template, variables)); |
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
variables.put("className",IdReplacer.className(valueType,valueTypeVersion)); |
||||
|
if (parameter.getValueTypeIsList()) { |
||||
|
String tpl ="arg.${fieldName} =TypeConvertor.getValue(map.get('${code}'),new TypeReference<List<${className}>>(){}); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} else { |
||||
|
String tpl ="arg.${fieldName} =TypeConvertor.getValue(map.get('${code}'),${className}.class); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
if (!ValueTypeUtil.isBase(valueType)) { |
||||
|
String tpl ="if(arg.${fieldName}!=null) { arg.${fieldName}.init(); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateConvertorFromJson(List<Parameter> parameters) { |
||||
|
if (!CollectionUtil.hasElements(parameters)) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(""); |
||||
|
for (Parameter parameter : parameters) { |
||||
|
String code = parameter.getCode(); |
||||
|
String name = parameter.getName(); |
||||
|
String valueType = parameter.getValueType(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
variables.put("name",name); |
||||
|
if (!ValueTypeUtil.isBase(valueType)) { |
||||
|
String tpl ="if(arg.${fieldName}!=null) { arg.${fieldName}.init(); } //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateValidator(List<Parameter> parameters) { |
||||
|
if (!CollectionUtil.hasElements(parameters)) { |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(""); |
||||
|
for (Parameter parameter : parameters) { |
||||
|
String code =parameter.getCode(); |
||||
|
String name =parameter.getName(); |
||||
|
if(CollectionUtil.hasElements(parameter.getValidators())){ |
||||
|
for(ParameterValidator validator : parameter.getValidators()){ |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
if(validator instanceof EmptyParameterValidator){ |
||||
|
String tpl ="FieldValidator.empty('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof NotEmptyParameterValidator){ |
||||
|
String tpl ="FieldValidator.notEmpty('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof TrueParameterValidator){ |
||||
|
String tpl ="FieldValidator.trueValue('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof FalseParameterValidator){ |
||||
|
String tpl ="FieldValidator.falseValue('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof EmailParameterValidator){ |
||||
|
String tpl ="FieldValidator.mail('${code}','${name}',this.${fieldName},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof IntegerRangeParameterValidator){ |
||||
|
IntegerRangeParameterValidator integerRangeParameterValidator =(IntegerRangeParameterValidator)validator; |
||||
|
variables.put("minInclude",integerRangeParameterValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(integerRangeParameterValidator.getMinValue())?integerRangeParameterValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(integerRangeParameterValidator.getMaxValue())?integerRangeParameterValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",integerRangeParameterValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.integerRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof DecimalRangeParameterValidator){ |
||||
|
DecimalRangeParameterValidator decimalRangeParameterValidator =(DecimalRangeParameterValidator)validator; |
||||
|
variables.put("minInclude",decimalRangeParameterValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(decimalRangeParameterValidator.getMinValue())?decimalRangeParameterValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(decimalRangeParameterValidator.getMaxValue())?decimalRangeParameterValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",decimalRangeParameterValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.decimalRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof LengthRangeParameterValidator){ |
||||
|
LengthRangeParameterValidator lengthRangeParameterValidator =(LengthRangeParameterValidator)validator; |
||||
|
variables.put("minInclude",lengthRangeParameterValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(lengthRangeParameterValidator.getMinValue())?lengthRangeParameterValidator.getMinValue():"null"); |
||||
|
variables.put("max",StringUtils.hasText(lengthRangeParameterValidator.getMaxValue())?lengthRangeParameterValidator.getMaxValue():"null"); |
||||
|
variables.put("maxInclude",lengthRangeParameterValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.lengthRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof DateRangeParameterValidator){ |
||||
|
DateRangeParameterValidator dateRangeParameterValidator =(DateRangeParameterValidator)validator; |
||||
|
variables.put("minInclude",dateRangeParameterValidator.getMinInclude()); |
||||
|
variables.put("min",StringUtils.hasText(dateRangeParameterValidator.getMinValue())?"DateUtil.parseDate(\"" + dateRangeParameterValidator.getMinValue() + "\",DateUtil.yyyy_MM_dd_HH_mm_ss)":"null"); |
||||
|
variables.put("max",StringUtils.hasText(dateRangeParameterValidator.getMaxValue())?"DateUtil.parseDate(\"" + dateRangeParameterValidator.getMaxValue() + "\",DateUtil.yyyy_MM_dd_HH_mm_ss)":"null"); |
||||
|
variables.put("maxInclude",dateRangeParameterValidator.getMaxInclude()); |
||||
|
String tpl ="FieldValidator.dateRange('${code}','${name}',this.${fieldName},${minInclude},${min},${max},${maxInclude},result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(validator instanceof PatternParameterValidator){ |
||||
|
PatternParameterValidator patternParameterValidator =(PatternParameterValidator)validator; |
||||
|
variables.put("pattern",StringUtils.hasText(patternParameterValidator.getPattern())?patternParameterValidator.getPattern():"null"); |
||||
|
String tpl ="FieldValidator.dateRange('${code}','${name}',this.${fieldName},'${pattern}',result);"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
return sb.toString(); |
return sb.toString(); |
||||
} |
} |
||||
|
|
||||
public static String generateMethodBodys(List<Parameter> parameters) throws Exception { |
public static String generateToResult(List<Parameter> parameters) { |
||||
|
if(!CollectionUtil.hasElements(parameters)){ return null; } |
||||
StringBuilder sb =new StringBuilder(""); |
StringBuilder sb =new StringBuilder(""); |
||||
if(CollectionUtil.hasElements(parameters)){ |
for(Parameter parameter : parameters){ |
||||
for(Parameter parameter : parameters) { |
String type =parameter.getType().toString(); |
||||
if(parameter instanceof IntermediateParameter || parameter instanceof OutParameter) { |
String code =parameter.getCode(); |
||||
sb.append(generate(parameter)); |
String name =parameter.getName(); |
||||
|
String valueType =parameter.getValueType(); |
||||
|
Integer valueScale =parameter.getValueScale(); |
||||
|
RoundingMode valueRoundingMode =parameter.getValueRoundingMode(); |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("type",type); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("valueType",valueType); |
||||
|
variables.put("valueScale",valueScale==null?8:valueScale); |
||||
|
variables.put("valueRoundingMode",valueRoundingMode==null?RoundingMode.HALF_UP:valueRoundingMode); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
|
||||
|
if(ParameterType.INDICATOR.equals(parameter.getType()) || ParameterType.OUT.equals(parameter.getType()) || ParameterType.INTERMEDIATE.equals(parameter.getType())){ |
||||
|
if(ValueTypeUtil.isDecimal(parameter.getValueType())){ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',${valueScale},RoundingMode.${valueRoundingMode},this.${fieldName}==null?null:this.${fieldName}.setScale(${valueScale},RoundingMode.${valueRoundingMode})));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else if(ValueTypeUtil.isBase(parameter.getValueType())){ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',null,null,this.${fieldName}==null?null:this.${fieldName}));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="result.addParameterResult(new ParameterResult('${code}','${name}',ParameterType.${type},'${valueType}',null,null,this.${fieldName}==null?null:ObjectMapperUtil.json().writeValueAsString(this.${fieldName})));"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
return sb.toString(); |
return sb.toString(); |
||||
} |
} |
||||
|
|
||||
|
public static String generateToMap(List<Parameter> parameters) { |
||||
|
if(!CollectionUtil.hasElements(parameters)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Parameter parameter : parameters){ |
||||
|
String code =parameter.getCode(); |
||||
|
String name =parameter.getName(); |
||||
|
|
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("name",name); |
||||
|
variables.put("fieldName",IdReplacer.fieldName(code)); |
||||
|
|
||||
|
String tpl ="map.put('${code}',this.${fieldName}); //${name}"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static String generateMergeResult(List<Parameter> parameters) { |
||||
|
if(!CollectionUtil.hasElements(parameters)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(""); |
||||
|
for(Parameter parameter : parameters){ |
||||
|
String code =parameter.getCode(); |
||||
|
String valueType = parameter.getValueType(); |
||||
|
Map<String,Object> variables =new HashMap<>(); |
||||
|
variables.put("code",code); |
||||
|
variables.put("valueType",valueType); |
||||
|
if(ValueTypeUtil.isBase(valueType)){ |
||||
|
String tpl ="if(result.exists('${code}')){ this.${code}=TypeConvertor.getValue(result.getValueByParameterCode('${code}'),${valueType}.class); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
}else{ |
||||
|
String tpl ="if(result.exists('${code}')){ this.${code}=result.getValueByParameterCode('${code}'); }"; |
||||
|
sb.append(StringUtil.format(tpl,variables)).append("\n"); |
||||
|
} |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
} |
} |
||||
|
@ -1,23 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.platform.util.FileUtil; |
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
|
|
||||
import java.util.HashMap; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
public class ParameterMethodGenerator { |
|
||||
private static final String TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/ParameterMethod.tpl"; |
|
||||
|
|
||||
public static String generate(Parameter parameter) throws Exception{ |
|
||||
List<String> templates = FileUtil.readStringAsList(TEMPLATE_CLASSPATH); |
|
||||
|
|
||||
Map<String,Object> variables =new HashMap<>(); |
|
||||
variables.put("code",parameter.getCode()); |
|
||||
variables.put("name",parameter.getName()); |
|
||||
|
|
||||
return StringUtil.format(templates,variables); |
|
||||
} |
|
||||
} |
|
@ -1,37 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.generator.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.model.Model; |
|
||||
import io.sc.engine.rule.core.po.resource.ModelResource; |
|
||||
import io.sc.engine.rule.core.po.resource.Resource; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.platform.util.FileUtil; |
|
||||
import io.sc.platform.util.StringUtil; |
|
||||
|
|
||||
import java.util.HashMap; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
public class ResourceClassGenerator { |
|
||||
private static final String TEMPLATE_CLASSPATH ="classpath:/io/sc/engine/rule/core/code/generator/templates/Resource.tpl"; |
|
||||
|
|
||||
public static String generate(Resource resource) throws Exception{ |
|
||||
StringBuilder sb =new StringBuilder(""); |
|
||||
if(resource!=null){ |
|
||||
List<String> templates = FileUtil.readStringAsList(TEMPLATE_CLASSPATH); |
|
||||
|
|
||||
Map<String,Object> variables =new HashMap<>(); |
|
||||
variables.put("resourceClassName", CodeReplacer.className(resource.getCode(),resource.getVersion())); |
|
||||
variables.put("resourceName",resource.getName()); |
|
||||
variables.put("resourceVersion",resource.getVersion()); |
|
||||
if(resource instanceof ModelResource) { |
|
||||
Model model =((ModelResource)resource).getModel(); |
|
||||
variables.put("topModelName", model.getName()); |
|
||||
variables.put("topModelMethodName", CodeReplacer.methodName(model.getCode())); |
|
||||
variables.put("topModelMethod", ModelGenerator.generate(model)); |
|
||||
} |
|
||||
|
|
||||
sb.append(StringUtil.format(templates,variables)); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,33 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.ArithmeticIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.ArithmeticParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
public class Arithmetic { |
||||
|
public String groovy(Parameter parameter, ArithmeticParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),processor.getArithmetic()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Arithmetic groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator, ArithmeticIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try{ |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),processor.getArithmetic()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s Arithmetic groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,String arthmetic) { |
||||
|
if(!StringUtils.hasText(arthmetic)){ return null; } |
||||
|
return GroovyExpressionReplacer.groovy(arthmetic,valueType); |
||||
|
} |
||||
|
} |
@ -0,0 +1,91 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.ConditionRangeIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.ConditionRangeParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class ConditionRange { |
||||
|
private String condition; |
||||
|
private String value; |
||||
|
|
||||
|
public List<ConditionRange> parse(String json) throws Exception{ |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<ConditionRange>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter,ConditionRangeParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<ConditionRange> conditionRanges =parse(processor.getConditionRange()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),conditionRanges); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s ConditionRange groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator, ConditionRangeIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try{ |
||||
|
List<ConditionRange> conditionRanges =parse(processor.getConditionRange()); |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),conditionRanges); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s ConditionRange groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,List<ConditionRange> conditionRanges){ |
||||
|
if(!CollectionUtil.hasElements(conditionRanges)){ return null; } |
||||
|
List<ConditionRange> _conditionRanges =new ArrayList<ConditionRange>(); |
||||
|
//移除没有填写条件的条件范围
|
||||
|
for(ConditionRange conditionRange : conditionRanges) { |
||||
|
if(StringUtils.hasText(conditionRange.getCondition())) { |
||||
|
_conditionRanges.add(conditionRange); |
||||
|
} |
||||
|
} |
||||
|
if(_conditionRanges.isEmpty()){ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
int size =_conditionRanges.size(); |
||||
|
for(int i=0;i<size;i++) { |
||||
|
ConditionRange conditionRange =_conditionRanges.get(i); |
||||
|
if(i==0) { |
||||
|
sb.append("if("); |
||||
|
}else { |
||||
|
sb.append("else if("); |
||||
|
} |
||||
|
sb.append(GroovyExpressionReplacer.groovy(conditionRange.condition)); |
||||
|
sb.append(")").append("{").append("\n"); |
||||
|
if(!StringUtils.hasText(conditionRange.value)){ |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(" =null;\n"); |
||||
|
}else { |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(" =").append(GroovyExpressionReplacer.groovy(conditionRange.value,valueType)).append(";\n"); |
||||
|
} |
||||
|
sb.append("}"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public String getCondition() { |
||||
|
return condition; |
||||
|
} |
||||
|
public void setCondition(String condition) { |
||||
|
this.condition = condition; |
||||
|
} |
||||
|
public String getValue() { |
||||
|
return value; |
||||
|
} |
||||
|
public void setValue(String value) { |
||||
|
this.value = value; |
||||
|
} |
||||
|
} |
@ -0,0 +1,137 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.DecisionTableParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.ExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class DecisionTable { |
||||
|
public List<Map<String,Object>> parse(String json) throws Exception{ |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Map<String,Object>>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter, DecisionTableParameterProcessor processor,List<Parameter> parameters) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<Map<String,Object>> decisionTables =parse(processor.getDecisionTable()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),decisionTables,parameters); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s DecisionTable groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,List<Map<String,Object>> decisionTables,List<Parameter> parameters){ |
||||
|
if(!CollectionUtil.hasElements(decisionTables)){ return null;} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
int conditionRowCount =getConditionRowCount(decisionTables); |
||||
|
int resultRowCount =getResultRowCount(decisionTables); |
||||
|
int colCount =getColCount(decisionTables); |
||||
|
for(int i=1;i<colCount;i++) {//一列一列生成
|
||||
|
List<String> conditions =new ArrayList<>(); |
||||
|
for(int j=0;j<conditionRowCount;j++) { |
||||
|
String condition =decisionTables.get(j).get("C0").toString(); |
||||
|
String conditionResult =decisionTables.get(j).get("C"+i)==null?"":decisionTables.get(j).get("C"+i).toString(); |
||||
|
if("Y".equalsIgnoreCase(conditionResult) || "T".equalsIgnoreCase(conditionResult)) { |
||||
|
conditions.add("(" + GroovyExpressionReplacer.groovy(condition) + ")"); |
||||
|
} |
||||
|
if(!StringUtils.hasText(conditionResult) || "N".equalsIgnoreCase(conditionResult) || "F".equalsIgnoreCase(conditionResult)){ |
||||
|
conditions.add("!(" + GroovyExpressionReplacer.groovy(condition) + ")"); |
||||
|
} |
||||
|
} |
||||
|
sb.append("if (").append(StringUtil.combine(" && ",conditions)).append("){").append("\n"); |
||||
|
for(int l=conditionRowCount;l<(conditionRowCount+resultRowCount);l++) { |
||||
|
String resultParameterCode =decisionTables.get(l).get("C0").toString(); |
||||
|
String resultParameterValueType =""; |
||||
|
for(Parameter param : parameters){ |
||||
|
if(("${" + param.getCode() + "}").equalsIgnoreCase(resultParameterCode)){ |
||||
|
resultParameterValueType =param.getValueType(); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.groovy(resultParameterCode)).append(" ="); |
||||
|
String expression =(String)decisionTables.get(l).get("C"+i); |
||||
|
sb.append(GroovyExpressionReplacer.groovy(expression, resultParameterValueType)); |
||||
|
sb.append(";"); |
||||
|
sb.append("\n"); |
||||
|
} |
||||
|
sb.append("}").append("\n"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
private String groovy2(String code, String valueType,List<Map<String,Object>> decisionTables){ |
||||
|
if(!CollectionUtil.hasElements(decisionTables)){ return null;} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
int conditionRowCount =getConditionRowCount(decisionTables); |
||||
|
int resultRowCount =getResultRowCount(decisionTables); |
||||
|
int colCount =getColCount(decisionTables); |
||||
|
sb.append("if (true){").append("\n"); |
||||
|
sb.append("}"); |
||||
|
for(int j=1;j<colCount;j++) {//一列一列生成
|
||||
|
sb.append("else if (true"); |
||||
|
for(int i=0;i<conditionRowCount;i++) { |
||||
|
String condition =decisionTables.get(i).get("C"+j)==null?"":decisionTables.get(i).get("C"+j).toString(); |
||||
|
if("Y".equalsIgnoreCase(condition) || "T".equalsIgnoreCase(condition)) { |
||||
|
sb.append(" && ").append("(").append(ExpressionReplacer.groovy((String)decisionTables.get(i).get("C0"),null)).append(")"); |
||||
|
} |
||||
|
} |
||||
|
sb.append("){").append("\n"); |
||||
|
for(int l=conditionRowCount;l<(conditionRowCount+resultRowCount);l++) { |
||||
|
sb.append("\t\t\t").append(ExpressionReplacer.groovy((String)decisionTables.get(l).get("C0"),null)).append(" ="); |
||||
|
String expression =(String)decisionTables.get(l).get("C"+j); |
||||
|
sb.append(ExpressionReplacer.groovy(expression, valueType)); |
||||
|
sb.append(";"); |
||||
|
sb.append("\n"); |
||||
|
} |
||||
|
sb.append("\t\t}"); |
||||
|
} |
||||
|
return StringUtil.tabs(sb.toString(),2); |
||||
|
} |
||||
|
|
||||
|
private int getColCount(List<Map<String,Object>> decisionTables) { |
||||
|
if(decisionTables!=null && decisionTables.size()>0) { |
||||
|
Map<String,Object> row =decisionTables.get(0); |
||||
|
if(row!=null && row.size()>0) { |
||||
|
int index =0; |
||||
|
while(row.containsKey("C"+index)) { |
||||
|
index++; |
||||
|
} |
||||
|
return index; |
||||
|
} |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
private int getConditionRowCount(List<Map<String,Object>> decisionTables) { |
||||
|
if(decisionTables!=null && decisionTables.size()>0) { |
||||
|
int index =0; |
||||
|
while("condition".equalsIgnoreCase((String)decisionTables.get(index).get("type"))) { |
||||
|
index++; |
||||
|
} |
||||
|
return index; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
private int getResultRowCount(List<Map<String,Object>> decisionTables) { |
||||
|
if(decisionTables!=null && decisionTables.size()>0) { |
||||
|
int index =0; |
||||
|
for(Map<String,Object> map : decisionTables) { |
||||
|
if("result".equalsIgnoreCase((String)map.get("type"))) { |
||||
|
index++; |
||||
|
} |
||||
|
} |
||||
|
return index; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
} |
@ -0,0 +1,74 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.DecisionTable2CParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.ExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class DecisionTable2C { |
||||
|
public List<Map<String,Object>> parse(String json) throws Exception{ |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Map<String,Object>>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter, DecisionTable2CParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<Map<String,Object>> decisionTable2Cs =parse(processor.getDecisionTable2C()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),decisionTable2Cs); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s DecisionTable2C groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,List<Map<String,Object>> decisionTable2Cs){ |
||||
|
if(!CollectionUtil.hasElements(decisionTable2Cs)){ return null;} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
int rowCount =decisionTable2Cs.size(); |
||||
|
int colCount =getColCount(decisionTable2Cs); |
||||
|
boolean isFirstCondition =true; |
||||
|
for(int i=1;i<rowCount;i++) { |
||||
|
for(int j=1;j<colCount;j++) { |
||||
|
if(isFirstCondition) { |
||||
|
sb.append("if ("); |
||||
|
isFirstCondition =false; |
||||
|
}else{ |
||||
|
sb.append("else if ("); |
||||
|
} |
||||
|
sb.append("(").append(GroovyExpressionReplacer.groovy(decisionTable2Cs.get(i).get("C0").toString())).append(")"); |
||||
|
sb.append(" && "); |
||||
|
sb.append("(").append(GroovyExpressionReplacer.groovy((String)decisionTable2Cs.get(0).get("C"+j))).append(")"); |
||||
|
sb.append(") {").append("\n"); |
||||
|
Object obj =decisionTable2Cs.get(i).get("C"+j); |
||||
|
if(obj!=null) { |
||||
|
sb.append("\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(" ="); |
||||
|
String expression =obj.toString(); |
||||
|
sb.append(GroovyExpressionReplacer.groovy(expression, valueType)).append(";\n"); |
||||
|
} |
||||
|
sb.append("}"); |
||||
|
} |
||||
|
} |
||||
|
sb.append("\n"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
private int getColCount(List<Map<String,Object>> decisionTable2Cs) { |
||||
|
if(decisionTable2Cs!=null && decisionTable2Cs.size()>0) { |
||||
|
Map<String,Object> row =decisionTable2Cs.get(0); |
||||
|
if(row!=null && row.size()>0) { |
||||
|
int index =0; |
||||
|
while(row.containsKey("C"+index)) { |
||||
|
index++; |
||||
|
} |
||||
|
return index; |
||||
|
} |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
} |
@ -0,0 +1,133 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.NumberRangeIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.NumberRangeParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class NumberRange { |
||||
|
private Boolean minIncluded; |
||||
|
private BigDecimal min; |
||||
|
private BigDecimal max; |
||||
|
private Boolean maxIncluded; |
||||
|
private String value; |
||||
|
|
||||
|
public List<NumberRange> parse(String json) throws Exception{ |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<NumberRange>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter,NumberRangeParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<NumberRange> numberRanges = parse(processor.getNumberRange()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),processor.getNumberRangeVar(),numberRanges); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s NumberRange groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator,NumberRangeIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<NumberRange> numberRanges = parse(processor.getNumberRange()); |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),processor.getNumberRangeVar(),numberRanges); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s NumberRange groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(String code, String valueType,String var,List<NumberRange> numberRanges){ |
||||
|
if(!CollectionUtil.hasElements(numberRanges)){ return null; } |
||||
|
List<NumberRange> _numberRanges =new ArrayList<NumberRange>(); |
||||
|
|
||||
|
//移除空行
|
||||
|
if(numberRanges!=null && numberRanges.size()>0) { |
||||
|
for(NumberRange numberRange : numberRanges) { |
||||
|
if(numberRange.min!=null || numberRange.max!=null || numberRange.value!=null) { |
||||
|
_numberRanges.add(numberRange); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if(_numberRanges.isEmpty()){ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
if (var == null || "".equals(var.trim())) { |
||||
|
var = GroovyExpressionReplacer.ARGUMENT_NAME + "." + code; |
||||
|
} |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
int size = _numberRanges.size(); |
||||
|
for (int i = 0; i < size; i++) { |
||||
|
NumberRange numberRange = _numberRanges.get(i); |
||||
|
if (i == 0) { |
||||
|
sb.append("if("); |
||||
|
} else { |
||||
|
sb.append("else if("); |
||||
|
} |
||||
|
if (numberRange.min == null && numberRange.max == null) { |
||||
|
sb.append(GroovyExpressionReplacer.groovy(var)).append("==null"); |
||||
|
} else if (numberRange.min != null) { |
||||
|
if (numberRange.minIncluded != null && numberRange.minIncluded) { |
||||
|
sb.append(GroovyExpressionReplacer.groovy(var)).append(">=").append(numberRange.min); |
||||
|
} else { |
||||
|
sb.append(GroovyExpressionReplacer.groovy(var)).append(">").append(numberRange.min); |
||||
|
} |
||||
|
} |
||||
|
if (numberRange.min != null && numberRange.max != null) { |
||||
|
sb.append(" && "); |
||||
|
} |
||||
|
if (numberRange.max != null) { |
||||
|
if (numberRange.maxIncluded != null && numberRange.maxIncluded) { |
||||
|
sb.append(GroovyExpressionReplacer.groovy(var)).append("<=").append(numberRange.max); |
||||
|
} else { |
||||
|
sb.append(GroovyExpressionReplacer.groovy(var)).append("<").append(numberRange.max); |
||||
|
} |
||||
|
} |
||||
|
sb.append(")").append("{").append("\n"); |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(code).append(" =").append(GroovyExpressionReplacer.groovy(numberRange.value, valueType)).append(";\n"); |
||||
|
sb.append("}"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public Boolean getMinIncluded() { |
||||
|
return minIncluded; |
||||
|
} |
||||
|
public void setMinIncluded(Boolean minIncluded) { |
||||
|
this.minIncluded = minIncluded; |
||||
|
} |
||||
|
public BigDecimal getMin() { |
||||
|
return min; |
||||
|
} |
||||
|
public void setMin(BigDecimal min) { |
||||
|
this.min = min; |
||||
|
} |
||||
|
public BigDecimal getMax() { |
||||
|
return max; |
||||
|
} |
||||
|
public void setMax(BigDecimal max) { |
||||
|
this.max = max; |
||||
|
} |
||||
|
public Boolean getMaxIncluded() { |
||||
|
return maxIncluded; |
||||
|
} |
||||
|
public void setMaxIncluded(Boolean maxIncluded) { |
||||
|
this.maxIncluded = maxIncluded; |
||||
|
} |
||||
|
public String getValue() { |
||||
|
return value; |
||||
|
} |
||||
|
public void setValue(String value) { |
||||
|
this.value = value; |
||||
|
} |
||||
|
} |
@ -0,0 +1,113 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.ObjectPropertiesIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.ObjectPropertiesParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import io.sc.platform.util.PlaceHolderExpressionUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class ObjectProperty { |
||||
|
private String code; |
||||
|
private String name; |
||||
|
private String expression; |
||||
|
|
||||
|
public ObjectProperty(){} |
||||
|
|
||||
|
public ObjectProperty(String code, String name){ |
||||
|
this.code =code; |
||||
|
this.name =name; |
||||
|
} |
||||
|
|
||||
|
public List<ObjectProperty> parse(String json) throws Exception { |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<ObjectProperty>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter, ObjectPropertiesParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<ObjectProperty> objectProperties =parse(processor.getObjectProperties()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),parameter.getValueTypeVersion(),processor.getObjectCondition(),objectProperties); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s ObjectProperty groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator, ObjectPropertiesIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<ObjectProperty> objectProperties =parse(processor.getObjectProperties()); |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),indicator.getValueTypeVersion(),processor.getObjectCondition(),objectProperties); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s ObjectProperty groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(String code, String valueType, Integer valueTypeVersion,String condition,List<ObjectProperty> objectProperties) throws Exception{ |
||||
|
if(!CollectionUtil.hasElements(objectProperties)){ return null; } |
||||
|
List<ObjectProperty> _objectProperties =new ArrayList<>(); |
||||
|
//移除没有填写表达式的对象属性
|
||||
|
if(objectProperties!=null && objectProperties.size()>0) { |
||||
|
for(ObjectProperty objectProperty : objectProperties) { |
||||
|
if(StringUtils.hasText(objectProperty.getExpression())){ |
||||
|
_objectProperties.add(objectProperty); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if(_objectProperties.isEmpty()){ |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
if(StringUtils.hasText(condition)){ |
||||
|
sb.append("if (").append(GroovyExpressionReplacer.groovy(condition)).append("){").append("\n"); |
||||
|
}else{ |
||||
|
sb.append("if (true) {").append("\n"); |
||||
|
} |
||||
|
sb.append("\tif (").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append("==null) {").append("\n"); |
||||
|
sb.append("t\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(" =new ").append(valueType).append("_V").append(valueTypeVersion).append("();").append("\n"); |
||||
|
sb.append("\t}").append("\n"); |
||||
|
int size =_objectProperties.size(); |
||||
|
for(int i=0;i<size;i++) { |
||||
|
ObjectProperty objectProperty =_objectProperties.get(i); |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(".").append(objectProperty.getCode()); |
||||
|
sb.append(" =").append(PlaceHolderExpressionUtil.replace(objectProperty.getExpression(),"arg.","")).append(";"); |
||||
|
sb.append("//").append(objectProperty.getName()).append("\n"); |
||||
|
} |
||||
|
sb.append("}").append("\n"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public String getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
public void setCode(String code) { |
||||
|
this.code = code; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public String getExpression() { |
||||
|
return expression; |
||||
|
} |
||||
|
|
||||
|
public void setExpression(String expression) { |
||||
|
this.expression = expression; |
||||
|
} |
||||
|
} |
@ -0,0 +1,52 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import io.sc.engine.rule.core.po.model.Model; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.ParameterInOptionItem; |
||||
|
import io.sc.engine.rule.core.po.model.parameter.InOptionParameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.OptionValueParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.engine.rule.core.util.ValueTypeUtil; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.PlaceHolderExpressionUtil; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public class OptionValue { |
||||
|
public String groovy(Parameter parameter, OptionValueParameterProcessor processor, Model model) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
Parameter optionParameter =model.getParameterByOptionParameterCode(PlaceHolderExpressionUtil.replace(processor.getOptionCode(),"","")); |
||||
|
if(optionParameter==null){ return null; } |
||||
|
if(optionParameter instanceof InOptionParameter){ |
||||
|
InOptionParameter inOptionParameter =(InOptionParameter)optionParameter; |
||||
|
List<ParameterInOptionItem> items = inOptionParameter.getOptions(); |
||||
|
if(!CollectionUtil.hasElements(items)){ return null; } |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
int size =items.size(); |
||||
|
for(int i=0;i<size;i++){ |
||||
|
ParameterInOptionItem item =items.get(i); |
||||
|
if(i==0){ |
||||
|
sb.append("if("); |
||||
|
}else{ |
||||
|
sb.append("else if("); |
||||
|
} |
||||
|
sb.append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(inOptionParameter.getCode())); |
||||
|
sb.append("=="); |
||||
|
if(ValueTypeUtil.isString(inOptionParameter.getValueType())){ |
||||
|
sb.append(GroovyExpressionReplacer.groovy(item.getInputValue(),"java.lang.String")); |
||||
|
}else{ |
||||
|
sb.append(GroovyExpressionReplacer.groovy(item.getInputValue())); |
||||
|
} |
||||
|
sb.append("){").append("\n"); |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(parameter.getCode())); |
||||
|
sb.append("="); |
||||
|
sb.append(GroovyExpressionReplacer.groovy(item.getInputValue(),parameter.getValueType())); |
||||
|
sb.append("\n"); |
||||
|
sb.append("}"); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import io.sc.engine.rule.core.enums.ProcessorType; |
import io.sc.engine.rule.core.enums.ProcessorType; |
@ -0,0 +1,89 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.SqlIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.SqlParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.ExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.platform.util.CollectionUtil; |
||||
|
import io.sc.platform.util.ObjectMapperUtil; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
public class SqlFieldMapping { |
||||
|
private String parameter; |
||||
|
private String field; |
||||
|
|
||||
|
public List<SqlFieldMapping> parse(String json) throws Exception{ |
||||
|
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<SqlFieldMapping>>(){}); |
||||
|
} |
||||
|
|
||||
|
public String groovy(Parameter parameter,SqlParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<SqlFieldMapping> sqlFieldMappings =parse(processor.getSqlFieldMapping()); |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),processor.getSqlDatasourceName(),processor.getSql(),sqlFieldMappings); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s SqlFieldMapping groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator,SqlIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try { |
||||
|
List<SqlFieldMapping> sqlFieldMappings =parse(processor.getSqlFieldMapping()); |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),processor.getSqlDatasourceName(),processor.getSql(),sqlFieldMappings); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s SqlFieldMapping groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,String sqlDatasourceName,String sql,List<SqlFieldMapping> sqlFieldMappings){ |
||||
|
if(!StringUtils.hasText(sql) || !CollectionUtil.hasElements(sqlFieldMappings)){ |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
if(StringUtils.hasText(sqlDatasourceName)){ |
||||
|
sb.append(StringUtil.format("javax.sql.DataSource ds =io.sc.engine.rule.client.spring.util.EngineSpringApplicationContextUtil.getDataSource('${0}');",sqlDatasourceName)).append("\n"); |
||||
|
}else{ |
||||
|
sb.append("javax.sql.DataSource ds =io.sc.engine.rule.client.spring.util.EngineSpringApplicationContextUtil.getDefaultDataSource();").append("\n"); |
||||
|
} |
||||
|
sb.append(StringUtil.format("String sql =${0};", GroovyExpressionReplacer.groovy(sql,String.class.getName()))).append("\n"); |
||||
|
sb.append("ESql.withInstance(ds){ db->").append("\n"); |
||||
|
sb.append("\t").append("db.query(sql) { rs ->").append("\n"); |
||||
|
sb.append("\t\t").append("if (rs.next()) {").append("\n"); |
||||
|
for(SqlFieldMapping sqlFieldMapping : sqlFieldMappings) { |
||||
|
sb.append("\t\t\t") |
||||
|
.append(ExpressionReplacer.groovy(IdReplacer.fieldName(sqlFieldMapping.parameter), null)) |
||||
|
.append(" =") |
||||
|
.append("rs.getObject(\"").append(sqlFieldMapping.getField()).append("\");").append("\n"); |
||||
|
} |
||||
|
sb.append("\t\t}").append("\n"); |
||||
|
sb.append("\t}").append("\n"); |
||||
|
sb.append("}").append("\n"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public String getParameter() { |
||||
|
return parameter; |
||||
|
} |
||||
|
|
||||
|
public void setParameter(String parameter) { |
||||
|
this.parameter = parameter; |
||||
|
} |
||||
|
|
||||
|
public String getField() { |
||||
|
return field; |
||||
|
} |
||||
|
|
||||
|
public void setField(String field) { |
||||
|
this.field = field; |
||||
|
} |
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.TernaryIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.TernaryParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
public class Ternary { |
||||
|
public String groovy(Parameter parameter, TernaryParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),processor.getTernaryCondition(),processor.getTernaryTrue(),processor.getTernaryFalse()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Ternary groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator, TernaryIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try{ |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),processor.getTernaryCondition(),processor.getTernaryTrue(),processor.getTernaryFalse()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s Ternary groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,String condition,String trueExpression,String falseExpression) { |
||||
|
if(!StringUtils.hasText(condition) || !StringUtils.hasText(trueExpression) || !StringUtils.hasText(falseExpression)){ |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
sb.append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)).append(" ="); |
||||
|
sb.append("(").append(GroovyExpressionReplacer.groovy(condition)).append(")?"); |
||||
|
sb.append("(").append(GroovyExpressionReplacer.groovy(trueExpression,valueType)).append(")"); |
||||
|
sb.append(" : "); |
||||
|
sb.append("(").append(GroovyExpressionReplacer.groovy(falseExpression,valueType)).append(")"); |
||||
|
sb.append(";\n"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package io.sc.engine.rule.core.code.generator.impl.processor; |
||||
|
|
||||
|
import io.sc.engine.rule.core.po.lib.Indicator; |
||||
|
import io.sc.engine.rule.core.po.lib.processor.WhenThenIndicatorProcessor; |
||||
|
import io.sc.engine.rule.core.po.model.Parameter; |
||||
|
import io.sc.engine.rule.core.po.model.processor.WhenThenParameterProcessor; |
||||
|
import io.sc.engine.rule.core.util.GroovyExpressionReplacer; |
||||
|
import io.sc.engine.rule.core.util.IdReplacer; |
||||
|
import io.sc.platform.util.StringUtil; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
public class WhenThen { |
||||
|
public String groovy(Parameter parameter, WhenThenParameterProcessor processor) throws Exception{ |
||||
|
if(parameter==null || processor==null){ return null; } |
||||
|
try { |
||||
|
return groovy(parameter.getCode(),parameter.getValueType(),processor.getWhen(),processor.getThen(),processor.getIsWhenThenShorted()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Ternary groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String groovy(Indicator indicator, WhenThenIndicatorProcessor processor) throws Exception{ |
||||
|
if(indicator==null || processor==null){ return null; } |
||||
|
try{ |
||||
|
return groovy(indicator.getCode(),indicator.getValueType(),processor.getWhen(),processor.getThen(),processor.getIsWhenThenShorted()); |
||||
|
}catch(Exception e) { |
||||
|
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s Ternary groovy source code.", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private String groovy(String code, String valueType,String when,String then,Boolean isShorted) { |
||||
|
if(!StringUtils.hasText(when) || !StringUtils.hasText(then)){ |
||||
|
return null; |
||||
|
} |
||||
|
StringBuilder sb =new StringBuilder(); |
||||
|
sb.append("if (").append(GroovyExpressionReplacer.groovy(when)).append(") {").append("\n"); |
||||
|
sb.append("\t").append(GroovyExpressionReplacer.ARGUMENT_NAME).append(".").append(IdReplacer.fieldName(code)); |
||||
|
sb.append(" = "); |
||||
|
sb.append(GroovyExpressionReplacer.groovy(then,valueType)).append(";").append("\n"); |
||||
|
String debug =StringUtil.format("if(log.isDebugEnabled()){log.debug(\" WhenThen 运算结果 : {}\",${0});}",GroovyExpressionReplacer.ARGUMENT_NAME + "." + IdReplacer.fieldName(code)); |
||||
|
sb.append("\t").append(debug).append("\n"); |
||||
|
if(isShorted!=null && isShorted){ |
||||
|
sb.append("\t").append("if(log.isDebugEnabled()){log.debug(\" WhenThen 短路!!!\");}").append("\n"); |
||||
|
debug =StringUtil.format("if(log.isDebugEnabled()){log.debug(\" 参数结果值 : {}\",${0});}",GroovyExpressionReplacer.ARGUMENT_NAME + "." + IdReplacer.fieldName(code)); |
||||
|
sb.append("\t").append(debug).append("\n"); |
||||
|
sb.append("\t").append("return;").append("\n"); |
||||
|
} |
||||
|
sb.append("}").append("\n"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
@ -1,10 +0,0 @@ |
|||||
/******************************************************** |
|
||||
* Functions |
|
||||
********************************************************/ |
|
||||
#for(function : executeUnit.functions) |
|
||||
|
|
||||
/** |
|
||||
* #(function.description) |
|
||||
*/ |
|
||||
#(function.body) |
|
||||
#end |
|
@ -1,28 +0,0 @@ |
|||||
#define renderModel(model) |
|
||||
//模型(#(model.fullName)) |
|
||||
private static void #(methodName(model.code))(Executor executor,Argument argument){ |
|
||||
if(log.isDebugEnabled()){log.debug("开始调用模型: {}","#(model.fullName)");} |
|
||||
#if(!model.enable) |
|
||||
if(log.isDebugEnabled()){log.debug("开始调用模型: enable==false");} |
|
||||
#end |
|
||||
#if(model.executeMode.toString()=="DOWN_UP") |
|
||||
//模型的执行模式为:自下而上,首先执行子模型 |
|
||||
#for(subModel : model.children) |
|
||||
#(methodName(subModel.code))(executor,argument);//#(subModel.name) |
|
||||
#end |
|
||||
#end |
|
||||
|
|
||||
//处理模型的中间值和结果值 |
|
||||
#for(parameter : model.parameters) |
|
||||
#if(parameter.type.toString()=="INTERMEDIATE" || parameter.type.toString()=="OUT") |
|
||||
#(methodName(parameter.code))(executor,argument);//#(parameter.name),#(parameter.type) |
|
||||
#end |
|
||||
#end |
|
||||
} |
|
||||
|
|
||||
#for(subModel : model.children) |
|
||||
#@renderModel(subModel) |
|
||||
#end |
|
||||
|
|
||||
#@renderParameter(model) |
|
||||
#end |
|
@ -1 +0,0 @@ |
|||||
package #(packageName); |
|
@ -1,37 +0,0 @@ |
|||||
#define renderParameter(model) |
|
||||
#for(parameter : model.allParameters) |
|
||||
//参数处理器, #(parameter.name)(#(parameter.type)) |
|
||||
private static void #(methodName(parameter.code))(Executor executor,Argument argument) { |
|
||||
if(log.isDebugEnabled()){log.debug(" {}","#(parameter.name)(#(parameter.type))");} |
|
||||
#for(processor : parameter.processors) |
|
||||
#if(processor.enable) |
|
||||
#switch (processor.type.toString()) |
|
||||
#case ("ARITHMETIC") |
|
||||
#include("/io/sc/engine/rule/core/code/generator/template/processor/arithmetic.tpl") |
|
||||
#case ("CONDITION_RANGE") |
|
||||
#include("/io/sc/engine/rule/core/code/generator/template/processor/conditionRange.ftl") |
|
||||
#case ("DECISION_TABLE") |
|
||||
#case ("DECISION_TABLE_2C") |
|
||||
#case ("DECISION_TREE") |
|
||||
#case ("EXECUTION_FLOW") |
|
||||
#case ("GROOVY_SCRIPT") |
|
||||
#case ("HTTP_REQUEST") |
|
||||
#case ("MATH_FORMULA") |
|
||||
#case ("NUMBER_RANGE") |
|
||||
#include("/io/sc/engine/rule/core/code/generator/template/processor/numberRange.ftl") |
|
||||
#case ("OBJECT_PROPERTIES") |
|
||||
#case ("OPTION_VALUE") |
|
||||
#case ("PMML") |
|
||||
#case ("RULE") |
|
||||
#case ("SCORE_CARD") |
|
||||
#case ("SINGLE_RULE") |
|
||||
#case ("SQL") |
|
||||
#case ("TERNARY") |
|
||||
#case ("WHEN_THEN") |
|
||||
#end |
|
||||
#end |
|
||||
#end |
|
||||
if(log.isDebugEnabled()){log.debug(" 参数结果值 : {}",argument.#(fieldName(parameter.code)));} |
|
||||
} |
|
||||
#end |
|
||||
#end |
|
@ -1,4 +0,0 @@ |
|||||
//决策表 |
|
||||
${DecisionTable.generateGroovyCode(parameter,processor)} |
|
||||
if(log.isDebugEnabled()){log.debug(" 决策表运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,4 +0,0 @@ |
|||||
//简单决策表 |
|
||||
${DecisionTable2C.generateGroovyCode(parameter,processor)} |
|
||||
if(log.isDebugEnabled()){log.debug(" 简单决策表运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,4 +0,0 @@ |
|||||
//决策树(此处只是入口,真正的决策树计算逻辑在后面通过一系列方法完成) |
|
||||
${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)} =Tree_${CodeReplacer.fieldName(parameter.code)}_${DecisionTree.parse(processor.decisionTree).id}(executor,${ExpressionReplacer.ARGUMENT_NAME}); |
|
||||
if(log.isDebugEnabled()){log.debug(" 决策树运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,13 +0,0 @@ |
|||||
/*-------------------------------------------------------------------------------* |
|
||||
* 决策树函数 * |
|
||||
*-------------------------------------------------------------------------------*/ |
|
||||
<#list (model.getAllParameters())! as parameter><#-- 循环将每个参数处理生成一个方法 --> |
|
||||
<#if ("INTERMEDIATE"==parameter.type || "OUT"==parameter.type)> |
|
||||
<#list (parameter.processors)! as processor> |
|
||||
<#if "DECISION_TREE"==processor.type> |
|
||||
//${parameter.name}(${parameter.type}) |
|
||||
${DecisionTree.generateGroovyCode(parameter,processor)} |
|
||||
</#if> |
|
||||
</#list> |
|
||||
</#if> |
|
||||
</#list> |
|
@ -1,13 +0,0 @@ |
|||||
/*-------------------------------------------------------------------------------* |
|
||||
* 执行流函数 * |
|
||||
*-------------------------------------------------------------------------------*/ |
|
||||
<#list (model.getAllParameters())! as parameter><#-- 循环将每个参数处理生成一个方法 --> |
|
||||
<#if ("INTERMEDIATE"==parameter.type || "OUT"==parameter.type)> |
|
||||
<#list (parameter.processors)! as processor> |
|
||||
<#if "EXECUTION_FLOW"==processor.type> |
|
||||
//${parameter.name}(${parameter.type}) |
|
||||
${ExecutionFlow.generateGroovyCode(parameter,processor)} |
|
||||
</#if> |
|
||||
</#list> |
|
||||
</#if> |
|
||||
</#list> |
|
@ -1,3 +0,0 @@ |
|||||
<#if processor.groovyScript??> |
|
||||
${ExpressionReplacer.groovy(processor.groovyScript,null)} |
|
||||
</#if> |
|
@ -1,4 +0,0 @@ |
|||||
//数学公式 |
|
||||
${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)} =${MathFormula.generateGroovyCode(parameter,processor)}; |
|
||||
if(log.isDebugEnabled()){log.debug(" 数学公式运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,4 +0,0 @@ |
|||||
//对象属性函数 |
|
||||
${ObjectProperty.generateGroovyCode(parameter,processor)} |
|
||||
if(log.isDebugEnabled()){log.debug(" 对象属性函数运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,34 +0,0 @@ |
|||||
//选项值运算 |
|
||||
<#assign optionParameter=model.getParameterByOptionParameterCode(processor.optionCode)> |
|
||||
<#if optionParameter??> |
|
||||
if(${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(optionParameter.code)}!=null){ |
|
||||
<#list model.getParameterOptionsByOptionParameterCode(processor.optionCode) as option> |
|
||||
<#if option_index==0> |
|
||||
<#if optionParameter.valueType=='java.lang.String'> |
|
||||
if(${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(optionParameter.code)}=="""${option.inputValue}"""){ |
|
||||
<#else> |
|
||||
if(${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(optionParameter.code)}==${option.inputValue}){ |
|
||||
</#if> |
|
||||
<#else> |
|
||||
<#if optionParameter.valueType=='java.lang.String'> |
|
||||
}else if(${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(optionParameter.code)}=="""${option.inputValue}"""){ |
|
||||
<#else> |
|
||||
}else if(${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(optionParameter.code)}==${option.inputValue}){ |
|
||||
</#if> |
|
||||
</#if> |
|
||||
<#if parameter.valueType=='java.lang.String'> |
|
||||
${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)} ="""${option.value}"""; |
|
||||
if(log.isDebugEnabled()){log.debug(" 选项运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
<#else> |
|
||||
${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)} =${option.value}; |
|
||||
if(log.isDebugEnabled()){log.debug(" 选项运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
</#if> |
|
||||
</#list> |
|
||||
}else{ |
|
||||
if(log.isDebugEnabled()){log.debug(" !!!未匹配到选项列表,选项运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
} |
|
||||
}else{ |
|
||||
if(log.isDebugEnabled()){log.debug(" !!!未提供选项输入值,选项运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
} |
|
||||
</#if> |
|
||||
|
|
@ -1,3 +0,0 @@ |
|||||
<#if processor.pmml??> |
|
||||
arg.${CodeReplacer.fieldName(parameter.code)} =JpmmlEvaluator.evaluate('${parameter.id}_${processor.id}','''${processor.pmml}''',arg.toMap()); |
|
||||
</#if> |
|
@ -1,17 +0,0 @@ |
|||||
//SQL 运算 |
|
||||
<#if processor.sqlDatasourceName??> |
|
||||
javax.sql.DataSource ds =io.sc.engine.rule.client.spring.util.EngineSpringApplicationContextUtil.getDataSource('${processor.sqlDatasourceName}'); |
|
||||
<#else> |
|
||||
javax.sql.DataSource ds =io.sc.engine.rule.client.spring.util.EngineSpringApplicationContextUtil.getDefaultDataSource(); |
|
||||
</#if> |
|
||||
String sql =${ExpressionReplacer.groovy("\n"+processor.sql,"java.lang.String")}; |
|
||||
<#if processor.sql??> |
|
||||
ESql.withInstance(ds){ db-> |
|
||||
db.query(sql) { rs -> |
|
||||
if (rs.next()) { |
|
||||
${SqlFieldMapping.generateGroovyCode(parameter,processor)} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
</#if> |
|
||||
|
|
@ -1,4 +0,0 @@ |
|||||
//三元运算 |
|
||||
${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)} =(${ExpressionReplacer.groovy(processor.ternaryCondition,null)}) ? (${ExpressionReplacer.groovy(processor.ternaryTrue,parameter.valueType)}) : (${ExpressionReplacer.groovy(processor.ternaryFalse,parameter.valueType)}); |
|
||||
if(log.isDebugEnabled()){log.debug(" 三元运算结果 : {}",${ExpressionReplacer.ARGUMENT_NAME}.${CodeReplacer.fieldName(parameter.code)});} |
|
||||
|
|
@ -1,4 +0,0 @@ |
|||||
|
|
||||
//算数运算 |
|
||||
argument.#(fieldName(parameter.code)) =#(groovy(processor.arithmetic,parameter.valueType));//#(parameter.type), #(parameter.name) |
|
||||
if(log.isDebugEnabled()){log.debug(" 算数运算结果 : {}",argument.#(fieldName(parameter.code)));} |
|
@ -1,3 +0,0 @@ |
|||||
//条件分段函数 |
|
||||
#(io.sc.engine.rule.core.code.impl.support.processor.ConditionRange::generateGroovyCode(parameter,processor)) |
|
||||
if(log.isDebugEnabled()){log.debug(" 条件分段函数运算结果 : {}",argument.#(fieldName(parameter.code)));} |
|
@ -1,4 +0,0 @@ |
|||||
//数值分段函数 |
|
||||
#(io.sc.engine.rule.core.code.impl.support.processor.NumberRange::generateGroovyCode(parameter,processor)) |
|
||||
if(log.isDebugEnabled()){log.debug(" 数值分段函数运算结果 : {}",argument.#(fieldName(parameter.code)));} |
|
||||
|
|
@ -1,118 +0,0 @@ |
|||||
#include("package.tpl") |
|
||||
|
|
||||
#include("import.tpl") |
|
||||
|
|
||||
#include("functions.tpl") |
|
||||
|
|
||||
#set(resource = executeUnit.resource) |
|
||||
#set(libs =executeUnit.libs) |
|
||||
#set(model = resource.model) |
|
||||
|
|
||||
/******************************************************** |
|
||||
* Execute Unit |
|
||||
********************************************************/ |
|
||||
/** |
|
||||
* type: #(executeUnit.type) |
|
||||
* code: #(resource.code), |
|
||||
* name: #(resource.name), |
|
||||
* version: #(resource.version) |
|
||||
*/ |
|
||||
class #(className(resource.code,resource.version)) { |
|
||||
private static final Logger log =LoggerFactory.getLogger(#(className(resource.code,resource.version)).class); |
|
||||
|
|
||||
/** |
|
||||
* 执行方法(Map作为输入参数,该 Map 封装了调用模型的参数) |
|
||||
* @param map 输入参数封装器 |
|
||||
* @return 执行结果 |
|
||||
*/ |
|
||||
public static ResourceResult execute(Map<String,Object> map) throws Exception{ |
|
||||
return execute(map["executor"],map["subModelCode"],map["argument"]); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 执行方法 |
|
||||
* @param executor 执行器对象 |
|
||||
* @param subModelCode 子模型代码 |
|
||||
* @param data 输入参数 |
|
||||
* @return 执行结果 |
|
||||
*/ |
|
||||
public static ResourceResult execute(Executor executor,String subModelCode,Object data) throws Exception{ |
|
||||
if(log.isDebugEnabled()){log.debug("开始执行: #(resource.name)");} |
|
||||
if(data instanceof Map){ |
|
||||
if(log.isDebugEnabled()){log.debug("显示输入参数(Map)=================================================\n{}",ObjectMapperUtil.json().writeValueAsString(data));} |
|
||||
}else if(data instanceof String){ |
|
||||
if(log.isDebugEnabled()){log.debug("显示输入参数(Json字符串)=================================================\n{}",data);} |
|
||||
} |
|
||||
|
|
||||
//初始化指标库 |
|
||||
#for(lib : libs) |
|
||||
if(log.isDebugEnabled()){log.debug("初始化指标库 : #(lib.name)_V#(lib.version)");} |
|
||||
|
|
||||
#(className(lib.code,lib.version)) #(varName(lib.code,lib.version)) =new #(className)(); |
|
||||
#(className(lib.code,lib.version)).convertArgument(data); |
|
||||
#end |
|
||||
|
|
||||
//初始化模型参数 |
|
||||
if(log.isDebugEnabled()){log.debug("从 {} 初始化模型参数","java.lang.String"==data.class?"Json":"Map");} |
|
||||
Argument argument =Argument.convertArgument(data); |
|
||||
|
|
||||
//输入参数合法性检查 |
|
||||
ValidateResult validateResult =new ValidateResult(); |
|
||||
|
|
||||
//输入参数合法性检查(指标库) |
|
||||
#for(lib : libs) |
|
||||
if(log.isDebugEnabled()){log.debug("开始检查输入参数数据合法性(指标库: #(lib.name)_V#(lib.version))");} |
|
||||
#(varName(lib.code,lib.version)).validate(validateResult); |
|
||||
if(validateResult.hasError()){ |
|
||||
if(log.isDebugEnabled()){log.debug("\t检查结果: 失败!");} |
|
||||
ResourceResult result =new ResourceResult(); |
|
||||
result.setValidateResult(validateResult); |
|
||||
return result; |
|
||||
} |
|
||||
if(log.isDebugEnabled()){log.debug("\t检查结果: 成功!");} |
|
||||
#end |
|
||||
|
|
||||
//输入参数合法性检查(模型) |
|
||||
if(log.isDebugEnabled()){log.debug("开始检查模型输入参数数据合法性");} |
|
||||
argument.validate(validateResult); |
|
||||
if(validateResult.hasError()){ |
|
||||
if(log.isDebugEnabled()){log.debug("\t检查结果: 失败!");} |
|
||||
ResourceResult result =new ResourceResult(); |
|
||||
result.setValidateResult(validateResult); |
|
||||
return result; |
|
||||
} |
|
||||
if(log.isDebugEnabled()){log.debug("\t检查结果: 成功!");} |
|
||||
|
|
||||
//执行指标库 |
|
||||
#for(lib : executeUnit.getLibs()) |
|
||||
if(log.isDebugEnabled()){log.debug("开始执行指标库: #(lib.name)_V#(lib.version)");} |
|
||||
#(varName(lib.code,lib.version)).execute(); |
|
||||
</#list> |
|
||||
#end |
|
||||
|
|
||||
//合并指标库指标 |
|
||||
if(log.isDebugEnabled()){log.debug("开始合并指标库指标");} |
|
||||
Map<String,Object> indicatorLibs =new HashMap<String,Object>(); |
|
||||
#for(lib : executeUnit.getLibs()) |
|
||||
indicatorLibs.put("#(varName(lib.code,lib.version))",#(varName(lib.code,lib.version))); |
|
||||
#end |
|
||||
argument.mergeParameterValueFromIndicatorLib(indicatorLibs); |
|
||||
if(log.isDebugEnabled()){log.debug("显示合并后输入参数:\n{}",ObjectMapperUtil.json().writeValueAsString(argument));} |
|
||||
|
|
||||
//执行模型 |
|
||||
if(subModelCode){ |
|
||||
subModelCode =CodeReplacer.methodName(subModelCode); |
|
||||
"${subModelCode}"(executor,argument);//调用子模型 |
|
||||
}else{ |
|
||||
#(methodName(model.code))(executor,argument);//调用顶级模型(#(model.name)) |
|
||||
} |
|
||||
|
|
||||
//返回结果 |
|
||||
ResourceResult result =argument.toResult(); |
|
||||
if(log.isDebugEnabled()){log.debug("模型调用成功,返回结果:\n{}",ObjectMapperUtil.json().writeValueAsString(result));} |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
#include("parameter.tpl") |
|
||||
#include("model.tpl") |
|
||||
#@renderModel(model) |
|
@ -1,128 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl; |
|
||||
|
|
||||
import java.io.StringWriter; |
|
||||
import java.util.HashMap; |
|
||||
import java.util.Map; |
|
||||
import java.util.concurrent.ConcurrentHashMap; |
|
||||
|
|
||||
import io.sc.engine.rule.core.code.CodeGenerator; |
|
||||
import io.sc.engine.rule.core.code.SourceCode; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper4Lib; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper4Resource; |
|
||||
import io.sc.engine.rule.core.enums.ResourceType; |
|
||||
import io.sc.engine.rule.core.po.lib.Lib; |
|
||||
import io.sc.engine.rule.core.po.resource.ModelResource; |
|
||||
import io.sc.engine.rule.core.po.resource.Resource; |
|
||||
|
|
||||
import freemarker.cache.ClassTemplateLoader; |
|
||||
import freemarker.ext.beans.BeansWrapper; |
|
||||
import freemarker.ext.beans.BeansWrapperBuilder; |
|
||||
import freemarker.template.Configuration; |
|
||||
import freemarker.template.Template; |
|
||||
import freemarker.template.TemplateExceptionHandler; |
|
||||
import freemarker.template.TemplateHashModel; |
|
||||
|
|
||||
public class GroovySourceCodeGenerator implements CodeGenerator{ |
|
||||
private static final String PACKAGE_NAME ="io.sc.engine.rule.core.code.generated"; |
|
||||
private Map<String,SourceCode> cache =new ConcurrentHashMap<String,SourceCode>(); |
|
||||
|
|
||||
@Override |
|
||||
public SourceCode generateSourceCodeById(String resourceId, ResourceWrapper wrapper) throws Exception { |
|
||||
if(wrapper!=null) { |
|
||||
return _generateSourceCode(wrapper); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public SourceCode generateSourceCodeByCode(String resourceCode, Integer version, ResourceWrapper wrapper) throws Exception { |
|
||||
if(wrapper!=null) { |
|
||||
if(version==null) { //未指定版本,不缓存
|
|
||||
return _generateSourceCode(wrapper); |
|
||||
}else { //指定了版本,缓存
|
|
||||
String key =getCacheKey(resourceCode,version); |
|
||||
SourceCode sourceCode =cache.get(key); |
|
||||
if(sourceCode==null) { |
|
||||
sourceCode =_generateSourceCode(wrapper); |
|
||||
cache.put(key, sourceCode); |
|
||||
return sourceCode; |
|
||||
}else { |
|
||||
return sourceCode; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
private SourceCode _generateSourceCode(ResourceWrapper wrapper) throws Exception{ |
|
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_32); |
|
||||
cfg.setTemplateLoader(new ClassTemplateLoader(GroovySourceCodeGenerator.class,"/io/sc/engine/rule/core/code/template/groovy")); |
|
||||
cfg.setDefaultEncoding("UTF-8"); |
|
||||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
|
||||
cfg.setLogTemplateExceptions(false); |
|
||||
cfg.setWrapUncheckedExceptions(true); |
|
||||
|
|
||||
Map<String,Object> root = new HashMap<String,Object>(); |
|
||||
|
|
||||
String wrapperCode =null; |
|
||||
if("RESOURCE".equals(wrapper.getType())){ |
|
||||
ResourceWrapper4Resource _wrapper =(ResourceWrapper4Resource)wrapper; |
|
||||
Resource resource =_wrapper.getResource(); |
|
||||
wrapperCode =resource.getCode(); |
|
||||
if(ResourceType.MODEL.equals(resource.getType())) { |
|
||||
ModelResource modelResource =(ModelResource)resource; |
|
||||
modelResource.getModel().buildFullName(); |
|
||||
} |
|
||||
}else if("LIB".equals(wrapper.getType())) { |
|
||||
ResourceWrapper4Lib _wrapper =(ResourceWrapper4Lib)wrapper; |
|
||||
Lib lib =_wrapper.getLib(); |
|
||||
wrapperCode =lib.getCode(); |
|
||||
} |
|
||||
|
|
||||
root.put("$wrapper", wrapper); |
|
||||
|
|
||||
root.put("CodeReplacer", getStaticMethod("io.sc.engine.rule.core.util.CodeReplacer")); |
|
||||
root.put("ExpressionReplacer", getStaticMethod("io.sc.engine.rule.core.util.ExpressionReplacer")); |
|
||||
root.put("ValueType", getStaticMethod("io.sc.engine.rule.core.enums.ValueType")); |
|
||||
|
|
||||
root.put("ObjectProperty", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.ObjectProperty")); |
|
||||
root.put("MathFormula", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.MathFormula")); |
|
||||
root.put("ConditionRange", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.ConditionRange")); |
|
||||
root.put("NumberRange", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.NumberRange")); |
|
||||
root.put("DecisionTable2C", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.DecisionTable2C")); |
|
||||
root.put("DecisionTable", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.DecisionTable")); |
|
||||
root.put("DecisionTree", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.DecisionTree")); |
|
||||
root.put("ExecutionFlow", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.ExecutionFlow")); |
|
||||
root.put("Option", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.Option")); |
|
||||
root.put("Rule", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.Rule")); |
|
||||
root.put("SingleRule", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.SingleRule")); |
|
||||
root.put("SqlFieldMapping", getStaticMethod("io.sc.engine.rule.core.code.impl.support.processor.SqlFieldMapping")); |
|
||||
|
|
||||
root.put("LibUtil", getStaticMethod("io.sc.engine.rule.core.code.impl.support.lib.LibUtil")); |
|
||||
|
|
||||
|
|
||||
Template template =cfg.getTemplate("groovy.ftl"); |
|
||||
StringWriter out =new StringWriter(); |
|
||||
template.process(root, out); |
|
||||
out.close(); |
|
||||
return new SourceCode(PACKAGE_NAME,wrapperCode,out.toString()); |
|
||||
} |
|
||||
|
|
||||
private TemplateHashModel getStaticMethod(String className) throws Exception{ |
|
||||
BeansWrapper wrapper =new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build(); |
|
||||
TemplateHashModel staticModels = wrapper.getStaticModels(); |
|
||||
return (TemplateHashModel)staticModels.get(className); |
|
||||
} |
|
||||
|
|
||||
private String getCacheKey(String code,Integer version) { |
|
||||
String result =""; |
|
||||
if(code!=null && !"".equals(code.trim())) { |
|
||||
result +=code; |
|
||||
} |
|
||||
if(version!=null) { |
|
||||
result +="_" + version; |
|
||||
} |
|
||||
return result; |
|
||||
} |
|
||||
} |
|
@ -1,24 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl; |
|
||||
|
|
||||
import io.sc.engine.rule.core.code.SourceCode; |
|
||||
import io.sc.engine.rule.core.code.SourceCodeGenerator; |
|
||||
import io.sc.engine.rule.core.code.generator.ExecuteUnit4LibGenerator; |
|
||||
import io.sc.engine.rule.core.code.generator.ExecuteUnit4ResourceGenerator; |
|
||||
import io.sc.engine.rule.core.code.generator.ExecuteUnitGenerator; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ExecuteUnit; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ExecuteUnit4Lib; |
|
||||
import io.sc.engine.rule.core.code.impl.support.ExecuteUnit4Resource; |
|
||||
|
|
||||
public class JavaSourceCodeGenerator implements SourceCodeGenerator { |
|
||||
@Override |
|
||||
public SourceCode generateSourceCode(ExecuteUnit executeUnit) throws Exception { |
|
||||
if(executeUnit instanceof ExecuteUnit4Resource){ |
|
||||
ExecuteUnitGenerator generator =new ExecuteUnit4ResourceGenerator((ExecuteUnit4Resource)executeUnit); |
|
||||
return generator.generateSourceCode(); |
|
||||
}else if(executeUnit instanceof ExecuteUnit4Lib){ |
|
||||
ExecuteUnitGenerator generator =new ExecuteUnit4LibGenerator((ExecuteUnit4Lib)executeUnit); |
|
||||
return generator.generateSourceCode(); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
@ -1,40 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes; |
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo; |
|
||||
import io.sc.engine.rule.core.po.function.Function; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type",defaultImpl=ResourceWrapper4Resource.class) |
|
||||
@JsonSubTypes({ |
|
||||
@JsonSubTypes.Type(value=ResourceWrapper4Resource.class), |
|
||||
@JsonSubTypes.Type(value=ResourceWrapper4Lib.class) |
|
||||
}) |
|
||||
public abstract class ResourceWrapper { |
|
||||
protected boolean isExecuteTestCase; |
|
||||
protected List<Function> functions; |
|
||||
|
|
||||
public boolean getIsExecuteTestCase() { |
|
||||
return isExecuteTestCase; |
|
||||
} |
|
||||
|
|
||||
public void setIsExecuteTestCase(boolean isExecuteTestCase) { |
|
||||
this.isExecuteTestCase = isExecuteTestCase; |
|
||||
} |
|
||||
|
|
||||
public List<Function> getFunctions() { |
|
||||
return functions; |
|
||||
} |
|
||||
|
|
||||
public void setFunctions(List<Function> functions) { |
|
||||
this.functions = functions; |
|
||||
} |
|
||||
|
|
||||
@JsonIgnore |
|
||||
public abstract String getType(); |
|
||||
} |
|
@ -1,43 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.dictionary.Dictionary; |
|
||||
import io.sc.engine.rule.core.po.lib.Lib; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName; |
|
||||
|
|
||||
@JsonTypeName("LIB") |
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class ResourceWrapper4Lib extends ResourceWrapper{ |
|
||||
private List<Dictionary> dictionaries; |
|
||||
private Lib lib; |
|
||||
|
|
||||
|
|
||||
@Override |
|
||||
@JsonIgnore |
|
||||
public String getType() { |
|
||||
return "LIB"; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public List<Dictionary> getDictionaries() { |
|
||||
return dictionaries; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public void setDictionaries(List<Dictionary> dictionaries) { |
|
||||
this.dictionaries = dictionaries; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
public Lib getLib() { |
|
||||
return lib; |
|
||||
} |
|
||||
|
|
||||
public void setLib(Lib lib) { |
|
||||
this.lib = lib; |
|
||||
} |
|
||||
} |
|
@ -1,52 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.dictionary.Dictionary; |
|
||||
import io.sc.engine.rule.core.po.lib.Lib; |
|
||||
import io.sc.engine.rule.core.po.resource.Resource; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName; |
|
||||
|
|
||||
|
|
||||
@JsonTypeName("RESOURCE") |
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class ResourceWrapper4Resource extends ResourceWrapper{ |
|
||||
private Resource resource; |
|
||||
private List<Dictionary> dictionaries; |
|
||||
private List<Lib> libs; |
|
||||
|
|
||||
|
|
||||
@Override |
|
||||
@JsonIgnore |
|
||||
public String getType() { |
|
||||
return "RESOURCE"; |
|
||||
} |
|
||||
|
|
||||
public Resource getResource() { |
|
||||
return resource; |
|
||||
} |
|
||||
|
|
||||
public void setResource(Resource resource) { |
|
||||
this.resource = resource; |
|
||||
} |
|
||||
|
|
||||
public List<Dictionary> getDictionaries() { |
|
||||
return dictionaries; |
|
||||
} |
|
||||
|
|
||||
public void setDictionaries(List<Dictionary> dictionaries) { |
|
||||
this.dictionaries = dictionaries; |
|
||||
} |
|
||||
|
|
||||
public List<Lib> getLibs() { |
|
||||
return libs; |
|
||||
} |
|
||||
|
|
||||
public void setLibs(List<Lib> libs) { |
|
||||
this.libs = libs; |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,54 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.lib; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import io.sc.engine.rule.core.po.lib.Indicator; |
|
||||
import io.sc.engine.rule.core.po.lib.IndicatorLib; |
|
||||
import io.sc.engine.rule.core.po.lib.Lib; |
|
||||
|
|
||||
/** |
|
||||
* 库辅助类 |
|
||||
* @author wangshaoping |
|
||||
* |
|
||||
*/ |
|
||||
public class LibUtil { |
|
||||
/** |
|
||||
* 查找指标 |
|
||||
* @param libs 库列表 |
|
||||
* @param libCode 被查找的指标所属库代码 |
|
||||
* @param libVersion 被查找的指标所属库版本 |
|
||||
* @param indicatorCode 被查找的指标代码 |
|
||||
* @return 指标对象 |
|
||||
*/ |
|
||||
public static Indicator findIndicator(List<Lib> libs,String libCode,Integer libVersion,String indicatorCode) { |
|
||||
if(libs==null || libs.size()==0) { |
|
||||
return null; |
|
||||
} |
|
||||
if(libCode==null || "".equals(libCode.trim())) { |
|
||||
return null; |
|
||||
} |
|
||||
if(libVersion==null) { |
|
||||
return null; |
|
||||
} |
|
||||
if(indicatorCode==null || "".equals(indicatorCode.trim())) { |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
for(Lib lib : libs) { |
|
||||
if(lib instanceof IndicatorLib) { |
|
||||
IndicatorLib indicatorLib =(IndicatorLib)lib; |
|
||||
if(libCode.equals(indicatorLib.getCode()) && libVersion.equals(indicatorLib.getVersion())) { |
|
||||
List<Indicator> indicators =indicatorLib.getIndicators(); |
|
||||
if(indicators!=null && indicators.size()>0) { |
|
||||
for(Indicator indicator : indicators) { |
|
||||
if(indicatorCode.equals(indicator.getCode())) { |
|
||||
return indicator; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
@ -1,9 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.parameter; |
|
||||
|
|
||||
import io.sc.engine.rule.core.code.impl.support.ResourceWrapper; |
|
||||
|
|
||||
public interface ParameterGroovyCodeContributionItem { |
|
||||
public String forArgumentField(ResourceWrapper wrapper); |
|
||||
public String forConvertArgumentFromMap(ResourceWrapper wrapper,String targetVarName); |
|
||||
public String forConvertArgumentFromJson(ResourceWrapper wrapper,String targetVarName); |
|
||||
} |
|
@ -1,125 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.lib.Indicator; |
|
||||
import io.sc.engine.rule.core.po.lib.processor.ConditionRangeIndicatorProcessor; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.ConditionRangeParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class ConditionRange { |
|
||||
private String condition; |
|
||||
private String value; |
|
||||
|
|
||||
public static List<ConditionRange> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<ConditionRange>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Indicator indicator, ConditionRangeIndicatorProcessor processor) throws Exception{ |
|
||||
if(indicator==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<ConditionRange> conditionRanges =parse(processor.getConditionRange()); |
|
||||
List<ConditionRange> _conditionRanges =new ArrayList<ConditionRange>(); |
|
||||
|
|
||||
//移除没有填写条件的条件范围
|
|
||||
if(conditionRanges!=null && conditionRanges.size()>0) { |
|
||||
for(ConditionRange conditionRange : conditionRanges) { |
|
||||
if(conditionRange.getCondition()!=null && !"".equals(conditionRange.getCondition().trim())) { |
|
||||
_conditionRanges.add(conditionRange); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_conditionRanges!=null && _conditionRanges.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int size =_conditionRanges.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
ConditionRange conditionRange =_conditionRanges.get(i); |
|
||||
if(i==0) { |
|
||||
sb.append("if("); |
|
||||
}else { |
|
||||
sb.append("else if("); |
|
||||
} |
|
||||
sb.append(ExpressionReplacer.groovy(conditionRange.condition, null)); |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
if(conditionRange.value==null || conditionRange.value.trim().isEmpty()){ |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(indicator.getCode())).append(" =null;\n"); |
|
||||
}else { |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(indicator.getCode())).append(" =").append(ExpressionReplacer.groovy(conditionRange.value,indicator.getValueType())).append(";\n"); |
|
||||
} |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s ConditionRange groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,ConditionRangeParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<ConditionRange> conditionRanges =parse(processor.getConditionRange()); |
|
||||
List<ConditionRange> _conditionRanges =new ArrayList<ConditionRange>(); |
|
||||
|
|
||||
//移除没有填写条件的条件范围
|
|
||||
if(conditionRanges!=null && conditionRanges.size()>0) { |
|
||||
for(ConditionRange conditionRange : conditionRanges) { |
|
||||
if(conditionRange.getCondition()!=null && !"".equals(conditionRange.getCondition().trim())) { |
|
||||
_conditionRanges.add(conditionRange); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_conditionRanges!=null && _conditionRanges.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int size =_conditionRanges.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
ConditionRange conditionRange =_conditionRanges.get(i); |
|
||||
if(i==0) { |
|
||||
sb.append("if("); |
|
||||
}else { |
|
||||
sb.append("else if("); |
|
||||
} |
|
||||
sb.append(ExpressionReplacer.groovy(conditionRange.condition, null)); |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
if(conditionRange.value==null || conditionRange.value.trim().isEmpty()){ |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(" =null;\n"); |
|
||||
}else { |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(" =").append(ExpressionReplacer.groovy(conditionRange.value,parameter.getValueType())).append(";\n"); |
|
||||
} |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s ConditionRange groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public String getCondition() { |
|
||||
return condition; |
|
||||
} |
|
||||
public void setCondition(String condition) { |
|
||||
this.condition = condition; |
|
||||
} |
|
||||
public String getValue() { |
|
||||
return value; |
|
||||
} |
|
||||
public void setValue(String value) { |
|
||||
this.value = value; |
|
||||
} |
|
||||
} |
|
@ -1,94 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.DecisionTableParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
public class DecisionTable { |
|
||||
public static List<Map<String,Object>> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Map<String,Object>>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,DecisionTableParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
try { |
|
||||
List<Map<String,Object>> decisionTables =parse(processor.getDecisionTable()); |
|
||||
if(decisionTables!=null && decisionTables.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int conditionRowCount =getConditionRowCount(decisionTables); |
|
||||
int resultRowCount =getResultRowCount(decisionTables); |
|
||||
int colCount =getColCount(decisionTables); |
|
||||
sb.append("if (1==2){").append("\n"); |
|
||||
sb.append("\t\t}"); |
|
||||
for(int j=1;j<colCount;j++) {//一列一列生成
|
|
||||
sb.append("else if (true"); |
|
||||
for(int i=0;i<conditionRowCount;i++) { |
|
||||
String condition =decisionTables.get(i).get("C"+j)==null?"":decisionTables.get(i).get("C"+j).toString(); |
|
||||
if("Y".equalsIgnoreCase(condition) || "T".equalsIgnoreCase(condition)) { |
|
||||
sb.append(" && ").append("(").append(ExpressionReplacer.groovy((String)decisionTables.get(i).get("C0"),null)).append(")"); |
|
||||
} |
|
||||
} |
|
||||
sb.append("){").append("\n"); |
|
||||
for(int l=conditionRowCount;l<(conditionRowCount+resultRowCount);l++) { |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.groovy((String)decisionTables.get(l).get("C0"),null)).append(" ="); |
|
||||
String expression =(String)decisionTables.get(l).get("C"+j); |
|
||||
sb.append(ExpressionReplacer.groovy(expression, parameter.getValueType())); |
|
||||
sb.append(";"); |
|
||||
sb.append("\n"); |
|
||||
} |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s DecisionTable groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
private static int getColCount(List<Map<String,Object>> decisionTables) { |
|
||||
if(decisionTables!=null && decisionTables.size()>0) { |
|
||||
Map<String,Object> row =decisionTables.get(0); |
|
||||
if(row!=null && row.size()>0) { |
|
||||
int index =0; |
|
||||
while(row.containsKey("C"+index)) { |
|
||||
index++; |
|
||||
} |
|
||||
return index; |
|
||||
} |
|
||||
} |
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
private static int getConditionRowCount(List<Map<String,Object>> decisionTables) { |
|
||||
if(decisionTables!=null && decisionTables.size()>0) { |
|
||||
int index =0; |
|
||||
while("condition".equalsIgnoreCase((String)decisionTables.get(index).get("type"))) { |
|
||||
index++; |
|
||||
} |
|
||||
return index; |
|
||||
} |
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
private static int getResultRowCount(List<Map<String,Object>> decisionTables) { |
|
||||
if(decisionTables!=null && decisionTables.size()>0) { |
|
||||
int index =0; |
|
||||
for(Map<String,Object> map : decisionTables) { |
|
||||
if("result".equalsIgnoreCase((String)map.get("type"))) { |
|
||||
index++; |
|
||||
} |
|
||||
} |
|
||||
return index; |
|
||||
} |
|
||||
return 0; |
|
||||
} |
|
||||
} |
|
@ -1,66 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.DecisionTable2CParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
public class DecisionTable2C { |
|
||||
public static List<Map<String,Object>> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Map<String,Object>>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,DecisionTable2CParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
try { |
|
||||
List<Map<String,Object>> decisionTable2Cs =parse(processor.getDecisionTable2C()); |
|
||||
if(decisionTable2Cs!=null && decisionTable2Cs.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int rowCount =decisionTable2Cs.size(); |
|
||||
int colCount =getColCount(decisionTable2Cs); |
|
||||
sb.append("if (1==2){").append("\n"); |
|
||||
sb.append("\t\t}"); |
|
||||
for(int i=1;i<rowCount;i++) { |
|
||||
for(int j=1;j<colCount;j++) { |
|
||||
sb.append("else if ("); |
|
||||
sb.append("(").append(ExpressionReplacer.groovy((String)decisionTable2Cs.get(i).get("C0"),null)).append(") && (").append(ExpressionReplacer.groovy((String)decisionTable2Cs.get(0).get("C"+j),null)).append(")"); |
|
||||
sb.append("){").append("\n"); |
|
||||
Object obj =decisionTable2Cs.get(i).get("C"+j); |
|
||||
if(obj!=null) { |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(" ="); |
|
||||
String expression =obj.toString(); |
|
||||
sb.append(ExpressionReplacer.groovy(expression, parameter.getValueType())).append(";\n"); |
|
||||
} |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s DecisionTable2C groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
private static int getColCount(List<Map<String,Object>> decisionTable2Cs) { |
|
||||
if(decisionTable2Cs!=null && decisionTable2Cs.size()>0) { |
|
||||
Map<String,Object> row =decisionTable2Cs.get(0); |
|
||||
if(row!=null && row.size()>0) { |
|
||||
int index =0; |
|
||||
while(row.containsKey("C"+index)) { |
|
||||
index++; |
|
||||
} |
|
||||
return index; |
|
||||
} |
|
||||
} |
|
||||
return 0; |
|
||||
} |
|
||||
} |
|
@ -1,187 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.lib.Indicator; |
|
||||
import io.sc.engine.rule.core.po.lib.processor.NumberRangeIndicatorProcessor; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.NumberRangeParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.math.BigDecimal; |
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class NumberRange { |
|
||||
private Boolean minIncluded; |
|
||||
private BigDecimal min; |
|
||||
private BigDecimal max; |
|
||||
private Boolean maxIncluded; |
|
||||
private String value; |
|
||||
|
|
||||
public static List<NumberRange> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<NumberRange>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,NumberRangeParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<NumberRange> numberRanges =parse(processor.getNumberRange()); |
|
||||
List<NumberRange> _numberRanges =new ArrayList<NumberRange>(); |
|
||||
|
|
||||
//移除空行
|
|
||||
if(numberRanges!=null && numberRanges.size()>0) { |
|
||||
for(NumberRange numberRange : numberRanges) { |
|
||||
if(numberRange.min!=null || numberRange.max!=null || numberRange.value!=null) { |
|
||||
_numberRanges.add(numberRange); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
String parameterName =CodeReplacer.fieldName(parameter.getCode()); |
|
||||
String var =processor.getNumberRangeVar(); |
|
||||
String valueType =parameter.getValueType(); |
|
||||
if(_numberRanges!=null && _numberRanges.size()>0) { |
|
||||
if(var==null || "".equals(var.trim())) { |
|
||||
var =ExpressionReplacer.ARGUMENT_NAME + "." + parameterName; |
|
||||
} |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int size =_numberRanges.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
NumberRange numberRange =_numberRanges.get(i); |
|
||||
if(i==0) { |
|
||||
sb.append("if("); |
|
||||
}else { |
|
||||
sb.append("else if("); |
|
||||
} |
|
||||
if(numberRange.min==null && numberRange.max==null) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("==null"); |
|
||||
}else if(numberRange.min!=null) { |
|
||||
if(numberRange.minIncluded!=null && numberRange.minIncluded) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append(">=").append(numberRange.min); |
|
||||
}else { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append(">").append(numberRange.min); |
|
||||
} |
|
||||
} |
|
||||
if(numberRange.min!=null && numberRange.max!=null) { |
|
||||
sb.append(" && "); |
|
||||
} |
|
||||
if(numberRange.max!=null) { |
|
||||
if(numberRange.maxIncluded!=null && numberRange.maxIncluded) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("<=").append(numberRange.max); |
|
||||
}else { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("<").append(numberRange.max); |
|
||||
} |
|
||||
} |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(parameterName).append(" =").append(ExpressionReplacer.groovy(numberRange.value, valueType)).append(";\n"); |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s NumberRange groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Indicator indicator,NumberRangeIndicatorProcessor processor) throws Exception{ |
|
||||
if(indicator==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<NumberRange> numberRanges =parse(processor.getNumberRange()); |
|
||||
List<NumberRange> _numberRanges =new ArrayList<NumberRange>(); |
|
||||
|
|
||||
//移除空行
|
|
||||
if(numberRanges!=null && numberRanges.size()>0) { |
|
||||
for(NumberRange numberRange : numberRanges) { |
|
||||
if(numberRange.min!=null || numberRange.max!=null || numberRange.value!=null) { |
|
||||
_numberRanges.add(numberRange); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
String parameterName =CodeReplacer.fieldName(indicator.getCode()); |
|
||||
String var =processor.getNumberRangeVar(); |
|
||||
String valueType =indicator.getValueType(); |
|
||||
if(_numberRanges!=null && _numberRanges.size()>0) { |
|
||||
if(var==null || "".equals(var.trim())) { |
|
||||
var =ExpressionReplacer.ARGUMENT_NAME + "." + parameterName; |
|
||||
} |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int size =_numberRanges.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
NumberRange numberRange =_numberRanges.get(i); |
|
||||
if(i==0) { |
|
||||
sb.append("if("); |
|
||||
}else { |
|
||||
sb.append("else if("); |
|
||||
} |
|
||||
if(numberRange.min==null && numberRange.max==null) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("==null"); |
|
||||
}else if(numberRange.min!=null) { |
|
||||
if(numberRange.minIncluded!=null && numberRange.minIncluded) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append(">=").append(numberRange.min); |
|
||||
}else { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append(">").append(numberRange.min); |
|
||||
} |
|
||||
} |
|
||||
if(numberRange.min!=null && numberRange.max!=null) { |
|
||||
sb.append(" && "); |
|
||||
} |
|
||||
if(numberRange.max!=null) { |
|
||||
if(numberRange.maxIncluded!=null && numberRange.maxIncluded) { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("<=").append(numberRange.max); |
|
||||
}else { |
|
||||
sb.append(ExpressionReplacer.groovy(var, null)).append("<").append(numberRange.max); |
|
||||
} |
|
||||
} |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(parameterName).append(" =").append(ExpressionReplacer.groovy(numberRange.value, valueType)).append(";\n"); |
|
||||
sb.append("\t\t}"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s NumberRange groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public Boolean getMinIncluded() { |
|
||||
return minIncluded; |
|
||||
} |
|
||||
public void setMinIncluded(Boolean minIncluded) { |
|
||||
this.minIncluded = minIncluded; |
|
||||
} |
|
||||
public BigDecimal getMin() { |
|
||||
return min; |
|
||||
} |
|
||||
public void setMin(BigDecimal min) { |
|
||||
this.min = min; |
|
||||
} |
|
||||
public BigDecimal getMax() { |
|
||||
return max; |
|
||||
} |
|
||||
public void setMax(BigDecimal max) { |
|
||||
this.max = max; |
|
||||
} |
|
||||
public Boolean getMaxIncluded() { |
|
||||
return maxIncluded; |
|
||||
} |
|
||||
public void setMaxIncluded(Boolean maxIncluded) { |
|
||||
this.maxIncluded = maxIncluded; |
|
||||
} |
|
||||
public String getValue() { |
|
||||
return value; |
|
||||
} |
|
||||
public void setValue(String value) { |
|
||||
this.value = value; |
|
||||
} |
|
||||
} |
|
@ -1,146 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.lib.Indicator; |
|
||||
import io.sc.engine.rule.core.po.lib.processor.ObjectPropertiesIndicatorProcessor; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.ObjectPropertiesParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
import org.springframework.util.StringUtils; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class ObjectProperty { |
|
||||
private String code; |
|
||||
private String name; |
|
||||
private String expression; |
|
||||
|
|
||||
public static List<ObjectProperty> parse(String json) throws Exception { |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<ObjectProperty>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Indicator indicator, ObjectPropertiesIndicatorProcessor processor) throws Exception{ |
|
||||
if(indicator==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
String objectCondition =processor.getObjectCondition(); |
|
||||
List<ObjectProperty> objectProperties =parse(processor.getObjectProperties()); |
|
||||
List<ObjectProperty> _objectProperties =new ArrayList<>(); |
|
||||
|
|
||||
//移除没有填写表达式的对象属性
|
|
||||
if(objectProperties!=null && objectProperties.size()>0) { |
|
||||
for(ObjectProperty objectProperty : objectProperties) { |
|
||||
if(StringUtils.hasText(objectProperty.getExpression())){ |
|
||||
_objectProperties.add(objectProperty); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_objectProperties!=null && !_objectProperties.isEmpty()) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
if(StringUtils.hasText(objectCondition)){ |
|
||||
sb.append("if (").append(ExpressionReplacer.groovy(objectCondition, null)).append("){").append("\n"); |
|
||||
} |
|
||||
sb.append("\t\t\tif (").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(indicator.getCode())).append("==null) {").append("\n"); |
|
||||
sb.append("\t\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(indicator.getCode())).append(" =new ").append(indicator.getValueType()).append("_V").append(indicator.getValueTypeVersion()).append("();").append("\n"); |
|
||||
sb.append("\t\t\t}").append("\n"); |
|
||||
int size =_objectProperties.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
ObjectProperty objectProperty =_objectProperties.get(i); |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(indicator.getCode())).append(".").append(objectProperty.getCode()); |
|
||||
sb.append(" =").append(ExpressionReplacer.groovy(objectProperty.getExpression(), null)).append(";"); |
|
||||
sb.append("//").append(objectProperty.getName()).append("\n"); |
|
||||
} |
|
||||
if(StringUtils.hasText(objectCondition)){ |
|
||||
sb.append("\t\t}").append("\n"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s ObjectProperty groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter, ObjectPropertiesParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
String objectCondition =processor.getObjectCondition(); |
|
||||
List<ObjectProperty> objectProperties =parse(processor.getObjectProperties()); |
|
||||
List<ObjectProperty> _objectProperties =new ArrayList<>(); |
|
||||
|
|
||||
//移除没有填写表达式的对象属性
|
|
||||
if(objectProperties!=null && objectProperties.size()>0) { |
|
||||
for(ObjectProperty objectProperty : objectProperties) { |
|
||||
if(StringUtils.hasText(objectProperty.getExpression())){ |
|
||||
_objectProperties.add(objectProperty); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_objectProperties!=null && !_objectProperties.isEmpty()) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
if(StringUtils.hasText(objectCondition)){ |
|
||||
sb.append("if (").append(ExpressionReplacer.groovy(objectCondition, null)).append("){").append("\n"); |
|
||||
} |
|
||||
int size =_objectProperties.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
if(i!=0){ |
|
||||
sb.append("\t\t"); |
|
||||
} |
|
||||
ObjectProperty objectProperty =_objectProperties.get(i); |
|
||||
sb.append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(".").append(objectProperty.getCode()); |
|
||||
sb.append(" =").append(ExpressionReplacer.groovy(objectProperty.getExpression(), null)).append(";"); |
|
||||
sb.append("//").append(objectProperty.getName()).append("\n"); |
|
||||
} |
|
||||
if(StringUtils.hasText(objectCondition)){ |
|
||||
sb.append("}").append("\n"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s ObjectProperty groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public ObjectProperty(){ |
|
||||
} |
|
||||
public ObjectProperty(String code, String name){ |
|
||||
this.code =code; |
|
||||
this.name =name; |
|
||||
} |
|
||||
|
|
||||
public String getCode() { |
|
||||
return code; |
|
||||
} |
|
||||
|
|
||||
public void setCode(String code) { |
|
||||
this.code = code; |
|
||||
} |
|
||||
|
|
||||
public String getName() { |
|
||||
return name; |
|
||||
} |
|
||||
|
|
||||
public void setName(String name) { |
|
||||
this.name = name; |
|
||||
} |
|
||||
|
|
||||
public String getExpression() { |
|
||||
return expression; |
|
||||
} |
|
||||
|
|
||||
public void setExpression(String expression) { |
|
||||
this.expression = expression; |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,54 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class Option { |
|
||||
private String uuid; |
|
||||
private String value; |
|
||||
private String title; |
|
||||
private String description; |
|
||||
private String score; |
|
||||
|
|
||||
public static List<Option> parse(String json) throws Exception{ |
|
||||
if(json!=null && !"".equals(json.trim())) { |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Option>>(){}); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public String getUuid() { |
|
||||
return uuid; |
|
||||
} |
|
||||
public void setUuid(String uuid) { |
|
||||
this.uuid = uuid; |
|
||||
} |
|
||||
public String getValue() { |
|
||||
return value; |
|
||||
} |
|
||||
public void setValue(String value) { |
|
||||
this.value = value; |
|
||||
} |
|
||||
public String getTitle() { |
|
||||
return title; |
|
||||
} |
|
||||
public void setTitle(String title) { |
|
||||
this.title = title; |
|
||||
} |
|
||||
public String getDescription() { |
|
||||
return description; |
|
||||
} |
|
||||
public void setDescription(String description) { |
|
||||
this.description = description; |
|
||||
} |
|
||||
public String getScore() { |
|
||||
return score; |
|
||||
} |
|
||||
public void setScore(String score) { |
|
||||
this.score = score; |
|
||||
} |
|
||||
} |
|
@ -1,145 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.RuleParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class Rule { |
|
||||
private String category; |
|
||||
private Integer level; |
|
||||
private String code; |
|
||||
private String name; |
|
||||
private String condition; |
|
||||
private String value; |
|
||||
private String message; |
|
||||
|
|
||||
public static List<Rule> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<Rule>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,RuleParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<Rule> rules =parse(processor.getRule()); |
|
||||
List<Rule> _rules =new ArrayList<Rule>(); |
|
||||
|
|
||||
//移除没有填写条件的条件范围
|
|
||||
if(rules!=null && rules.size()>0) { |
|
||||
for(Rule rule : rules) { |
|
||||
if(rule.getCondition()!=null && !"".equals(rule.getCondition().trim())) { |
|
||||
_rules.add(rule); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_rules!=null && _rules.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
int size =_rules.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
Rule rule =_rules.get(i); |
|
||||
sb.append("\t\tif("); |
|
||||
sb.append(ExpressionReplacer.groovy(rule.condition, null)); |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(".addRule("); |
|
||||
sb.append("true,"); |
|
||||
sb.append(rule.level).append(","); |
|
||||
sb.append("'''" + rule.code + "''',"); |
|
||||
sb.append("'''" + rule.name + "''',"); |
|
||||
if(rule.category!=null && !"".equals(rule.category)) { |
|
||||
sb.append("'''" + rule.category + "''',"); |
|
||||
}else { |
|
||||
sb.append("null,"); |
|
||||
} |
|
||||
sb.append(ExpressionReplacer.groovy(rule.value, "java.lang.String")).append(","); |
|
||||
sb.append(ExpressionReplacer.groovy(rule.message, "java.lang.String")); |
|
||||
sb.append(")").append(";\n"); |
|
||||
sb.append("\t\t}else{").append("\n"); |
|
||||
sb.append("\t\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(".addRule("); |
|
||||
sb.append("false,"); |
|
||||
sb.append(rule.level).append(","); |
|
||||
sb.append("'''" + rule.code + "''',"); |
|
||||
sb.append("'''" + rule.name + "''',"); |
|
||||
if(rule.category!=null && !"".equals(rule.category)) { |
|
||||
sb.append("'''" + rule.category + "''',"); |
|
||||
}else { |
|
||||
sb.append("null,"); |
|
||||
} |
|
||||
sb.append(ExpressionReplacer.groovy(rule.value, "java.lang.String")).append(","); |
|
||||
sb.append(ExpressionReplacer.groovy(rule.message, "java.lang.String")); |
|
||||
sb.append(")").append(";\n"); |
|
||||
sb.append("\t\t}\n"); |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Rule groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public Integer getLevel() { |
|
||||
return level; |
|
||||
} |
|
||||
|
|
||||
public void setLevel(Integer level) { |
|
||||
this.level = level; |
|
||||
} |
|
||||
|
|
||||
public String getCode() { |
|
||||
return code; |
|
||||
} |
|
||||
|
|
||||
public void setCode(String code) { |
|
||||
this.code = code; |
|
||||
} |
|
||||
|
|
||||
public String getName() { |
|
||||
return name; |
|
||||
} |
|
||||
|
|
||||
public void setName(String name) { |
|
||||
this.name = name; |
|
||||
} |
|
||||
|
|
||||
public String getCategory() { |
|
||||
return category; |
|
||||
} |
|
||||
|
|
||||
public void setCategory(String category) { |
|
||||
this.category = category; |
|
||||
} |
|
||||
|
|
||||
public String getCondition() { |
|
||||
return condition; |
|
||||
} |
|
||||
|
|
||||
public void setCondition(String condition) { |
|
||||
this.condition = condition; |
|
||||
} |
|
||||
|
|
||||
public String getValue() { |
|
||||
return value; |
|
||||
} |
|
||||
|
|
||||
public void setValue(String value) { |
|
||||
this.value = value; |
|
||||
} |
|
||||
|
|
||||
public String getMessage() { |
|
||||
return message; |
|
||||
} |
|
||||
|
|
||||
public void setMessage(String message) { |
|
||||
this.message = message; |
|
||||
} |
|
||||
} |
|
@ -1,117 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.SingleRuleParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class SingleRule { |
|
||||
private String category; |
|
||||
private Integer level; |
|
||||
private String condition; |
|
||||
private String value; |
|
||||
private String message; |
|
||||
|
|
||||
public static List<SingleRule> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<SingleRule>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,SingleRuleParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
List<SingleRule> rules =parse(processor.getSingleRule()); |
|
||||
List<SingleRule> _rules =new ArrayList<SingleRule>(); |
|
||||
|
|
||||
//移除没有填写条件的条件范围
|
|
||||
if(rules!=null && rules.size()>0) { |
|
||||
for(SingleRule rule : rules) { |
|
||||
if(rule.getCondition()!=null && !"".equals(rule.getCondition().trim())) { |
|
||||
_rules.add(rule); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if(_rules!=null && _rules.size()>0) { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
sb.append("\t\tSingleRuleResult result =new SingleRuleResult();").append("\n"); |
|
||||
int size =_rules.size(); |
|
||||
for(int i=0;i<size;i++) { |
|
||||
SingleRule rule =_rules.get(i); |
|
||||
sb.append("\t\tif("); |
|
||||
sb.append(ExpressionReplacer.groovy(rule.condition, null)); |
|
||||
sb.append(")").append("{").append("\n"); |
|
||||
sb.append("\t\t\t").append("SingleRuleResult temp =new SingleRuleResult("); |
|
||||
sb.append("true,"); |
|
||||
sb.append(rule.level).append(","); |
|
||||
if(rule.category!=null && !"".equals(rule.category)) { |
|
||||
sb.append("'''" + rule.category + "''',"); |
|
||||
}else { |
|
||||
sb.append("null,"); |
|
||||
} |
|
||||
sb.append(ExpressionReplacer.groovy(rule.value, "java.lang.String")).append(","); |
|
||||
sb.append(ExpressionReplacer.groovy(rule.message, "java.lang.String")); |
|
||||
sb.append(")").append(";\n"); |
|
||||
sb.append("\t\t\tif(!result.isTriggered() || result.getLevel()>temp.getLevel()){").append("\n"); |
|
||||
sb.append("\t\t\t\t").append("result =temp;").append("\n"); |
|
||||
sb.append("\t\t\t}").append("\n"); |
|
||||
sb.append("\t\t}\n"); |
|
||||
} |
|
||||
sb.append("\n"); |
|
||||
sb.append("\t\t").append(ExpressionReplacer.ARGUMENT_NAME).append(".").append(CodeReplacer.fieldName(parameter.getCode())).append(" =result;").append("\n"); |
|
||||
return sb.toString(); |
|
||||
} |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Single Rule groovy source code.", e); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public Integer getLevel() { |
|
||||
return level; |
|
||||
} |
|
||||
|
|
||||
public void setLevel(Integer level) { |
|
||||
this.level = level; |
|
||||
} |
|
||||
|
|
||||
public String getCategory() { |
|
||||
return category; |
|
||||
} |
|
||||
|
|
||||
public void setCategory(String category) { |
|
||||
this.category = category; |
|
||||
} |
|
||||
|
|
||||
public String getCondition() { |
|
||||
return condition; |
|
||||
} |
|
||||
|
|
||||
public void setCondition(String condition) { |
|
||||
this.condition = condition; |
|
||||
} |
|
||||
|
|
||||
public String getValue() { |
|
||||
return value; |
|
||||
} |
|
||||
|
|
||||
public void setValue(String value) { |
|
||||
this.value = value; |
|
||||
} |
|
||||
|
|
||||
public String getMessage() { |
|
||||
return message; |
|
||||
} |
|
||||
|
|
||||
public void setMessage(String message) { |
|
||||
this.message = message; |
|
||||
} |
|
||||
} |
|
@ -1,81 +0,0 @@ |
|||||
package io.sc.engine.rule.core.code.impl.support.processor; |
|
||||
|
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|
||||
import com.fasterxml.jackson.core.type.TypeReference; |
|
||||
import io.sc.engine.rule.core.po.lib.Indicator; |
|
||||
import io.sc.engine.rule.core.po.lib.processor.SqlIndicatorProcessor; |
|
||||
import io.sc.engine.rule.core.po.model.Parameter; |
|
||||
import io.sc.engine.rule.core.po.model.processor.SqlParameterProcessor; |
|
||||
import io.sc.engine.rule.core.util.CodeReplacer; |
|
||||
import io.sc.engine.rule.core.util.ExpressionReplacer; |
|
||||
import io.sc.platform.util.ObjectMapperUtil; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
@JsonIgnoreProperties(ignoreUnknown=true) |
|
||||
public class SqlFieldMapping { |
|
||||
private String parameter; |
|
||||
private String field; |
|
||||
|
|
||||
public static List<SqlFieldMapping> parse(String json) throws Exception{ |
|
||||
return ObjectMapperUtil.json().readValue(json, new TypeReference<List<SqlFieldMapping>>(){}); |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Indicator indicator,SqlIndicatorProcessor processor) throws Exception{ |
|
||||
if(indicator==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
List<SqlFieldMapping> mappings =parse(processor.getSqlFieldMapping()); |
|
||||
if(mappings!=null && mappings.size()>0) { |
|
||||
for(SqlFieldMapping mapping : mappings) { |
|
||||
sb.append("\t\t\t\t\t") |
|
||||
.append(ExpressionReplacer.groovy(CodeReplacer.fieldName(mapping.parameter), null)) |
|
||||
.append(" =") |
|
||||
.append("rs.getObject(\"").append(mapping.getField()).append("\");"); |
|
||||
} |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + indicator.getName()+ "'s Rule groovy source code.", e); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public static String generateGroovyCode(Parameter parameter,SqlParameterProcessor processor) throws Exception{ |
|
||||
if(parameter==null || processor==null){ |
|
||||
return null; |
|
||||
} |
|
||||
try { |
|
||||
StringBuilder sb =new StringBuilder(); |
|
||||
List<SqlFieldMapping> mappings =parse(processor.getSqlFieldMapping()); |
|
||||
if(mappings!=null && mappings.size()>0) { |
|
||||
for(SqlFieldMapping mapping : mappings) { |
|
||||
sb.append("\t\t\t\t\t") |
|
||||
.append(ExpressionReplacer.groovy(CodeReplacer.fieldName(mapping.parameter), null)) |
|
||||
.append(" =") |
|
||||
.append("rs.getObject(\"").append(mapping.getField()).append("\");"); |
|
||||
} |
|
||||
} |
|
||||
return sb.toString(); |
|
||||
}catch(Exception e) { |
|
||||
throw new RuntimeException("There was a Error when generate " + parameter.getName()+ "'s Rule groovy source code.", e); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public String getParameter() { |
|
||||
return parameter; |
|
||||
} |
|
||||
|
|
||||
public void setParameter(String parameter) { |
|
||||
this.parameter = parameter; |
|
||||
} |
|
||||
|
|
||||
public String getField() { |
|
||||
return field; |
|
||||
} |
|
||||
|
|
||||
public void setField(String field) { |
|
||||
this.field = field; |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,49 @@ |
|||||
|
#define renderArgument(model) |
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
class Argument { |
||||
|
#(tabs(ParameterGenerator.generateFields(model.getAllParameters()),1)) |
||||
|
|
||||
|
public static Argument convertArgument(Map<String,Object> map){ |
||||
|
if(map!=null){ |
||||
|
Argument arg =new Argument(); |
||||
|
#(tabs(ParameterGenerator.generateConvertorFromMap(model.getAllParameters()),3)) |
||||
|
return arg; |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
public static Argument convertArgument(String json){ |
||||
|
if(json!=null && !"".equals(json.trim())){ |
||||
|
Argument arg =ObjectMapperUtil.json().readValue(json, Argument.class); |
||||
|
#(tabs(ParameterGenerator.generateConvertorFromJson(model.getAllParameters()),3)) |
||||
|
return arg; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void validate(ValidateResult result) { |
||||
|
#(tabs(ParameterGenerator.generateValidator(model.getAllParameters()),2)) |
||||
|
} |
||||
|
|
||||
|
public void mergeParameterValueFromIndicatorLib(Map<String,Object> libs){ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public ResourceResult toResult(){ |
||||
|
ResourceResult result =new ResourceResult(); |
||||
|
#(tabs(ParameterGenerator.generateToResult(model.getAllParameters()),2)) |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public Map<String,Object> toMap(){ |
||||
|
Map<String,Object> map =new HashMap<String,Object>(); |
||||
|
#(tabs(ParameterGenerator.generateToMap(model.getAllParameters()),2)) |
||||
|
return map; |
||||
|
} |
||||
|
|
||||
|
public void mergeResult(ResourceResult result){ |
||||
|
if(result!=null){ |
||||
|
#(tabs(ParameterGenerator.generateMergeResult(model.getAllParameters()),3)) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
#end |
@ -0,0 +1,13 @@ |
|||||
|
#define renderDictionary(dictionary) |
||||
|
/** |
||||
|
* #(dictionary.name) |
||||
|
*/ |
||||
|
@JsonIgnoreProperties(ignoreUnknown=true) |
||||
|
class #(className(dictionary.code,dictionary.version)) { |
||||
|
#(tabs(DictionaryGenerator.generateFields(dictionary.getFields()),1)) |
||||
|
|
||||
|
public void init(){ |
||||
|
#(tabs(DictionaryGenerator.generateInits(dictionary.getFields()),2)) |
||||
|
} |
||||
|
} |
||||
|
#end |
@ -0,0 +1,6 @@ |
|||||
|
#define renderFunction(function) |
||||
|
/** |
||||
|
* #(function.description) |
||||
|
*/ |
||||
|
#(function.body) |
||||
|
#end |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue