2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.graph.component;
18 import org.wamblee.general.Pair;
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;
29 import java.util.ArrayList;
30 import java.util.List;
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.
38 public class ComponentGraph extends Graph {
42 private boolean isLinked;
47 private CompositeEdgeFilter edgeFilter;
50 * Constructs an empty component graph.
52 public ComponentGraph() {
54 edgeFilter = new CompositeEdgeFilter();
58 * Adds an externally required interface of a container. This
59 * should be called before any components of the container are added.
61 * @param aComponent Component requiring the interface.
62 * @param aInterface Required interface.
64 public void addRequiredInterface(Component aComponent,
65 RequiredInterface aInterface) {
66 addNode(new ExternalRequiredInterfaceNode(aComponent, aInterface));
70 * Adds an externally provided interface of a container. This
71 * should be called after all components of the container have been added.
73 * @param aComponent Component providing the interface.
74 * @param aInterface Provided interface.
76 public void addProvidedInterface(Component aComponent,
77 ProvidedInterface aInterface) {
78 addNode(new ExternalProvidedInterfaceNode(aComponent, aInterface));
82 * Validates the component graph.
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));
94 * Links provided and required interfaces together in the component
95 * model based on the graph model.
102 accept(new LinkVisitor());
107 * Finds a list of mappings of external provided interface to
108 * internal provided interface.
110 * @return List of pairs of external to internal interface.
112 public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
113 List<Pair<ProvidedInterface, ProvidedInterface>> result = new ArrayList<Pair<ProvidedInterface, ProvidedInterface>>();
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())
121 ((ProvidedInterfaceNode) edge.getTo()).getProvided()));
129 * Adds a component by adding required interfaces, components, and
130 * provided interfaces.
132 * @param aComponent Component to add.
134 public void addComponent(Component<?> aComponent) {
135 // Add required interfaces.
136 Node compNode = new ComponentNode(aComponent);
137 List<Node> requiredNodes = new ArrayList<Node>();
139 for (RequiredInterface required : aComponent.getRequiredInterfaces()) {
140 Node reqNode = new RequiredInterfaceNode(aComponent, required);
142 requiredNodes.add(reqNode);
148 // Edges from component to required interface.
149 for (Node reqNode : requiredNodes) {
150 addEdge(new DefaultEdge(compNode, reqNode));
153 // Add provided interfaces
154 List<Node> providedNodes = new ArrayList<Node>();
156 for (ProvidedInterface provided : aComponent.getProvidedInterfaces()) {
157 Node provNode = new ProvidedInterfaceNode(aComponent, provided);
159 providedNodes.add(provNode);
162 // Edges from provided interface to component
163 for (Node provNode : providedNodes) {
164 addEdge(new DefaultEdge(provNode, compNode));
171 * @param aEdgeFilter DOCUMENT ME!
173 public void addEdgeFilter(CompositeEdgeFilter aEdgeFilter) {
174 edgeFilter.add(aEdgeFilter);