58e8e64ce54dc7454dcb9f4c1b136944da35aea0
[Mograsim.git] /
1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.TreeMap;
8 import java.util.function.BiFunction;
9 import java.util.function.Function;
10
11 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
12 import net.mograsim.logic.model.serializing.IdentifyParams;
13 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
14 import net.mograsim.logic.model.snippets.SnippetDefinintion;
15 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
16 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.AtomicHighLevelStateHandler;
17 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.AtomicHighLevelStateHandler.AtomicHighLevelStateHandlerParams;
18 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.SubcomponentHighLevelStateHandler;
19 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.SubcomponentHighLevelStateHandler.SubcomponentHighLevelStateHandlerParams;
20
21 public class StandardHighLevelStateHandler implements HighLevelStateHandler
22 {
23         private final SubmodelComponent component;
24         private final Map<String, SubcomponentHighLevelStateHandler> subcomponentHighLevelStateHandlers;
25         private final Map<String, SubcomponentHighLevelStateHandler> subcomponentHighLevelStateHandlersUnmodifiable;
26         private final Map<String, AtomicHighLevelStateHandler> atomicHighLevelStateHandlers;
27         private final Map<String, AtomicHighLevelStateHandler> atomicHighLevelStateHandlersUnmodifiable;
28
29         public StandardHighLevelStateHandler(SubmodelComponent component)
30         {
31                 this(component, null);
32         }
33
34         public StandardHighLevelStateHandler(SubmodelComponent component, StandardHighLevelStateHandlerParams params)
35         {
36                 this.component = component;
37                 this.subcomponentHighLevelStateHandlers = new HashMap<>();
38                 this.subcomponentHighLevelStateHandlersUnmodifiable = Collections.unmodifiableMap(subcomponentHighLevelStateHandlers);
39                 this.atomicHighLevelStateHandlers = new HashMap<>();
40                 this.atomicHighLevelStateHandlersUnmodifiable = Collections.unmodifiableMap(atomicHighLevelStateHandlers);
41                 if (params != null)
42                 {
43                         params.subcomponentHighLevelStates.forEach(this::addSubcomponentHighLevelState);
44                         params.atomicHighLevelStates.forEach(this::addAtomicHighLevelState);
45                 }
46         }
47
48         public SubcomponentHighLevelStateHandler addSubcomponentHighLevelState(String subcomponentStateID,
49                         SubcomponentHighLevelStateHandlerParams handlerParams)
50         {
51                 return addSubcomponentHighLevelState(subcomponentStateID,
52                                 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getSnippetSupplier(handlerParams.id)::create,
53                                 handlerParams.params);
54         }
55
56         public <P, H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
57                         BiFunction<SubmodelComponent, P, H> handlerConstructor, P handlerParams)
58         {
59                 return addSubcomponentHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams));
60         }
61
62         public <H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
63                         Function<SubmodelComponent, H> handlerConstructor)
64         {
65                 H handler = handlerConstructor.apply(component);
66                 addSubcomponentHighLevelState(subcomponentStateID, handler);
67                 return handler;
68         }
69
70         public void addSubcomponentHighLevelState(String subcomponentStateID, SubcomponentHighLevelStateHandler handler)
71         {
72                 checkHighLevelStateIDPart(subcomponentStateID);
73                 subcomponentHighLevelStateHandlers.put(subcomponentStateID, handler);
74         }
75
76         public void removeSubcomponentHighLevelState(String subcomponentStateID)
77         {
78                 checkHighLevelStateIDPart(subcomponentStateID);
79                 subcomponentHighLevelStateHandlers.remove(subcomponentStateID);
80         }
81
82         public Map<String, SubcomponentHighLevelStateHandler> getSubcomponentHighLevelStates()
83         {
84                 return subcomponentHighLevelStateHandlersUnmodifiable;
85         }
86
87         public AtomicHighLevelStateHandler addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandlerParams handlerParams)
88         {
89                 return addAtomicHighLevelState(atomicStateID,
90                                 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getSnippetSupplier(handlerParams.id)::create,
91                                 handlerParams.params);
92         }
93
94         public <P, H extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
95                         BiFunction<SubmodelComponent, P, H> handlerConstructor, P handlerParams)
96         {
97                 return addAtomicHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams));
98         }
99
100         public <H extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
101                         Function<SubmodelComponent, H> handlerConstructor)
102         {
103                 H handler = handlerConstructor.apply(component);
104                 addAtomicHighLevelState(subcomponentStateID, handler);
105                 return handler;
106         }
107
108         public void addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandler handler)
109         {
110                 checkHighLevelStateIDPart(atomicStateID);
111                 atomicHighLevelStateHandlers.put(atomicStateID, handler);
112         }
113
114         public void removeAtomicHighLevelState(String atomicStateID)
115         {
116                 checkHighLevelStateIDPart(atomicStateID);
117                 atomicHighLevelStateHandlers.remove(atomicStateID);
118         }
119
120         public Map<String, AtomicHighLevelStateHandler> getAtomicHighLevelStates()
121         {
122                 return atomicHighLevelStateHandlersUnmodifiable;
123         }
124
125         private static void checkHighLevelStateIDPart(String stateIDPart)
126         {
127                 if (stateIDPart.indexOf('.') != -1)
128                         throw new IllegalArgumentException("Illegal high level state ID part (contains dot): " + stateIDPart);
129         }
130
131         @Override
132         public Object getHighLevelState(String stateID)
133         {
134                 int indexOfDot = stateID.indexOf('.');
135                 if (indexOfDot == -1)
136                 {
137                         AtomicHighLevelStateHandler handler = atomicHighLevelStateHandlers.get(stateID);
138                         if (handler != null)
139                                 return handler.getHighLevelState();
140                 } else
141                 {
142                         SubcomponentHighLevelStateHandler handler = subcomponentHighLevelStateHandlers.get(stateID.substring(0, indexOfDot));
143                         if (handler != null)
144                                 return handler.getHighLevelState(stateID.substring(indexOfDot + 1));
145                 }
146                 throw new IllegalArgumentException("No high level state with ID " + stateID);
147         }
148
149         @Override
150         public void setHighLevelState(String stateID, Object newState)
151         {
152                 int indexOfDot = stateID.indexOf('.');
153                 if (indexOfDot == -1)
154                 {
155                         AtomicHighLevelStateHandler handler = atomicHighLevelStateHandlers.get(stateID);
156                         if (handler != null)
157                                 handler.setHighLevelState(newState);
158                         else
159                                 throw new IllegalArgumentException("No high level state with ID " + stateID);
160                 } else
161                 {
162                         SubcomponentHighLevelStateHandler handler = subcomponentHighLevelStateHandlers.get(stateID.substring(0, indexOfDot));
163                         if (handler != null)
164                                 handler.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
165                         else
166                                 throw new IllegalArgumentException("No high level state with ID " + stateID);
167                 }
168         }
169
170         @Override
171         public String getIDForSerializing(IdentifyParams idParams)
172         {
173                 return "standard";
174         }
175
176         @Override
177         public StandardHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
178         {
179                 StandardHighLevelStateHandlerParams params = new StandardHighLevelStateHandlerParams();
180                 params.subcomponentHighLevelStates = new TreeMap<>();
181                 params.atomicHighLevelStates = new TreeMap<>();
182                 for (Entry<String, SubcomponentHighLevelStateHandler> e : subcomponentHighLevelStateHandlers.entrySet())
183                 {
184                         String stateID = e.getKey();
185                         SubcomponentHighLevelStateHandler handler = e.getValue();
186                         SubcomponentHighLevelStateHandlerParams handlerParams = new SubcomponentHighLevelStateHandlerParams();
187                         handlerParams.id = handler.getIDForSerializing(idParams);
188                         handlerParams.params = handler.getParamsForSerializingJSON(idParams);
189                         params.subcomponentHighLevelStates.put(stateID, handlerParams);
190                 }
191                 for (Entry<String, AtomicHighLevelStateHandler> e : atomicHighLevelStateHandlers.entrySet())
192                 {
193                         String stateID = e.getKey();
194                         AtomicHighLevelStateHandler handler = e.getValue();
195                         AtomicHighLevelStateHandlerParams handlerParams = new AtomicHighLevelStateHandlerParams();
196                         handlerParams.id = handler.getIDForSerializing(idParams);
197                         handlerParams.params = handler.getParamsForSerializingJSON(idParams);
198                         params.atomicHighLevelStates.put(stateID, handlerParams);
199                 }
200                 return params;
201         }
202
203         public static class StandardHighLevelStateHandlerParams
204         {
205                 public Map<String, SubcomponentHighLevelStateHandlerParams> subcomponentHighLevelStates;
206                 public Map<String, AtomicHighLevelStateHandlerParams> atomicHighLevelStates;
207         }
208
209         static
210         {
211                 SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.setSnippetSupplier(
212                                 StandardHighLevelStateHandler.class.getCanonicalName(),
213                                 SnippetDefinintion.create(StandardHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new));
214         }
215 }