X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fgraph%2Fcomponent%2FComponentGraph.java;fp=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fgraph%2Fcomponent%2FComponentGraph.java;h=20313b5983785b82d8b9d033a929a6fb3942e64f;hb=aa78ce0aeaa36871bd926eefa1eabf9cb3254c7a;hp=0000000000000000000000000000000000000000;hpb=436718e7b7ee0bb9f37db496dbde5c011d5f84e3;p=utils diff --git a/system/general/src/main/java/org/wamblee/system/graph/component/ComponentGraph.java b/system/general/src/main/java/org/wamblee/system/graph/component/ComponentGraph.java new file mode 100644 index 00000000..20313b59 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/graph/component/ComponentGraph.java @@ -0,0 +1,119 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.graph.component; + +import java.util.ArrayList; +import java.util.List; + +import org.wamblee.general.Pair; +import org.wamblee.system.container.CompositeInterfaceRestriction; +import org.wamblee.system.container.InterfaceRestriction; +import org.wamblee.system.core.Component; +import org.wamblee.system.core.ProvidedInterface; +import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.graph.DefaultEdge; +import org.wamblee.system.graph.Edge; +import org.wamblee.system.graph.Graph; +import org.wamblee.system.graph.Node; + + +// TODO info superfluous required interfaces +// TODO check optional external required but mandatory internal. + +public class ComponentGraph extends Graph { + + private CompositeInterfaceRestriction _restriction; + + public ComponentGraph() { + _restriction = new CompositeInterfaceRestriction(); + } + + public void addRestriction(InterfaceRestriction aRestriction) { + _restriction.add(aRestriction); + } + + public void addRequiredInterface(RequiredInterface aInterface) { + addNode(new ExternalRequiredInterfaceNode(aInterface)); + } + + public void addProvidedInterface(ProvidedInterface aInterface) { + addNode(new ExternalProvidedInterfaceNode(aInterface)); + } + + public void validate() { + extend(new RequiredProvidedEdgeFactory()); + accept(new ApplyRestrictionsVisitor(this, _restriction)); + accept(new CheckRequiredProvidedMultiplicityVisitor(this)); + accept(new CheckExternallyRequiredVisitor()); + accept(new CheckExternallyProvidedVisitor(this)); + accept(new CheckStartupDependenciesVisitor(this)); + } + + public void link() { + accept(new LinkVisitor()); + } + + /** + * Finds a list of mappings of external provided interface to internal provided interface. + * + * @return List of pairs of external to internal interface. + */ + public List> findExternalProvidedInterfaceMapping() { + List> result = + new ArrayList>(); + for (Edge edge: getEdges()) { + if ( edge.getFrom() instanceof ExternalProvidedInterfaceNode && + edge.getTo() instanceof ProvidedInterfaceNode ) { + result.add(new Pair( + ((ExternalProvidedInterfaceNode)edge.getFrom()).getProvided(), + ((ProvidedInterfaceNode)edge.getTo()).getProvided() + )); + } + } + return result; + } + + public void addComponent(Component aComponent) { + // Add required interfaces. + Node compNode = new ComponentNode(aComponent); + List requiredNodes = new ArrayList(); + for (RequiredInterface required: aComponent.getRequiredInterfaces()) { + Node reqNode = new RequiredInterfaceNode(aComponent, required); + addNode(reqNode); + requiredNodes.add(reqNode); + } + // Add the component + addNode(compNode); + + // Edges from component to required interface. + for (Node reqNode: requiredNodes) { + addEdge(new DefaultEdge(compNode, reqNode)); + } + + // Add provided interfaces + List providedNodes = new ArrayList(); + for (ProvidedInterface provided: aComponent.getProvidedInterfaces()) { + Node provNode = new ProvidedInterfaceNode(aComponent, provided); + addNode(provNode); + providedNodes.add(provNode); + } + + // Edges from provided interface to component + for (Node provNode: providedNodes) { + addEdge(new DefaultEdge(provNode, compNode)); + } + } +}