From 102eebeba2200f9bb2d8523bdfe7ace4ff663628 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 18 May 2008 10:28:30 +0000 Subject: [PATCH] Removed InterfaceRestriction. Now introduced a friendlier API in 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). --- .../CompositeInterfaceRestriction.java | 75 --------------- .../wamblee/system/container/Container.java | 45 ++++++--- .../DefaultInterfaceRestriction.java | 96 ------------------- .../container/InterfaceRestriction.java | 42 -------- .../system/core/AbstractComponent.java | 18 ++++ .../system/graph/CompositeEdgeFilter.java | 43 +++++++++ .../org/wamblee/system/graph/EdgeFilter.java | 21 ++++ .../wamblee/system/graph/EdgeSelector.java | 37 +++++++ .../java/org/wamblee/system/graph/Graph.java | 13 +++ .../component/ApplyRestrictionsVisitor.java | 66 ------------- .../graph/component/ComponentGraph.java | 21 ++-- .../ConnectRequiredProvidedEdgeFilter.java | 82 ++++++++++++++++ .../system/container/ContainerTest.java | 24 +++-- .../CompositeEdgeFilterTest.java} | 69 ++++++------- .../org/wamblee/system/graph/GraphTest.java | 26 +++++ ...onnectRequiredProvidedEdgeFilterTest.java} | 61 +++++------- 16 files changed, 356 insertions(+), 383 deletions(-) delete mode 100644 system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java delete mode 100644 system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java delete mode 100644 system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java create mode 100644 system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java create mode 100644 system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java create mode 100644 system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java delete mode 100644 system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java create mode 100644 system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java rename system/general/src/test/java/org/wamblee/system/{container/CompositeInterfaceRestrictionTest.java => graph/CompositeEdgeFilterTest.java} (53%) rename system/general/src/test/java/org/wamblee/system/{container/DefaultInterfaceRestrictionTest.java => graph/component/ConnectRequiredProvidedEdgeFilterTest.java} (57%) diff --git a/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java deleted file mode 100644 index 041c75d2..00000000 --- a/system/general/src/main/java/org/wamblee/system/container/CompositeInterfaceRestriction.java +++ /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 _restrictions; - - /** - * Constructs the restriction. - */ - public CompositeInterfaceRestriction() { - _restrictions = new ArrayList(); - } - - /** - * Constructs the restriction. - * @param aRestrictions List of contained restrictions. - */ - public CompositeInterfaceRestriction(Collection 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; - } - -} diff --git a/system/general/src/main/java/org/wamblee/system/container/Container.java b/system/general/src/main/java/org/wamblee/system/container/Container.java index 92aba214..59067f97 100644 --- a/system/general/src/main/java/org/wamblee/system/container/Container.java +++ b/system/general/src/main/java/org/wamblee/system/container/Container.java @@ -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 { private List _components; private Set _componentNames; - private CompositeInterfaceRestriction _restriction; + private CompositeEdgeFilter _edgeFilter; private boolean _sealed; /** @@ -64,7 +68,7 @@ public class Container extends AbstractComponent { _components = new ArrayList(); _componentNames = new HashSet(); - _restriction = new CompositeInterfaceRestriction(); + _edgeFilter = new CompositeEdgeFilter(); _sealed = false; for (Component component : aComponents) { addComponent(component); @@ -93,21 +97,36 @@ public class Container extends AbstractComponent { 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 { graph.addProvidedInterface(this, prov); } - graph.addRestriction(_restriction); + graph.addEdgeFilter(_edgeFilter); return graph; } diff --git a/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java deleted file mode 100644 index 27066844..00000000 --- a/system/general/src/main/java/org/wamblee/system/container/DefaultInterfaceRestriction.java +++ /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/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java b/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java deleted file mode 100644 index 5f9749b4..00000000 --- a/system/general/src/main/java/org/wamblee/system/container/InterfaceRestriction.java +++ /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); -} diff --git a/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java b/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java index 04f04801..9000f4ae 100644 --- a/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java +++ b/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java @@ -179,5 +179,23 @@ public abstract class AbstractComponent implements Component { 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/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java b/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java new file mode 100644 index 00000000..692c881f --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/graph/CompositeEdgeFilter.java @@ -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 _filters; + + public CompositeEdgeFilter() { + _filters = new ArrayList(); + } + + 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/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java b/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java new file mode 100644 index 00000000..b372f082 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/graph/EdgeFilter.java @@ -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/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java b/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java new file mode 100644 index 00000000..c49f82c1 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/graph/EdgeSelector.java @@ -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 { + + public static interface Selector { + void execute(FromType aFrom, ToType aTo); + } + + private Class _fromType; + private Class _toType; + + public EdgeSelector(Class aFrom, Class aTo) { + _fromType = aFrom; + _toType = aTo; + } + + public void execute(Selector aSelector, Edge aEdge) { + if ( _fromType.isInstance(aEdge.getFrom()) && _toType.isInstance(aEdge.getTo())) { + aSelector.execute((FromType)aEdge.getFrom(), (ToType)aEdge.getTo()); + } + } +} diff --git a/system/general/src/main/java/org/wamblee/system/graph/Graph.java b/system/general/src/main/java/org/wamblee/system/graph/Graph.java index 387d5e4f..0e4f6ccb 100644 --- a/system/general/src/main/java/org/wamblee/system/graph/Graph.java +++ b/system/general/src/main/java/org/wamblee/system/graph/Graph.java @@ -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 = _edges.iterator(); edge.hasNext(); ) { + if (aFilter.isViolated(edge.next())) { + edge.remove(); + } + } + } /** * Finds all outgoing edges of a node. More specifically, finds diff --git a/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java b/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java deleted file mode 100644 index 72cbc273..00000000 --- a/system/general/src/main/java/org/wamblee/system/graph/component/ApplyRestrictionsVisitor.java +++ /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 - } - -} 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 index be861f30..aedaa0c3 100644 --- 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 @@ -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/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java b/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java new file mode 100644 index 00000000..f04b381f --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilter.java @@ -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; + } + } +} diff --git a/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java b/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java index 5908a85e..c4f64ba8 100644 --- a/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java +++ b/system/general/src/test/java/org/wamblee/system/container/ContainerTest.java @@ -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())); diff --git a/system/general/src/test/java/org/wamblee/system/container/CompositeInterfaceRestrictionTest.java b/system/general/src/test/java/org/wamblee/system/graph/CompositeEdgeFilterTest.java similarity index 53% rename from system/general/src/test/java/org/wamblee/system/container/CompositeInterfaceRestrictionTest.java rename to system/general/src/test/java/org/wamblee/system/graph/CompositeEdgeFilterTest.java index e724178e..6507efa9 100644 --- a/system/general/src/test/java/org/wamblee/system/container/CompositeInterfaceRestrictionTest.java +++ b/system/general/src/test/java/org/wamblee/system/graph/CompositeEdgeFilterTest.java @@ -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(); } + } diff --git a/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java b/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java index 305765f9..7a5a8797 100644 --- a/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java +++ b/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java @@ -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"); diff --git a/system/general/src/test/java/org/wamblee/system/container/DefaultInterfaceRestrictionTest.java b/system/general/src/test/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilterTest.java similarity index 57% rename from system/general/src/test/java/org/wamblee/system/container/DefaultInterfaceRestrictionTest.java rename to system/general/src/test/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilterTest.java index cf6c3a82..d9ab61bd 100644 --- a/system/general/src/test/java/org/wamblee/system/container/DefaultInterfaceRestrictionTest.java +++ b/system/general/src/test/java/org/wamblee/system/graph/component/ConnectRequiredProvidedEdgeFilterTest.java @@ -13,18 +13,24 @@ * 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 result = new ArrayList(); // 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); -- 2.31.1