From 5015a20ce4ae6985199d246e3352685147ddfd33 Mon Sep 17 00:00:00 2001 From: cogmission Date: Sun, 16 Oct 2016 19:08:24 -0500 Subject: [PATCH] Add new tests to LayerTest --- .../java/org/numenta/nupic/network/Layer.java | 17 +++ .../org/numenta/nupic/network/LayerTest.java | 124 ++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/src/main/java/org/numenta/nupic/network/Layer.java b/src/main/java/org/numenta/nupic/network/Layer.java index 71458b78..e8fa3fb2 100644 --- a/src/main/java/org/numenta/nupic/network/Layer.java +++ b/src/main/java/org/numenta/nupic/network/Layer.java @@ -344,6 +344,14 @@ public Layer postDeSerialize() { public void setNetwork(Network network) { this.parentNetwork = network; } + + /** + * Returns the parent {@link Network} + * @return the parent Network; + */ + public Network getNetwork() { + return this.parentNetwork; + } /** * Creates a new {@code Layer} initialized with the specified algorithmic @@ -415,6 +423,15 @@ public CheckPointOp delegateCheckPointCall() { public void setRegion(Region r) { this.parentRegion = r; } + + /** + * Returns the parent {@link Region} + * + * @return the parent Region + */ + public Region getRegion() { + return this.parentRegion; + } /** * Finalizes the initialization in one method call so that side effect diff --git a/src/test/java/org/numenta/nupic/network/LayerTest.java b/src/test/java/org/numenta/nupic/network/LayerTest.java index 80436dea..42d15561 100644 --- a/src/test/java/org/numenta/nupic/network/LayerTest.java +++ b/src/test/java/org/numenta/nupic/network/LayerTest.java @@ -48,6 +48,7 @@ import org.numenta.nupic.algorithms.TemporalMemory; import org.numenta.nupic.datagen.ResourceLocator; import org.numenta.nupic.encoders.MultiEncoder; +import org.numenta.nupic.model.Connections; import org.numenta.nupic.model.SDR; import org.numenta.nupic.network.Layer.FunctionFactory; import org.numenta.nupic.network.sensor.FileSensor; @@ -111,6 +112,129 @@ public void testMasking() { algo_content_mask ^= Layer.CLA_CLASSIFIER; assertEquals(0, algo_content_mask); } + + @Test + public void callsOnClosedLayer() { + Parameters p = NetworkTestHarness.getParameters().copy(); + p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); + p.set(KEY.RANDOM, new UniversalRandom(42)); + + Network n = new Network("AlreadyClosed", p) + .add(Network.createRegion("AlreadyClosed") + .add(Network.createLayer("AlreadyClosed", p))); + + Layer l = n.lookup("AlreadyClosed").lookup("AlreadyClosed"); + l.using(new Connections()); + l.using(p); + + l.close(); + + try { + l.using(new Connections()); + + fail(); // Should fail here, disallowing "using" call on closed layer + }catch(Exception e) { + assertEquals(IllegalStateException.class, e.getClass()); + assertEquals("Layer already \"closed\"", e.getMessage()); + } + + try { + l.using(p); + + fail(); // Should fail here, disallowing "using" call on closed layer + }catch(Exception e) { + assertEquals(IllegalStateException.class, e.getClass()); + assertEquals("Layer already \"closed\"", e.getMessage()); + } + } + + @Test + public void testNoName() { + Parameters p = Parameters.getAllDefaultParameters(); + + try { + new Network("", p) + .add(Network.createRegion("") + .add(Network.createLayer("", p) + .add(Sensor.create( + FileSensor::create, + SensorParams.create( + Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv")))))); + + fail(); // Fails due to no name... + }catch(Exception e) { + assertEquals(IllegalStateException.class, e.getClass()); + assertEquals("All Networks must have a name. Increases digestion, and overall happiness!", + e.getMessage()); + } + + try { + new Network("Name", p) + .add(Network.createRegion("") + .add(Network.createLayer("", p) + .add(Sensor.create( + FileSensor::create, + SensorParams.create( + Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv")))))); + + fail(); // Fails due to no name on Region... + }catch(Exception e) { + assertEquals(IllegalArgumentException.class, e.getClass()); + assertEquals("Name may not be null or empty. ...not that anyone here advocates name calling!", + e.getMessage()); + } + + try { + p = NetworkTestHarness.getParameters().copy(); + p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); + p.set(KEY.RANDOM, new UniversalRandom(42)); + + PublisherSupplier supplier = PublisherSupplier.builder() + .addHeader("dayOfWeek") + .addHeader("int") + .addHeader("B").build(); + + Network n = new Network("Name", p) + .add(Network.createRegion("Name") + .add(Network.createLayer("Name", p))); + + Layer l = n.lookup("Name").lookup("Name"); + l.add(Sensor.create( + ObservableSensor::create, + SensorParams.create( + Keys::obs, "", supplier))); + + assertEquals(n, l.getNetwork()); + assertTrue(l.getRegion() != null); + + }catch(Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testAddSensor() { + Parameters p = NetworkTestHarness.getParameters().copy(); + p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams()); + p.set(KEY.RANDOM, new UniversalRandom(42)); + + try { + new Network("Name", p) + .add(Network.createRegion("") + .add(Network.createLayer("", p) + .add(Sensor.create( + FileSensor::create, + SensorParams.create( + Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv")))))); + + fail(); // Fails due to no name... + }catch(Exception e) { + e.printStackTrace(); + assertEquals(IllegalStateException.class, e.getClass()); + assertEquals("All Networks must have a name. Increases digestion, and overall happiness!", + e.getMessage()); + } + } @Test public void testGetAllValues() {