Skip to content

Commit 49d6cf3

Browse files
factory refactoring
1 parent f5a37cf commit 49d6cf3

File tree

5 files changed

+46
-58
lines changed

5 files changed

+46
-58
lines changed

src/main/java/network/aika/AbstractNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void reactivate() {}
5151
public static <P extends Provider> AbstractNode read(DataInput in, P p) throws IOException {
5252
AbstractNode n;
5353
if(in.readBoolean()) {
54-
n = INeuron.readNeuron(in, (Neuron) p);
54+
n = p.getModel().readNeuron(in, (Neuron) p);
5555
} else {
5656
n = Node.readNode(in, p);
5757
}

src/main/java/network/aika/Document.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,17 @@ public Document(Model model, int id, String content, int threadId) {
160160

161161
this.model = model;
162162
this.threadId = threadId;
163-
this.linker = model.getLinkerFactory().createLinker(this);
163+
this.linker = initLinker();
164164

165165
model.acquireThread(threadId, this);
166166
}
167167

168168

169+
protected Linker initLinker() {
170+
return new Linker(this);
171+
}
172+
173+
169174
public int getId() {
170175
return id;
171176
}

src/main/java/network/aika/Model.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import network.aika.neuron.activation.Linker;
2727
import network.aika.neuron.activation.SearchNode;
2828

29+
import java.io.DataInput;
30+
import java.io.DataOutput;
31+
import java.io.IOException;
2932
import java.lang.ref.WeakReference;
3033
import java.util.*;
3134
import java.util.concurrent.atomic.AtomicInteger;
@@ -54,10 +57,6 @@ public class Model {
5457

5558
public SuspensionHook suspensionHook;
5659

57-
58-
protected SynapseFactory synapseFactory = (input, output, id) -> new Synapse(input, output, id);
59-
protected LinkerFactory linkerFactory = (doc) -> new Linker(doc);
60-
6160
public SearchNode.SkipSelectStep skipSelectStep = (act) -> false;
6261

6362
public AtomicInteger docIdCounter = new AtomicInteger(0);
@@ -90,14 +89,6 @@ public Model(SuspensionHook sh, int numberOfThreads) {
9089
}
9190

9291

93-
public SynapseFactory getSynapseFactory() {
94-
return synapseFactory;
95-
}
96-
97-
public void setSynapseFactory(SynapseFactory synapseFactory) {
98-
this.synapseFactory = synapseFactory;
99-
}
100-
10192
public SuspensionHook getSuspensionHook() {
10293
return suspensionHook;
10394
}
@@ -108,15 +99,6 @@ public void setSuspensionHook(SuspensionHook suspensionHook) {
10899
}
109100

110101

111-
public LinkerFactory getLinkerFactory() {
112-
return linkerFactory;
113-
}
114-
115-
public void setLinkerFactory(LinkerFactory linkerFactory) {
116-
this.linkerFactory = linkerFactory;
117-
}
118-
119-
120102
public SearchNode.SkipSelectStep getSkipSelectStep() {
121103
return skipSelectStep;
122104
}
@@ -150,6 +132,25 @@ public Neuron createNeuron(String label, Type type, ActivationFunction actF, Str
150132
}
151133

152134

135+
public INeuron readNeuron(DataInput in, Neuron p) throws IOException {
136+
INeuron n = new INeuron(p);
137+
n.readFields(in, this);
138+
return n;
139+
}
140+
141+
142+
public Synapse readSynapse(DataInput in) throws IOException {
143+
Synapse s = new Synapse();
144+
s.readFields(in, this);
145+
return s;
146+
}
147+
148+
149+
public void writeSynapse(Synapse s, DataOutput out) throws IOException {
150+
s.write(out);
151+
}
152+
153+
153154
public int getNewDocumentId() {
154155
return docIdCounter.addAndGet(1);
155156
}
@@ -275,17 +276,6 @@ public void removeProvider(Provider p) {
275276
}
276277

277278

278-
public interface SynapseFactory {
279-
Synapse createSynapse(Neuron input, Neuron output, Integer id);
280-
}
281-
282-
283-
public interface LinkerFactory {
284-
285-
Linker createLinker(Document doc);
286-
}
287-
288-
289279
public static class StaleDocumentException extends RuntimeException {
290280

291281
public StaleDocumentException() {

src/main/java/network/aika/neuron/INeuron.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ private INeuron() {
362362
}
363363

364364

365+
public INeuron(Neuron p) {
366+
provider = p;
367+
threads = new INeuron.ThreadState[p.getModel().numberOfThreads];
368+
}
369+
370+
365371
public INeuron(Model m, String label, Type type, ActivationFunction actF) {
366372
this(m, label, null, type, actF);
367373
}
@@ -575,7 +581,7 @@ public void write(DataOutput out) throws IOException {
575581
for (Synapse s : inputSynapses.values()) {
576582
if (s.getInput() != null) {
577583
out.writeBoolean(true);
578-
s.write(out);
584+
getModel().writeSynapse(s, out);
579585

580586
out.writeBoolean(passiveInputSynapses != null && passiveInputSynapses.containsKey(s));
581587
}
@@ -584,7 +590,7 @@ public void write(DataOutput out) throws IOException {
584590
for (Synapse s : outputSynapses.values()) {
585591
if (s.getOutput() != null) {
586592
out.writeBoolean(true);
587-
s.write(out);
593+
getModel().writeSynapse(s, out);
588594
}
589595
}
590596
out.writeBoolean(false);
@@ -630,7 +636,7 @@ public void readFields(DataInput in, Model m) throws IOException {
630636

631637
synapseIdCounter = in.readInt();
632638
while (in.readBoolean()) {
633-
Synapse syn = Synapse.read(in, m);
639+
Synapse syn = m.readSynapse(in);
634640
inputSynapses.put(syn, syn);
635641

636642
if(in.readBoolean()) {
@@ -639,7 +645,7 @@ public void readFields(DataInput in, Model m) throws IOException {
639645
}
640646

641647
while (in.readBoolean()) {
642-
Synapse syn = Synapse.read(in, m);
648+
Synapse syn = m.readSynapse(in);
643649
outputSynapses.put(syn, syn);
644650
}
645651

@@ -764,15 +770,6 @@ public void register(Activation act) {
764770
}
765771

766772

767-
public static INeuron readNeuron(DataInput in, Neuron p) throws IOException {
768-
INeuron n = new INeuron();
769-
n.provider = p;
770-
n.threads = new ThreadState[p.getModel().numberOfThreads];
771-
n.readFields(in, p.getModel());
772-
return n;
773-
}
774-
775-
776773
public boolean isPassiveInputNeuron() {
777774
return passiveInputFunction != null;
778775
}

src/main/java/network/aika/neuron/Synapse.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,7 @@ public void readFields(DataInput in, Model m) throws IOException {
426426
}
427427

428428

429-
public static Synapse read(DataInput in, Model m) throws IOException {
430-
Synapse s = new Synapse();
431-
s.readFields(in, m);
432-
return s;
433-
}
434-
435-
436-
437-
public static Synapse createOrReplace(Document doc, Integer synapseId, Neuron inputNeuron, Neuron outputNeuron) {
429+
public static Synapse createOrReplace(Document doc, Integer synapseId, Neuron inputNeuron, Neuron outputNeuron, SynapseFactory synapseFactory) {
438430
outputNeuron.get(doc);
439431
inputNeuron.get(doc);
440432
outputNeuron.lock.acquireWriteLock();
@@ -445,7 +437,7 @@ public static Synapse createOrReplace(Document doc, Integer synapseId, Neuron in
445437
outputNeuron.lock.releaseWriteLock();
446438

447439
if(synapse == null) {
448-
synapse = outputNeuron.getModel().getSynapseFactory().createSynapse(inputNeuron, outputNeuron, synapseId);
440+
synapse = synapseFactory.createSynapse(inputNeuron, outputNeuron, synapseId);
449441
} else {
450442
synapse.input = inputNeuron;
451443
synapse.output = outputNeuron;
@@ -580,7 +572,7 @@ public void registerSynapseIds(Neuron n) {
580572

581573

582574
public Synapse getSynapse(Neuron outputNeuron) {
583-
Synapse s = createOrReplace(null, synapseId, neuron, outputNeuron);
575+
Synapse s = createOrReplace(null, synapseId, neuron, outputNeuron, (input, output, id) -> new Synapse(input, output, id));
584576

585577
s.isRecurrent = recurrent;
586578
s.identity = identity;
@@ -589,4 +581,8 @@ public Synapse getSynapse(Neuron outputNeuron) {
589581
return s;
590582
}
591583
}
584+
585+
public interface SynapseFactory {
586+
Synapse createSynapse(Neuron input, Neuron output, Integer id);
587+
}
592588
}

0 commit comments

Comments
 (0)