1 package org.simantics.db.layer0.variable;
3 import java.util.Collections;
7 import org.simantics.databoard.util.ObjectUtils;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
10 import org.simantics.db.common.utils.Logger;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variables.NodeStructure;
13 import org.simantics.db.procedure.Listener;
14 import org.simantics.simulator.variable.exceptions.NodeManagerException;
16 import gnu.trove.map.hash.THashMap;
18 @SuppressWarnings("rawtypes")
19 class NodeStructureRequest extends ParametrizedPrimitiveRead<VariableNode, NodeStructure> implements VariableNodeReadRunnable {
21 private Listener<NodeStructure> listener = null;
22 private NodeStructure value = Variables.PENDING_NODE_STRUCTURE;
23 private boolean wasRun = false;
25 static class Probe implements Runnable {
27 private VariableNode node;
28 public NodeStructure result;
30 public Probe(VariableNode node) {
34 @SuppressWarnings("unchecked")
38 result = NodeStructureRequest.get(node);
39 node.support.structureCache.put(node.node, result, 1000000000L);
40 } catch (NodeManagerException e) {
47 public NodeStructureRequest(VariableNode node) {
51 @SuppressWarnings("unchecked")
53 public void register(ReadGraph graph, final Listener<NodeStructure> procedure) {
55 if(procedure.isDisposed()) {
57 // We are not listening
58 NodeStructure result = (NodeStructure)parameter.support.structureCache.get(parameter.node);
61 // Return cached value immediately
62 procedure.execute(result);
64 NodeStructureRequest.Probe probe = new Probe(parameter);
65 parameter.support.manager.getRealm().asyncExec(probe);
66 if(probe.result != null) {
67 procedure.execute(probe.result);
69 procedure.execute(Variables.PENDING_NODE_STRUCTURE);
80 parameter.support.manager.addNodeListener(parameter.node, this);
83 procedure.execute(value);
85 NodeStructure result = (NodeStructure)parameter.support.structureCache.get(parameter.node);
87 procedure.execute(result);
89 procedure.execute(Variables.PENDING_NODE_STRUCTURE);
96 static class NodeListener implements VariableNodeReadRunnable {
98 private VariableNode node;
99 private NodeStructureRequest request;
101 public NodeListener(VariableNode node, NodeStructureRequest request) {
103 this.request = request;
106 @SuppressWarnings("unchecked")
109 node.support.manager.addNodeListener(node.node, request);
114 @SuppressWarnings("unchecked")
116 public void unregistered() {
117 parameter.support.manager.removeNodeListener(parameter.node, this);
118 parameter.support.structureCache.removeListening(parameter.node);
122 @SuppressWarnings("unchecked")
123 public static NodeStructure get(VariableNode parameter) throws NodeManagerException {
124 List<?> children = parameter.support.manager.getChildren(parameter.node);
125 List<?> properties = parameter.support.manager.getProperties(parameter.node);
126 Map<String, Object> childMap = Collections.emptyMap();
127 Map<String, Object> propertyMap = childMap;
128 if(!children.isEmpty()) {
129 childMap = new THashMap<>(children.size());
130 for(Object o : children) {
131 String name = parameter.support.manager.getName(o);
132 childMap.put(name, o);
135 if(!properties.isEmpty()) {
136 propertyMap = new THashMap<>(properties.size());
137 for(Object o : properties) {
138 String name = parameter.support.manager.getName(o);
139 propertyMap.put(name, o);
142 return new NodeStructure(childMap, propertyMap);
145 @SuppressWarnings("unchecked")
147 public synchronized void run() {
149 // Cache this value with infinite cache time since we are listening
150 NodeStructure newValue = get(parameter);
151 if (wasRun && ObjectUtils.objectEquals(value, newValue)) {
152 //System.out.println("CACHE VALUE MATCH (" + newValue + ") for " + node.node);
156 parameter.support.structureCache.put(parameter.node, value);
157 } catch (Throwable e) {
158 // Must catch everything to prevent DB client from getting stuck.
159 Logger.defaultLogError(e);
160 // Invoke the exception method of the listener
161 Listener<NodeStructure> listener = this.listener;
162 if (listener != null) listener.exception(new DatabaseException("External data access error", e));
167 // Must always invoke an existing listener, regardless of earlier errors.
168 Listener<NodeStructure> listener = this.listener;
169 if (listener != null) {
170 listener.execute(value);
176 public String toString() {
177 return "NodeStructureRequest.run @ " + System.identityHashCode(this);