|
1 | 1 | package de.dmi3y.behaiv.kernel;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.core.type.TypeReference; |
4 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 3 | import de.dmi3y.behaiv.storage.BehaivStorage;
|
6 | 4 | import de.dmi3y.behaiv.tools.Pair;
|
7 | 5 |
|
8 |
| -import java.io.BufferedReader; |
9 |
| -import java.io.BufferedWriter; |
10 |
| -import java.io.FileReader; |
11 |
| -import java.io.FileWriter; |
12 | 6 | import java.io.IOException;
|
13 |
| -import java.util.ArrayList; |
14 | 7 | import java.util.List;
|
15 | 8 |
|
16 |
| -public abstract class Kernel { |
| 9 | +public interface Kernel { |
| 10 | + void setId(String id); |
17 | 11 |
|
18 |
| - protected String id; |
19 |
| - protected Long treshold = 10L; |
20 |
| - protected ObjectMapper objectMapper; |
21 |
| - protected boolean partialFitAllowed = false; |
22 |
| - protected boolean alwaysKeepData = true; |
| 12 | + boolean isEmpty(); |
23 | 13 |
|
24 |
| - public Kernel(String id) { |
25 |
| - this.id = id; |
26 |
| - objectMapper = new ObjectMapper(); |
27 |
| - } |
| 14 | + void fit(List<Pair<List<Double>, String>> data); |
28 | 15 |
|
| 16 | + void fit(); |
29 | 17 |
|
30 |
| - public void setId(String id) { |
31 |
| - this.id = id; |
32 |
| - } |
| 18 | + void setTreshold(Long treshold); |
33 | 19 |
|
| 20 | + boolean readyToPredict(); |
34 | 21 |
|
35 |
| - //list<features>, label |
36 |
| - protected List<Pair<List<Double>, String>> data = new ArrayList<>(); |
| 22 | + void update(List<Pair<List<Double>, String>> data); |
37 | 23 |
|
| 24 | + boolean isPartialFitAllowed(); |
38 | 25 |
|
39 |
| - public abstract boolean isEmpty(); |
| 26 | + void updateSingle(List<Double> features, String label); |
40 | 27 |
|
41 |
| - public abstract void fit(List<Pair<List<Double>, String>> data); |
| 28 | + String predictOne(List<Double> features); |
42 | 29 |
|
43 |
| - public void fit() { |
44 |
| - fit(this.data); |
45 |
| - } |
| 30 | + boolean isAlwaysKeepData(); |
46 | 31 |
|
47 |
| - public void setTreshold(Long treshold) { |
48 |
| - this.treshold = treshold; |
49 |
| - } |
| 32 | + void setAlwaysKeepData(boolean alwaysKeepData); |
50 | 33 |
|
51 |
| - public boolean readyToPredict() { |
52 |
| - return data.size() > treshold; |
53 |
| - } |
| 34 | + void save(BehaivStorage storage) throws IOException; |
54 | 35 |
|
55 |
| - public void update(List<Pair<List<Double>, String>> data) { |
56 |
| - } |
57 |
| - |
58 |
| - public boolean isPartialFitAllowed() { |
59 |
| - return partialFitAllowed; |
60 |
| - } |
61 |
| - |
62 |
| - public void updateSingle(List<Double> features, String label) { |
63 |
| - data.add(new Pair<>(features, label)); |
64 |
| - } |
65 |
| - |
66 |
| - public abstract String predictOne(List<Double> features); |
67 |
| - |
68 |
| - public boolean isAlwaysKeepData() { |
69 |
| - return alwaysKeepData; |
70 |
| - } |
71 |
| - |
72 |
| - public void setAlwaysKeepData(boolean alwaysKeepData) { |
73 |
| - this.alwaysKeepData = alwaysKeepData; |
74 |
| - } |
75 |
| - |
76 |
| - public void save(BehaivStorage storage) throws IOException { |
77 |
| - |
78 |
| - try (final BufferedWriter writer = new BufferedWriter(new FileWriter(storage.getDataFile(id)))) { |
79 |
| - writer.write(objectMapper.writeValueAsString(data)); |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - public void restore(BehaivStorage storage) throws IOException { |
84 |
| - final TypeReference<List<Pair<List<Double>, String>>> typeReference = new TypeReference<List<Pair<List<Double>, String>>>() { |
85 |
| - }; |
86 |
| - try (final BufferedReader reader = new BufferedReader(new FileReader(storage.getDataFile(id)))) { |
87 |
| - final String content = reader.readLine(); |
88 |
| - if (content == null || content.isEmpty()) { |
89 |
| - data = new ArrayList<>(); |
90 |
| - } else { |
91 |
| - data = objectMapper.readValue(content, typeReference); |
92 |
| - } |
93 |
| - } |
94 |
| - } |
| 36 | + void restore(BehaivStorage storage) throws IOException; |
95 | 37 | }
|
0 commit comments