Removed InterfaceRestriction. Now introduced a friendlier API in
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 18 May 2008 10:28:30 +0000 (10:28 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 18 May 2008 10:28:30 +0000 (10:28 +0000)
the Container and filtering edges at the graph level. Filtering at graph level is more generic since it allows filtering of any edge (also required -> external required and external provided -> provided).

16 files changed:
trunk/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java [deleted file]
trunk/system/general/src/main/java/org/wamblee/system/container/Container.java
trunk/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java [deleted file]
trunk/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java [deleted file]
trunk/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java
trunk/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java [new file with mode: 0644]
trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java [new file with mode: 0644]
trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java [new file with mode: 0644]
trunk/system/general/src/main/java/org/wamblee/system/graph/Graph.java
trunk/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java [deleted file]
trunk/system/general/src/main/java/org/wamblee/system/graph/component/ComponentGraph.java
trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java [new file with mode: 0644]
trunk/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java
trunk/system/general/src/test/java/org/wamblee/system/graph/CompositeEdgeFilterTest.java [moved from trunk/system/general/src/test/java/org/wamblee/system/container/CompositeInterfaceRestrictionTest.java with 53% similarity]
trunk/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java
trunk/system/general/src/test/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilterTest.java [moved from trunk/system/general/src/test/java/org/wamblee/system/container/DefaultInterfaceRestrictionTest.java with 57% similarity]

diff --git a/trunk/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java b/trunk/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java
deleted file mode 100644 (file)
index 041c75d..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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.container;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.wamblee.system.core.Component;
-import org.wamblee.system.core.ProvidedInterface;
-import org.wamblee.system.core.RequiredInterface;
-
-
-/**
- * Composite restriction that wraps a number of other restrictions. The restriction is 
- * violated if one of the contained restrictions is violated. 
- * 
- * @author Erik Brakkee
- *
- */
-public class CompositeInterfaceRestriction implements InterfaceRestriction {
-    
-    private List<InterfaceRestriction> _restrictions; 
-    
-    /**
-     * Constructs the restriction. 
-     */
-    public CompositeInterfaceRestriction() { 
-        _restrictions = new ArrayList<InterfaceRestriction>();
-    }
-    
-    /**
-     * Constructs the restriction. 
-     * @param aRestrictions List of contained restrictions. 
-     */
-    public CompositeInterfaceRestriction(Collection<InterfaceRestriction> aRestrictions) { 
-        this(); 
-        _restrictions.addAll(aRestrictions);
-    }
-    
-    /**
-     * Adds a restriction. 
-     * @param aRestriction Restriction. 
-     * @return Reference to this composite restriction to allow call chaining. 
-     */
-    public CompositeInterfaceRestriction add(InterfaceRestriction aRestriction) { 
-        _restrictions.add(aRestriction);
-        return this; 
-    }
-
-    @Override
-    public boolean isViolated(Component aClient, RequiredInterface aRequired,
-            Component aServer, ProvidedInterface aProvided) {
-        for (InterfaceRestriction restriction: _restrictions) { 
-            if ( restriction.isViolated(aClient, aRequired, aServer, aProvided)) { 
-                return true; 
-            }
-        }
-        return false; 
-    }
-
-}
index 92aba2144c3286f49ef0554fa5b537b6acf9e1b3..59067f974f2835d6980078ab8b240a518e628d58 100644 (file)
@@ -30,7 +30,11 @@ import org.wamblee.system.core.ProvidedInterface;
 import org.wamblee.system.core.RequiredInterface;
 import org.wamblee.system.core.Scope;
 import org.wamblee.system.core.SystemAssemblyException;
+import org.wamblee.system.graph.CompositeEdgeFilter;
+import org.wamblee.system.graph.EdgeFilter;
 import org.wamblee.system.graph.component.ComponentGraph;
+import org.wamblee.system.graph.component.ConnectRequiredProvidedEdgeFilter;
+import org.wamblee.system.graph.component.RequiredProvidedEdgeFactory;
 
 /**
  * Container consisting of multiple components.
@@ -43,7 +47,7 @@ public class Container extends AbstractComponent<Scope> {
 
     private List<Component> _components;
     private Set<String> _componentNames;
-    private CompositeInterfaceRestriction _restriction;
+    private CompositeEdgeFilter _edgeFilter; 
     private boolean _sealed;
 
     /**
@@ -64,7 +68,7 @@ public class Container extends AbstractComponent<Scope> {
         _components = new ArrayList<Component>();
 
         _componentNames = new HashSet<String>();
-        _restriction = new CompositeInterfaceRestriction();
+        _edgeFilter = new CompositeEdgeFilter();
         _sealed = false;
         for (Component component : aComponents) {
             addComponent(component);
@@ -93,21 +97,36 @@ public class Container extends AbstractComponent<Scope> {
         aComponent.addContext(getQualifiedName());
         return this;
     }
-
+    
     /**
-     * Adds an interface restriction for explicitly configuring the relations
-     * between components.
-     * 
-     * @param aRestriction
-     *            Restriction to add.
-     * @return Reference to this to allow call chaining.
+     * Explictly connects required and provided interfaces. 
+     * @param aClientComponent Client component, may not be null. 
+     * @param aRequiredInterface Required interface. If null it means all required interfaces. 
+     * @param aServerComponent Server component to connect to. If null, it means that no server components
+     *         may be connected to and the provider of the required interface will be null. 
+     * @param aProvidedInterface Provided interface. If null, it means that there is no restriction on the 
+     *   name of the provided interface and that it is automatically selected. 
      */
-    public Container addRestriction(InterfaceRestriction aRestriction) {
+    public void connectRequiredProvided(String aClientComponent, String aRequiredInterface, 
+            String aServerComponent, String aProvidedInterface) {
         checkSealed();
-        _restriction.add(aRestriction);
-        return this;
+        _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
+    }
+    
+    public void connectExternalRequired(String aComponent, String aRequiredInterface, 
+            String aExternalRequiredInterface) {
+        checkSealed();
+        // TODO implement. 
+        throw new RuntimeException("not implemented");
+    }
+    
+    public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
+        checkSealed();
+        // TODO implement. 
+        throw new RuntimeException("not implemented"); 
     }
 
+
     @Override
     public Container addProvidedInterface(ProvidedInterface aProvided) {
         checkSealed();
@@ -233,7 +252,7 @@ public class Container extends AbstractComponent<Scope> {
             graph.addProvidedInterface(this, prov);
         }
 
-        graph.addRestriction(_restriction);
+        graph.addEdgeFilter(_edgeFilter);
         return graph;
     }
 
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java b/trunk/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java
deleted file mode 100644 (file)
index 2706684..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.container;
-
-import org.wamblee.system.core.Component;
-import org.wamblee.system.core.ProvidedInterface;
-import org.wamblee.system.core.RequiredInterface;
-
-/**
- * An interface restriction on the required interface of a component. 
- * 
- * @author Erik Brakkee
- *
- */
-public class DefaultInterfaceRestriction implements InterfaceRestriction {
-
-    private String _client;
-    private String _required;
-    private String _server;
-    private String _provided;
-
-    /**
-     * Constructs the restriction. If the client name and required interface
-     * name match, then the server name and interface name must also match. 
-     * Otherwise the restriction is violated. 
-     * 
-     * This can be used to explicitly connect required and provided interfaces
-     * of components. 
-     * 
-     * @param aClient
-     *            Client or null if no restriction on the client.
-     * @param aRequired
-     *            Required interface name or null if no restriction on the
-     *            required interface.
-     * @param aServer
-     *            Server or null if no restriction on the server.
-     * @param aProvided
-     *            Provided interface name or null if no restriction on the
-     *            provided interface.
-     */
-    public DefaultInterfaceRestriction(String aClient, String aRequired,
-            String aServer, String aProvided) {
-        
-        if ( aClient == null && aRequired == null ) { 
-            throw new IllegalArgumentException("No restriction on the required interface");
-        }
-        if ( aServer == null && aProvided == null ) { 
-            throw new IllegalArgumentException("No restriction on the provided interface");
-        }
-        _client = aClient;
-        _required = aRequired;
-        _server = aServer;
-        _provided = aProvided;
-    }
-
-    @Override
-    public boolean isViolated(Component aClient, RequiredInterface aRequired,
-            Component aServer, ProvidedInterface aProvided) {
-        
-        if ( _client != null && !aClient.getName().equals(_client)) { 
-            return false; 
-        }
-        if ( _required != null && !aRequired.getName().equals(_required)) { 
-            return false; 
-        }
-        
-        // The required interface matches
-        
-        if ( _server != null && !aServer.getName().equals(_server)) {
-            // Server was specified and does not match.
-            return true; 
-        }
-        if ( _provided != null && !aProvided.getName().equals(_provided)) { 
-            // provided interface was specified and doe not match. 
-            return true; 
-        }
-        
-        // The required and provided interfaces match so this is not a violation.
-        
-        return false; 
-    }
-
-}
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java b/trunk/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java
deleted file mode 100644 (file)
index 5f9749b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.container;
-
-import org.wamblee.system.core.Component;
-import org.wamblee.system.core.ProvidedInterface;
-import org.wamblee.system.core.RequiredInterface;
-
-/**
- * Interface restriction for restricting the linking of provided and required
- * interfaces. 
- * 
- * @author Erik Brakkee
- *
- */
-public interface InterfaceRestriction {
-
-    /**
-     * Checks if connecting the given provided and required interface would violate
-     * the restriction. 
-     * 
-     * @param aClient Client component requiring an interface. 
-     * @param aRequired Required interface. 
-     * @param aServer Server component providing an interface. 
-     * @param aProvided Provided interface. 
-     */
-    boolean isViolated(Component aClient, RequiredInterface aRequired, 
-            Component aServer, ProvidedInterface aProvided); 
-}
index 04f0480125c36ff5737258b728b64461b3d3b11d..9000f4ae5234a1bc8e2b54536fdd20d29f15c0a2 100644 (file)
@@ -179,5 +179,23 @@ public abstract class AbstractComponent<Type> implements Component<Type> {
        public String toString() {
                return getQualifiedName();
        }
+       
+       public ProvidedInterface findProvidedInterface(String aName) { 
+           for (ProvidedInterface provided: getProvidedInterfaces()) { 
+               if ( provided.getName().equals(aName)) { 
+                   return provided; 
+               }
+           }
+           return null; 
+       }
+       
+       public RequiredInterface findRequiredInterface(String aName) { 
+           for (RequiredInterface required: getRequiredInterfaces()) { 
+               if ( required.getName().equals(aName)) { 
+                   return required; 
+               }
+           }
+           return null; 
+       }
 
 }
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java
new file mode 100644 (file)
index 0000000..692c881
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CompositeEdgeFilter implements EdgeFilter {
+    
+    private List<EdgeFilter> _filters; 
+    
+    public CompositeEdgeFilter() { 
+        _filters = new ArrayList<EdgeFilter>();
+    }
+    
+    public void add(EdgeFilter aFilter) { 
+        _filters.add(aFilter);
+    }
+
+    @Override
+    public boolean isViolated(Edge aEdge) {
+        for (EdgeFilter filter: _filters) { 
+            if ( filter.isViolated(aEdge) ) { 
+                return true; 
+            }
+        }
+        return false; 
+    }
+
+}
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java
new file mode 100644 (file)
index 0000000..b372f08
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * 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;
+
+public interface EdgeFilter {
+
+    boolean isViolated(Edge aEdge); 
+}
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java
new file mode 100644 (file)
index 0000000..c49f82c
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+public class EdgeSelector<FromType extends Node, ToType extends Node> {
+    
+    public static interface Selector<FromType extends Node, ToType extends Node> { 
+        void execute(FromType aFrom, ToType aTo);
+    }
+    
+    private Class<FromType> _fromType; 
+    private Class<ToType> _toType;
+    
+    public EdgeSelector(Class<FromType> aFrom, Class<ToType> aTo) {
+        _fromType = aFrom; 
+        _toType = aTo;
+    }
+
+    public void execute(Selector<FromType,ToType> aSelector, Edge aEdge) { 
+        if ( _fromType.isInstance(aEdge.getFrom()) && _toType.isInstance(aEdge.getTo())) { 
+            aSelector.execute((FromType)aEdge.getFrom(), (ToType)aEdge.getTo());
+        }    
+    }
+}
index 387d5e4fd3ffc563d2690495078602d9928ccd6c..0e4f6ccb0b28c82020ed8dd0e17b8aab5c5f3164 100644 (file)
@@ -16,6 +16,7 @@
 package org.wamblee.system.graph;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -174,6 +175,18 @@ public class Graph {
             }
         }
     }
