source: java/main/src/main/java/com/framsticks/core/Path.java @ 100

Last change on this file since 100 was 100, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

File size: 8.8 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.params.Access;
4import com.framsticks.params.CompositeParam;
5import com.framsticks.params.Param;
6import com.framsticks.util.FramsticksException;
7import com.framsticks.util.dispatching.Dispatching;
8import com.framsticks.util.dispatching.ExceptionResultHandler;
9import com.framsticks.util.dispatching.IgnoreExceptionHandler;
10import com.framsticks.util.lang.Pair;
11
12import java.util.Iterator;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.regex.Pattern;
16
17import javax.annotation.Nonnull;
18import javax.annotation.concurrent.Immutable;
19
20import org.apache.commons.collections.ListUtils;
21
22/**
23 * @author Piotr Sniegowski
24 */
25@Immutable
26public final class Path {
27        // private final static Logger log = LogManager.getLogger(Path.class.getName());
28
29        final Tree tree;
30        final String textual;
31        final LinkedList<Node> nodes;
32
33        protected static Object getKnownChild(Tree tree, Access access, CompositeParam param, ExceptionResultHandler handler) {
34                Object child = access.get(param, Object.class);
35                if (child == null) {
36                        return null;
37                }
38                try {
39                        tree.prepareAccess(param);
40                        tree.putSideNote(child, CompositeParam.class, param);
41                        return child;
42                } catch (FramsticksException e) {
43                        handler.handle(e);
44                }
45                return null;
46        }
47
48        /**
49         * @param tree
50         * @param textual
51         * @param nodes
52         */
53        Path(Tree tree, String textual, LinkedList<Node> nodes) {
54                this.tree = tree;
55                this.textual = textual;
56                this.nodes = nodes;
57        }
58
59        public Path appendNode(Node node) {
60                assert isResolved();
61                return new PathBuilder().tree(tree).textual(textual + ((size() == 1) ? "" : "/") + node.getParam().getId()).add(nodes).add(node).finish();
62        }
63
64        public Path appendParam(CompositeParam param) {
65                assert isResolved();
66                return appendNode(new Node(tree, param, null));
67        }
68
69        public static class PathBuilder {
70
71                Tree tree;
72                String textual;
73                final LinkedList<Node> nodes = new LinkedList<Node>();
74
75                public Path finish() {
76                        assert tree != null;
77                        assert textual != null;
78                        return new Path(tree, textual, nodes);
79                }
80
81                public PathBuilder()
82                {
83                }
84
85                public PathBuilder tree(Tree tree) {
86                        this.tree = tree;
87                        return this;
88                }
89
90                public PathBuilder textual(String textual) {
91                        this.textual = textual;
92                        return this;
93                }
94
95                public PathBuilder add(List<Node> nodes) {
96                        this.nodes.addAll(nodes);
97                        return this;
98                }
99
100                public PathBuilder add(Node node) {
101                        this.nodes.add(node);
102                        return this;
103                }
104
105                public PathBuilder setLast(Object object) {
106                        Node n = nodes.pollLast();
107                        nodes.add(new Node(n.getTree(), n.getParam(), object));
108                        return this;
109                }
110
111                public PathBuilder buildUpTo(List<Node> nodes, Node node) {
112                        StringBuilder b = new StringBuilder();
113                        boolean add = false;
114                        for (Node n : nodes) {
115                                this.nodes.add(n);
116                                if (add) {
117                                        b.append("/").append(n.getParam().getId());
118                                }
119                                add = true;
120                                if (n == node) {
121                                        break;
122                                }
123                        }
124                        this.textual = (nodes.size() == 1) ? "/" : b.toString();
125                        return this;
126                }
127
128                public static Iterator<String> splitPath(String path) {
129                        List<String> list = new LinkedList<String>();
130                        for (String s : path.split("/")) {
131                                if (!s.isEmpty()) {
132                                        list.add(s);
133                                }
134                        }
135                        return list.iterator();
136                }
137
138                public PathBuilder resolve(@Nonnull Tree tree, String textual) {
139                        return resolve(tree, textual, IgnoreExceptionHandler.getInstance());
140                }
141
142                public PathBuilder resolve(@Nonnull Tree tree, String textual, ExceptionResultHandler handler) {
143
144                        assert nodes.isEmpty();
145                        assert tree.isActive();
146                        this.tree = tree;
147
148                        Node current = tree.getAssignedRoot();
149                        nodes.add(current);
150
151                        StringBuilder b = new StringBuilder();
152                        Iterator<String> i = splitPath(textual);
153                        while (i.hasNext() && current.getObject() != null) {
154                                Access access = TreeOperations.bindAccess(current);// tree.prepareAccess(current.getParam());
155                                String e = i.next();
156                                Param p = access.getParam(e);
157                                if (p == null) {
158                                        break;
159                                }
160                                if (!(p instanceof CompositeParam)) {
161                                        throw new FramsticksException().msg("param is not a composite").arg("param", p).arg("tree", tree).arg("textual", textual).arg("access", access);
162                                }
163                                CompositeParam c = (CompositeParam) p;
164                                b.append("/").append(e);
165                                access.select(current.getObject());
166                                tree.putSideNote(current.getObject(), CompositeParam.class, current.getParam());
167                                current = new Node(current.getTree(), c, getKnownChild(tree, access, c, handler));
168                                nodes.add(current);
169                        }
170                        this.textual = (nodes.size() == 1) ? "/" : b.toString();
171
172                        return this;
173                }
174        }
175
176        public static PathBuilder build() {
177                return new PathBuilder();
178        }
179
180        public Path appendResolution(Object object) {
181                assert !isResolved();
182                Path result = new PathBuilder().textual(textual).tree(tree).add(nodes).setLast(object).finish();
183                assert size() == result.size();
184                return result;
185        }
186
187        public final Object getTopObject() {
188                return getTop().getObject();
189        }
190
191        public final Node getTop() {
192                return nodes.getLast();
193        }
194
195        public final Node getUnder() {
196                assert nodes.size() >= 2;
197                return nodes.get(nodes.size() - 2);
198        }
199
200        public final String getTextual() {
201                return textual;
202        }
203
204        public String toString() {
205                return tree + textual + (!isResolved() ? "!" : "");
206        }
207
208        public String getFullTextual() {
209                return tree.getName() + textual;
210        }
211
212        public final int size() {
213                assert Dispatching.isThreadSafe();
214                return nodes.size();
215        }
216
217        public final boolean isResolved() {
218                assert Dispatching.isThreadSafe();
219                return getTop().getObject() != null;
220        }
221
222        public final boolean isResolved(String textual) {
223                assert Dispatching.isThreadSafe();
224                return isTheSame(textual) && isResolved();
225        }
226
227        public final boolean isTheSame(String textual) {
228                assert Dispatching.isThreadSafe();
229                return this.textual.equals(textual);
230        }
231
232        public final boolean isTheSameTextually(Path path) {
233                assert Dispatching.isThreadSafe();
234                return (tree == path.getTree()) && textual.equals(path.getTextual());
235        }
236
237        public final @Nonnull Tree getTree() {
238                assert Dispatching.isThreadSafe();
239                return tree;
240        }
241
242        public Path tryResolveIfNeeded() {
243                if (isResolved()) {
244                        return this;
245                }
246                return tryFindResolution();
247        }
248
249        /** Attach resolution at end, if available.
250         *
251         * @return Modified path, if resolution was available, this otherwise.
252         */
253        public Path tryFindResolution() {
254                assert tree.isActive();
255                assert !isResolved();
256                if (size() == 1) {
257                        return Path.build().resolve(tree, "/").finish();
258                }
259                Object child = getKnownChild(tree, TreeOperations.bindAccess(getUnder()), getTop().getParam(), IgnoreExceptionHandler.getInstance());
260                if (child == null) {
261                        return this;
262                }
263                return appendResolution(child);
264        }
265
266
267        public String getLastElement() {
268                return getTop().getParam().getId();
269        }
270
271        public final boolean isOwner(Tree tree) {
272                return this.tree == tree;
273        }
274
275        @SuppressWarnings("unchecked")
276        public
277        List<Node> getNodes() {
278                return ListUtils.unmodifiableList(nodes);
279        }
280
281        public Path assureResolved() {
282                if (!isResolved()) {
283                        throw new FramsticksException().msg("path is not resolved").arg("path", this);
284                }
285                return this;
286        }
287
288        public static Path tryTo(@Nonnull Tree tree, String textual) {
289                return Path.build().resolve(tree, textual).finish();
290        }
291
292        public static Path to(@Nonnull Tree tree, String textual) {
293                Path path = tryTo(tree, textual);
294                if (path.getTextual().equals(textual)) {
295                        return path;
296                }
297                throw new FramsticksException().msg("failed to create path").arg("textual", textual).arg("result", path).arg("tree", tree);
298        }
299
300        public boolean isTheSameObjects(Path path) {
301                if (tree != path.getTree()) {
302                        return false;
303                }
304                if (size() != path.size()) {
305                        return false;
306                }
307                if (!getTextual().equals(path.getTextual())) {
308                        return false;
309                }
310                Iterator<Node> a = nodes.iterator();
311                Iterator<Node> b = path.getNodes().iterator();
312                while (a.hasNext()) {
313                        assert b.hasNext();
314                        if (a.next() != b.next()) {
315                                return false;
316                        }
317                }
318                return true;
319        }
320
321        public static final Pattern pathPattern = Pattern.compile("(\\/)|((\\/[^/]+)+)");
322
323        public static boolean isValidString(String path) {
324                return pathPattern.matcher(path).matches();
325        }
326
327        public static String appendString(String path, String element) {
328                if (path.equals("/")) {
329                        return path + element;
330                }
331                return path + "/" + element;
332        }
333
334        public static Pair<String, String> removeLastElement(String path) {
335                assert isValidString(path);
336                if (path.equals("/")) {
337                        throw new FramsticksException().msg("cannot remove last element from root path");
338                }
339                int index = path.lastIndexOf('/');
340                assert index != -1;
341                if (index == 0) {
342                        return new Pair<String, String>("/", path.substring(1));
343                }
344                return new Pair<String, String>(path.substring(0, index), path.substring(index + 1));
345        }
346
347        public static String validateString(String path) {
348                if (!isValidString(path)) {
349                        throw new FramsticksException().msg("path string validation failured").arg("path", path);
350                }
351                return path;
352        }
353
354
355        // public boolean isEmpty() {
356        //      return nodes.isEmpty();
357        // }
358}
359
Note: See TracBrowser for help on using the repository browser.