From: Gary P. <gpa...@gm...> - 2009-08-24 08:51:20
|
Refactored the Algorithm hierarchy to revolve around interfaces and not abstract classes. The only remaining abstract class is the AbstractAlgorithm. Refer to Effective Java for a detailed description of this type of refactor. Signed-off-by: Gary Pampara <gpa...@gm...> --- .../cilib/algorithm/AbstractAlgorithm.java | 9 +---- .../net/sourceforge/cilib/algorithm/Algorithm.java | 8 ++++ .../cilib/algorithm/SingularAlgorithm.java | 23 +----------- .../algorithm/{Algorithm.java => Stoppable.java} | 36 +++--------------- .../population/MultiPopulationBasedAlgorithm.java | 3 +- .../population/PopulationBasedAlgorithm.java | 38 ++----------------- .../population/SinglePopulationBasedAlgorithm.java | 3 +- .../cilib/clustering/kmeans/KMeans.java | 3 +- .../cilib/coevolution/CoevolutionAlgorithm.java | 3 +- .../cilib/entity/visitor/TopologyVisitor.java | 8 ++-- src/main/java/net/sourceforge/cilib/hs/HS.java | 3 +- .../cilib/measurement/generic/Iterations.java | 2 +- .../foundation/EvaluationMediator.java | 3 +- .../foundation/NeuralNetworkController.java | 3 +- src/main/java/net/sourceforge/cilib/pso/PSO.java | 1 - .../net/sourceforge/cilib/pso/niching/Niche.java | 3 +- .../container/visitor/TopologyVisitorTest.java | 26 +------------- .../cilib/measurement/generic/IterationsTest.java | 32 +++++++++++----- .../cilib/measurement/single/DiameterTest.java | 33 +++++++++++------ 19 files changed, 87 insertions(+), 153 deletions(-) copy src/main/java/net/sourceforge/cilib/algorithm/{Algorithm.java => Stoppable.java} (55%) diff --git a/src/main/java/net/sourceforge/cilib/algorithm/AbstractAlgorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/AbstractAlgorithm.java index 80d0784..04a02a7 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/AbstractAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/AbstractAlgorithm.java @@ -39,7 +39,7 @@ import net.sourceforge.cilib.stoppingcondition.StoppingCondition; * </p> * @author Edwin Peer */ -public abstract class AbstractAlgorithm implements Algorithm, Runnable { +public abstract class AbstractAlgorithm implements Algorithm, Stoppable, Runnable { private static final long serialVersionUID = 7197544770653732632L; private List<StoppingCondition> stoppingConditions; private List<AlgorithmListener> algorithmListeners; @@ -99,12 +99,6 @@ public abstract class AbstractAlgorithm implements Algorithm, Runnable { optimisationProblem = copy.optimisationProblem.getClone(); } - /** - * {@inheritDoc} - */ - @Override - public abstract AbstractAlgorithm getClone(); - /** * Reset the {@linkplain Algorithm} internals if needed. @@ -348,6 +342,7 @@ public abstract class AbstractAlgorithm implements Algorithm, Runnable { * @param problem An implementation of the * {@link net.sourceforge.cilib.problem.OptimisationProblemAdapter} interface. */ + @Override public void setOptimisationProblem(OptimisationProblem problem) { this.optimisationProblem = problem; } diff --git a/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java index d49e5d2..fc4969b 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java @@ -57,4 +57,12 @@ public interface Algorithm extends Cloneable { * Not Needed>??>???????????? */ public int getIterations(); + + /** + * Set the optimisation problem to be solved. By default, the problem is <code>null</code>. + * That is, it is necessary to set the optimisation problem before calling {@link #initialise()}. + * @param problem An implementation of the + * {@link net.sourceforge.cilib.problem.OptimisationProblemAdapter} interface. + */ + public void setOptimisationProblem(OptimisationProblem problem); } diff --git a/src/main/java/net/sourceforge/cilib/algorithm/SingularAlgorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/SingularAlgorithm.java index 166a1c9..4956447 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/SingularAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/SingularAlgorithm.java @@ -28,27 +28,6 @@ package net.sourceforge.cilib.algorithm; * Examples of such algorithms will include <tt>KMeans</tt>, <tt>Gradient Decent</tt> etc. * */ -public abstract class SingularAlgorithm extends AbstractAlgorithm { - private static final long serialVersionUID = 8524134633474124524L; - - /** - * Create a new instance of {@linkplain SingularAlgorithm}. - */ - public SingularAlgorithm() { - } - - /** - * Create a copy of the provided instance. - * @param copy The instance to copy. - */ - public SingularAlgorithm(SingularAlgorithm copy) { - super(copy); - } - - /** - * {@inheritDoc} - */ - @Override - public abstract void algorithmIteration(); +public interface SingularAlgorithm extends Algorithm, Stoppable { } diff --git a/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/Stoppable.java similarity index 55% copy from src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java copy to src/main/java/net/sourceforge/cilib/algorithm/Stoppable.java index d49e5d2..b926c60 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/Algorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/Stoppable.java @@ -19,42 +19,18 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + package net.sourceforge.cilib.algorithm; -import net.sourceforge.cilib.problem.OptimisationProblem; -import net.sourceforge.cilib.problem.OptimisationSolution; -import net.sourceforge.cilib.util.Cloneable; +import net.sourceforge.cilib.stoppingcondition.StoppingCondition; /** * */ -public interface Algorithm extends Cloneable { - - public OptimisationSolution getBestSolution(); - - public void performIteration(); - - /** - * Not Needed>??>???????????? - */ - public void performInitialisation(); - - /** - * Not Needed>??>???????????? - */ - public void performUninitialisation(); - - public Iterable<OptimisationSolution> getSolutions(); - - public OptimisationProblem getOptimisationProblem(); +public interface Stoppable { - /** - * ???? Not needed? - */ - public void reset(); + public void addStoppingCondition(StoppingCondition condition); - /** - * Not Needed>??>???????????? - */ - public int getIterations(); + public void removeStoppingCondition(StoppingCondition condition); + } diff --git a/src/main/java/net/sourceforge/cilib/algorithm/population/MultiPopulationBasedAlgorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/population/MultiPopulationBasedAlgorithm.java index a620770..46b6e21 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/population/MultiPopulationBasedAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/population/MultiPopulationBasedAlgorithm.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.Algorithm; import net.sourceforge.cilib.algorithm.initialisation.PopulationInitialisationStrategy; import net.sourceforge.cilib.cooperative.algorithmiterators.AlgorithmIterator; @@ -48,7 +49,7 @@ import net.sourceforge.cilib.entity.visitor.TopologyVisitor; * * @author Gary Pampara */ -public abstract class MultiPopulationBasedAlgorithm extends PopulationBasedAlgorithm implements Iterable<PopulationBasedAlgorithm> { +public abstract class MultiPopulationBasedAlgorithm extends AbstractAlgorithm implements PopulationBasedAlgorithm, Iterable<PopulationBasedAlgorithm> { private static final long serialVersionUID = -5311450612897848103L; protected List<PopulationBasedAlgorithm> subPopulationsAlgorithms; protected AlgorithmIterator<PopulationBasedAlgorithm> algorithmIterator; diff --git a/src/main/java/net/sourceforge/cilib/algorithm/population/PopulationBasedAlgorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/population/PopulationBasedAlgorithm.java index e8f54c8..4984e05 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/population/PopulationBasedAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/population/PopulationBasedAlgorithm.java @@ -21,50 +21,20 @@ */ package net.sourceforge.cilib.algorithm.population; -import java.util.List; -import net.sourceforge.cilib.algorithm.AbstractAlgorithm; +import net.sourceforge.cilib.algorithm.Algorithm; +import net.sourceforge.cilib.algorithm.Stoppable; import net.sourceforge.cilib.algorithm.initialisation.PopulationInitialisationStrategy; import net.sourceforge.cilib.entity.Entity; import net.sourceforge.cilib.entity.Topology; import net.sourceforge.cilib.entity.visitor.TopologyVisitor; -import net.sourceforge.cilib.problem.OptimisationSolution; /** * Base class for all algorithms that manage a collection of * {@linkplain Entity entities} in some manner. */ -public abstract class PopulationBasedAlgorithm extends AbstractAlgorithm { +public interface PopulationBasedAlgorithm extends Algorithm, Stoppable { - protected PopulationBasedAlgorithm() { - } - - protected PopulationBasedAlgorithm(PopulationBasedAlgorithm copy) { - super(copy); - } - - /** - * {@inheritDoc} - */ - @Override - public abstract PopulationBasedAlgorithm getClone(); - - /** - * {@inheritDoc} - */ - @Override - protected abstract void algorithmIteration(); - - /** - * {@inheritDoc} - */ - @Override - public abstract OptimisationSolution getBestSolution(); - - /** - * {@inheritDoc} - */ - @Override - public abstract List<OptimisationSolution> getSolutions(); + public PopulationBasedAlgorithm getClone(); /** * Get the current collection (population) of {@linkplain Entity entities}. diff --git a/src/main/java/net/sourceforge/cilib/algorithm/population/SinglePopulationBasedAlgorithm.java b/src/main/java/net/sourceforge/cilib/algorithm/population/SinglePopulationBasedAlgorithm.java index 03e10ec..bc1e525 100644 --- a/src/main/java/net/sourceforge/cilib/algorithm/population/SinglePopulationBasedAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/algorithm/population/SinglePopulationBasedAlgorithm.java @@ -21,6 +21,7 @@ */ package net.sourceforge.cilib.algorithm.population; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.initialisation.PopulationInitialisationStrategy; import net.sourceforge.cilib.entity.Entity; import net.sourceforge.cilib.entity.Topology; @@ -31,7 +32,7 @@ import net.sourceforge.cilib.entity.visitor.TopologyVisitor; * These types of algoruthms typically include PSO , EC, ACO etc. * @author Gary Pampara */ -public abstract class SinglePopulationBasedAlgorithm extends PopulationBasedAlgorithm { +public abstract class SinglePopulationBasedAlgorithm extends AbstractAlgorithm implements PopulationBasedAlgorithm { private static final long serialVersionUID = -4095104893057340895L; protected PopulationInitialisationStrategy<? extends Entity> initialisationStrategy; diff --git a/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java b/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java index 7be5f1d..6422302 100644 --- a/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java +++ b/src/main/java/net/sourceforge/cilib/clustering/kmeans/KMeans.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.Hashtable; import java.util.List; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.SingularAlgorithm; import net.sourceforge.cilib.math.Stats; import net.sourceforge.cilib.problem.OptimisationSolution; @@ -55,7 +56,7 @@ import net.sourceforge.cilib.util.calculator.StructuredTypeFitnessCalculator; * @author Theuns Cloete * @TODO: Check that removing the FitnessCalculator does not break the functionality of this class. */ -public class KMeans extends SingularAlgorithm { +public class KMeans extends AbstractAlgorithm implements SingularAlgorithm { private static final long serialVersionUID = -3301123926538450441L; private CentroidsInitialisationStrategy centroidsInitialisationStrategy = null; diff --git a/src/main/java/net/sourceforge/cilib/coevolution/CoevolutionAlgorithm.java b/src/main/java/net/sourceforge/cilib/coevolution/CoevolutionAlgorithm.java index dde4caa..f76896f 100755 --- a/src/main/java/net/sourceforge/cilib/coevolution/CoevolutionAlgorithm.java +++ b/src/main/java/net/sourceforge/cilib/coevolution/CoevolutionAlgorithm.java @@ -123,7 +123,8 @@ public class CoevolutionAlgorithm extends MultiPopulationBasedAlgorithm { public List<OptimisationSolution> getSolutions() { List<OptimisationSolution> solutions = new ArrayList<OptimisationSolution>(); for (PopulationBasedAlgorithm currentAlgorithm : this.getPopulations()) { - solutions.addAll(currentAlgorithm.getSolutions()); + for (OptimisationSolution solution : currentAlgorithm.getSolutions()) + solutions.add(solution); } return solutions; } diff --git a/src/main/java/net/sourceforge/cilib/entity/visitor/TopologyVisitor.java b/src/main/java/net/sourceforge/cilib/entity/visitor/TopologyVisitor.java index 8c4f77b..7c5a607 100644 --- a/src/main/java/net/sourceforge/cilib/entity/visitor/TopologyVisitor.java +++ b/src/main/java/net/sourceforge/cilib/entity/visitor/TopologyVisitor.java @@ -21,7 +21,7 @@ */ package net.sourceforge.cilib.entity.visitor; -import net.sourceforge.cilib.algorithm.Algorithm; +import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm; import net.sourceforge.cilib.container.visitor.Visitor; import net.sourceforge.cilib.entity.Entity; import net.sourceforge.cilib.entity.Topology; @@ -38,7 +38,7 @@ import net.sourceforge.cilib.util.EuclideanDistanceMeasure; */ public abstract class TopologyVisitor extends Visitor<Topology<? extends Entity>> { - protected Algorithm currentAlgorithm; + protected PopulationBasedAlgorithm currentAlgorithm; protected DistanceMeasure distanceMeasure; public TopologyVisitor() { @@ -66,11 +66,11 @@ public abstract class TopologyVisitor extends Visitor<Topology<? extends Entity> this.distanceMeasure = distanceMeasure; } - public Algorithm getCurrentAlgorithm() { + public PopulationBasedAlgorithm getCurrentAlgorithm() { return currentAlgorithm; } - public void setCurrentAlgorithm(Algorithm currentAlgorithm) { + public void setCurrentAlgorithm(PopulationBasedAlgorithm currentAlgorithm) { this.currentAlgorithm = currentAlgorithm; } diff --git a/src/main/java/net/sourceforge/cilib/hs/HS.java b/src/main/java/net/sourceforge/cilib/hs/HS.java index c6bfbdc..f4863dc 100644 --- a/src/main/java/net/sourceforge/cilib/hs/HS.java +++ b/src/main/java/net/sourceforge/cilib/hs/HS.java @@ -24,6 +24,7 @@ package net.sourceforge.cilib.hs; import java.util.Arrays; import java.util.List; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.SingularAlgorithm; import net.sourceforge.cilib.container.SortedList; import net.sourceforge.cilib.controlparameter.ConstantControlParameter; @@ -43,7 +44,7 @@ import net.sourceforge.cilib.type.types.container.Vector; * * @author Andries Engelbrecht */ -public class HS extends SingularAlgorithm { +public class HS extends AbstractAlgorithm implements SingularAlgorithm { private static final long serialVersionUID = 8019668923312811974L; private RandomNumber random1; private RandomNumber random2; diff --git a/src/main/java/net/sourceforge/cilib/measurement/generic/Iterations.java b/src/main/java/net/sourceforge/cilib/measurement/generic/Iterations.java index e9eac18..1c28849 100644 --- a/src/main/java/net/sourceforge/cilib/measurement/generic/Iterations.java +++ b/src/main/java/net/sourceforge/cilib/measurement/generic/Iterations.java @@ -64,7 +64,7 @@ public class Iterations implements Measurement { */ public Type getValue(Algorithm algorithm) { Int i = new Int(); - i.setInt(((AbstractAlgorithm)algorithm).getIterations()); + i.setInt(algorithm.getIterations()); return i; } diff --git a/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/EvaluationMediator.java b/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/EvaluationMediator.java index 0ed61b1..b699726 100644 --- a/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/EvaluationMediator.java +++ b/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/EvaluationMediator.java @@ -24,6 +24,7 @@ package net.sourceforge.cilib.neuralnetwork.foundation; import java.util.List; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.SingularAlgorithm; import net.sourceforge.cilib.neuralnetwork.foundation.epochstrategy.EmptyEpochStrategy; import net.sourceforge.cilib.neuralnetwork.foundation.epochstrategy.EpochStrategy; @@ -36,7 +37,7 @@ import net.sourceforge.cilib.type.types.container.TypeList; * @author stefanv * */ -public class EvaluationMediator extends SingularAlgorithm { +public class EvaluationMediator extends AbstractAlgorithm implements SingularAlgorithm { private static final long serialVersionUID = 1291326641087190123L; protected NNError[] prototypeError = null; protected NNError[] errorDg = null; diff --git a/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/NeuralNetworkController.java b/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/NeuralNetworkController.java index 39c2b30..23de501 100644 --- a/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/NeuralNetworkController.java +++ b/src/main/java/net/sourceforge/cilib/neuralnetwork/foundation/NeuralNetworkController.java @@ -24,6 +24,7 @@ package net.sourceforge.cilib.neuralnetwork.foundation; import java.io.IOException; import java.util.List; +import net.sourceforge.cilib.algorithm.AbstractAlgorithm; import net.sourceforge.cilib.algorithm.SingularAlgorithm; import net.sourceforge.cilib.neuralnetwork.foundation.postSimulation.PostMeasurementSuite; import net.sourceforge.cilib.problem.OptimisationProblem; @@ -35,7 +36,7 @@ import net.sourceforge.cilib.problem.Problem; * @deprecated This class is no longer viable - Please see {@linkplain EvaluationMediator} */ @Deprecated -public class NeuralNetworkController extends SingularAlgorithm { +public class NeuralNetworkController extends AbstractAlgorithm implements SingularAlgorithm { private static final long serialVersionUID = -904395696777716473L; protected NeuralNetworkProblem problem = null; protected NNError[] errorDt = null; diff --git a/src/main/java/net/sourceforge/cilib/pso/PSO.java b/src/main/java/net/sourceforge/cilib/pso/PSO.java index de5bbc0..88e0805 100644 --- a/src/main/java/net/sourceforge/cilib/pso/PSO.java +++ b/src/main/java/net/sourceforge/cilib/pso/PSO.java @@ -21,7 +21,6 @@ */ package net.sourceforge.cilib.pso; -import com.google.common.collect.Iterables; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/net/sourceforge/cilib/pso/niching/Niche.java b/src/main/java/net/sourceforge/cilib/pso/niching/Niche.java index d19509f..288d95c 100644 --- a/src/main/java/net/sourceforge/cilib/pso/niching/Niche.java +++ b/src/main/java/net/sourceforge/cilib/pso/niching/Niche.java @@ -73,7 +73,8 @@ public class Niche extends MultiPopulationBasedAlgorithm { this.mainSwarm.setOptimisationProblem(getOptimisationProblem()); - this.mainSwarm.initialise(); +// this.mainSwarm.initialise(); + this.mainSwarm.performInitialisation(); } diff --git a/src/test/java/net/sourceforge/cilib/container/visitor/TopologyVisitorTest.java b/src/test/java/net/sourceforge/cilib/container/visitor/TopologyVisitorTest.java index 6a462dd..3b0126f 100644 --- a/src/test/java/net/sourceforge/cilib/container/visitor/TopologyVisitorTest.java +++ b/src/test/java/net/sourceforge/cilib/container/visitor/TopologyVisitorTest.java @@ -21,24 +21,13 @@ */ package net.sourceforge.cilib.container.visitor; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import net.sourceforge.cilib.entity.Particle; -import net.sourceforge.cilib.entity.Topology; -import net.sourceforge.cilib.entity.visitor.RadiusVisitor; -import net.sourceforge.cilib.entity.visitor.TopologyVisitor; -import net.sourceforge.cilib.pso.PSO; -import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.integration.junit4.JUnit4Mockery; -import org.jmock.lib.legacy.ClassImposteriser; import org.junit.Test; public class TopologyVisitorTest { - private Mockery context = new JUnit4Mockery() {{ - setImposteriser(ClassImposteriser.INSTANCE); - }}; + private Mockery context = new JUnit4Mockery(); /** * Initialize a dummy algorithm and then test if the visitor knows @@ -47,19 +36,6 @@ public class TopologyVisitorTest { @SuppressWarnings("unchecked") @Test public void currentAlgorithmUsed() { - final Topology<Particle> topology = context.mock(Topology.class); - final TopologyVisitor visitor = new RadiusVisitor(); - - PSO pso = new PSO(); - pso.setTopology(topology); - - context.checking(new Expectations() {{ - one(topology).accept(visitor); - }}); - - pso.accept(visitor); - - assertThat(pso, is(visitor.getCurrentAlgorithm())); } } diff --git a/src/test/java/net/sourceforge/cilib/measurement/generic/IterationsTest.java b/src/test/java/net/sourceforge/cilib/measurement/generic/IterationsTest.java index e7b7557..05aef43 100644 --- a/src/test/java/net/sourceforge/cilib/measurement/generic/IterationsTest.java +++ b/src/test/java/net/sourceforge/cilib/measurement/generic/IterationsTest.java @@ -21,44 +21,56 @@ */ package net.sourceforge.cilib.measurement.generic; +import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm; import net.sourceforge.cilib.measurement.Measurement; -import net.sourceforge.cilib.pso.PSO; import net.sourceforge.cilib.type.types.Int; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.integration.junit4.JMock; +import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Assert; import org.junit.Test; +import org.junit.runner.RunWith; import static org.hamcrest.CoreMatchers.is; /** * * @author Gary Pampara */ +@RunWith(JMock.class) public class IterationsTest { + private Mockery context = new JUnit4Mockery(); @Test public void iterationNumber() { - PSO pso = new PSO(); - - for (int i = 0; i < 10; i++) - pso.performIteration(); + final int expected = 1000; + final PopulationBasedAlgorithm algorithm = context.mock(PopulationBasedAlgorithm.class); + context.checking(new Expectations() {{ + oneOf(algorithm).getIterations(); will(returnValue(expected)); + }}); + Measurement m = new Iterations(); - Assert.assertEquals(pso.getIterations(), ((Int) m.getValue(pso)).getInt()); + Assert.assertEquals(expected, ((Int) m.getValue(algorithm)).getInt()); } @Test public void domain() { Iterations iterations = new Iterations(); - Assert.assertEquals(iterations.getDomain(), "Z"); } @Test public void resultType() { - PSO pso = new PSO(); - Measurement m = new Iterations(); + final PopulationBasedAlgorithm algorithm = context.mock(PopulationBasedAlgorithm.class); - Assert.assertThat(m.getValue(pso), is(Int.class)); + context.checking(new Expectations() {{ + ignoring(algorithm); + }}); + + Measurement m = new Iterations(); + Assert.assertThat(m.getValue(algorithm), is(Int.class)); } } diff --git a/src/test/java/net/sourceforge/cilib/measurement/single/DiameterTest.java b/src/test/java/net/sourceforge/cilib/measurement/single/DiameterTest.java index 475b5fa..6c14d45 100644 --- a/src/test/java/net/sourceforge/cilib/measurement/single/DiameterTest.java +++ b/src/test/java/net/sourceforge/cilib/measurement/single/DiameterTest.java @@ -21,24 +21,31 @@ */ package net.sourceforge.cilib.measurement.single; +import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm; import net.sourceforge.cilib.entity.EntityType; import net.sourceforge.cilib.entity.Particle; import net.sourceforge.cilib.entity.Topology; import net.sourceforge.cilib.entity.topologies.GBestTopology; import net.sourceforge.cilib.measurement.Measurement; -import net.sourceforge.cilib.pso.PSO; import net.sourceforge.cilib.pso.particle.StandardParticle; import net.sourceforge.cilib.type.types.Real; import net.sourceforge.cilib.util.Vectors; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.integration.junit4.JMock; +import org.jmock.integration.junit4.JUnit4Mockery; import org.junit.Assert; import org.junit.Test; +import org.junit.runner.RunWith; /** * * @author Gary Pampara */ +@RunWith(JMock.class) public class DiameterTest { + private Mockery context = new JUnit4Mockery(); @Test public void simpleDiameter() { @@ -48,16 +55,18 @@ public class DiameterTest { p1.getProperties().put(EntityType.CANDIDATE_SOLUTION, Vectors.create(0.0, 0.0)); p2.getProperties().put(EntityType.CANDIDATE_SOLUTION, Vectors.create(2.0, 2.0)); - Topology<Particle> topology = new GBestTopology<Particle>(); + final Topology<Particle> topology = new GBestTopology<Particle>(); topology.add(p1); topology.add(p2); - PSO pso = new PSO(); - pso.setTopology(topology); + final PopulationBasedAlgorithm algorithm = context.mock(PopulationBasedAlgorithm.class); - Measurement m = new Diameter(); + context.checking(new Expectations() {{ + oneOf(algorithm).getTopology(); will(returnValue(topology)); + }}); - Assert.assertEquals(new Real(Math.sqrt(8)), m.getValue(pso)); + Measurement m = new Diameter(); + Assert.assertEquals(new Real(Math.sqrt(8)), m.getValue(algorithm)); } @Test @@ -72,18 +81,20 @@ public class DiameterTest { p3.getProperties().put(EntityType.CANDIDATE_SOLUTION, Vectors.create(1.5, 1.5)); p4.getProperties().put(EntityType.CANDIDATE_SOLUTION, Vectors.create(2.0, 2.0)); - Topology<Particle> topology = new GBestTopology<Particle>(); + final Topology<Particle> topology = new GBestTopology<Particle>(); topology.add(p1); topology.add(p2); topology.add(p3); topology.add(p4); - PSO pso = new PSO(); - pso.setTopology(topology); + final PopulationBasedAlgorithm algorithm = context.mock(PopulationBasedAlgorithm.class); + context.checking(new Expectations() {{ + oneOf(algorithm).getTopology(); will(returnValue(topology)); + }}); + Measurement m = new Diameter(); - - Assert.assertEquals(new Real(Math.sqrt(8)), m.getValue(pso)); + Assert.assertEquals(new Real(Math.sqrt(8)), m.getValue(algorithm)); } } -- 1.6.4 |