/** {@inheritDoc} */ @Override public void handleMessage(Exchange exchange) throws HandlerException { if (!ExchangePhase.IN.equals(exchange.getPhase())) { return; } Context context = exchange.getContext(); ServiceOperation serviceOperation = exchange.getContract().getServiceOperation(); ProcessActionModel processActionModel = _actionModels.get(serviceOperation.getName()); ProcessActionType processActionType = getProcessActionType(context, processActionModel); Message messageIn = exchange.getMessage(); Long processInstanceId = null; ProcessInstance processInstance = null; switch (processActionType) { case START_PROCESS: if (_processId != null) { Object messageContentIn = messageIn.getContent(); if (messageContentIn != null) { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(_messageContentInName, messageContentIn); processInstance = _ksession.startProcess(_processId, parameters); } else { processInstance = _ksession.startProcess(_processId); } processInstanceId = Long.valueOf(processInstance.getId()); } else { throwNullParameterException(processActionType, PROCESS_ID_VAR); } break; case SIGNAL_EVENT: String processEventType = getProcessEventType(context, processActionModel); Object processEvent = getProcessEvent(context, messageIn); processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.signalEvent(processEventType, processEvent, processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; case ABORT_PROCESS_INSTANCE: processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.abortProcessInstance(processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; } if (processInstanceId != null) { context.setProperty(PROCESS_INSTANCE_ID_VAR, processInstanceId, Scope.EXCHANGE); ExchangePattern exchangePattern = serviceOperation.getExchangePattern(); if (ExchangePattern.IN_OUT.equals(exchangePattern)) { if (processInstance == null) { processInstance = _ksession.getProcessInstance(processInstanceId.longValue()); } Message messageOut = exchange.createMessage(); Object messageContentOut = null; if (processInstance != null) { messageContentOut = ((WorkflowProcessInstance) processInstance).getVariable(_messageContentOutName); } if (messageContentOut != null) { messageOut.setContent(messageContentOut); } exchange.send(messageOut); } } }
/** {@inheritDoc} */ @Override public void handleAction(Exchange exchange, KnowledgeAction action) throws HandlerException { RulesActionType actionType = (RulesActionType) action.getType(); switch (actionType) { case EXECUTE: { KnowledgeSession session = newStatelessSession(); setGlobals(exchange, action, session, true); List<Object> input = getInputList(exchange, action); session.getStateless().execute(input); break; } case INSERT: case FIRE_ALL_RULES: { /* if (!isContinue(exchange)) { disposeStatefulSession(); } */ KnowledgeSession session = getStatefulSession(); setGlobals(exchange, action, session, true); List<Object> input = getInputList(exchange, action); for (Object fact : input) { session.getStateful().insert(fact); } if (RulesActionType.FIRE_ALL_RULES.equals(actionType)) { session.getStateful().fireAllRules(); } if (isDispose(exchange)) { disposeStatefulSession(); } break; } case FIRE_UNTIL_HALT: { /* if (!isContinue(exchange)) { disposeStatefulSession(); } */ KnowledgeSession session = getStatefulSession(); setGlobals(exchange, action, session, false); if (_fireUntilHaltThread == null) { FireUntilHalt fireUntilHalt = new FireUntilHalt(this, session, getLoader()); session.addDisposals(fireUntilHalt); _fireUntilHaltThread = fireUntilHalt.startThread(); } final String undefinedVariable = toVariable(exchange); Map<String, List<Object>> listMap = getListMap(exchange, action.getInputExpressionMappings(), true, undefinedVariable); if (listMap.size() > 0) { for (Entry<String, List<Object>> entry : listMap.entrySet()) { String key = entry.getKey(); if (undefinedVariable.equals(key)) { String id = Strings.trimToNull(action.getId()); if (id != null) { key = id; } } List<Object> input = entry.getValue(); if (undefinedVariable.equals(key)) { for (Object fact : input) { session.getStateful().insert(fact); } } else { SessionEntryPoint sep = session.getStateful().getEntryPoint(key); if (sep != null) { for (Object fact : input) { sep.insert(fact); } } else { throw new HandlerException( "Unknown entry point: " + sep + "; please check your rules source."); } } } } else { Object content = exchange.getMessage().getContent(); if (content != null) { session.getStateful().insert(content); } } if (isDispose(exchange)) { disposeStatefulSession(); } break; } default: { throw new HandlerException("Unsupported action type: " + actionType); } } Object output = getOutput(exchange, action); if (ExchangePattern.IN_OUT.equals( exchange.getContract().getProviderOperation().getExchangePattern())) { Message message = exchange.createMessage().setContent(output); exchange.send(message); } }