Now basing the implementation on a component graph.
[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 java.util.ArrayList;
19 import java.util.List;
20
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;
31
32
33 // TODO info superfluous required interfaces
34 // TODO check optional external required but mandatory internal.  
35
36 public class ComponentGraph extends Graph {
37     
38     private CompositeInterfaceRestriction _restriction; 
39
40     public ComponentGraph() { 
41         _restriction = new CompositeInterfaceRestriction(); 
42     }
43     
44     public void addRestriction(InterfaceRestriction aRestriction) { 
45         _restriction.add(aRestriction);
46     }
47     
48     public void addRequiredInterface(RequiredInterface aInterface) { 
49         addNode(new ExternalRequiredInterfaceNode(aInterface));
50     }
51     
52     public void addProvidedInterface(ProvidedInterface aInterface) { 
53         addNode(new ExternalProvidedInterfaceNode(aInterface));
54     }
55     
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));
63     }
64     
65     public void link() { 
66         accept(new LinkVisitor());
67     }
68     
69     /**
70      * Finds a list of mappings of external provided interface to internal provided interface. 
71      * 
72      * @return List of pairs of external to internal interface. 
73      */
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()
83                         )); 
84             }
85         }
86         return result; 
87     }
88     
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); 
95             addNode(reqNode);
96             requiredNodes.add(reqNode);
97         }
98         // Add the component
99         addNode(compNode);
100      
101         // Edges from component to required interface. 
102         for (Node reqNode: requiredNodes) { 
103             addEdge(new DefaultEdge(compNode, reqNode));
104         }
105         
106         // Add provided interfaces
107         List<Node> providedNodes = new ArrayList<Node>();
108         for (ProvidedInterface provided: aComponent.getProvidedInterfaces()) { 
109             Node provNode = new ProvidedInterfaceNode(aComponent, provided); 
110             addNode(provNode);
111             providedNodes.add(provNode);
112         }
113         
114         // Edges from provided interface to component
115         for (Node provNode: providedNodes) { 
116             addEdge(new DefaultEdge(provNode, compNode));
117         }
118     }
119 }