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

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

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 6.2 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.params.AccessInterface;
4import com.framsticks.params.CompositeParam;
5import com.framsticks.params.Param;
6import com.framsticks.util.FramsticksException;
7import com.framsticks.util.dispatching.Dispatching;
8
9import java.util.Iterator;
10import java.util.LinkedList;
11import java.util.List;
12
13import javax.annotation.concurrent.Immutable;
14
15import org.apache.commons.collections.ListUtils;
16
17/**
18 * @author Piotr Sniegowski
19 */
20@Immutable
21public final class Path {
22        // private final static Logger log = Logger.getLogger(Path.class.getName());
23
24        final Instance instance;
25        final String textual;
26        final LinkedList<Node> nodes;
27
28        protected static Object getKnownChild(Instance instance, AccessInterface access, CompositeParam param) {
29                Object child = access.get(param, Object.class);
30                if (child == null) {
31                        return null;
32                }
33                try {
34                        instance.registry.prepareAccess(param);
35                        return child;
36                } catch (FramsticksException e) {
37                }
38                return null;
39        }
40
41        /**
42         * @param instance
43         * @param textual
44         * @param nodes
45         */
46        Path(Instance instance, String textual, LinkedList<Node> nodes) {
47                this.instance = instance;
48                this.textual = textual;
49                this.nodes = nodes;
50        }
51
52        public Path appendNode(Node node) {
53                assert isResolved();
54                return new PathBuilder().instance(instance).textual(textual + ((size() == 1) ? "" : "/") + node.getParam().getId()).add(nodes).add(node).finish();
55        }
56
57        public Path appendParam(CompositeParam param) {
58                assert isResolved();
59                return appendNode(new Node(param, null));
60        }
61
62        public static class PathBuilder {
63
64                Instance instance;
65                String textual;
66                final LinkedList<Node> nodes = new LinkedList<Node>();
67
68                public Path finish() {
69                        assert instance != null;
70                        assert textual != null;
71                        return new Path(instance, textual, nodes);
72                }
73
74                public PathBuilder()
75                {
76                }
77
78                public PathBuilder instance(Instance instance) {
79                        this.instance = instance;
80                        return this;
81                }
82
83                public PathBuilder textual(String textual) {
84                        this.textual = textual;
85                        return this;
86                }
87
88                public PathBuilder add(List<Node> nodes) {
89                        this.nodes.addAll(nodes);
90                        return this;
91                }
92
93                public PathBuilder add(Node node) {
94                        this.nodes.add(node);
95                        return this;
96                }
97
98                public PathBuilder setLast(Object object) {
99                        Node n = nodes.pollLast();
100                        nodes.add(new Node(n.getParam(), object));
101                        return this;
102                }
103
104                public PathBuilder buildUpTo(List<Node> nodes, Node node) {
105                        StringBuilder b = new StringBuilder();
106                        boolean add = false;
107                        for (Node n : nodes) {
108                                this.nodes.add(n);
109                                if (add) {
110                                        b.append("/").append(n.getParam().getId());
111                                }
112                                add = true;
113                                if (n == node) {
114                                        break;
115                                }
116                        }
117                        this.textual = (nodes.size() == 1) ? "/" : b.toString();
118                        return this;
119                }
120
121                public PathBuilder resolve(Instance instance, String textual) {
122
123                        assert nodes.isEmpty();
124                        assert instance.isActive();
125                        this.instance = instance;
126
127                        nodes.add(instance.getRoot());
128                        Node current = instance.getRoot();
129
130                        StringBuilder b = new StringBuilder();
131                        Iterator<String> i = Instance.splitPath(textual);
132                        while (i.hasNext() && current.getObject() != null) {
133                                AccessInterface access = instance.registry.prepareAccess(current.getParam());
134                                if (access == null) {
135                                        break;
136                                }
137                                String e = i.next();
138                                Param p = access.getParam(e);
139                                if (!(p instanceof CompositeParam)) {
140                                        //entries.add(new Entry());
141                                        break;
142                                }
143                                CompositeParam c = (CompositeParam)p;
144                                b.append("/").append(e);
145                                access.select(current.getObject());
146                                current = new Node(c, getKnownChild(instance, access, c));
147                                nodes.add(current);
148                        }
149                        this.textual = (nodes.size() == 1) ? "/" : b.toString();
150
151                        return this;
152                }
153        }
154
155        public static PathBuilder build() {
156                return new PathBuilder();
157        }
158
159        public Path appendResolution(Object object) {
160                assert !isResolved();
161                Path result = new PathBuilder().textual(textual).instance(instance).add(nodes).setLast(object).finish();
162                assert size() == result.size();
163                return result;
164        }
165
166        public final Object getTopObject() {
167                return getTop().getObject();
168        }
169
170        public final Node getTop() {
171                return nodes.getLast();
172        }
173
174        public final Node getUnder() {
175                assert nodes.size() >= 2;
176                return nodes.get(nodes.size() - 2);
177        }
178
179        public final String getTextual() {
180                return textual;
181        }
182
183        public String toString() {
184                return instance + textual + (!isResolved() ? "!" : "");
185        }
186
187        public final int size() {
188                assert Dispatching.isThreadSafe();
189                return nodes.size();
190        }
191
192        public final boolean isResolved() {
193                assert Dispatching.isThreadSafe();
194                return getTop().getObject() != null;
195        }
196
197        public final boolean isResolved(String textual) {
198                assert Dispatching.isThreadSafe();
199                return isTheSame(textual) && isResolved();
200        }
201
202        public final boolean isTheSame(String textual) {
203                assert Dispatching.isThreadSafe();
204                return this.textual.equals(textual);
205        }
206
207        public final Instance getInstance() {
208                assert Dispatching.isThreadSafe();
209                return instance;
210        }
211
212
213        /** Attach resolution at end, if available.
214         *
215         * @return Modified path, if resolution was available, this otherwise.
216         */
217        public Path tryFindResolution() {
218                assert instance.isActive();
219                assert !isResolved();
220                if (size() == 1) {
221                        return Path.build().resolve(instance, "/").finish();//appendResolution(instance.root.object);
222                }
223                Object child = getKnownChild(instance, instance.bindAccess(getUnder()), getTop().getParam());
224                if (child == null) {
225                        return this;
226                }
227                return appendResolution(child);
228        }
229
230        public boolean matches(Path p) {
231                assert Dispatching.isThreadSafe();
232                assert instance == p.instance;
233                Iterator<Node> a = nodes.iterator();
234                Iterator<Node> b = p.nodes.iterator();
235                while (a.hasNext() && b.hasNext()) {
236                        Node an = a.next();
237                        Node bn = b.next();
238                        if (an.object != bn.object) {
239                                return false;
240                        }
241                }
242                return a.hasNext() == b.hasNext();
243        }
244
245        public String getLastElement() {
246                return getTop().getParam().getId();
247        }
248
249        public final boolean isOwner(Instance instance) {
250                return this.instance == instance;
251        }
252
253        // public void setInstance(Instance instance) {
254        //      this.instance = instance;
255        // }
256
257        @SuppressWarnings("unchecked")
258        public
259        List<Node> getNodes() {
260                return ListUtils.unmodifiableList(nodes);
261        }
262
263        public void assureResolved() {
264                if (!isResolved()) {
265                        throw new FramsticksException().msg("path is not resolved").arg("path", this);
266                }
267        }
268}
269
Note: See TracBrowser for help on using the repository browser.