+    
+    /**
+     * Applies a filter to the graph for removing elements. 
+     * @param aFilter Filter to apply. 
+     */
+    public void applyFilter(EdgeFilter aFilter) {
+        for (Iterator<Edge> edge = _edges.iterator(); edge.hasNext(); ) { 
+            if (aFilter.isViolated(edge.next())) { 
+                edge.remove();
+            }
+        }
+    }
 
     /**
      * Finds all outgoing edges of a node. More specifically, finds 
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java
deleted file mode 100644 (file)
index 72cbc27..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.List;
-
-import org.wamblee.system.container.InterfaceRestriction;
-import org.wamblee.system.core.Component;
-import org.wamblee.system.graph.Edge;
-import org.wamblee.system.graph.Graph;
-import org.wamblee.system.graph.Node;
-import org.wamblee.system.graph.Visitor;
-
-/**
- * Applies restrictions on a graph by removing all edges that violate the restriction. 
- * 
- * @author Erik Brakkee. 
- *
- */
-public class ApplyRestrictionsVisitor implements Visitor {
-    
-    private Graph _graph; 
-    private InterfaceRestriction _restriction; 
-
-    /**
-     * Constructs the visitor. 
-     * @param aGraph Graph. 
-     * @param aRestriction Restriction. 
-     */
-    public ApplyRestrictionsVisitor(Graph aGraph, InterfaceRestriction aRestriction) { 
-        _graph = aGraph; 
-        _restriction = aRestriction;
-    }
-
-    @Override
-    public void visitEdge(Edge aEdge) {
-        if ( aEdge.getFrom() instanceof RequiredInterfaceNode &&
-             aEdge.getTo() instanceof ProvidedInterfaceNode) {
-            RequiredInterfaceNode from = (RequiredInterfaceNode)aEdge.getFrom(); 
-            ProvidedInterfaceNode to = (ProvidedInterfaceNode) aEdge.getTo();
-       
-            if ( _restriction.isViolated(from.getComponent(), from.getRequired(), to.getComponent(), to.getProvided())) { 
-                _graph.removeEdge(aEdge);
-            }
-        }
-    }
-
-    @Override
-    public void visitNode(Node aNode) {
-        // Empty
-    }
-
-}
index be861f309255da4deb64ed63cd12e63f15ab56da..aedaa0c3e94021a33c86cc7bdaf14bcc47e48a75 100644 (file)
@@ -19,11 +19,10 @@ 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;
@@ -37,21 +36,13 @@ import org.wamblee.system.graph.Node;
  */
 public class ComponentGraph extends Graph {
     
-    private CompositeInterfaceRestriction _restriction
+    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. 
-     */
-    public void addRestriction(InterfaceRestriction aRestriction) { 
-        _restriction.add(aRestriction);
+        _edgeFilter = new CompositeEdgeFilter(); 
     }
     
     /**
@@ -80,7 +71,7 @@ public class ComponentGraph extends Graph {
      */
     public void validate() { 
         extend(new RequiredProvidedEdgeFactory());
-        accept(new ApplyRestrictionsVisitor(this, _restriction));
+        applyFilter(_edgeFilter);
         accept(new CheckRequiredProvidedMultiplicityVisitor(this));
         accept(new CheckExternallyRequiredVisitor(this)); 
         accept(new CheckExternallyProvidedVisitor(this));
@@ -150,4 +141,8 @@ public class ComponentGraph extends Graph {
             addEdge(new DefaultEdge(provNode, compNode));
         }
     }
+
+    public void addEdgeFilter(CompositeEdgeFilter aEdgeFilter) {
+        _edgeFilter.add(aEdgeFilter);    
+    }
 }
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java b/trunk/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java
new file mode 100644 (file)
index 0000000..f04b381
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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 org.wamblee.system.graph.Edge;
+import org.wamblee.system.graph.EdgeFilter;
+import org.wamblee.system.graph.EdgeSelector;
+import org.wamblee.system.graph.Node;
+
+/**
+ * Filter used to explicitly connect required and provided interfaces within a
+ * container.
+ * 
+ * @author Erik Brakkee
+ * 
+ */
+public class ConnectRequiredProvidedEdgeFilter implements EdgeFilter {
+
+    private String _client;
+    private String _required;
+    private String _server;
+    private String _provided;
+
+    public ConnectRequiredProvidedEdgeFilter(String aClient, String aRequired,
+            String aServer, String aProvided) {
+        _client = aClient;
+        _required = aRequired;
+        _server = aServer;
+        _provided = aProvided;
+        if ( _client == null ) { 
+            throw new IllegalArgumentException("Client component must be specified"); 
+        }
+    }
+
+    @Override
+    public boolean isViolated(Edge aEdge) {
+        if (aEdge.getFrom() instanceof RequiredInterfaceNode
+                && aEdge.getTo() instanceof ProvidedInterfaceNode) {
+            return isViolated((RequiredInterfaceNode) aEdge.getFrom(),
+                    (ProvidedInterfaceNode) aEdge.getTo());
+        }
+        return false;
+    }
+
+    private boolean isViolated(RequiredInterfaceNode aFrom,
+            ProvidedInterfaceNode aTo) {
+        if (_client.equals(aFrom.getComponent().getName())
+                && (_required == null || _required.equals(aFrom.getRequired()
+                        .getName()))) {
+            // From part matches.
+            if ( _server == null ) { 
+                return true; // all connections are eliminated
+            }
+            if (_server.equals(aTo.getComponent().getName())
+                    && (_provided == null || _provided.equals(aTo.getProvided()
+                            .getName()))) {
+                // to part matches also
+                return false;
+            }
+            else { 
+                // From matches and to doesn't so edgefilter is violated.
+                return true; 
+            }
+        } else {
+            // From part does not match, restriction does not apply.
+            return false;
+        }
+    }
+}
index 5908a85e30c029eb60e5f5579138b5cb1e3037fd..c4f64ba88eee53a6651bb1b7b0ee46c2cfa31e65 100644 (file)
@@ -422,14 +422,19 @@ public class ContainerTest extends TestCase {
         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
             @Override
             public void run() throws Exception {
-                container.addRestriction(new InterfaceRestriction() {
-                    @Override
-                    public boolean isViolated(Component aClient,
-                            RequiredInterface aRequired, Component aServer,
-                            ProvidedInterface aProvided) {
-                        return false;
-                    }
-                });
+                container.connectRequiredProvided("x", "y", "a", "b");
+            }
+        }, SystemAssemblyException.class);
+        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+            @Override
+            public void run() throws Exception {
+                container.connectExternalRequired("x", "y", "a");
+            }
+        }, SystemAssemblyException.class);
+        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
+            @Override
+            public void run() throws Exception {
+                container.connectExternalProvided("x", "y", "z");
             }
         }, SystemAssemblyException.class);
         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
