/*
- * Copyright 2008 the original author or authors.
+ * Copyright 2005-2010 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.
*/
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.CompositeEdgeFilter;
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.
+import java.util.ArrayList;
+import java.util.List;
/**
- * Represents a component graph and provides the bridge from the
- * component model to a graph model. The graph model is easier
- * to work with to implement specific actions and validations than
- * the component model.
+ * Represents a component graph and provides the bridge from the component model
+ * to a graph model. The graph model is easier to work with to implement
+ * specific actions and validations than the component model.
*/
public class ComponentGraph extends Graph {
-
- private CompositeInterfaceRestriction _restriction;
+ private boolean isLinked;
+
+ private CompositeEdgeFilter edgeFilter;
/**
- * Constructs an empty component graph.
- */
- public ComponentGraph() {
- _restriction = new CompositeInterfaceRestriction();
- }
-
- /**
- * Adds a restriction that must be satisfied by the component model.
- * @param aRestriction Restriction.
+ * Constructs an empty component graph.
*/
- public void addRestriction(InterfaceRestriction aRestriction) {
- _restriction.add(aRestriction);
+ public ComponentGraph() {
+ isLinked = false;
+ edgeFilter = new CompositeEdgeFilter();
}
-
+
/**
- * Adds an externally required interface of a container.
- * This should be called before any components of the container are
- * added.
- * @param aInterface Required interface.
+ * Adds an externally required interface of a container. This should be
+ * called before any components of the container are added.
+ *
+ * @param aComponent
+ * Component requiring the interface.
+ * @param aInterface
+ * Required interface.
*/
- public void addRequiredInterface(RequiredInterface aInterface) {
- addNode(new ExternalRequiredInterfaceNode(aInterface));
+ public void addRequiredInterface(Component aComponent,
+ RequiredInterface aInterface) {
+ addNode(new ExternalRequiredInterfaceNode(aComponent, aInterface));
}
-
+
/**
- * Adds an externally provided interface of a container.
- * This should be called after all components of the container have been added.
- * @param aInterface Provided interface.
+ * Adds an externally provided interface of a container. This should be
+ * called after all components of the container have been added.
+ *
+ * @param aComponent
+ * Component providing the interface.
+ * @param aInterface
+ * Provided interface.
*/
- public void addProvidedInterface(ProvidedInterface aInterface) {
- addNode(new ExternalProvidedInterfaceNode(aInterface));
+ public void addProvidedInterface(Component aComponent,
+ ProvidedInterface aInterface) {
+ addNode(new ExternalProvidedInterfaceNode(aComponent, aInterface));
}
-
+
/**
- * Validates the component graph.
+ * Validates the component graph.
*/
- public void validate() {
+ public void validate() {
extend(new RequiredProvidedEdgeFactory());
- accept(new ApplyRestrictionsVisitor(this, _restriction));
+ applyFilter(edgeFilter);
accept(new CheckRequiredProvidedMultiplicityVisitor(this));
- accept(new CheckExternallyRequiredVisitor());
+ accept(new CheckExternallyRequiredVisitor(this));
accept(new CheckExternallyProvidedVisitor(this));
accept(new CheckStartupDependenciesVisitor(this));
}
-
+
/**
- * Links provided and required interfaces together in the component
- * model based on the graph model.
+ * Links provided and required interfaces together in the component model
+ * based on the graph model.
*/
- public void link() {
+ public void link() {
+ if (isLinked) {
+ return;
+ }
+
accept(new LinkVisitor());
+ isLinked = true;
}
-
+
/**
- * Finds a list of mappings of external provided interface to internal provided interface.
+ * Finds a list of mappings of external provided interface to internal
+ * provided interface.
*
- * @return List of pairs of external to internal interface.
+ * @return List of pairs of external to internal interface.
*/
- public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
- List<Pair<ProvidedInterface, ProvidedInterface>> result =
- new ArrayList<Pair<ProvidedInterface,ProvidedInterface>>();
- for (Edge edge: getEdges()) {
- if ( edge.getFrom() instanceof ExternalProvidedInterfaceNode &&
- edge.getTo() instanceof ProvidedInterfaceNode ) {
- result.add(new Pair<ProvidedInterface,ProvidedInterface>(
- ((ExternalProvidedInterfaceNode)edge.getFrom()).getProvided(),
- ((ProvidedInterfaceNode)edge.getTo()).getProvided()
- ));
+ public List<Pair<ProvidedInterface, ProvidedInterface>> findExternalProvidedInterfaceMapping() {
+ List<Pair<ProvidedInterface, ProvidedInterface>> result = new ArrayList<Pair<ProvidedInterface, ProvidedInterface>>();
+
+ for (Edge edge : getEdges()) {
+ if (edge.getFrom() instanceof ExternalProvidedInterfaceNode &&
+ edge.getTo() instanceof ProvidedInterfaceNode) {
+ result.add(new Pair<ProvidedInterface, ProvidedInterface>(
+ ((ExternalProvidedInterfaceNode) edge.getFrom())
+ .getProvided(), ((ProvidedInterfaceNode) edge.getTo())
+ .getProvided()));
}
}
- return result;
+
+ return result;
}
-
+
/**
- * Adds a component by adding required interfaces, components, and
- * provided interfaces.
- * @param aComponent Component to add.
+ * Adds a component by adding required interfaces, components, and provided
+ * interfaces.
+ *
+ * @param aComponent
+ * Component to add.
*/
- public void addComponent(Component aComponent) {
- // Add required interfaces.
+ public void addComponent(Component<?> aComponent) {
+ // Add required interfaces.
Node compNode = new ComponentNode(aComponent);
List<Node> requiredNodes = new ArrayList<Node>();
- for (RequiredInterface required: aComponent.getRequiredInterfaces()) {
- Node reqNode = new RequiredInterfaceNode(aComponent, required);
+
+ 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) {
+
+ // Edges from component to required interface.
+ for (Node reqNode : requiredNodes) {
addEdge(new DefaultEdge(compNode, reqNode));
}
-
+
// Add provided interfaces
List<Node> providedNodes = new ArrayList<Node>();
- for (ProvidedInterface provided: aComponent.getProvidedInterfaces()) {
- Node provNode = new ProvidedInterfaceNode(aComponent, provided);
+
+ 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) {
+ for (Node provNode : providedNodes) {
addEdge(new DefaultEdge(provNode, compNode));
}
}
+
+ public void addEdgeFilter(CompositeEdgeFilter aEdgeFilter) {
+ edgeFilter.add(aEdgeFilter);
+ }
}