- Timestamp:
- 07/12/13 23:41:06 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/core/TreeOperations.java
r99 r100 8 8 import javax.annotation.Nonnull; 9 9 10 import org.apache.log4j.Logger; 10 import org.apache.logging.log4j.Logger; 11 import org.apache.logging.log4j.LogManager; 11 12 12 13 import com.framsticks.communication.File; 13 import com.framsticks.params.AccessInterface; 14 import com.framsticks.params.Access; 15 import com.framsticks.params.CompositeParam; 14 16 import com.framsticks.params.EventListener; 15 17 import com.framsticks.params.FramsClass; 16 18 import com.framsticks.params.ListAccess; 17 19 import com.framsticks.params.Param; 20 import com.framsticks.params.ParamBuilder; 18 21 import com.framsticks.params.PrimitiveParam; 22 import com.framsticks.params.PropertiesAccess; 19 23 import com.framsticks.params.UniqueListAccess; 20 24 import com.framsticks.params.Util; … … 25 29 import com.framsticks.parsers.MultiParamLoader; 26 30 import com.framsticks.util.FramsticksException; 31 import com.framsticks.util.dispatching.Dispatching; 27 32 import com.framsticks.util.dispatching.Future; 28 33 import com.framsticks.util.dispatching.FutureHandler; … … 33 38 public final class TreeOperations { 34 39 35 private static final Logger log = Log ger.getLogger(TreeOperations.class);40 private static final Logger log = LogManager.getLogger(TreeOperations.class); 36 41 37 42 private TreeOperations() { 38 43 } 39 44 45 public static final Object FETCHED_MARK = new Object(); 46 40 47 public static @Nonnull FramsClass processFetchedInfo(Tree tree, File file) { 41 48 assert tree.isActive(); 42 49 FramsClass framsClass = Loaders.loadFramsClass(file.getContent()); 43 log.debug("process fetched info for " + tree + ": " +framsClass);50 log.debug("process fetched info for {}: {}", tree, framsClass); 44 51 tree.putInfoIntoCache(framsClass); 45 52 return framsClass; 46 53 } 47 54 48 public static void processFetchedValues(Path path, List<File> files) { 49 Tree tree = path.getTree(); 50 assert tree.isActive(); 55 public static Path create(Path path) { 56 assert !path.isResolved(); 57 58 Access access = path.getTree().prepareAccess(path.getTop().getParam()); 59 Object child = createAccessee(path.getTree(), access); 60 assert child != null; 61 if (path.size() == 1) { 62 path.getTree().assignRootObject(child); 63 } else { 64 Access parentAccess = bindAccess(path.getUnder()); 65 66 /** this special case is not very good - maybe hide it in createAccessee? */ 67 if (parentAccess instanceof UniqueListAccess) { 68 access.select(child); 69 access.set(((UniqueListAccess) parentAccess).getUidName(), path.getTop().getParam().getId()); 70 } 71 72 parentAccess.set(path.getTop().getParam(), child); 73 } 74 path = path.appendResolution(child); 75 return path; 76 } 77 78 public static void processFetchedValues(final Path path, final List<File> files, final Access access, final Future<Path> future) { 51 79 assert files.size() == 1; 52 80 assert path.isTheSame(files.get(0).getPath()); 53 81 54 if (!path.isResolved()) {55 AccessInterface access = tree.prepareAccess(path.getTop().getParam());56 Object child = access.createAccessee();57 assert child != null;58 if (path.size() == 1) {59 tree.assignRootObject(child);60 } else {61 bindAccess(path.getUnder()).set(path.getTop().getParam(), child);62 }63 path = path.appendResolution(child);64 }65 66 log.debug("process fetched values: " + path);67 Node node = path.getTop();68 MultiParamLoader loader = new MultiParamLoader();69 loader.setNewSource(files.get(0).getContent());70 loader.addBreakCondition(MultiParamLoader.Status.AfterObject);71 82 72 83 try { 73 if (node.getParam() instanceof ObjectParam) { 74 loader.addAccessInterface(bindAccess(node)); 75 loader.go(); 76 return; 77 } 78 79 ListAccess listAccess = (ListAccess) bindAccess(node); 80 81 Set<String> oldValuesIds = new HashSet<>(); 82 for (Param p : listAccess.getParams()) { 83 oldValuesIds.add(p.getId()); 84 } 85 86 // listAccess.clearValues(); 87 88 AccessInterface elementAccess = listAccess.getElementAccess(); 89 AccessInterface clonerInterface = elementAccess.cloneAccess(); 90 91 loader.addAccessInterface(elementAccess); 92 MultiParamLoader.Status status; 93 int number = 0; 94 while ((status = loader.go()) != MultiParamLoader.Status.Finished) { 95 if (status == MultiParamLoader.Status.AfterObject) { 96 AccessInterface accessInterface = loader.getLastAccessInterface(); 97 98 String id; 99 if (listAccess instanceof UniqueListAccess) { 100 id = ((UniqueListAccess) listAccess).computeIdentifierFor(accessInterface.getSelected()); 101 } else { 102 id = Integer.toString(number); 103 } 104 ++number; 105 106 Object childTo = listAccess.get(id, Object.class); 107 boolean newOne; 108 if (childTo == null) { 109 childTo = clonerInterface.createAccessee(); 110 newOne = true; 111 } else { 112 assert oldValuesIds.contains(id); 113 newOne = false; 114 } 115 oldValuesIds.remove(id); 116 clonerInterface.select(childTo); 117 Util.takeAllNonNullValues(clonerInterface, accessInterface); 118 if (newOne) { 119 listAccess.set(id, childTo); 120 } 121 122 // listAccess.set(id, accessInterface.getSelected()); 123 accessInterface.select(null); 124 84 log.debug("process fetched values: {}", path); 85 final Access parsingAccess = new PropertiesAccess(access.getFramsClass()); 86 final List<Object> results = MultiParamLoader.loadAll(files.get(0).getContent(), parsingAccess); 87 88 Dispatching.dispatchIfNotActive(path.getTree(), new RunAt<Tree>(future) { 89 @Override 90 protected void runAt() { 91 92 Path result = path.tryResolveIfNeeded(); 93 94 if (!result.isResolved()) { 95 result = create(result); 96 } 97 98 if (path.getTop().getParam() instanceof ObjectParam) { 99 assert results.size() == 1; 100 Util.takeAllNonNullValues(bindAccess(result), parsingAccess.select(results.get(0))); 101 mark(result.getTree(), result.getTopObject(), FETCHED_MARK, true); 102 future.pass(result); 103 return; 104 } 105 106 107 final ListAccess listAccess = (ListAccess) access; 108 109 listAccess.select(result.getTopObject()); 110 Set<String> oldValuesIds = new HashSet<>(); 111 for (Param p : listAccess.getParams()) { 112 oldValuesIds.add(p.getId()); 113 } 114 115 Access targetAccess = listAccess.getElementAccess();//.cloneAccess(); 116 117 int number = 0; 118 for (Object r : results) { 119 120 parsingAccess.select(r); 121 String id; 122 if (listAccess instanceof UniqueListAccess) { 123 id = parsingAccess.get(((UniqueListAccess) listAccess).getUidName(), String.class); 124 } else { 125 id = Integer.toString(number); 126 } 127 ++number; 128 129 Object childTo = listAccess.get(id, Object.class); 130 boolean newOne; 131 if (childTo == null) { 132 childTo = createAccessee(result.getTree(), targetAccess); 133 newOne = true; 134 } else { 135 assert oldValuesIds.contains(id); 136 newOne = false; 137 } 138 oldValuesIds.remove(id); 139 140 targetAccess.select(childTo); 141 Util.takeAllNonNullValues(targetAccess, parsingAccess); 142 if (newOne) { 143 listAccess.set(id, childTo); 144 } 145 mark(result.getTree(), childTo, FETCHED_MARK, true); 146 147 } 148 mark(result.getTree(), result.getTopObject(), FETCHED_MARK, true); 149 150 /** It looks tricky for ArrayListAccess but should also work. 151 * 152 * They should be sorted. 153 */ 154 for (String id : oldValuesIds) { 155 listAccess.set(id, null); 156 } 157 future.pass(result); 125 158 } 126 } 127 /** It looks tricky for ArrayListAccess but should also work. 128 * 129 * They should be sorted. 130 */ 131 for (String id : oldValuesIds) { 132 listAccess.set(id, null); 133 } 159 }); 134 160 135 161 } catch (FramsticksException e) { … … 141 167 Tree tree = path.getTree(); 142 168 assert tree.isActive(); 143 log.debug("get info for: " +path);169 log.debug("get info for: {}", path); 144 170 final String name = path.getTop().getParam().getContainedTypeName(); 145 171 return tree.getInfoFromCache(name); … … 147 173 148 174 public static void findInfo(final Path path, final Future<FramsClass> future) { 149 log.debug("find info for: " +path);175 log.debug("find info for: {}", path); 150 176 try { 151 177 Tree tree = path.getTree(); … … 163 189 164 190 165 166 public static @Nonnull AccessInterface bindAccess(Tree tree, String path) { 167 log.debug("bind access for textual: " + path + " in " + tree); 191 public static @Nonnull Access bindAccessFromSideNote(Tree tree, Object object) { 192 CompositeParam param = tree.getSideNote(object, CompositeParam.class, CompositeParam.class); 193 if (param == null) { 194 throw new FramsticksException().msg("failed to bind access from side node").arg("tree", tree).arg("object", object).arg("type", object.getClass()); 195 } 196 return tree.prepareAccess(param).select(object); 197 } 198 199 public static @Nonnull Access bindAccess(Tree tree, String path) { 200 log.debug("bind access for textual: {} in {}", path, tree); 168 201 return bindAccess(Path.to(tree, path)); 169 202 } 170 203 171 public static @Nonnull Access InterfacebindAccess(Node node) {204 public static @Nonnull Access bindAccess(Node node) { 172 205 Tree tree = node.getTree(); 173 206 assert tree.isActive(); … … 175 208 176 209 try { 177 return tree.prepareAccess(node.getParam()).select(node.getObject()); 210 Access access = tree.prepareAccess(node.getParam()); 211 tree.putSideNote(node.getObject(), CompositeParam.class, node.getParam()); 212 213 return access.select(node.getObject()); 178 214 } catch (FramsticksException e) { 179 215 throw new FramsticksException().msg("failed to prepare access for param").arg("param", node.getParam()).cause(e); 180 // log.error("failed to bind access for " + node.getParam() + ": " +e);181 } 182 } 183 184 public static @Nonnull Access InterfacebindAccess(Path path) {216 // log.error("failed to bind access for {}: ", node.getParam(), e); 217 } 218 } 219 220 public static @Nonnull Access bindAccess(Path path) { 185 221 assert path.getTree().isActive(); 186 222 path.assureResolved(); 187 log.debug("bind access for: " +path);223 log.debug("bind access for: {}", path); 188 224 return bindAccess(path.getTop()); 189 225 } … … 196 232 protected void runAt() { 197 233 tree.set(path, param, value, future); 234 } 235 }); 236 } 237 238 public static void call(final Path path, final String procedureName, final Object[] arguments, final Future<Object> future) { 239 final Tree tree = path.getTree(); 240 241 dispatchIfNotActive(tree, new RunAt<Tree>(future) { 242 @Override 243 protected void runAt() { 244 path.assureResolved(); 245 tree.call(path, tree.getRegistry().getFramsClass(path.getTop().getParam()).getParamEntry(procedureName, ProcedureParam.class), arguments, future); 198 246 } 199 247 }); … … 241 289 * */ 242 290 public static void tryGet(final Tree tree, final String targetPath, final Future<Path> future) { 243 log.debug("resolve textual: " + targetPath + " for " +tree);291 log.debug("resolve textual: {} for {}", targetPath, tree); 244 292 dispatchIfNotActive(tree, new RunAt<Tree>(future) { 245 293 … … 247 295 protected void runAt() { 248 296 final Path path = Path.tryTo(tree, targetPath).tryResolveIfNeeded(); 249 log.debug("found: " +path);297 log.debug("found: {}", path); 250 298 if (path.isResolved()) { 251 299 future.pass(path); … … 257 305 protected void result(Path result) { 258 306 // if (result.isResolved(targetPath)) { 259 // 260 // 307 // future.pass(result); 308 // return; 261 309 // } 262 log.debug("retrying resolve textual: " + targetPath + " for " + tree + " with " +result);310 log.debug("retrying resolve textual: {} for {} with {}", targetPath, tree, result); 263 311 tryGet(tree, targetPath, future); 264 312 } … … 273 321 } 274 322 323 public static Object createAccessee(Tree tree, CompositeParam param) { 324 Object object = tree.prepareAccess(param).createAccessee(); 325 tree.putSideNote(object, CompositeParam.class, param); 326 return object; 327 } 328 329 public static Object createAccessee(Tree tree, Access access) { 330 Object object = access.createAccessee(); 331 tree.putSideNote(object, CompositeParam.class, access.buildParam(new ParamBuilder()).finish(CompositeParam.class)); 332 return object; 333 } 334 335 public static boolean isMarked(Tree tree, Object object, Object mark, boolean defValue) { 336 assert tree.isActive(); 337 Boolean v = tree.getSideNote(object, mark, Boolean.class); 338 return (v != null ? v : defValue); 339 } 340 341 public static void mark(Tree tree, Object object, Object mark, boolean value) { 342 assert tree.isActive(); 343 tree.putSideNote(object, mark, value); 344 } 345 346 public static FramsClass getFramsClass(Path path) { 347 return path.getTree().getRegistry().getFramsClass(path.getTop().getParam()); 348 } 349 275 350 }
Note: See TracChangeset
for help on using the changeset viewer.