Skip to content

Commit 5015a20

Browse files
author
cogmission
committed
Add new tests to LayerTest
1 parent bb72e4c commit 5015a20

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

src/main/java/org/numenta/nupic/network/Layer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ public Layer<T> postDeSerialize() {
344344
public void setNetwork(Network network) {
345345
this.parentNetwork = network;
346346
}
347+
348+
/**
349+
* Returns the parent {@link Network}
350+
* @return the parent Network;
351+
*/
352+
public Network getNetwork() {
353+
return this.parentNetwork;
354+
}
347355

348356
/**
349357
* Creates a new {@code Layer} initialized with the specified algorithmic
@@ -415,6 +423,15 @@ public CheckPointOp<byte[]> delegateCheckPointCall() {
415423
public void setRegion(Region r) {
416424
this.parentRegion = r;
417425
}
426+
427+
/**
428+
* Returns the parent {@link Region}
429+
*
430+
* @return the parent Region
431+
*/
432+
public Region getRegion() {
433+
return this.parentRegion;
434+
}
418435

419436
/**
420437
* Finalizes the initialization in one method call so that side effect

src/test/java/org/numenta/nupic/network/LayerTest.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.numenta.nupic.algorithms.TemporalMemory;
4949
import org.numenta.nupic.datagen.ResourceLocator;
5050
import org.numenta.nupic.encoders.MultiEncoder;
51+
import org.numenta.nupic.model.Connections;
5152
import org.numenta.nupic.model.SDR;
5253
import org.numenta.nupic.network.Layer.FunctionFactory;
5354
import org.numenta.nupic.network.sensor.FileSensor;
@@ -111,6 +112,129 @@ public void testMasking() {
111112
algo_content_mask ^= Layer.CLA_CLASSIFIER;
112113
assertEquals(0, algo_content_mask);
113114
}
115+
116+
@Test
117+
public void callsOnClosedLayer() {
118+
Parameters p = NetworkTestHarness.getParameters().copy();
119+
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
120+
p.set(KEY.RANDOM, new UniversalRandom(42));
121+
122+
Network n = new Network("AlreadyClosed", p)
123+
.add(Network.createRegion("AlreadyClosed")
124+
.add(Network.createLayer("AlreadyClosed", p)));
125+
126+
Layer<?> l = n.lookup("AlreadyClosed").lookup("AlreadyClosed");
127+
l.using(new Connections());
128+
l.using(p);
129+
130+
l.close();
131+
132+
try {
133+
l.using(new Connections());
134+
135+
fail(); // Should fail here, disallowing "using" call on closed layer
136+
}catch(Exception e) {
137+
assertEquals(IllegalStateException.class, e.getClass());
138+
assertEquals("Layer already \"closed\"", e.getMessage());
139+
}
140+
141+
try {
142+
l.using(p);
143+
144+
fail(); // Should fail here, disallowing "using" call on closed layer
145+
}catch(Exception e) {
146+
assertEquals(IllegalStateException.class, e.getClass());
147+
assertEquals("Layer already \"closed\"", e.getMessage());
148+
}
149+
}
150+
151+
@Test
152+
public void testNoName() {
153+
Parameters p = Parameters.getAllDefaultParameters();
154+
155+
try {
156+
new Network("", p)
157+
.add(Network.createRegion("")
158+
.add(Network.createLayer("", p)
159+
.add(Sensor.create(
160+
FileSensor::create,
161+
SensorParams.create(
162+
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));
163+
164+
fail(); // Fails due to no name...
165+
}catch(Exception e) {
166+
assertEquals(IllegalStateException.class, e.getClass());
167+
assertEquals("All Networks must have a name. Increases digestion, and overall happiness!",
168+
e.getMessage());
169+
}
170+
171+
try {
172+
new Network("Name", p)
173+
.add(Network.createRegion("")
174+
.add(Network.createLayer("", p)
175+
.add(Sensor.create(
176+
FileSensor::create,
177+
SensorParams.create(
178+
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));
179+
180+
fail(); // Fails due to no name on Region...
181+
}catch(Exception e) {
182+
assertEquals(IllegalArgumentException.class, e.getClass());
183+
assertEquals("Name may not be null or empty. ...not that anyone here advocates name calling!",
184+
e.getMessage());
185+
}
186+
187+
try {
188+
p = NetworkTestHarness.getParameters().copy();
189+
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
190+
p.set(KEY.RANDOM, new UniversalRandom(42));
191+
192+
PublisherSupplier supplier = PublisherSupplier.builder()
193+
.addHeader("dayOfWeek")
194+
.addHeader("int")
195+
.addHeader("B").build();
196+
197+
Network n = new Network("Name", p)
198+
.add(Network.createRegion("Name")
199+
.add(Network.createLayer("Name", p)));
200+
201+
Layer<?> l = n.lookup("Name").lookup("Name");
202+
l.add(Sensor.create(
203+
ObservableSensor::create,
204+
SensorParams.create(
205+
Keys::obs, "", supplier)));
206+
207+
assertEquals(n, l.getNetwork());
208+
assertTrue(l.getRegion() != null);
209+
210+
}catch(Exception e) {
211+
e.printStackTrace();
212+
}
213+
}
214+
215+
@Test
216+
public void testAddSensor() {
217+
Parameters p = NetworkTestHarness.getParameters().copy();
218+
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
219+
p.set(KEY.RANDOM, new UniversalRandom(42));
220+
221+
try {
222+
new Network("Name", p)
223+
.add(Network.createRegion("")
224+
.add(Network.createLayer("", p)
225+
.add(Sensor.create(
226+
FileSensor::create,
227+
SensorParams.create(
228+
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));
229+
230+
fail(); // Fails due to no name...
231+
}catch(Exception e) {
232+
e.printStackTrace();
233+
assertEquals(IllegalStateException.class, e.getClass());
234+
assertEquals("All Networks must have a name. Increases digestion, and overall happiness!",
235+
e.getMessage());
236+
}
237+
}
114238

115239
@Test
116240
public void testGetAllValues() {

0 commit comments

Comments
 (0)