-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicChain.java
210 lines (169 loc) · 5.09 KB
/
LogicChain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.Arrays;
public class LogicChain implements LogicFunction {
private LogicNode first;
private boolean[] values;
private char[] names;
private String query;
public LogicChain(String description) {
Parser p;
// build the logic
try {
p = new Parser(description);
} catch (RuntimeException e) {
e.printStackTrace();
throw new RuntimeException("Unable to parse string");
}
// get references
values = p.getValues();
names = p.getNames();
first = p.getFirst();
query = p.getCleanedQuery();
}
public String[] getNames() {
String[] nms = new String[names.length];
for (int i = 0; i < nms.length; i++)
nms[i] = String.valueOf(names[i]);
return nms;
}
private boolean call() {
return first.call();
}
public boolean call(boolean... vs) {
if (vs.length != values.length)
throw new RuntimeException("not the right number of values");
for (int i = 0; i < values.length; i++)
values[i] = vs[i];
return first.call();
}
public String getTruthTableRepresentation() {
String res = "";
resetValues();
res += "•-";
for (int i = 0; i < names.length; i++)
res += "--";
res += "----•\n";
res += "| ";
for (int i = 0; i < names.length; i++)
res += names[i] + " ";
res += "| f |\n";
res += "|-";
for (int i = 0; i < names.length; i++)
res += "--";
res += "+---|";
do {
res += "\n| ";
for (int i = 0; i < values.length; i++)
res += (values[i] ? "1 " : "0 ");
res += "| " + (call() ? "1" : "0") + " |";
} while (incrementValues());
res += "\n•-";
for (int i = 0; i < names.length; i++)
res += "--";
res += "----•";
return res;
}
public boolean[] getTruthTable() {
boolean[] table = new boolean[1 << values.length];
int count = 0;
do {
table[count++] = call();
} while (incrementValues());
return table;
}
public String expressAsSumOfMinterms() {
String s = "";
resetValues();
do {
if (call()) {
if (s != "")
s += "+";
s += "(";
for (int i = 0; i < values.length; i++) {
if (i != 0)
s += "*";
if (!values[i])
s += "!";
s += String.valueOf(names[i]);
}
s += ")";
}
} while (incrementValues());
return s;
}
public int[] getMinTermsIndex() {
int[] mins = new int[1 << values.length];
int counter = 0;
int v = 0;
resetValues();
do {
if (call())
mins[counter++] = v;
v++;
} while (incrementValues());
return Arrays.copyOf(mins, counter);
}
public String expressAsProductOfMaxterms() {
String s = "";
resetValues();
do {
if (!call()) {
if (s != "")
s += "*";
s += "(";
for (int i = 0; i < values.length; i++) {
if (i != 0)
s += "+";
if (values[i])
s += "!";
s += String.valueOf(names[i]);
}
s += ")";
}
} while (incrementValues());
return s;
}
public int[] getMaxTermsIndex() {
int[] mins = new int[1 << values.length];
int counter = 0;
int v = 0;
resetValues();
do {
if (!call())
mins[counter++] = v;
v++;
} while (incrementValues());
return Arrays.copyOf(mins, counter);
}
private void resetValues() {
for (int i = 0; i < values.length; i++)
values[i] = false;
}
private boolean incrementValues() {
if (values.length < 1)
return false;
if (values.length == 1) {
if (values[0])
return false;
values[0] = !values[0];
return true;
}
boolean reminder = values[values.length - 1];
boolean temp;
values[values.length - 1] = !values[values.length - 1];
for (int k = values.length - 2; k >= 0 && reminder; k--) {
temp = reminder && values[k];
if (k == 0 && reminder && values[0])
return false;
if (reminder)
values[k] = !values[k];
reminder = temp;
}
return true;
}
@Override
public String toString() {
String s = "Logic function with variables " + Arrays.toString(names);
s += "\n\torinigal query : [ " + query + " ]";
return s + "\n\tand body : " + first.toString();
}
}