source code formatting.
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / ComponentGraph.java
1 /*
2  * Copyright 2008 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.graph.component;
17
18 import org.wamblee.general.Pair;
19
20 import org.wamblee.system.core.Component;
21 import org.wamblee.system.core.ProvidedInterface;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.graph.CompositeEdgeFilter;
24 import org.wamblee.system.graph.DefaultEdge;
25 import org.wamblee.system.graph.Edge;
26 import org.wamblee.system.graph.Graph;
27 import org.wamblee.system.graph.Node;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32
33 /**
34  * Represents a component graph and provides the bridge from the  component
35  * model to a graph model. The graph model is easier to work with to implement
36  * specific actions and validations than  the component model.
37  */
38 public class ComponentGraph extends Graph {
39     /**
40      * DOCUMENT ME!
41      */
42     private boolean isLinked;
43
44     /**
45      * DOCUMENT ME!
46      */
47     private CompositeEdgeFilter edgeFilter;
48
49 /**
50      * Constructs an empty component graph. 
51      */
52     public ComponentGraph() {
53         isLinked       = false;
54         edgeFilter     = new CompositeEdgeFilter();
55     }
56
57     /**
58      * Adds an externally required interface of a container.   This
59      * should be called before any components of the container are added.
60      *
61      * @param aComponent Component requiring the interface.
62      * @param aInterface Required interface.
63      */
64     public void addRequiredInterface(Component aComponent,
65         RequiredInterface aInterface) {
66         addNode(new ExternalRequiredInterfaceNode(aComponent, aInterface));
67     }
68
69     /**
70      * Adds an externally provided interface of a container. This
71      * should be called after all components of the container have been added.
72      *
73      * @param aComponent Component providing the interface.
74      * @param aInterface Provided interface.
75      */
76     public void addProvidedInterface(Component aComponent,
77         ProvidedInterface aInterface) {
78         addNode(new ExternalProvidedInterfaceNode(aComponent, aInterface));
79     }
80
81     /**
82      * Validates the component graph.
83      */
84     public void validate() {
85         extend(new RequiredProvidedEdgeFactory());
86         applyFilter(edgeFilter);
87         accept(new CheckRequiredProvidedMultiplicityVisitor(this));
88         accept(new CheckExternallyRequiredVisitor(this));
89         accept(new CheckExternallyProvidedVisitor(this));
90         accept(new CheckStartupDependenciesVisitor(this));
91     }
92
93     /**
94      * Links provided and required interfaces together in the component
95      * model based on the graph model.
96      */
97     public void link() {
98         if (isLinked) {
99             return;
100         }
101
102         accept(new LinkVisitor());
103         isLinked = true;
104     }
105
106     /**
107      * Finds a list of mappings of external provided interface to
108      * internal provided interface.
109      *
110      * @return List of pairs of external to internal interface.
111      */
112     public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
113         List<Pair<ProvidedInterface, ProvidedInterface>> result = new ArrayList<Pair<ProvidedInterface, ProvidedInterface>>();
114
115         for (Edge edge : getEdges()) {
116             if (edge.getFrom() instanceof ExternalProvidedInterfaceNode
117                     && edge.getTo() instanceof ProvidedInterfaceNode) {
118                 result.add(new Pair<ProvidedInterface, ProvidedInterface>(
119                         ((ExternalProvidedInterfaceNode) edge.getFrom())
120                         .getProvided(),
121                         ((ProvidedInterfaceNode) edge.getTo()).getProvided()));
122             }
123         }
124
125         return result;
126     }
127
128     /**
129      * Adds a component by adding required interfaces, components, and
130      * provided interfaces.
131      *
132      * @param aComponent Component to add.
133      */
134     public void addComponent(Component<?> aComponent) {
135         // Add required interfaces. 
136         Node       compNode      = new ComponentNode(aComponent);
137         List<Node> requiredNodes = new ArrayList<Node>();
138
139         for (RequiredInterface required : aComponent.getRequiredInterfaces()) {
140             Node reqNode = new RequiredInterfaceNode(aComponent, required);
141             addNode(reqNode);
142             requiredNodes.add(reqNode);
143         }
144
145         // Add the component
146         addNode(compNode);
147
148         // Edges from component to required interface. 
149         for (Node reqNode : requiredNodes) {
150             addEdge(new DefaultEdge(compNode, reqNode));
151         }
152
153         // Add provided interfaces
154         List<Node> providedNodes = new ArrayList<Node>();
155
156         for (ProvidedInterface provided : aComponent.getProvidedInterfaces()) {
157             Node provNode = new ProvidedInterfaceNode(aComponent, provided);
158             addNode(provNode);
159             providedNodes.add(provNode);
160         }
161
162         // Edges from provided interface to component
163         for (Node provNode : providedNodes) {
164             addEdge(new DefaultEdge(provNode, compNode));
165         }
166     }
167
168     /**
169      * DOCUMENT ME!
170      *
171      * @param aEdgeFilter DOCUMENT ME!
172      */
173     public void addEdgeFilter(CompositeEdgeFilter aEdgeFilter) {
174         edgeFilter.add(aEdgeFilter);
175     }
176 }