]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/state/StateNodeManager.java
Merge "Refactoring of simulator toolkit"
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / state / StateNodeManager.java
1 package org.simantics.document.server.state;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.binding.Binding;
8 import org.simantics.databoard.binding.error.BindingConstructionException;
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.databoard.serialization.RuntimeSerializerConstructionException;
11 import org.simantics.databoard.serialization.SerializerConstructionException;
12 import org.simantics.simulator.toolkit.StandardRealm;
13 import org.simantics.simulator.toolkit.db.StandardVariableNodeManager;
14 import org.simantics.simulator.variable.exceptions.NodeManagerException;
15 import org.slf4j.LoggerFactory;
16
17 public class StateNodeManager extends StandardVariableNodeManager<StateNode, StateNodeManagerSupport> {
18
19         private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(StateNodeManager.class);
20
21         public StateNodeManager(StandardRealm<StateNode, StateNodeManagerSupport> realm, StateNode root) {
22                 super(realm, root);
23         }
24
25         public void setState(String key, Object value) {
26                 try {
27                         getRealm().syncExec(() -> {
28                                 try {
29                                         StateRootNode rootNode = (StateRootNode) getRoot();
30                                         StatePropertyNode propertyNode = rootNode.getProperty(key);
31                                         if (propertyNode == null) {
32                                                 setValue(rootNode.createProperty(key), value, Bindings.OBJECT);
33                                                 refreshVariable(rootNode);
34                                         } else {
35                                                 setValue(propertyNode, value, Bindings.OBJECT);
36                                         }
37                                 } catch (NodeManagerException e) {
38                                         LOGGER.error("Failed to set state.", e);
39                                 }
40                         });
41                 } catch (InterruptedException e) {
42                         LOGGER.error("Setting state was interrupted.", e);
43                 }
44         }
45
46         public byte[] serialize() {
47                 final byte [][] result = new byte[1][];
48                 try {
49                         getRealm().syncExec(() -> {
50                                 State state = new State();
51                                 StateRootNode root = (StateRootNode) getRoot();
52                                 for (StateNode node : root.getProperties().values()) {
53                                         StatePropertyNode property = (StatePropertyNode)node;
54                                         try {
55                                                 Binding binding = Bindings.getInstanceBinding(property.getValue());
56                                                 if (binding != null) {
57                                                         state.properties.put(property.getName(), new Variant(binding, property.getValue()));
58                                                 }
59                                         } catch (BindingConstructionException e) {
60                                                 // skip
61                                         }
62                                 }
63
64                                 try {
65                                         result[0] = Bindings.getSerializerUnchecked(State.BINDING).serialize(state);
66                                 } catch (RuntimeSerializerConstructionException | IOException e) {
67                                 }
68                         });
69                 } catch (InterruptedException e) {
70                         LOGGER.error("Serializing state was interrupted.", e);
71                 }
72                 return result[0];
73         }
74
75         public void deserialize(byte[] bytes) {
76                 try {
77                         getRealm().syncExec(() -> {
78                                 StateRootNode rootNode = (StateRootNode) getRoot();
79                                 rootNode.clear();
80                                 try {
81                                         State state = (State) Bindings.getSerializer(State.BINDING).deserialize(bytes);
82                                         for (Map.Entry<String, Variant> entry : state.properties.entrySet()) {
83                                                 String key = entry.getKey();
84                                                 Object value = entry.getValue().getValue();
85                                                 try {
86                                                         setValue(rootNode.createProperty(key), value, Bindings.OBJECT);
87                                                 } catch (NodeManagerException e) {
88                                                         LOGGER.error("Failed to deserialize state.", e);
89                                                 }
90                                         }
91                                         refreshVariable(rootNode);
92                                 } catch (IOException e1) {
93                                         LOGGER.error("Failed to deserialize state.", e1);
94                                 } catch (SerializerConstructionException e1) {
95                                         LOGGER.error("Failed to deserialize state.", e1);
96                                 }
97                         });
98                 } catch (InterruptedException e) {
99                         LOGGER.error("Deserializing state was interrupted.", e);
100                 }
101         }
102
103         public void clearState() {
104                 try {
105                         getRealm().syncExec(() -> {
106                                 StateRootNode rootNode = (StateRootNode) getRoot();
107                                 rootNode.clear();
108                                 refreshVariable(rootNode);
109                         });
110                 } catch (InterruptedException e) {
111                         LOGGER.error("Clearing state was interrupted.", e);
112                 }
113         }
114
115         public void removeState(String key) {
116                 try {
117                         getRealm().syncExec(() -> {
118                                 StateRootNode rootNode = (StateRootNode) getRoot();
119                                 if (rootNode.removeProperty(key)) {
120                                         refreshVariable(rootNode);
121                                 }
122                         });
123                 } catch (InterruptedException e) {
124                         LOGGER.error("Removing state was interrupted.", e);
125                 }
126         }
127
128 }