[97] | 1 | package com.framsticks.core; |
---|
| 2 | |
---|
| 3 | import java.util.List; |
---|
| 4 | |
---|
| 5 | import javax.annotation.Nonnull; |
---|
| 6 | |
---|
| 7 | import org.apache.log4j.Logger; |
---|
| 8 | |
---|
| 9 | import com.framsticks.communication.File; |
---|
| 10 | import com.framsticks.params.AccessInterface; |
---|
| 11 | import com.framsticks.params.CompositeParam; |
---|
| 12 | import com.framsticks.params.FramsClass; |
---|
| 13 | import com.framsticks.params.ListAccess; |
---|
| 14 | import com.framsticks.params.Param; |
---|
| 15 | import com.framsticks.params.PrimitiveParam; |
---|
| 16 | import com.framsticks.params.ValueParam; |
---|
| 17 | import com.framsticks.params.types.ObjectParam; |
---|
| 18 | import com.framsticks.params.types.ProcedureParam; |
---|
| 19 | import com.framsticks.parsers.Loaders; |
---|
| 20 | import com.framsticks.parsers.MultiParamLoader; |
---|
| 21 | import com.framsticks.util.FramsticksException; |
---|
| 22 | import com.framsticks.util.dispatching.Future; |
---|
| 23 | import com.framsticks.util.dispatching.FutureHandler; |
---|
| 24 | import com.framsticks.util.dispatching.RunAt; |
---|
| 25 | |
---|
| 26 | import static com.framsticks.util.dispatching.Dispatching.*; |
---|
| 27 | |
---|
| 28 | public final class TreeOperations { |
---|
| 29 | |
---|
| 30 | private static final Logger log = Logger.getLogger(TreeOperations.class.getName()); |
---|
| 31 | |
---|
| 32 | private TreeOperations() { |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | public static @Nonnull FramsClass processFetchedInfo(Tree tree, File file) { |
---|
| 36 | assert tree.isActive(); |
---|
| 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 | } |
---|
| 43 | tree.putInfoIntoCache(framsClass); |
---|
| 44 | return framsClass; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public static FramsClass getInfo(Path path) { |
---|
| 48 | Tree tree = path.getTree(); |
---|
| 49 | assert tree.isActive(); |
---|
| 50 | final String name = path.getTop().getParam().getContainedTypeName(); |
---|
| 51 | return tree.getInfoFromCache(name); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public static void findInfo(final Path path, final Future<FramsClass> future) { |
---|
| 55 | try { |
---|
| 56 | Tree tree = path.getTree(); |
---|
| 57 | assert tree.isActive(); |
---|
| 58 | final FramsClass framsClass = getInfo(path); |
---|
| 59 | if (framsClass != null) { |
---|
| 60 | future.pass(framsClass); |
---|
| 61 | return; |
---|
| 62 | } |
---|
| 63 | tree.info(path, future); |
---|
| 64 | } catch (FramsticksException e) { |
---|
| 65 | future.handle(e); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 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 | } |
---|
| 176 | |
---|
| 177 | public static @Nonnull AccessInterface bindAccess(Tree tree, String path) { |
---|
| 178 | return bindAccess(Path.to(tree, path)); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public static @Nonnull AccessInterface bindAccess(Tree tree, Node node) { |
---|
| 182 | assert tree.isActive(); |
---|
| 183 | assert node.getObject() != null; |
---|
| 184 | |
---|
| 185 | try { |
---|
| 186 | return tree.prepareAccess(node.getParam()).select(node.getObject()); |
---|
| 187 | } catch (FramsticksException e) { |
---|
| 188 | throw new FramsticksException().msg("failed to prepare access for param").arg("param", node.getParam()).cause(e); |
---|
| 189 | // log.error("failed to bind access for " + node.getParam() + ": " + e); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | public static @Nonnull AccessInterface bindAccess(Path path) { |
---|
| 194 | assert path.getTree().isActive(); |
---|
| 195 | path.assureResolved(); |
---|
| 196 | return bindAccess(path.getTree(), path.getTop()); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | public static void set(final Path path, final PrimitiveParam<?> param, final Object value, final Future<Integer> future) { |
---|
| 200 | final Tree tree = path.getTree(); |
---|
| 201 | |
---|
| 202 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
| 203 | @Override |
---|
| 204 | protected void runAt() { |
---|
| 205 | tree.set(path, param, value, future); |
---|
| 206 | } |
---|
| 207 | }); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | public static void call(final Path path, final ProcedureParam param, final Object[] arguments, final Future<Object> future) { |
---|
| 211 | final Tree tree = path.getTree(); |
---|
| 212 | |
---|
| 213 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
| 214 | @Override |
---|
| 215 | protected void runAt() { |
---|
| 216 | tree.call(path, param, arguments, future); |
---|
| 217 | } |
---|
| 218 | }); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /** This might not be correct. */ |
---|
| 222 | public static void resolve(final Tree tree, final String targetPath, final Future<Path> future) { |
---|
| 223 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
| 224 | |
---|
| 225 | @Override |
---|
| 226 | protected void runAt() { |
---|
| 227 | tree.resolve(Path.to(tree, targetPath), new FutureHandler<Path>(future) { |
---|
| 228 | @Override |
---|
| 229 | 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); |
---|
| 236 | } |
---|
| 237 | }); |
---|
| 238 | } |
---|
| 239 | }); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | public static FramsClass getInfoFromCache(Path path) { |
---|
| 243 | assert path.getTree().isActive(); |
---|
| 244 | return path.getTree().getInfoFromCache(path.getTop().getParam().getContainedTypeName()); |
---|
| 245 | } |
---|
| 246 | } |
---|