All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snaker.engine.model.NodeModel Maven / Gradle / Ivy

There is a newer version: 2.5.1
Show newest version
/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.model;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snaker.engine.Action;
import org.snaker.engine.SnakerException;
import org.snaker.engine.SnakerInterceptor;
import org.snaker.engine.core.Execution;
import org.snaker.engine.helper.ClassHelper;
import org.snaker.engine.helper.StringHelper;

/**
 * 节点元素(存在输入输出的变迁)
 * @author yuqs
 * @since 1.0
 */
public abstract class NodeModel extends BaseModel implements Action {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2377317472320109317L;
	private static final Logger log = LoggerFactory.getLogger(NodeModel.class);
	/**
	 * 输入变迁集合
	 */
	private List inputs = new ArrayList();
	/**
	 * 输出变迁集合
	 */
	private List outputs = new ArrayList();
	/**
	 * layout
	 */
	private String layout;
	/**
	 * 局部前置拦截器
	 */
	private String preInterceptors;
	/**
	 * 局部后置拦截器
	 */
	private String postInterceptors;
	/**
	 * 前置局部拦截器实例集合
	 */
	private List preInterceptorList = new ArrayList();
	/**
	 * 后置局部拦截器实例集合
	 */
	private List postInterceptorList = new ArrayList();
	
	/**
	 * 具体节点模型需要完成的执行逻辑
	 * @param execution
	 */
	protected abstract void exec(Execution execution);
	
	/**
	 * 对执行逻辑增加前置、后置拦截处理
	 * @param execution
	 * @return
	 */
	public void execute(Execution execution) {
		intercept(preInterceptorList, execution);
		exec(execution);
		intercept(postInterceptorList, execution);
	}
	
	/**
	 * 运行变迁继续执行
	 * @param execution
	 */
	protected void runOutTransition(Execution execution) {
		for (TransitionModel tm : getOutputs()) {
			tm.setEnabled(true);
			tm.execute(execution);
		}
	}
	
	/**
	 * 拦截方法
	 * @param interceptorList
	 * @param execution
	 */
	private void intercept(List interceptorList, Execution execution) {
		try {
			for(SnakerInterceptor interceptor : interceptorList) {
				interceptor.intercept(execution);
			}
		} catch(Exception e) {
			//拦截器执行过程中出现的异常不影响流程执行逻辑
			log.error("拦截器执行失败=" + e.getMessage());
            throw new SnakerException(e);
		}
	}
	
	/**
	 * 根据父节点模型、当前节点模型判断是否可退回。可退回条件:
	 * 1、满足中间无fork、join、subprocess模型
	 * 2、满足父节点模型如果为任务模型时,参与类型为any
	 * @param parent
	 * @return
	 */
	public boolean canRejected(NodeModel parent) {
		if(parent instanceof TaskModel && !((TaskModel)parent).isPerformAny()) {
			return false;
		}
        boolean result = false;
		for(TransitionModel tm : parent.getOutputs()) {
			NodeModel target = tm.getTarget();
			if(target.getName().equals(this.getName())) {
				return true;
			}
			if(target instanceof ForkModel 
					|| target instanceof JoinModel 
					|| target instanceof SubProcessModel
                    || target instanceof EndModel) {
				continue;
			}
            result = result || canRejected(target);
		}
		return result;
	}

    public  List getNextModels(Class clazz) {
        List models = new ArrayList();
        for(TransitionModel tm : this.getOutputs()) {
            addNextModels(models, tm, clazz);
        }
        return models;
    }

    protected  void addNextModels(List models, TransitionModel tm, Class clazz) {
        if(clazz.isInstance(tm.getTarget())) {
            models.add((T)tm.getTarget());
        } else {
            for(TransitionModel tm2 : tm.getTarget().getOutputs()) {
                addNextModels(models, tm2, clazz);
            }
        }
    }
	
	public List getInputs() {
		return inputs;
	}
	public void setInputs(List inputs) {
		this.inputs = inputs;
	}
	public List getOutputs() {
		return outputs;
	}
	public void setOutputs(List outputs) {
		this.outputs = outputs;
	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public String getPreInterceptors() {
		return preInterceptors;
	}

	public void setPreInterceptors(String preInterceptors) {
		this.preInterceptors = preInterceptors;
		if(StringHelper.isNotEmpty(preInterceptors)) {
			for(String interceptor : preInterceptors.split(",")) {
				SnakerInterceptor instance = (SnakerInterceptor)ClassHelper.newInstance(interceptor);
				if(instance != null) this.preInterceptorList.add(instance);
			}
		}
	}

	public String getPostInterceptors() {
		return postInterceptors;
	}

	public void setPostInterceptors(String postInterceptors) {
		this.postInterceptors = postInterceptors;
		if(StringHelper.isNotEmpty(postInterceptors)) {
			for(String interceptor : postInterceptors.split(",")) {
				SnakerInterceptor instance = (SnakerInterceptor)ClassHelper.newInstance(interceptor);
				if(instance != null) this.postInterceptorList.add(instance);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy