View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.models.state.xml;
24  
25  import org.nuiton.eugene.models.state.StateModelComplexState;
26  import org.nuiton.eugene.models.state.StateModelSimpleState;
27  import org.nuiton.eugene.models.state.StateModelState;
28  import org.nuiton.eugene.models.state.StateModelTransition;
29  
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  /**
35   * StateModelComplexeStateImpl.java
36   *
37   * @author chatellier
38   */
39  public class StateModelComplexeStateImpl extends StateModelStateImpl implements
40          StateModelComplexState {
41  
42      /**
43       * States'set of this state
44       */
45      protected Map<String, StateModelState> mapState;
46  
47      /**
48       * Constructor
49       */
50      public StateModelComplexeStateImpl() {
51          mapState = new HashMap<>();
52      }
53  
54      /**
55       * Add a state
56       *
57       * @param state the state
58       */
59      public void addState(StateModelState state) {
60          mapState.put(state.getName(), state);
61      }
62  
63      /* (non-Javadoc)
64       * @see org.nuiton.eugene.models.state.StateModelComplexeState#getStates()
65       */
66      public Collection<StateModelState> getStates() {
67          return mapState.values();
68      }
69  
70      /**
71       * Correct association, because, the xml file migth be non ordonated
72       *
73       * @param parent
74       */
75      void correctTransitionNameToInstance(StateModelComplexeStateImpl parent) {
76  
77          // iterator
78  
79          for (StateModelState stateModelState : mapState.values()) {
80              StateModelState state = stateModelState;
81  
82              // reboucle si l'etat est complexe
83              if (state instanceof StateModelComplexeStateImpl) {
84                  ((StateModelComplexeStateImpl) state)
85                          .correctTransitionNameToInstance(this);
86              } else {
87                  for (StateModelTransition tr : state.getTransitions()) {
88                      StateModelTransitionImpl tri =
89                              (StateModelTransitionImpl) tr;
90                      String name = tri.getStateName();
91  
92                      // l'etat apartient au cas complexe courant
93                      if (getState(name) != null) {
94                          tri.setState(getState(name));
95                      } else {
96                          // l'etat apartient au cas complexe parent
97                          if (parent != null && parent.getState(name) != null) {
98                              tri.setState(parent.getState(name));
99                          }
100                         // sinon il reste a null, tant pis
101                     }
102                 }
103             }
104         }
105     }
106 
107     /**
108      * @param stateName a state or null if state doesnt exists
109      * @return a state ref by his name
110      */
111     StateModelState getState(String stateName) {
112         return mapState.get(stateName);
113     }
114 
115     @Override
116     public boolean isComplex() {
117         // TODO Auto-generated method stub
118         return true;
119     }
120 
121     @Override
122     public StateModelState getInitialState() {
123 
124         StateModelState response = null;
125 
126         // iterator
127 
128         for (StateModelState stateModelState : mapState.values()) {
129             StateModelState state = stateModelState;
130 
131             // if state is simple
132             if (!state.isComplex()) {
133                 StateModelSimpleState simpleState =
134                         (StateModelSimpleState) state;
135 
136                 if (simpleState.isInitial()) {
137                     // get(0), normalement il n'y a qu'une transition sur
138                     // un etat initial
139                     response = simpleState.getTransitions().get(0)
140                             .getDestinationState();
141                 }
142             }
143         }
144 
145         return response;
146     }
147 
148 }