Skip to content

Commit b0bd6c6

Browse files
committed
#2 - factory start
1 parent 4d37a89 commit b0bd6c6

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package dev.obrienlabs.ml.neural_network.entity;
22

3+
import java.util.List;
4+
35
public interface Node {
6+
7+
long getId();
8+
9+
void setId(long id);
10+
11+
long getLayer();
12+
13+
void setLayer(long layer);
14+
15+
boolean isState();
16+
17+
void setState(boolean state);
18+
19+
Connection getInput();
20+
21+
void setInput(Connection input);
22+
23+
Connection getOutput();
24+
25+
void setOutput(Connection output);
26+
27+
List<Connection> getOutgoing();
28+
29+
void setOutgoing(List<Connection> outgoing);
30+
31+
List<Connection> getIncoming();
32+
33+
void setIncoming(List<Connection> incoming);
34+
35+
Node getLeft();
36+
37+
void setLeft(Node left);
38+
39+
Node getRight();
40+
41+
void setRight(Node right);
42+
443

544
}

src/main/java/dev/obrienlabs/ml/neural_network/entity/NodeImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ public class NodeImpl implements Node {
1212
private List<Connection> incoming;
1313
private Node left;
1414
private Node right;
15+
16+
17+
public NodeImpl() {
18+
// synchronized by nature of the owning object's thread
19+
20+
}
21+
22+
// serialize object construction not at the method but at the statement level
23+
public static Node instance(long layer, long neuron) {
24+
Node node = new NodeImpl();
25+
node.setId(Long.valueOf(Long.toString(layer) + Long.toString(neuron)));
26+
27+
return node;
28+
}
29+
1530
public long getId() {
1631
return id;
1732
}

src/main/java/dev/obrienlabs/ml/neural_network/service/NeuralNetwork.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
public interface NeuralNetwork {
99

10+
String display();
1011
List<Node> getNodes();
1112
void setNodes(List<Node> nodes);
1213
Node getNode(long neuron, long layer);
@@ -21,7 +22,7 @@ public interface NeuralNetwork {
2122

2223

2324
long init();
24-
long configureFullMesh(long neurons, long layers);
25+
long configureFullMesh(long layers, long neurons);
2526
long train();
2627
long trainFor(long iterations);
2728
long inference();

src/main/java/dev/obrienlabs/ml/neural_network/service/NeuralNetworkImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.obrienlabs.ml.neural_network.service;
22

33
import java.util.List;
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
46
import java.util.stream.LongStream;
57

68
import dev.obrienlabs.ml.neural_network.entity.Connection;
@@ -9,6 +11,7 @@
911

1012
public class NeuralNetworkImpl implements NeuralNetwork {
1113

14+
Map<Long, Node> nodes = new ConcurrentHashMap<>();
1215

1316

1417
@Override
@@ -30,7 +33,7 @@ public long configureFullMesh(long neurons, long layers) {
3033
LongStream.range(0, neurons).forEach(neuron -> {
3134
System.out.println(layer + ":" + neuron);
3235
// TODO: use a factory method
33-
Node node = new NodeImpl();
36+
addNode(NodeImpl.instance(layer, neuron));
3437

3538

3639
}
@@ -83,8 +86,7 @@ public Node getNode(long neuron, long layer) {
8386

8487
@Override
8588
public void addNode(Node node) {
86-
// TODO Auto-generated method stub
87-
89+
nodes.put(node.getId(), node);
8890
}
8991

9092
@Override
@@ -129,4 +131,11 @@ public List<Connection> getConnectionsTo(Node to) {
129131
return null;
130132
}
131133

134+
@Override
135+
public String display() {
136+
StringBuffer buffer = new StringBuffer();
137+
nodes.entrySet().stream().forEach(n -> System.out.println(n));
138+
return buffer.toString();
139+
}
140+
132141
}

src/test/java/dev/obrienlabs/ml/neural_network/service/NeuralNetworkImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public void testIteration1() {
77
nn.init();
88
nn.configureFullMesh(3, 3);
99
nn.trainFor(10L);
10+
nn.display();
1011

1112
}
1213

0 commit comments

Comments
 (0)