~fredericp/zaluum/rt

« back to all changes in this revision

Viewing changes to org.zaluum.cmodel/src/org/zaluum/cmodel/CompleteModelImpl.java

  • Committer: Frederic Perez Ordeig
  • Date: 2010-04-07 07:55:38 UTC
  • mfrom: (308.1.63 wip-scala)
  • Revision ID: frederic@zaluum.com-20100407075538-muaneuoz134fqu5o
merged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *  This file is part of Zaluum
3
 
 *   
4
 
 *  Zaluum is free software: you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU Lesser General Public License as published by
6
 
 *  the Free Software Foundation, either version 3 of the License, or
7
 
 *  (at your option) any later version.
8
 
 *
9
 
 *  Zaluum is distributed in the hope that it will be useful,
10
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *  GNU Lesser General Public License for more details.
13
 
 *
14
 
 *  You should have received a copy of the GNU Lesser General Public License
15
 
 *  along with Zaluum.  If not, see <http://www.gnu.org/licenses/>.
16
 
 *  
17
 
 *  Copyright   2008,2009       Frederic Perez Ordeig
18
 
 */
19
 
package org.zaluum.cmodel;
20
 
 
21
 
import java.util.Iterator;
22
 
import java.util.Set;
23
 
 
24
 
import org.zaluum.model.Box;
25
 
import org.zaluum.model.BoxVisitor;
26
 
import org.zaluum.model.ComposedBox;
27
 
import org.zaluum.model.Port;
28
 
import org.zaluum.model.Wire;
29
 
import org.zaluum.util.Observer;
30
 
 
31
 
import com.google.common.base.Preconditions;
32
 
import com.google.common.base.Splitter;
33
 
import com.google.common.collect.ImmutableSet;
34
 
import com.google.common.collect.Iterables;
35
 
import com.google.common.collect.Sets;
36
 
 
37
 
public class CompleteModelImpl implements CompleteModel {
38
 
        private Set<Box> allBoxes;
39
 
        private Set<Box> leafBoxes;
40
 
        private final ComposedBox root;
41
 
 
42
 
        public CompleteModelImpl(ComposedBox root) {
43
 
                Preconditions.checkNotNull(root);
44
 
                this.root = root;
45
 
        }
46
 
        private void calc(){
47
 
                allBoxes = Sets.newHashSet();
48
 
                leafBoxes = Sets.newHashSet();
49
 
                root.acceptVisitor(new BoxVisitor() {
50
 
                        @Override
51
 
                        public void visitLeaf(Box b) {
52
 
                                leafBoxes.add(b);
53
 
                                allBoxes.add(b);
54
 
                        }
55
 
                        
56
 
                        @Override
57
 
                        public void visitComposed(ComposedBox b) {
58
 
                                allBoxes.add(b);
59
 
                        }
60
 
                });
61
 
        
62
 
        }
63
 
        @Override
64
 
        public Set<Box> getAllBoxes() {
65
 
                if (allBoxes == null)
66
 
                        calc();
67
 
                return allBoxes;
68
 
        }
69
 
 
70
 
        @Override
71
 
        public Set<Box> getLeafBoxes() {
72
 
                if (leafBoxes ==null)
73
 
                        calc();
74
 
                return leafBoxes;
75
 
        }
76
 
 
77
 
        @Override
78
 
        public ComposedBox getRootBox() {
79
 
                return root;
80
 
        }
81
 
 
82
 
        @Override
83
 
        public void addObserver(Observer o) { // nothing as it's immutable
84
 
        }
85
 
 
86
 
        @Override
87
 
        public void removeObserver(Observer o) { // nothing as it's immutable
88
 
        }
89
 
 
90
 
        @Override
91
 
        public Port getPortByFqName(String fqName) {
92
 
                Iterable<String> split = Splitter.on('#').omitEmptyStrings()
93
 
                                .trimResults().split(fqName);
94
 
                String[] array = Iterables.toArray(split, String.class);
95
 
                String boxPathName;
96
 
                String portName;
97
 
                if (array.length==1){ // root port
98
 
                        portName = array[0];
99
 
                        boxPathName = "";
100
 
                }else if (array.length == 2){
101
 
                        portName = array[1];
102
 
                        boxPathName = array[0];
103
 
                }else 
104
 
                        return null;
105
 
                Iterable<String> boxPath = Splitter.on('/').omitEmptyStrings()
106
 
                                .trimResults().split(boxPathName);
107
 
                Box currentBox = getRootBox();
108
 
                Iterator<String> it = boxPath.iterator();
109
 
                while (true) {
110
 
                        if (!it.hasNext()) {
111
 
                                return currentBox.getPortByName(portName);
112
 
                        } else {
113
 
                                String childName = it.next();
114
 
                                if (currentBox instanceof ComposedBox) {
115
 
                                        currentBox = ((ComposedBox) currentBox).getChild(childName);
116
 
                                        if (currentBox == null)
117
 
                                                return null;
118
 
                                } else {
119
 
                                        return null;
120
 
                                }
121
 
                        }
122
 
                }
123
 
        }
124
 
 
125
 