@@ -455,8 +460,7 @@ public class ContainerTest extends TestCase {
         Application app = new Application("app");
         Container container = new Container("top").addComponent(env1)
                 .addComponent(env2).addComponent(app);
-        container.addRestriction(new DefaultInterfaceRestriction("app", null,
-                "env1", null));
+        container.connectRequiredProvided("app", null, "env1", null);
         container.start();
         assertEquals(env1.getString(), app.getString());
         assertFalse(env2.getString().equals(app.getString()));
similarity index 53%
rename from trunk/system/general/src/test/java/org/wamblee/system/container/CompositeInterfaceRestrictionTest.java
rename to trunk/system/general/src/test/java/org/wamblee/system/graph/CompositeEdgeFilterTest.java
index e724178eef4e2af253ff3317f6e7830c5387b312..6507efa93bcb093553c61198fd50f54d03507651 100644 (file)
@@ -13,9 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-package org.wamblee.system.container;
-
-import static org.easymock.EasyMock.anyObject;
+package org.wamblee.system.graph;
 
 import java.util.Arrays;
 
@@ -23,45 +21,51 @@ import junit.framework.TestCase;
 
 import org.easymock.classextension.EasyMock;
 import org.easymock.classextension.IMocksControl;
+import static org.easymock.classextension.EasyMock.*;
+import org.wamblee.system.container.Application;
 import org.wamblee.system.core.Component;
 import org.wamblee.system.core.Environment;
 import org.wamblee.system.core.ProvidedInterface;
 import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.graph.component.ProvidedInterfaceNode;
+import org.wamblee.system.graph.component.RequiredInterfaceNode;
 
-public class CompositeInterfaceRestrictionTest extends TestCase {
-    
+public class CompositeEdgeFilterTest extends TestCase {
     private Application _app = new Application(); 
-    private Environment _env = new Environment(); 
+    private Environment _env = new Environment();
+    
+    private Edge createEdge(Component aClient, RequiredInterface aRequired, 
+            Component aServer, ProvidedInterface aProvided) { 
+        Node from = new RequiredInterfaceNode(aClient, aRequired);
+        Node to = new ProvidedInterfaceNode(aServer, aProvided);
+        return new DefaultEdge(from, to);
+    }
 
     public void testEmpty() { 
-        InterfaceRestriction restriction = new CompositeInterfaceRestriction(); 
-        assertFalse(restriction.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        EdgeFilter restriction = new CompositeEdgeFilter(); 
+        assertFalse(restriction.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
     }
     
-    private void configureRestriction(InterfaceRestriction base, boolean aResult) {
-        base.isViolated(
-                (Component)anyObject(), 
-                (RequiredInterface)anyObject(), 
-                (Component)anyObject(), 
-                (ProvidedInterface)anyObject());
+    private void configureRestriction(EdgeFilter base, boolean aResult) {
+        base.isViolated( (Edge)EasyMock.anyObject());
         EasyMock.expectLastCall().andReturn(aResult);
     }
     
     public void testOneRestriction() { 
         IMocksControl control = EasyMock.createStrictControl();
       
-        InterfaceRestriction base = control.createMock(InterfaceRestriction.class);
-        InterfaceRestriction composite = new CompositeInterfaceRestriction(
-                Arrays.asList(new InterfaceRestriction[] { base } ));
+        EdgeFilter base = control.createMock(EdgeFilter.class);
+        CompositeEdgeFilter composite = new CompositeEdgeFilter();
+        composite.add(base);
         
         // First let the base return false and verify the result. 
         
         configureRestriction(base, false);
         
         control.replay();
-        assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
         control.verify();
         
         // Second let the base return true and verify the result.
@@ -69,8 +73,8 @@ public class CompositeInterfaceRestrictionTest extends TestCase {
         configureRestriction(base, true);
         
         control.replay();
-        assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
         control.verify();
     }
 
@@ -79,10 +83,10 @@ public class CompositeInterfaceRestrictionTest extends TestCase {
     public void testTwoRestrictions() { 
         IMocksControl control = EasyMock.createStrictControl();
         
-        InterfaceRestriction base1 = control.createMock(InterfaceRestriction.class);
-        CompositeInterfaceRestriction composite = new CompositeInterfaceRestriction(
-                Arrays.asList(new InterfaceRestriction[] { base1 } ));
-        InterfaceRestriction base2 = control.createMock(InterfaceRestriction.class);
+        EdgeFilter base1 = control.createMock(EdgeFilter.class);
+        CompositeEdgeFilter composite = new CompositeEdgeFilter();
+        composite.add(base1);
+        EdgeFilter base2 = control.createMock(EdgeFilter.class);
         composite.add(base2);
         
         // 1. base1 not violated and base 2 not violated -> not violated. 
@@ -90,8 +94,8 @@ public class CompositeInterfaceRestrictionTest extends TestCase {
         configureRestriction(base1, false);
         configureRestriction(base2, false);
         control.replay();
-        assertFalse(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        assertFalse(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
         control.verify();
         control.reset();
         
@@ -99,8 +103,8 @@ public class CompositeInterfaceRestrictionTest extends TestCase {
         configureRestriction(base1, false);
         configureRestriction(base2, true);
         control.replay();
-        assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
         control.verify();
         control.reset();
         
@@ -108,9 +112,10 @@ public class CompositeInterfaceRestrictionTest extends TestCase {
         configureRestriction(base1, true);
         // base 2 should not be called.
         control.replay();
-        assertTrue(composite.isViolated(_app, _app.getRequiredInterfaces()[0], 
-                _env, _env.getProvidedInterfaces()[0]));
+        assertTrue(composite.isViolated(createEdge(_app, _app.getRequiredInterfaces()[0], 
+                _env, _env.getProvidedInterfaces()[0])));
         control.verify();
         control.reset();
     }
+    
 }
index 305765f90670da4abb60d599083d5f62bba6edbf..7a5a87971204255aa1c945af36b5b972fc826a04 100644 (file)
@@ -99,6 +99,32 @@ public class GraphTest extends TestCase {
         assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
     }
     
+    public void testApplyFilter() { 
+        Graph graph = new Graph(); 
+        graph.addNode(new DefaultNode("x"));
+        graph.addNode(new DefaultNode("y"));
+        graph.addNode(new DefaultNode("z"));
+        graph.addEdge(new DefaultEdge(graph.findNode("x"), graph.findNode("y")));
+        graph.addEdge(new DefaultEdge(graph.findNode("y"), graph.findNode("z")));
+        graph.addEdge(new DefaultEdge(graph.findNode("z"), graph.findNode("x")));
+        
+        assertEquals(3, graph.getEdges().size());
+        
+        graph.applyFilter(new EdgeFilter() { 
+            @Override
+            public boolean isViolated(Edge aEdge) {
+                if (aEdge.getFrom().getName().equals("x")) { 
+                    return false; 
+                }
+                return true; 
+            }
+        }); 
+        
+        assertEquals(1, graph.getEdges().size());
+        assertEquals("x", graph.getEdges().get(0).getFrom().getName());
+           
+    }
+    
     public void testFindIncomingOutgoing() { 
         Graph graph = new Graph();
         Node x = new DefaultNode("x");
similarity index 57%
rename from trunk/system/general/src/test/java/org/wamblee/system/container/DefaultInterfaceRestrictionTest.java
rename to trunk/system/general/src/test/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilterTest.java
index cf6c3a822319d9e453465011d6247b24417505e1..d9ab61bd458f805d4879c7686cfb547fa7eb4c52 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
-package org.wamblee.system.container;
+package org.wamblee.system.graph.component;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-import junit.framework.TestCase;
-
+import org.wamblee.system.container.Application;
 import org.wamblee.system.core.Environment;
+import org.wamblee.system.graph.DefaultEdge;
+import org.wamblee.system.graph.Edge;
+import org.wamblee.system.graph.EdgeFilter;
+import org.wamblee.system.graph.Node;
 import org.wamblee.test.AssertionUtils;
 
-public class DefaultInterfaceRestrictionTest extends TestCase {
+import junit.framework.TestCase;
+
+public class ConnectRequiredProvidedEdgeFilterTest extends TestCase {
+
     private Application _app1 = new Application("app1", "pf1.");
     private Application _app2 = new Application("app2", "pf2.");
     
@@ -32,7 +38,7 @@ public class DefaultInterfaceRestrictionTest extends TestCase {
     private Environment _env2 = new Environment("env2", "pf4.");
  
     
-    private void compare(Boolean[] aExpected, InterfaceRestriction aRestriction) { 
+    private void compare(Boolean[] aExpected, EdgeFilter aRestriction) { 
        List<Boolean> result = new ArrayList<Boolean>();
        
        // order will be: 
@@ -41,12 +47,13 @@ public class DefaultInterfaceRestrictionTest extends TestCase {
        //     env2, app1
        //     env2, app2
        for (Environment env: new Environment[] { _env1, _env2} ) { 
-           for (Application app: new Application[] { _app1, _app2} ) { 
-               result.add(aRestriction.isViolated(
-                       app, app.getRequiredInterfaces()[0], 
-                       env, 
-                       env.getProvidedInterfaces()[0]
-                       ));
+           for (Application app: new Application[] { _app1, _app2} ) {
+               Node from = new RequiredInterfaceNode(
+                       app, app.getRequiredInterfaces()[0]);
+               Node to = new ProvidedInterfaceNode(
+                       env, env.getProvidedInterfaces()[0]);
+               Edge edge = new DefaultEdge(from, to); 
+               result.add(aRestriction.isViolated(edge));
            }
        }
      
@@ -58,44 +65,26 @@ public class DefaultInterfaceRestrictionTest extends TestCase {
         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
             @Override
             public void run() throws Exception {
-                InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, null, null);
+                EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null, null, null, null);
                 
             }
         }, IllegalArgumentException.class);
         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
             @Override
             public void run() throws Exception {
-                InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, null, "x", "y");
-                
-            }
-        }, IllegalArgumentException.class);
-        AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
-            @Override
-            public void run() throws Exception {
-                InterfaceRestriction restriction = new DefaultInterfaceRestriction("x", "y", null, null);
-                
+                EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(null, null, "x", "y");
             }
         }, IllegalArgumentException.class);
     }
     
     public void testClientServer() { 
-        InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, "env1", null);
-        compare(new Boolean[] { false, false, true, false}, restriction);
-    }
-    
-    public void testRequiredServer() { 
-        InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", "env1", null);
+        EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1", null, "env1", null);
         compare(new Boolean[] { false, false, true, false}, restriction);
     }
     
-    public void testClientProvided() { 
-        InterfaceRestriction restriction = new DefaultInterfaceRestriction("app1", null, null, "pf4.datasource");
-        compare(new Boolean[] { true, false, false, false}, restriction);
-    }
-    
-    public void testRequiredProvide() { 
-        InterfaceRestriction restriction = new DefaultInterfaceRestriction(null, "pf1.string", null, "pf4.datasource");
-        compare(new Boolean[] { true, false, false, false}, restriction);
+    public void testNoConnectionsAtAll() { 
+        EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter("app1", null, null, null);
+        compare(new Boolean[] { true, false, true, false}, restriction);
     }
     
     public void testExplicitConfig() { 
@@ -104,7 +93,7 @@ public class DefaultInterfaceRestrictionTest extends TestCase {
         _env1 = new Environment("env1");
         _env2 = new Environment("env2");
         
-        InterfaceRestriction restriction = new DefaultInterfaceRestriction(
+        EdgeFilter restriction = new ConnectRequiredProvidedEdgeFilter(
                 "app2", "string", "env1", "datasource");
         compare(new Boolean[] { false, false, false, true}, restriction);