You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					108 lines
				
				4.3 KiB
			
		
		
			
		
	
	
					108 lines
				
				4.3 KiB
			| 
											1 year ago
										 | = 客户端集成示例 | ||
|  | == 需要的客户端 jar 包 | ||
|  | 
 | ||
|  | == 示例代码 | ||
|  | [source%nowrap,java] | ||
|  | ---- | ||
|  | package org.wsp.engine.rule.client.test; | ||
|  | 
 | ||
|  | import java.util.HashMap; | ||
|  | import java.util.List; | ||
|  | import java.util.Map; | ||
|  | 
 | ||
|  | import org.wsp.engine.rule.client.Executor; | ||
|  | import org.wsp.engine.rule.client.ExecutorBuilder; | ||
|  | import org.wsp.engine.rule.client.ExecutorFactory; | ||
|  | import org.wsp.engine.rule.client.enums.ExecutorMode; | ||
|  | import org.wsp.engine.rule.client.enums.LoaderMode; | ||
|  | import org.wsp.engine.rule.core.classes.Rule; | ||
|  | import org.wsp.engine.rule.core.classes.RuleResult; | ||
|  | import org.wsp.engine.rule.core.code.impl.support.ParameterResult; | ||
|  | import org.wsp.engine.rule.core.code.impl.support.ResourceResult; | ||
|  | import org.wsp.engine.rule.core.enums.ParameterType; | ||
|  | import org.wsp.engine.rule.core.util.TimeTracer; | ||
|  | 
 | ||
|  | public class ResourceCallTest { | ||
|  | 	private static final String MODEL_API_URL ="http://localhost:8080/re/resource";	//远程模型引擎 API URL 地址 | ||
|  | 	private static final String EXECUTOR_NAME ="executor";							//执行器名称 | ||
|  | 	private static final String MODEL_CODE ="M116762018316101";						//需要执行的模型代码 | ||
|  | 	private static final Integer MODEL_VERSION =null;								//需要执行的模型版本 | ||
|  | 	private static final int COUNT =1;												//需要执行的次数 | ||
|  | 	 | ||
|  | 	private static TimeTracer tracer =TimeTracer.getInstance("tracer");				//创建一个时间跟踪器,用于跟踪执行时间,在生产环境中不要这样做 | ||
|  | 	 | ||
|  | 	public static void main(String[] args) throws Exception{ | ||
|  | 		init(); | ||
|  | 		testLocalExecutorRemoteLoader_map(); | ||
|  | 	} | ||
|  | 	 | ||
|  | 	public static void init() throws Exception{ | ||
|  | 		//创建执行器(本地执行,远程获取模型定义) | ||
|  | 		Executor executor =new ExecutorBuilder().build(ExecutorMode.LOCAL, LoaderMode.REMOTE, MODEL_API_URL); | ||
|  | 		 | ||
|  | 		//注册执行器,便于之后获取,如果每次都构建一个新的执行器,将显著影响性能 | ||
|  | 		ExecutorFactory.register(EXECUTOR_NAME, executor); | ||
|  | 		 | ||
|  | 		//第一次执行,消除缓存对性能对比测试的影响 | ||
|  | 		//executor.executeByCode(MODEL_CODE, MODEL_VERSION,JacksonObjectMapper.getDefaultObjectMapper().writeValueAsString(prepareData())); | ||
|  | 		ResourceResult result =executor.executeByCode(MODEL_CODE, MODEL_VERSION,prepareData()); | ||
|  | 		List<ParameterResult> parameters =result.getData(); | ||
|  | 		for(ParameterResult parameter : parameters) { | ||
|  | 			if(ParameterType.RULE_RESULT.equals(parameter.getType())) { | ||
|  | 				RuleResult ruleResult =parameter.getRuleResult(); | ||
|  | 				ruleResult.isTriggered();//规则是否触发 | ||
|  | 				ruleResult.getLevel();//规则触发的子规则最大等级 | ||
|  | 				List<Rule> rules =ruleResult.getRules(); | ||
|  | 				for(Rule rule : rules) { | ||
|  | 					rule.getCode(); | ||
|  | 					rule.getName(); | ||
|  | 					rule.getValue(); | ||
|  | 					rule.getLevel(); | ||
|  | 					rule.getMessage(); | ||
|  | 				} | ||
|  | 			} | ||
|  | 		} | ||
|  | 		//tracer.log("初始化完毕===================================="); | ||
|  | 	 | ||
|  | 	} | ||
|  | 
 | ||
|  | 	/** | ||
|  | 	 * 测试本地执行器,远程模型定义加载器,输入参数为 map 对象 | ||
|  | 	 * @throws Exception 违例 | ||
|  | 	 */ | ||
|  | 	public static void testLocalExecutorRemoteLoader_map() throws Exception{ | ||
|  | 		//从注册器中获取执行器 | ||
|  | 		Executor executor =ExecutorFactory.getExecutor(EXECUTOR_NAME); | ||
|  | 		tracer.log("开始执行(采用 map 对象作为参数)===================================="); | ||
|  | 		for(int i=0;i<COUNT;i++) { | ||
|  | 			//准备执行的数据(输入参数) | ||
|  | 			Map<String,Object> data =prepareData(); | ||
|  | 			//executor.getLoader().getResourceByCode(MODEL_CODE, MODEL_VERSION); | ||
|  | 			executor.executeByCode(MODEL_CODE, MODEL_VERSION,data); | ||
|  | 		} | ||
|  | 		tracer.log("执行完毕(采用 map 对象作为参数)"); | ||
|  | 		tracer.log("采用 map 对象作为输入参数将执行效率提高 20+ 倍!!!"); | ||
|  | 	} | ||
|  | 	 | ||
|  | 	 | ||
|  | 	private static Map<String,Object> prepareData() throws Exception{ | ||
|  | 		//上期 | ||
|  | 		Map<String,Object> preReport =new HashMap<String,Object>(); | ||
|  | 		preReport.put("F1573645395675", 1.0);//收入 | ||
|  | 		preReport.put("F1573645418335", 1.0);//毛利 | ||
|  | 		 | ||
|  | 		//本期 | ||
|  | 		Map<String,Object> report =new HashMap<String,Object>(); | ||
|  | 		report.put("F1573645395675", 2.0);//收入 | ||
|  | 		report.put("F1573645418335", 1.0);//毛利 | ||
|  | 		 | ||
|  | 		Map<String,Object> map =new HashMap<String,Object>(); | ||
|  | 		//map.put("current_1_issue_report", JacksonObjectMapper.getDefaultObjectMapper().writeValueAsString(preReport)); | ||
|  | 		//map.put("current_issue_report", JacksonObjectMapper.getDefaultObjectMapper().writeValueAsString(report)); | ||
|  | 		map.put("current_1_issue_report", preReport); | ||
|  | 		map.put("current_issue_report", report); | ||
|  | 		return map; | ||
|  | 	} | ||
|  | } | ||
|  | 
 | ||
|  | ---- |