-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicNode.java
151 lines (128 loc) · 3.73 KB
/
LogicNode.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
@FunctionalInterface
public interface LogicNode {
public boolean call();
public static LogicNode Variable(boolean[] a, int i, char name) {
return new LogicNode() {
private int index = i;
private boolean[] arrays = a;
@Override
public boolean call() {
return arrays[index];
}
@Override
public String toString() {
return String.valueOf(name);
}
};
}
public static LogicNode TRUE() {
return new LogicNode() {
@Override
public boolean call() {
return true;
}
@Override
public String toString() {
return "1";
}
};
}
public static LogicNode FALSE() {
return new LogicNode() {
@Override
public boolean call() {
return false;
}
@Override
public String toString() {
return "0";
}
};
}
public static LogicNode AND(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
return a.call() && b.call();
}
@Override
public String toString() {
return "( " + a.toString() + " AND " + b.toString() + " )";
}
};
}
public static LogicNode OR(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
return a.call() || b.call();
}
@Override
public String toString() {
return "( " + a.toString() + " OR " + b.toString() + " )";
}
};
}
public static LogicNode NOT(LogicNode a) {
return new LogicNode() {
@Override
public boolean call() {
return !a.call();
}
@Override
public String toString() {
return "!" + a.toString();
}
};
}
public static LogicNode NAND(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
return !(a.call() && b.call());
}
@Override
public String toString() {
return "( " + a.toString() + " NAND " + b.toString() + " )";
}
};
}
public static LogicNode NOR(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
return !(a.call() || b.call());
}
@Override
public String toString() {
return "( " + a.toString() + " NOR " + b.toString() + " )";
}
};
}
public static LogicNode XOR(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
boolean ra = a.call(), rb = b.call();
return ra && !rb || !ra && rb;
}
@Override
public String toString() {
return "( " + a.toString() + " XOR " + b.toString() + " )";
}
};
}
public static LogicNode NXOR(LogicNode a, LogicNode b) {
return new LogicNode() {
@Override
public boolean call() {
boolean ra = a.call(), rb = b.call();
return !ra && !rb || ra && rb;
}
@Override
public String toString() {
return "( " + a.toString() + " NXOR " + b.toString() + " )";
}
};
}
}