1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard;
3 import java.util.Collections;
4 import java.util.HashMap;
6 import java.util.Map.Entry;
7 import java.util.TreeMap;
8 import java.util.function.BiFunction;
9 import java.util.function.Function;
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;
21 public class StandardHighLevelStateHandler implements HighLevelStateHandler
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;
29 public StandardHighLevelStateHandler(SubmodelComponent component)
31 this(component, null);
34 public StandardHighLevelStateHandler(SubmodelComponent component, StandardHighLevelStateHandlerParams params)
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);
43 params.subcomponentHighLevelStates.forEach(this::addSubcomponentHighLevelState);
44 params.atomicHighLevelStates.forEach(this::addAtomicHighLevelState);
48 public SubcomponentHighLevelStateHandler addSubcomponentHighLevelState(String subcomponentStateID,
49 SubcomponentHighLevelStateHandlerParams handlerParams)
51 return addSubcomponentHighLevelState(subcomponentStateID,
52 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getSnippetSupplier(handlerParams.id)::create,
53 handlerParams.params);
56 public <P, H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
57 BiFunction<SubmodelComponent, P, H> handlerConstructor, P handlerParams)
59 return addSubcomponentHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams));
62 public <H extends SubcomponentHighLevelStateHandler> H addSubcomponentHighLevelState(String subcomponentStateID,
63 Function<SubmodelComponent, H> handlerConstructor)
65 H handler = handlerConstructor.apply(component);
66 addSubcomponentHighLevelState(subcomponentStateID, handler);
70 public void addSubcomponentHighLevelState(String subcomponentStateID, SubcomponentHighLevelStateHandler handler)
72 checkHighLevelStateIDPart(subcomponentStateID);
73 subcomponentHighLevelStateHandlers.put(subcomponentStateID, handler);
76 public void removeSubcomponentHighLevelState(String subcomponentStateID)
78 checkHighLevelStateIDPart(subcomponentStateID);
79 subcomponentHighLevelStateHandlers.remove(subcomponentStateID);
82 public Map<String, SubcomponentHighLevelStateHandler> getSubcomponentHighLevelStates()
84 return subcomponentHighLevelStateHandlersUnmodifiable;
87 public AtomicHighLevelStateHandler addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandlerParams handlerParams)
89 return addAtomicHighLevelState(atomicStateID,
90 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getSnippetSupplier(handlerParams.id)::create,
91 handlerParams.params);
94 public <P, H extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
95 BiFunction<SubmodelComponent, P, H> handlerConstructor, P handlerParams)
97 return addAtomicHighLevelState(subcomponentStateID, c -> handlerConstructor.apply(c, handlerParams));
100 public <H extends AtomicHighLevelStateHandler> H addAtomicHighLevelState(String subcomponentStateID,
101 Function<SubmodelComponent, H> handlerConstructor)
103 H handler = handlerConstructor.apply(component);
104 addAtomicHighLevelState(subcomponentStateID, handler);
108 public void addAtomicHighLevelState(String atomicStateID, AtomicHighLevelStateHandler handler)
110 checkHighLevelStateIDPart(atomicStateID);
111 atomicHighLevelStateHandlers.put(atomicStateID, handler);
114 public void removeAtomicHighLevelState(String atomicStateID)
116 checkHighLevelStateIDPart(atomicStateID);
117 atomicHighLevelStateHandlers.remove(atomicStateID);
120 public Map<String, AtomicHighLevelStateHandler> getAtomicHighLevelStates()
122 return atomicHighLevelStateHandlersUnmodifiable;
125 private static void checkHighLevelStateIDPart(String stateIDPart)
127 if (stateIDPart.indexOf('.') != -1)
128 throw new IllegalArgumentException("Illegal high level state ID part (contains dot): " + stateIDPart);
132 public Object getHighLevelState(String stateID)
134 int indexOfDot = stateID.indexOf('.');
135 if (indexOfDot == -1)
137 AtomicHighLevelStateHandler handler = atomicHighLevelStateHandlers.get(stateID);
139 return handler.getHighLevelState();
142 SubcomponentHighLevelStateHandler handler = subcomponentHighLevelStateHandlers.get(stateID.substring(0, indexOfDot));
144 return handler.getHighLevelState(stateID.substring(indexOfDot + 1));
146 throw new IllegalArgumentException("No high level state with ID " + stateID);
150 public void setHighLevelState(String stateID, Object newState)
152 int indexOfDot = stateID.indexOf('.');
153 if (indexOfDot == -1)
155 AtomicHighLevelStateHandler handler = atomicHighLevelStateHandlers.get(stateID);
157 handler.setHighLevelState(newState);
159 throw new IllegalArgumentException("No high level state with ID " + stateID);
162 SubcomponentHighLevelStateHandler handler = subcomponentHighLevelStateHandlers.get(stateID.substring(0, indexOfDot));
164 handler.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
166 throw new IllegalArgumentException("No high level state with ID " + stateID);
171 public String getIDForSerializing(IdentifyParams idParams)
177 public StandardHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
179 StandardHighLevelStateHandlerParams params = new StandardHighLevelStateHandlerParams();
180 params.subcomponentHighLevelStates = new TreeMap<>();
181 params.atomicHighLevelStates = new TreeMap<>();
182 for (Entry<String, SubcomponentHighLevelStateHandler> e : subcomponentHighLevelStateHandlers.entrySet())
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);
191 for (Entry<String, AtomicHighLevelStateHandler> e : atomicHighLevelStateHandlers.entrySet())
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);
203 public static class StandardHighLevelStateHandlerParams
205 public Map<String, SubcomponentHighLevelStateHandlerParams> subcomponentHighLevelStates;
206 public Map<String, AtomicHighLevelStateHandlerParams> atomicHighLevelStates;
211 SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.setSnippetSupplier(
212 StandardHighLevelStateHandler.class.getCanonicalName(),
213 SnippetDefinintion.create(StandardHighLevelStateHandlerParams.class, StandardHighLevelStateHandler::new));