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 java.util.ArrayList;
19 import java.util.List;
21 import org.wamblee.general.Pair;
22 import org.wamblee.system.core.Component;
23 import org.wamblee.system.core.ProvidedInterface;
24 import org.wamblee.system.core.RequiredInterface;
25 import org.wamblee.system.graph.CompositeEdgeFilter;
26 import org.wamblee.system.graph.DefaultEdge;
27 import org.wamblee.system.graph.Edge;
28 import org.wamblee.system.graph.Graph;
29 import org.wamblee.system.graph.Node;
32 * Represents a component graph and provides the bridge from the
33 * component model to a graph model. The graph model is easier
34 * to work with to implement specific actions and validations than
35 * the component model.
37 public class ComponentGraph extends Graph {
39 private boolean _isLinked;
40 private CompositeEdgeFilter _edgeFilter;
43 * Constructs an empty component graph.
45 public ComponentGraph() {
47 _edgeFilter = new CompositeEdgeFilter();
51 * Adds an externally required interface of a container.
52 * This should be called before any components of the container are
54 * @param aComponent Component requiring the interface.
55 * @param aInterface Required interface.
57 public void addRequiredInterface(Component aComponent, RequiredInterface aInterface) {
58 addNode(new ExternalRequiredInterfaceNode(aComponent, aInterface));
62 * Adds an externally provided interface of a container.
63 * This should be called after all components of the container have been added.
64 * @param aComponent Component providing the interface.
65 * @param aInterface Provided interface.
67 public void addProvidedInterface(Component aComponent, ProvidedInterface aInterface) {
68 addNode(new ExternalProvidedInterfaceNode(aComponent, aInterface));
72 * Validates the component graph.
74 public void validate() {
75 extend(new RequiredProvidedEdgeFactory());
76 applyFilter(_edgeFilter);
77 accept(new CheckRequiredProvidedMultiplicityVisitor(this));
78 accept(new CheckExternallyRequiredVisitor(this));
79 accept(new CheckExternallyProvidedVisitor(this));
80 accept(new CheckStartupDependenciesVisitor(this));
84 * Links provided and required interfaces together in the component
85 * model based on the graph model.
91 accept(new LinkVisitor());
96 * Finds a list of mappings of external provided interface to internal provided interface.
98 * @return List of pairs of external to internal interface.
100 public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
101 List<Pair<ProvidedInterface, ProvidedInterface>> result =
102 new ArrayList<Pair<ProvidedInterface,ProvidedInterface>>();
103 for (Edge edge: getEdges()) {
104 if ( edge.getFrom() instanceof ExternalProvidedInterfaceNode &&
105 edge.getTo() instanceof ProvidedInterfaceNode ) {
106 result.add(new Pair<ProvidedInterface,ProvidedInterface>(
107 ((ExternalProvidedInterfaceNode)edge.getFrom()).getProvided(),
108 ((ProvidedInterfaceNode)edge.getTo()).getProvided()
116 * Adds a component by adding required interfaces, components, and
117 * provided interfaces.
118 * @param aComponent Component to add.
120 public void addComponent(Component<?> aComponent) {
121 // Add required interfaces.
122 Node compNode = new ComponentNode(aComponent);
123 List<Node> requiredNodes = new ArrayList<Node>();
124 for (RequiredInterface required: aComponent.getRequiredInterfaces()) {
125 Node reqNode = new RequiredInterfaceNode(aComponent, required);
127 requiredNodes.add(reqNode);
132 // Edges from component to required interface.
133 for (Node reqNode: requiredNodes) {
134 addEdge(new DefaultEdge(compNode, reqNode));
137 // Add provided interfaces
138 List<Node> providedNodes = new ArrayList<Node>();
139 for (ProvidedInterface provided: aComponent.getProvidedInterfaces()) {
140 Node provNode = new ProvidedInterfaceNode(aComponent, provided);
142 providedNodes.add(provNode);
145 // Edges from provided interface to component
146 for (Node provNode: providedNodes) {
147 addEdge(new DefaultEdge(provNode, compNode));
151 public void addEdgeFilter(CompositeEdgeFilter aEdgeFilter) {
152 _edgeFilter.add(aEdgeFilter);