Changeset 84 for java/main/src/main/java/com/framsticks/core/Instance.java
- Timestamp:
- 06/22/13 21:51:33 (11 years ago)
- Location:
- java/main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main
-
Property
svn:ignore
set to
target
-
Property
svn:ignore
set to
-
java/main/src/main/java/com/framsticks/core/Instance.java
r78 r84 3 3 import com.framsticks.communication.*; 4 4 import com.framsticks.params.*; 5 import com.framsticks.params.types.CompositeParam;6 5 import com.framsticks.params.types.ObjectParam; 7 6 import com.framsticks.parsers.Loaders; … … 9 8 import com.framsticks.util.*; 10 9 import com.framsticks.util.UnsupportedOperationException; 10 import com.framsticks.util.dispatching.Dispatching; 11 import com.framsticks.util.dispatching.Future; 12 import com.framsticks.util.lang.Casting; 11 13 import org.apache.log4j.Logger; 12 14 … … 18 20 public abstract class Instance extends Entity { 19 21 20 private static final Logger LOGGER = Logger.getLogger(Instance.class.getName()); 21 22 23 protected Node root; 24 25 public Set<InstanceListener> listeners = new HashSet<InstanceListener>(); 26 27 public Instance(Parameters parameters) { 28 super(parameters); 29 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o").build(), null); 30 } 31 32 @Override 33 protected void run() { 34 super.run(); 22 private static final Logger log = Logger.getLogger(Instance.class.getName()); 23 24 25 protected Node root; 26 27 public Set<InstanceListener> listeners = new HashSet<InstanceListener>(); 28 29 public Instance() { 30 } 31 32 @Override 33 protected void run() { 34 super.run(); 35 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o").build(), null); 35 36 com.framsticks.model.Package.register(registry); 36 } 37 38 @Override 39 protected void configure() throws Exception { 40 super.configure(); 41 } 42 43 protected void fetchInfo(Path path, Future<FramsClass> future) { 37 } 38 39 protected void fetchInfo(Path path, Future<FramsClass> future) { 44 40 future.result(null, new UnsupportedOperationException()); 45 41 } … … 61 57 } 62 58 63 59 public void fetchValue(Path path, Param param, StateFunctor stateFunctor) { 64 60 stateFunctor.call(null); 65 61 } 66 62 67 63 public void fetchValues(Path path, StateFunctor stateFunctor) { 68 64 stateFunctor.call(null); 69 65 } 70 66 71 67 protected void tryRegisterOnChangeEvents(Path path) { 72 68 73 69 } … … 83 79 } 84 80 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 81 protected void fireRun(Exception e) { 82 for (InstanceListener l : this.listeners) { 83 l.onRun(e); 84 } 85 } 86 87 protected void fireStop(Exception e) { 88 for (InstanceListener l : this.listeners) { 89 l.onStop(e); 90 } 91 } 92 93 public void addListener(final InstanceListener listener) { 94 assert Dispatching.isThreadSafe(); 95 Dispatching.invokeLaterOrNow(this, new Runnable() { 96 @Override 97 public void run() { 98 listeners.add(listener); 99 } 100 }); 101 } 102 103 public void removeListener(final InstanceListener listener) { 104 assert Dispatching.isThreadSafe(); 105 Dispatching.invokeLaterOrNow(this, new Runnable() { 106 @Override 107 public void run() { 108 listeners.remove(listener); 109 } 110 }); 111 } 112 113 114 protected void fireListChange(Path path, ListChange change) { 115 assert isActive(); 116 for (InstanceListener l : this.listeners) { 121 117 l.onListChange(path, change); 122 118 } 123 119 } 124 120 125 126 public final FramsClass getInfoFromCache(Path path) { 127 return getInfoFromCache(path.getTop().getParam().getContainedTypeName()); 128 } 129 130 131 public FramsClass getInfoFromCache(String id) { 132 assert isActive(); 121 protected void fireFetch(Path path) { 122 assert isActive(); 123 for (InstanceListener l : this.listeners) { 124 l.onFetch(path); 125 } 126 } 127 128 129 public final FramsClass getInfoFromCache(Path path) { 130 return getInfoFromCache(path.getTop().getParam().getContainedTypeName()); 131 } 132 133 134 public FramsClass getInfoFromCache(String id) { 135 assert isActive(); 133 136 return registry.getInfoFromCache(id); 134 137 } … … 137 140 138 141 public AccessInterface createAccess(String name) { 139 assert isActive(); 140 if (name == null) { 142 assert isActive(); 143 return registry.createAccess(name); 144 } 145 146 147 // TODO: make ValueParam 148 public <T> T get(Node node, Param childParam, Class<T> type) { 149 return bindAccess(node).get((ValueParam) childParam, type); 150 } 151 152 public void findInfo(final Path path, final Future<FramsClass> future) { 153 assert isActive(); 154 final String name = path.getTop().getParam().getContainedTypeName(); 155 final FramsClass framsClass = getInfoFromCache(name); 156 if (framsClass != null) { 157 log.trace("info for " + name + " found in cache"); 158 future.result(framsClass, null); 159 return; 160 } 161 fetchInfo(path, future); 162 } 163 164 public final AccessInterface bindAccess(Node node) { 165 assert node.getObject() != null; 166 AccessInterface access = registry.prepareAccess(node.getParam()); 167 if (access == null) { 168 log.error("missing access for: " + node.getParam()); 141 169 return null; 142 170 } 143 FramsClass framsClass = getInfoFromCache(name); 144 if (framsClass == null) { 145 return null; 146 } 147 148 return registry.createAccess(name, framsClass); 149 } 150 151 public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) { 152 if (access == null) { 153 return null; 154 } 155 return param.prepareAccessInterface(access); 156 } 157 158 public AccessInterface prepareAccess(CompositeParam param) { 159 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName())); 160 } 161 162 public <T> T get(Node node, Param childParam, Class<T> type) { 163 return bindAccess(node).get(childParam, type); 164 } 165 166 public void findInfo(final Path path, final Future<FramsClass> future) { 167 assert isActive(); 168 final String name = path.getTop().getParam().getContainedTypeName(); 169 final FramsClass framsClass = getInfoFromCache(name); 170 if (framsClass != null) { 171 LOGGER.trace("info for " + name + " found in cache"); 172 future.result(framsClass, null); 173 return; 174 } 175 fetchInfo(path, future); 176 } 177 178 public final AccessInterface bindAccess(Node node) { 179 assert node.getObject() != null; 180 AccessInterface access = prepareAccess(node.getParam()); 181 if (access == null) { 182 LOGGER.error("missing access for: " + node.getParam()); 183 return null; 184 } 185 access.select(node.getObject()); 186 return access; 187 } 188 189 public final <T> T getParam(Path path, String id, Class<T> type) { 190 return Casting.tryCast(type, prepareAccess(path.getTop().getParam()).getParam(id)); 191 } 192 193 public final AccessInterface bindAccess(Path path) { 194 assert path.isResolved(); 195 return bindAccess(path.getTop()); 196 } 197 198 public void resolve(final String targetPath, final Future<Path> future) { 199 assert isActive(); 200 final Path path = new Path(this, targetPath); 201 resolve(path, new Future<Path>() { 202 @Override 203 public void result(Path result, Exception e) { 204 assert isActive(); 205 if (e != null) { 206 future.result(path, e); 207 return; 208 } 209 if (path.isResolved(targetPath)) { 210 future.result(path, null); 211 return; 212 } 213 if (path.isResolved()) { 214 future.result(path, new Exception("testing")); 215 return; 216 } 217 resolve(targetPath, future); 218 } 219 }); 220 } 221 222 public void resolveAndFetch(final String targetPath, final Future<Path> future) { 223 assert isActive(); 224 resolve(targetPath, new Future<Path>() { 225 @Override 226 public void result(final Path path, Exception e) { 227 if (e != null) { 228 future.result(path, e); 229 return; 230 } 231 assert path.isResolved(targetPath); 232 fetchValues(path, new StateFunctor() { 233 @Override 234 public void call(Exception e) { 235 future.result(path, e); 236 } 237 }); 238 } 239 }); 240 } 241 242 public Path createIfNeeded(String path) { 243 Path p; 244 while (!(p = new Path(this, path)).isResolved(path)) { 245 create(p); 246 } 247 return p; 248 } 249 250 public Path create(Path path) { 251 assert isActive(); 252 assert !path.isResolved(); 253 Path resolved = path.findResolution(); 254 if (!resolved.isResolved()) { 255 LOGGER.debug("creating: " + path); 256 AccessInterface access = prepareAccess(path.getTop().getParam()); 257 assert access != null; 258 Object child = access.createAccessee(); 259 assert child != null; 260 if (path.size() == 1) { 261 root = new Node(root.getParam(), child); 262 } else { 263 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); 264 } 265 resolved = path.appendResolution(child); 266 } 267 tryRegisterOnChangeEvents(resolved); 268 return resolved; 269 } 270 271 272 273 274 public FramsClass processFetchedInfo(File file) { 275 assert isActive(); 276 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 277 if ("/".equals(file.getPath())) { 278 if (root.getParam().getContainedTypeName() == null) { 279 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o " + framsClass.getId()).build(), root.getObject()); 280 } 281 } 282 registry.putInfoIntoCache(framsClass); 283 return framsClass; 284 } 285 286 public void processFetchedValues(Path path, List<File> files) { 287 assert isActive(); 288 assert files.size() == 1; 289 assert path.isTheSame(files.get(0).getPath()); 290 Node node = path.getTop(); 291 MultiParamLoader loader = new MultiParamLoader(); 292 loader.setNewSource(files.get(0).getContent()); 293 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 294 295 try { 296 if (node.getParam() instanceof ObjectParam) { 297 loader.addAccessInterface(bindAccess(node)); 298 loader.go(); 299 // for (NodeListener l : listeners) { 300 // l.onChange(this); 301 // } 302 return; 303 } 304 305 ListAccess listAccess = ((ListAccess)bindAccess(node)); 306 assert listAccess != null; 307 listAccess.clearValues(); 308 309 AccessInterface elementAccess = listAccess.getElementAccess(); 310 loader.addAccessInterface(elementAccess); 311 MultiParamLoader.Status status; 312 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 313 if (status == MultiParamLoader.Status.AfterObject) { 314 AccessInterface accessInterface = loader.getLastAccessInterface(); 315 316 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 317 Param param = new ParamBuilder().setType("o " + accessInterface.getId()).setId(id).build(); 318 Object child = accessInterface.getSelected(); 319 accessInterface.select(null); 320 assert child != null; 321 bindAccess(node).set(param, child); 322 } 323 } 324 // for (NodeListener l : listeners) { 325 // l.onChange(this); 326 // } 327 } catch (Exception e) { 328 LOGGER.error("exception occurred while loading: " + e); 329 } 330 331 } 332 333 public static Iterator<String> splitPath(String path) { 334 List<String> list = new LinkedList<String>(); 335 for (String s : path.split("/")) { 336 if (!s.isEmpty()) { 337 list.add(s); 338 } 339 } 340 return list.iterator(); 341 } 171 access.select(node.getObject()); 172 return access; 173 } 174 175 public final <T> T getParam(Path path, String id, Class<T> type) { 176 return Casting.tryCast(type, registry.prepareAccess(path.getTop().getParam()).getParam(id)); 177 } 178 179 public final AccessInterface bindAccess(Path path) { 180 assert path.isResolved(); 181 return bindAccess(path.getTop()); 182 } 183 184 public void resolve(final String targetPath, final Future<Path> future) { 185 assert isActive(); 186 final Path path = getPath(targetPath); 187 resolve(path, new Future<Path>() { 188 @Override 189 public void result(Path result, Exception e) { 190 assert isActive(); 191 if (e != null) { 192 future.result(path, e); 193 return; 194 } 195 if (path.isResolved(targetPath)) { 196 future.result(path, null); 197 return; 198 } 199 if (path.isResolved()) { 200 future.result(path, new Exception("testing")); 201 return; 202 } 203 resolve(targetPath, future); 204 } 205 }); 206 } 207 208 public void resolveAndFetch(final String targetPath, final Future<Path> future) { 209 assert isActive(); 210 resolve(targetPath, new Future<Path>() { 211 @Override 212 public void result(final Path path, Exception e) { 213 if (e != null) { 214 future.result(path, e); 215 return; 216 } 217 assert path.isResolved(targetPath); 218 fetchValues(path, new StateFunctor() { 219 @Override 220 public void call(Exception e) { 221 future.result(path, e); 222 } 223 }); 224 } 225 }); 226 } 227 228 public Path createIfNeeded(String path) { 229 Path p; 230 while (!(p = getPath(path)).isResolved(path)) { 231 create(p); 232 } 233 return p; 234 } 235 236 public Path createIfNeeded(Path path) { 237 assert isActive(); 238 if (path.isResolved()) { 239 return path; 240 } 241 return create(path); 242 } 243 244 public Path create(Path path) { 245 assert isActive(); 246 assert !path.isResolved(); 247 Path resolved = path.tryFindResolution(); 248 if (!resolved.isResolved()) { 249 log.debug("creating: " + path); 250 AccessInterface access = registry.prepareAccess(path.getTop().getParam()); 251 assert access != null; 252 Object child = access.createAccessee(); 253 assert child != null; 254 if (path.size() == 1) { 255 root = new Node(root.getParam(), child); 256 } else { 257 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); 258 } 259 resolved = path.appendResolution(child); 260 } 261 tryRegisterOnChangeEvents(resolved); 262 return resolved; 263 } 264 265 266 267 268 public FramsClass processFetchedInfo(File file) { 269 assert isActive(); 270 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 271 if ("/".equals(file.getPath())) { 272 if (root.getParam().getContainedTypeName() == null) { 273 root = new Node((CompositeParam)new ParamBuilder().setName("Instance").setId(name).setType("o " + framsClass.getId()).build(), root.getObject()); 274 } 275 } 276 registry.putInfoIntoCache(framsClass); 277 return framsClass; 278 } 279 280 public void processFetchedValues(Path path, List<File> files) { 281 assert isActive(); 282 assert files.size() == 1; 283 assert path.isTheSame(files.get(0).getPath()); 284 Node node = path.getTop(); 285 MultiParamLoader loader = new MultiParamLoader(); 286 loader.setNewSource(files.get(0).getContent()); 287 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 288 289 try { 290 if (node.getParam() instanceof ObjectParam) { 291 loader.addAccessInterface(bindAccess(node)); 292 loader.go(); 293 fireFetch(path); 294 // for (NodeListener l : listeners) { 295 // l.onChange(this); 296 // } 297 return; 298 } 299 300 ListAccess listAccess = ((ListAccess)bindAccess(node)); 301 assert listAccess != null; 302 listAccess.clearValues(); 303 304 AccessInterface elementAccess = listAccess.getElementAccess(); 305 loader.addAccessInterface(elementAccess); 306 MultiParamLoader.Status status; 307 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 308 if (status == MultiParamLoader.Status.AfterObject) { 309 AccessInterface accessInterface = loader.getLastAccessInterface(); 310 311 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 312 Param param = new ParamBuilder().setType("o " + accessInterface.getId()).setId(id).build(); 313 Object child = accessInterface.getSelected(); 314 accessInterface.select(null); 315 assert child != null; 316 bindAccess(node).set((ValueParam) param, child); 317 } 318 } 319 320 fireFetch(path); 321 // for (NodeListener l : listeners) { 322 // l.onChange(this); 323 // } 324 } catch (Exception e) { 325 log.error("exception occurred while loading: " + e); 326 } 327 328 } 329 330 public static Iterator<String> splitPath(String path) { 331 List<String> list = new LinkedList<String>(); 332 for (String s : path.split("/")) { 333 if (!s.isEmpty()) { 334 list.add(s); 335 } 336 } 337 return list.iterator(); 338 } 342 339 343 340 public Registry getRegistry() { 344 341 return registry; 345 342 } 343 344 public Path getPath(String textual) { 345 return new Path(this, textual); 346 } 347 348 public Path getRootPath() { 349 return getPath("/"); 350 } 346 351 } 347 352
Note: See TracChangeset
for help on using the changeset viewer.