package org.arefgard.icerya.flow.node;

import java.util.Map;

import org.arefgard.icerya.flow.core.UsecaseExpression;

/**
 * <while>ノード。
 * 
 * @author Takashi Yamashina
 * @since 1.0.0
 *
 */
public class WhileNode extends Node {
	private String flow;
	private String nextTo;
	private String expr;
	
	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

	public String getNextTo() {
		return nextTo;
	}

	public void setNextTo(String nextTo) {
		this.nextTo = nextTo;
	}

	public String getExpr() {
		return expr;
	}

	public void setExpr(String expr) {
		this.expr = expr;
	}
	
	/**
	 * whileを実行する。
	 * 
	 * @param params 入力パラメータ
	 * @return 遷移先
	 * @throws Exception while実行中に例外が発生した場合
	 */
	public String doWhile(Map<String, Object> params) throws Exception {
		Flow flow = this.getParent().getParent().getFlow(this.flow);
		UsecaseExpression expression = new UsecaseExpression(expr);
		expression.setArgs(params);
		while(expression.evaluateBoolean()) {
			flow.execute(params);
		}
		return this.nextTo;
	}
	
}
