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.container.CompositeInterfaceRestriction;
23 import org.wamblee.system.container.InterfaceRestriction;
24 import org.wamblee.system.core.Component;
25 import org.wamblee.system.core.ProvidedInterface;
26 import org.wamblee.system.core.RequiredInterface;
27 import org.wamblee.system.graph.DefaultEdge;
28 import org.wamblee.system.graph.Edge;
29 import org.wamblee.system.graph.Graph;
30 import org.wamblee.system.graph.Node;
33 // TODO info superfluous required interfaces
34 // TODO check optional external required but mandatory internal.
36 public class ComponentGraph extends Graph {
38 private CompositeInterfaceRestriction _restriction;
40 public ComponentGraph() {
41 _restriction = new CompositeInterfaceRestriction();
44 public void addRestriction(InterfaceRestriction aRestriction) {
45 _restriction.add(aRestriction);
48 public void addRequiredInterface(RequiredInterface aInterface) {
49 addNode(new ExternalRequiredInterfaceNode(aInterface));
52 public void addProvidedInterface(ProvidedInterface aInterface) {
53 addNode(new ExternalProvidedInterfaceNode(aInterface));
56 public void validate() {
57 extend(new RequiredProvidedEdgeFactory());
58 accept(new ApplyRestrictionsVisitor(this, _restriction));
59 accept(new CheckRequiredProvidedMultiplicityVisitor(this));
60 accept(new CheckExternallyRequiredVisitor());
61 accept(new CheckExternallyProvidedVisitor(this));
62 accept(new CheckStartupDependenciesVisitor(this));
66 accept(new LinkVisitor());
70 * Finds a list of mappings of external provided interface to internal provided interface.
72 * @return List of pairs of external to internal interface.
74 public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
75 List<Pair<ProvidedInterface, ProvidedInterface>> result =
76 new ArrayList<Pair<ProvidedInterface,ProvidedInterface>>();
77 for (Edge edge: getEdges()) {
78 if ( edge.getFrom() instanceof ExternalProvidedInterfaceNode &&
79 edge.getTo() instanceof ProvidedInterfaceNode ) {
80 result.add(new Pair<ProvidedInterface,ProvidedInterface>(
81 ((ExternalProvidedInterfaceNode)edge.getFrom()).getProvided(),
82 ((ProvidedInterfaceNode)edge.getTo()).getProvided()
89 public void addComponent(Component aComponent) {
90 // Add required interfaces.
91 Node compNode = new ComponentNode(aComponent);
92 List<Node> requiredNodes = new ArrayList<Node>();
93 for (RequiredInterface required: aComponent.getRequiredInterfaces()) {
94 Node reqNode = new RequiredInterfaceNode(aComponent, required);
96 requiredNodes.add(reqNode);
101 // Edges from component to required interface.
102 for (Node reqNode: requiredNodes) {
103 addEdge(new DefaultEdge(compNode, reqNode));
106 // Add provided interfaces
107 List<Node> providedNodes = new ArrayList<Node>();
108 for (ProvidedInterface provided: aComponent.getProvidedInterfaces()) {
109 Node provNode = new ProvidedInterfaceNode(aComponent, provided);
111 providedNodes.add(provNode);
114 // Edges from provided interface to component
115 for (Node provNode: providedNodes) {
116 addEdge(new DefaultEdge(provNode, compNode));