/** * 构建者模式 * * @return */ public PolicyContext builderPolicy(EventIf eventif, PolicyObject policyObject) { /** * 在炉子里锻造PolicyContext,基本上从eventIf可以推断出必要的PolicyCondition和PolicyAction * * <p>需要根据taskid去查找对应的关联关系表 */ if (eventif != null) { List<PolicyCondition> policyConditions = policyObject.getPolicyConditionList(); List<PolicyAction> policyActions = policyObject.getPolicyActions(); if (eventif != null && policyConditions != null && policyActions != null) { PolicyContext policyContext = new PolicyContext(); policyContext.setPolicyConditions(policyConditions); policyContext.setPolicyActions(policyActions); policyContext.setEvent(eventif); for (PolicyCondition policyCondition : policyConditions) { policyCondition.setParameters(eventif.getParameters()); } for (PolicyAction policyAction : policyActions) { policyAction.setParameters(eventif.getParameters()); } return policyContext; } } return null; }
public void simplePolicyTest() { EventIf eventIf = new TaskInstance(); eventIf.setEmployeeId("1001"); List<PolicyObject> policyObjects = new ArrayList<>(1); eventIf.setPolicyObjects(policyObjects); PolicyBean policyBean = new PolicyBean(); policyObjects.add(policyBean); // 设定condition { List<String> policyConditions = new ArrayList<>(1); policyConditions.add("condition_1"); policyBean.setConditions(policyConditions); } // 设定action { List<String> policyActions = new ArrayList<>(1); policyActions.add("action_score"); policyBean.setActions(policyActions); } // 设定Params { Map<String, Object> parameters = new HashMap<>(); eventIf.setParameters(parameters); parameters.put("day", 7); } List<PolicyResult> policyResults = processPolicies(eventIf); System.out.println(policyResults.get(0).getMessage()); }
/** * 给外部调用此消息接口,依赖于正在开发的领域对象关系 Task关联多个Policies , 一个Policy映射多个Condition和1个Action * * @param eventif * @return */ public List<PolicyResult> processPolicies(EventIf eventif) { if (eventif == null || CollectionUtils.isEmpty(eventif.getPolicyObjects())) { return null; } List<PolicyResult> policyResults = new ArrayList<>(eventif.getPolicyObjects().size()); if (eventif != null) { List<PolicyObject> policyObjectList = eventif.getPolicyObjects(); if (policyObjectList != null) { for (PolicyObject policyObject : policyObjectList) { // 调用builder模式构建上下文 PolicyContext policyContext = builderPolicy(eventif, policyObject); List<PolicyResult> policyResultAll = processPolicy(policyContext); // 结果放入结果集 if (CollectionUtils.isNotEmpty(policyResultAll)) { policyResults.addAll(policyResultAll); } } } } return policyResults; }