1 | package com.framsticks.core; |
---|
2 | |
---|
3 | |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import javax.annotation.Nonnull; |
---|
7 | |
---|
8 | import org.apache.log4j.Logger; |
---|
9 | |
---|
10 | import com.framsticks.communication.File; |
---|
11 | import com.framsticks.params.AccessInterface; |
---|
12 | import com.framsticks.params.CompositeParam; |
---|
13 | import com.framsticks.params.FramsClass; |
---|
14 | import com.framsticks.params.ListAccess; |
---|
15 | import com.framsticks.params.Param; |
---|
16 | import com.framsticks.params.PrimitiveParam; |
---|
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); |
---|
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 | log.debug("process fetched info for " + tree + ": " + framsClass); |
---|
39 | tree.putInfoIntoCache(framsClass); |
---|
40 | return framsClass; |
---|
41 | } |
---|
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 | |
---|
99 | public static FramsClass getInfo(Path path) { |
---|
100 | Tree tree = path.getTree(); |
---|
101 | assert tree.isActive(); |
---|
102 | log.debug("get info for: " + path); |
---|
103 | final String name = path.getTop().getParam().getContainedTypeName(); |
---|
104 | return tree.getInfoFromCache(name); |
---|
105 | } |
---|
106 | |
---|
107 | public static void findInfo(final Path path, final Future<FramsClass> future) { |
---|
108 | log.debug("find info for: " + path); |
---|
109 | try { |
---|
110 | Tree tree = path.getTree(); |
---|
111 | assert tree.isActive(); |
---|
112 | final FramsClass framsClass = getInfo(path); |
---|
113 | if (framsClass != null) { |
---|
114 | future.pass(framsClass); |
---|
115 | return; |
---|
116 | } |
---|
117 | tree.info(path, future); |
---|
118 | } catch (FramsticksException e) { |
---|
119 | future.handle(e); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | public static @Nonnull AccessInterface bindAccess(Tree tree, String path) { |
---|
126 | log.debug("bind access for textual: " + path + " in " + tree); |
---|
127 | return bindAccess(Path.to(tree, path)); |
---|
128 | } |
---|
129 | |
---|
130 | public static @Nonnull AccessInterface bindAccess(Node node) { |
---|
131 | Tree tree = node.getTree(); |
---|
132 | assert tree.isActive(); |
---|
133 | assert node.getObject() != null; |
---|
134 | |
---|
135 | try { |
---|
136 | return tree.prepareAccess(node.getParam()).select(node.getObject()); |
---|
137 | } catch (FramsticksException e) { |
---|
138 | throw new FramsticksException().msg("failed to prepare access for param").arg("param", node.getParam()).cause(e); |
---|
139 | // log.error("failed to bind access for " + node.getParam() + ": " + e); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | public static @Nonnull AccessInterface bindAccess(Path path) { |
---|
144 | assert path.getTree().isActive(); |
---|
145 | path.assureResolved(); |
---|
146 | log.debug("bind access for: " + path); |
---|
147 | return bindAccess(path.getTop()); |
---|
148 | } |
---|
149 | |
---|
150 | public static void set(final Path path, final PrimitiveParam<?> param, final Object value, final Future<Integer> future) { |
---|
151 | final Tree tree = path.getTree(); |
---|
152 | |
---|
153 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
154 | @Override |
---|
155 | protected void runAt() { |
---|
156 | tree.set(path, param, value, future); |
---|
157 | } |
---|
158 | }); |
---|
159 | } |
---|
160 | |
---|
161 | public static void call(final Path path, final ProcedureParam param, final Object[] arguments, final Future<Object> future) { |
---|
162 | final Tree tree = path.getTree(); |
---|
163 | |
---|
164 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
165 | @Override |
---|
166 | protected void runAt() { |
---|
167 | tree.call(path, param, arguments, future); |
---|
168 | } |
---|
169 | }); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | /** This might not be correct. */ |
---|
174 | public static void tryGet(final Tree tree, final String targetPath, final Future<Path> future) { |
---|
175 | log.debug("resolve textual: " + targetPath + " for " + tree); |
---|
176 | dispatchIfNotActive(tree, new RunAt<Tree>(future) { |
---|
177 | |
---|
178 | @Override |
---|
179 | protected void runAt() { |
---|
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) { |
---|
187 | @Override |
---|
188 | protected void result(Path result) { |
---|
189 | tryGet(tree, targetPath, future); |
---|
190 | } |
---|
191 | }); |
---|
192 | } |
---|
193 | }); |
---|
194 | } |
---|
195 | |
---|
196 | public static FramsClass getInfoFromCache(Path path) { |
---|
197 | assert path.getTree().isActive(); |
---|
198 | return path.getTree().getInfoFromCache(path.getTop().getParam().getContainedTypeName()); |
---|
199 | } |
---|
200 | } |
---|