2
* This file is part of Zaluum
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.
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.
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/>.
17
* Copyright 2008,2009 Frederic Perez Ordeig
19
package org.zaluum.cmodel;
21
import java.util.Iterator;
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;
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;
37
public class CompleteModelImpl implements CompleteModel {
38
private Set<Box> allBoxes;
39
private Set<Box> leafBoxes;
40
private final ComposedBox root;
42
public CompleteModelImpl(ComposedBox root) {
43
Preconditions.checkNotNull(root);
47
allBoxes = Sets.newHashSet();
48
leafBoxes = Sets.newHashSet();
49
root.acceptVisitor(new BoxVisitor() {
51
public void visitLeaf(Box b) {
57
public void visitComposed(ComposedBox b) {
64
public Set<Box> getAllBoxes() {
71
public Set<Box> getLeafBoxes() {
78
public ComposedBox getRootBox() {
83
public void addObserver(Observer o) { // nothing as it's immutable
87
public void removeObserver(Observer o) { // nothing as it's immutable
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);
97
if (array.length==1){ // root port
100
}else if (array.length == 2){
102
boxPathName = array[0];
105
Iterable<String> boxPath = Splitter.on('/').omitEmptyStrings()
106
.trimResults().split(boxPathName);
107
Box currentBox = getRootBox();
108
Iterator<String> it = boxPath.iterator();
111
return currentBox.getPortByName(portName);
113
String childName = it.next();
114
if (currentBox instanceof ComposedBox) {
115
currentBox = ((ComposedBox) currentBox).getChild(childName);
116
if (currentBox == null)
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()));
134
extra.addAll(myWires);
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()));
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());
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());
177
r.addAll(getPortBackwardTransitiveClosure(wire.getSource()));
178
} else if (!p.getInternalTargetWires().isEmpty()) {
179
Wire wire = Iterables.getOnlyElement(p.getInternalTargetWires());
181
r.addAll(getPortBackwardTransitiveClosure(wire.getSource()));
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);
193
for (Wire w : port.getExternalSourceWires()) {
194
finalPorts.addAll(getLeafPortsFromWire(w));
196
for (Wire w : port.getInternalSourceWires()) {
197
finalPorts.addAll(getLeafPortsFromWire(w));
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));
213
if (port.getInternalSourceWires().isEmpty())
214
return ImmutableSet.of(port);
216
Set<Port> result = Sets.newHashSet();
217
for (Wire wire : port.getInternalSourceWires()) {
218
result.addAll(getLeafPortsFromWire(wire));
b'\\ No newline at end of file'