        @Override
126
 
        public Set<Wire> getPortWiresForward(Port p) {
127
 
                Set<Wire> myWires = Sets.newHashSet();
128
 
                myWires.addAll(p.getExternalSourceWires());
129
 
                myWires.addAll(p.getInternalSourceWires());
130
 
                Set<Wire> extra = Sets.newHashSet();
131
 
                for (Wire w : myWires) {
132
 
                        extra.addAll(getPortWiresForward(w.getTarget()));
133
 
                }
134
 
                extra.addAll(myWires);
135
 
                return extra;
136
 
        }
137
 
 
138
 
        /**
139
 
         * @deprecated
140
 
         */
141
 
        public Port getWireSource(Wire w) {
142
 
                Port source = w.getSource();
143
 
                if (!source.getExternalTargetWires().isEmpty()) {
144
 
                        return getWireSource(Iterables.getOnlyElement(source
145
 
                                        .getExternalTargetWires()));
146
 
                } else if (!source.getInternalTargetWires().isEmpty()) {
147
 
                        return getWireSource(Iterables.getOnlyElement(source
148
 
                                        .getInternalTargetWires()));
149
 
                }
150
 
                return source;
151
 
        }
152
 
 
153
 
        /**
154
 
         * @deprecated
155
 
         */
156
 
        @Override
157
 
        public Port getInputPortValueSource(Port p) {
158
 
                if (!p.getExternalTargetWires().isEmpty())
159
 
                        return getInputPortValueSource(Iterables.getOnlyElement(
160
 
                                        p.getExternalTargetWires()).getSource());
161
 
                if (!p.getInternalTargetWires().isEmpty()) {
162
 
                        return getInputPortValueSource(Iterables.getOnlyElement(
163
 
                                        p.getInternalTargetWires()).getSource());
164
 
                }
165
 
                if (p.isOut())
166
 
                        return p;
167
 
                else
168
 
                        return null;
169
 
        }
170
 
 
171
 
        @Override
172
 
        public Set<Wire> getPortBackwardTransitiveClosure(Port p) {
173
 
                Set<Wire> r = Sets.newHashSet();
174
 
                if (!p.getExternalTargetWires().isEmpty()) {
175
 
                        Wire wire = Iterables.getOnlyElement(p.getExternalTargetWires());
176
 
                        r.add(wire);
177
 
                        r.addAll(getPortBackwardTransitiveClosure(wire.getSource()));
178
 
                } else if (!p.getInternalTargetWires().isEmpty()) {
179
 
                        Wire wire = Iterables.getOnlyElement(p.getInternalTargetWires());
180
 
                        r.add(wire);
181
 
                        r.addAll(getPortBackwardTransitiveClosure(wire.getSource()));
182
 
                }
183
 
                return r;
184
 
        }
185
 
 
186
 
        @Override
187
 
        public Set<Port> getLeafPortsFromPort(Port port) {
188
 
                Set<Port> finalPorts = Sets.newHashSet();
189
 
                if (port.isIn() && port.getExternalSourceWires().isEmpty()
190
 
                                && port.getInternalSourceWires().isEmpty())
191
 
                        finalPorts.add(port);
192
 
                else {
193
 
                        for (Wire w : port.getExternalSourceWires()) {
194
 
                                finalPorts.addAll(getLeafPortsFromWire(w));
195
 
                        }
196
 
                        for (Wire w : port.getInternalSourceWires()) {
197
 
                                finalPorts.addAll(getLeafPortsFromWire(w));
198
 
                        }
199
 
                }
200
 
                return finalPorts;
201
 
        }
202
 
 
203
 
        @Override
204
 
        public Set<Port> getLeafPortsFromWire(Wire startWire) {
205
 
                Port port = startWire.getTarget();
206
 
                if (startWire.isTargetInternal()) {
207
 
                        Set<Port> result = Sets.newHashSet();
208
 
                        for (Wire w : port.getExternalSourceWires()) {
209
 
                                result.addAll(getLeafPortsFromWire(w));
210
 
                        }
211
 
                        return result;
212
 
                } else {
213
 
                        if (port.getInternalSourceWires().isEmpty())
214
 
                                return ImmutableSet.of(port);
215
 
                        else {
216
 
                                Set<Port> result = Sets.newHashSet();
217
 
                                for (Wire wire : port.getInternalSourceWires()) {
218
 
                                        result.addAll(getLeafPortsFromWire(wire));
219
 
                                }
220
 
                                return result;
221
 
                        }
222
 
                }
223
 
        }
224
 
 
225
 
}
 
 
b'\\ No newline at end of file'