View Javadoc

1   package org.state4j.sm;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.springframework.beans.factory.InitializingBean;
6   
7   public class SmTransitionImpl implements SmTransition, InitializingBean {
8   	Log logger = LogFactory.getLog(SmTransitionImpl.class.getName());
9   
10  	private SmGuardCondition guardCondition;
11  
12  	private SmAction action;
13  
14  	private SmState stateIn;
15  
16  	private SmState stateOut;
17  
18  	private String name;
19  
20  	private String subtype;
21  
22  	private SmCompositeState lca;
23  
24  	public boolean hasGuardCondition() {
25  		return this.guardCondition != null;
26  	}
27  
28  	public boolean hasAction() {
29  		return this.action != null;
30  	}
31  
32  	public SmGuardCondition getGuardCondition() {
33  		return guardCondition;
34  	}
35  
36  	public void setGuardCondition(SmGuardCondition guardCondition) {
37  		this.guardCondition = guardCondition;
38  	}
39  
40  	public SmAction getAction() {
41  		return action;
42  	}
43  
44  	public void setAction(SmAction action) {
45  		this.action = action;
46  	}
47  
48  	public SmState getStateIn() {
49  		return stateIn;
50  	}
51  
52  	public void setStateIn(SmState stateIn) {
53  		this.stateIn = stateIn;
54  	}
55  
56  	public SmState getStateOut() {
57  		return stateOut;
58  	}
59  
60  	public void setStateOut(SmState stateOut) {
61  		this.stateOut = stateOut;
62  	}
63  
64  	public String getName() {
65  		return name;
66  	}
67  
68  	public void setName(String name) {
69  		this.name = name;
70  	}
71  
72  	public String getSubtype() {
73  		return subtype;
74  	}
75  
76  	public void setSubtype(String subtype) {
77  		this.subtype = subtype;
78  	}
79  
80  	public boolean hasSubtype() {
81  		return this.subtype != null;
82  	}
83  
84  	public void fire(SmContext cntx) {
85  		logger.debug("fire transition: " + this.getName());
86  		if (isInternal()) {
87  			if (this.hasAction()) {
88  				this.getAction().execute(cntx);
89  			}
90  			return;
91  		}
92  		cntx.getCurrentState().exitUp(cntx, this.getLCA());
93  		if (this.hasAction()) {
94  			this.getAction().execute(cntx);
95  		}
96  		this.getStateIn().closeDownState().entryUp(cntx, this.getLCA());
97  	}
98  
99  	public boolean fireable(SmContext cntx, boolean withTrigger) {
100 		boolean result = false;
101 		if (!cntx.hasCurrentState() || !this.getStateOut().isActive(cntx)) {
102 			result = false; 
103 		} else
104 			if(withTrigger && this.hasGuardCondition() && this.getGuardCondition().hasTriggerClassName()) {
105 				result = this.getGuardCondition().evaluate(cntx);
106 			} else
107 			if(!withTrigger && 
108 					(!this.hasGuardCondition() || !this.getGuardCondition().hasTriggerClassName()) &&
109 					((this.getStateOut() instanceof SmCompositeState && cntx.getCurrentState().isFinal()) ||
110 							!(this.getStateOut() instanceof SmCompositeState))){
111 				
112 				result = this.hasGuardCondition() ? this.getGuardCondition().evaluate(cntx) : true;
113 			}
114 		return result;
115 	}
116 
117 	public boolean isInternal() {
118 		return this.hasSubtype()
119 				&& this.getSubtype().equals(SmNaming.ATT_INTERNAL_TRANSITION);
120 	}
121 
122 	public boolean hasStateIn() {
123 		return this.stateIn != null;
124 	}
125 
126 	public boolean hasStateOut() {
127 		return this.stateOut != null;
128 	}
129 
130 	public boolean hasName() {
131 		return this.name != null;
132 	}
133 
134 	/*
135 	 * (non-Javadoc)
136 	 * 
137 	 * @see com.tim.sm.interfaces.MyTransition#setLCA(com.tim.sm.interfaces.MyCompositeState)
138 	 */
139 	public void setLCA(SmCompositeState lca) {
140 		this.lca = lca;
141 	}
142 
143 	/*
144 	 * (non-Javadoc)
145 	 * 
146 	 * @see com.tim.sm.interfaces.MyTransition#getLCA()
147 	 */
148 	public SmCompositeState getLCA() {
149 		return this.lca;
150 	}
151 
152 	/*
153 	 * (non-Javadoc)
154 	 * 
155 	 * @see com.tim.sm.interfaces.MyTransition#hasLCA()
156 	 */
157 	public boolean hasLCA() {
158 		return this.lca != null;
159 	}
160 
161 	public void afterPropertiesSet() throws Exception {
162 		this.getStateOut().addOutTransition(this);
163 	}
164 }