- Timestamp:
- 07/08/13 23:04:56 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/core/TreeOperations.java
r97 r98 1 1 package com.framsticks.core; 2 2 3 3 4 import java.util.List; … … 14 15 import com.framsticks.params.Param; 15 16 import com.framsticks.params.PrimitiveParam; 16 import com.framsticks.params.ValueParam;17 17 import com.framsticks.params.types.ObjectParam; 18 18 import com.framsticks.params.types.ProcedureParam; … … 28 28 public final class TreeOperations { 29 29 30 private static final Logger log = Logger.getLogger(TreeOperations.class .getName());30 private static final Logger log = Logger.getLogger(TreeOperations.class); 31 31 32 32 private TreeOperations() { … … 36 36 assert tree.isActive(); 37 37 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 38 if ("/".equals(file.getPath())) { 39 if (tree.getRoot().getParam().getContainedTypeName() == null) { 40 tree.setRoot(new Node(Param.build().name("Tree").id(tree.getName()).type("o " + framsClass.getId()).finish(CompositeParam.class), tree.getRoot().getObject())); 41 } 42 } 38 log.debug("process fetched info for " + tree + ": " + framsClass); 43 39 tree.putInfoIntoCache(framsClass); 44 40 return framsClass; 45 41 } 46 42 43 public static void processFetchedValues(Path path, List<File> files) { 44 Tree tree = path.getTree(); 45 assert tree.isActive(); 46 assert files.size() == 1; 47 assert path.isTheSame(files.get(0).getPath()); 48 49 if (!path.isResolved()) { 50 AccessInterface access = tree.prepareAccess(path.getTop().getParam()); 51 Object child = access.createAccessee(); 52 assert child != null; 53 if (path.size() == 1) { 54 tree.assignRootObject(child); 55 } else { 56 bindAccess(path.getUnder()).set(path.getTop().getParam(), child); 57 } 58 path = path.appendResolution(child); 59 } 60 61 log.debug("process fetched values: " + path); 62 Node node = path.getTop(); 63 MultiParamLoader loader = new MultiParamLoader(); 64 loader.setNewSource(files.get(0).getContent()); 65 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 66 67 try { 68 if (node.getParam() instanceof ObjectParam) { 69 loader.addAccessInterface(bindAccess(node)); 70 loader.go(); 71 return; 72 } 73 74 ListAccess listAccess = (ListAccess) bindAccess(node); 75 listAccess.clearValues(); 76 77 AccessInterface elementAccess = listAccess.getElementAccess(); 78 loader.addAccessInterface(elementAccess); 79 MultiParamLoader.Status status; 80 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 81 if (status == MultiParamLoader.Status.AfterObject) { 82 AccessInterface accessInterface = loader.getLastAccessInterface(); 83 84 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 85 //TODO listAccessParam 86 CompositeParam param = Param.build().forAccess(accessInterface).id(id).finish(CompositeParam.class); 87 Object child = accessInterface.getSelected(); 88 accessInterface.select(null); 89 assert child != null; 90 bindAccess(node).set(param, child); 91 } 92 } 93 94 } catch (FramsticksException e) { 95 throw new FramsticksException().msg("exception occurred while loading").cause(e); 96 } 97 } 98 47 99 public static FramsClass getInfo(Path path) { 48 100 Tree tree = path.getTree(); 49 101 assert tree.isActive(); 102 log.debug("get info for: " + path); 50 103 final String name = path.getTop().getParam().getContainedTypeName(); 51 104 return tree.getInfoFromCache(name); … … 53 106 54 107 public static void findInfo(final Path path, final Future<FramsClass> future) { 108 log.debug("find info for: " + path); 55 109 try { 56 110 Tree tree = path.getTree(); … … 67 121 } 68 122 69 public static void processFetchedValues(Path path, List<File> files) { 70 Tree tree = path.getTree(); 71 assert tree.isActive(); 72 assert files.size() == 1; 73 assert path.isTheSame(files.get(0).getPath()); 74 Node node = path.getTop(); 75 MultiParamLoader loader = new MultiParamLoader(); 76 loader.setNewSource(files.get(0).getContent()); 77 loader.addBreakCondition(MultiParamLoader.Status.AfterObject); 78 79 try { 80 if (node.getParam() instanceof ObjectParam) { 81 loader.addAccessInterface(TreeOperations.bindAccess(tree, node)); 82 loader.go(); 83 tree.notifyOfFetch(path); 84 return; 85 } 86 87 ListAccess listAccess = ((ListAccess) TreeOperations.bindAccess(tree, node)); 88 assert listAccess != null; 89 listAccess.clearValues(); 90 91 AccessInterface elementAccess = listAccess.getElementAccess(); 92 loader.addAccessInterface(elementAccess); 93 MultiParamLoader.Status status; 94 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 95 if (status == MultiParamLoader.Status.AfterObject) { 96 AccessInterface accessInterface = loader.getLastAccessInterface(); 97 98 String id = listAccess.computeIdentifierFor(accessInterface.getSelected()); 99 //TODO listAccessParam 100 Param param = Param.build().forAccess(accessInterface).id(id).finish(); 101 Object child = accessInterface.getSelected(); 102 accessInterface.select(null); 103 assert child != null; 104 TreeOperations.bindAccess(tree, node).set((ValueParam) param, child); 105 } 106 } 107 108 tree.notifyOfFetch(path); 109 } catch (Exception e) { 110 log.error("exception occurred while loading: " + e); 111 } 112 113 } 114 115 public static void resolveAndGet(final Tree tree, final String targetPath, final Future<Path> future) { 116 resolve(tree, targetPath, new FutureHandler<Path>(future) { 117 @Override 118 protected void result(final Path path) { 119 assert path.isResolved(targetPath); 120 //TODO Future 121 tree.get(path, Mode.FETCH, new FutureHandler<Object>(future) { 122 @Override 123 protected void result(Object object) { 124 future.pass(path); 125 } 126 }); 127 } 128 }); 129 } 130 131 132 public static void resolve(final Path path, final Future<Path> future) { 133 final Tree tree = path.getTree(); 134 assert tree.isActive(); 135 if (path.getTop().getObject() != null) { 136 if (getInfoFromCache(path) != null) { 137 future.pass(path); 138 return; 139 } 140 TreeOperations.findInfo(path, new FutureHandler<FramsClass>(future) { 141 @Override 142 protected void result(FramsClass result) { 143 future.pass(path); 144 } 145 }); 146 return; 147 } 148 TreeOperations.findInfo(path, new FutureHandler<FramsClass>(future) { 149 @Override 150 protected void result(FramsClass result) { 151 assert tree.isActive(); 152 assert path.getTop().getParam().isMatchingContainedName(result.getId()); 153 Path p = (path.getTop().getParam().getContainedTypeName() != null ? path : path.tryFindResolution()); 154 future.pass(TreeOperations.createIfNeeded(p)); 155 } 156 }); 157 } 158 159 public static Path createIfNeeded(Tree tree, String path) { 160 assert tree.isActive(); 161 Path p; 162 while (!(p = Path.to(tree, path)).isResolved(path)) { 163 tree.create(p); 164 } 165 return p; 166 } 167 168 public static Path createIfNeeded(Path path) { 169 Tree tree = path.getTree(); 170 assert tree.isActive(); 171 if (path.isResolved()) { 172 return path; 173 } 174 return tree.create(path); 175 } 123 176 124 177 125 public static @Nonnull AccessInterface bindAccess(Tree tree, String path) { 126 log.debug("bind access for textual: " + path + " in " + tree); 178 127 return bindAccess(Path.to(tree, path)); 179 128 } 180 129 181 public static @Nonnull AccessInterface bindAccess(Tree tree, Node node) { 130 public static @Nonnull AccessInterface bindAccess(Node node) { 131 Tree tree = node.getTree(); 182 132 assert tree.isActive(); 183 133 assert node.getObject() != null; … … 194 144 assert path.getTree().isActive(); 195 145 path.assureResolved(); 196 return bindAccess(path.getTree(), path.getTop()); 146 log.debug("bind access for: " + path); 147 return bindAccess(path.getTop()); 197 148 } 198 149 … … 219 170 } 220 171 172 221 173 /** This might not be correct. */ 222 public static void resolve(final Tree tree, final String targetPath, final Future<Path> future) { 174 public static void tryGet(final Tree tree, final String targetPath, final Future<Path> future) { 175 log.debug("resolve textual: " + targetPath + " for " + tree); 223 176 dispatchIfNotActive(tree, new RunAt<Tree>(future) { 224 177 225 178 @Override 226 179 protected void runAt() { 227 tree.resolve(Path.to(tree, targetPath), new FutureHandler<Path>(future) { 180 final Path path = Path.to(tree, targetPath); 181 if (path.isResolved()) { 182 future.pass(path); 183 return; 184 } 185 186 tree.get(path, Mode.FETCH, new FutureHandler<Path>(future) { 228 187 @Override 229 188 protected void result(Path result) { 230 assert result.getTree().isActive(); 231 if (result.isResolved(targetPath)) { 232 future.pass(result); 233 return; 234 } 235 resolve(tree, targetPath, future); 189 tryGet(tree, targetPath, future); 236 190 } 237 191 });
Note: See TracChangeset
for help on using the changeset viewer.