jtulach@52
|
1 |
/*
|
jtulach@52
|
2 |
* The contents of this file are subject to the terms of the Common Development
|
jtulach@52
|
3 |
* and Distribution License (the License). You may not use this file except in
|
jtulach@52
|
4 |
* compliance with the License.
|
jtulach@52
|
5 |
*
|
jtulach@52
|
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html
|
jtulach@52
|
7 |
* or http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
8 |
*
|
jtulach@52
|
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file
|
jtulach@52
|
10 |
* and include the License file at http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
11 |
* If applicable, add the following below the CDDL Header, with the fields
|
jtulach@52
|
12 |
* enclosed by brackets [] replaced by your own identifying information:
|
jtulach@52
|
13 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@52
|
14 |
*
|
jtulach@52
|
15 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@52
|
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
|
jtulach@52
|
17 |
* Microsystems, Inc. All Rights Reserved.
|
jtulach@52
|
18 |
*/
|
jtulach@52
|
19 |
|
jtulach@52
|
20 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
21 |
|
jtulach@52
|
22 |
import java.util.ArrayList;
|
jtulach@52
|
23 |
import java.util.Arrays;
|
jtulach@52
|
24 |
import java.util.LinkedHashSet;
|
jtulach@52
|
25 |
|
jtulach@52
|
26 |
/**
|
jtulach@52
|
27 |
*/
|
jtulach@52
|
28 |
public abstract class Circuit {
|
jtulach@52
|
29 |
/** Creates a new instance of Circuits */
|
jtulach@52
|
30 |
protected Circuit() {
|
jtulach@52
|
31 |
}
|
jtulach@52
|
32 |
|
jtulach@52
|
33 |
public static Circuit negate(Input input) {
|
jtulach@52
|
34 |
return new Negation(new Primitive(input));
|
jtulach@52
|
35 |
}
|
jtulach@52
|
36 |
|
jtulach@52
|
37 |
@Deprecated
|
jtulach@52
|
38 |
public static Circuit negate(boolean input) {
|
jtulach@52
|
39 |
return new Negation(input);
|
jtulach@52
|
40 |
}
|
jtulach@52
|
41 |
|
jtulach@52
|
42 |
public static Circuit negate(Circuit input) {
|
jtulach@52
|
43 |
return new Negation(input);
|
jtulach@52
|
44 |
}
|
jtulach@52
|
45 |
|
jtulach@52
|
46 |
public static Circuit and(Input input1, Input input2) {
|
jtulach@52
|
47 |
return new And(new Primitive(input1), new Primitive(input2));
|
jtulach@52
|
48 |
}
|
jtulach@52
|
49 |
|
jtulach@52
|
50 |
@Deprecated
|
jtulach@52
|
51 |
public static Circuit and(boolean input1, boolean input2) {
|
jtulach@52
|
52 |
return new And(input1, input2);
|
jtulach@52
|
53 |
}
|
jtulach@52
|
54 |
|
jtulach@52
|
55 |
public static Circuit and(Circuit input1, Input input2) {
|
jtulach@52
|
56 |
return new And(input1, new Primitive(input2));
|
jtulach@52
|
57 |
}
|
jtulach@52
|
58 |
|
jtulach@52
|
59 |
@Deprecated
|
jtulach@52
|
60 |
public static Circuit and(Circuit input1, boolean input2) {
|
jtulach@52
|
61 |
return new And(input1, new Primitive(input2));
|
jtulach@52
|
62 |
}
|
jtulach@52
|
63 |
|
jtulach@52
|
64 |
public static Circuit and(Input input1, Circuit input2) {
|
jtulach@52
|
65 |
return new And(new Primitive(input1), input2);
|
jtulach@52
|
66 |
}
|
jtulach@52
|
67 |
|
jtulach@52
|
68 |
@Deprecated
|
jtulach@52
|
69 |
public static Circuit and(boolean input1, Circuit input2) {
|
jtulach@52
|
70 |
return new And(new Primitive(input1), input2);
|
jtulach@52
|
71 |
}
|
jtulach@52
|
72 |
|
jtulach@52
|
73 |
public static Circuit and(Circuit input1, Circuit input2) {
|
jtulach@52
|
74 |
return new And(input1, input2);
|
jtulach@52
|
75 |
}
|
jtulach@52
|
76 |
|
jtulach@52
|
77 |
public static Circuit or(Input input1, Input input2) {
|
jtulach@52
|
78 |
return new Or(new Primitive(input1), new Primitive(input2));
|
jtulach@52
|
79 |
}
|
jtulach@52
|
80 |
|
jtulach@52
|
81 |
@Deprecated
|
jtulach@52
|
82 |
public static Circuit or(boolean input1, boolean input2) {
|
jtulach@52
|
83 |
return new Or(input1, input2);
|
jtulach@52
|
84 |
}
|
jtulach@52
|
85 |
|
jtulach@52
|
86 |
public static Circuit or(Circuit input1, Input input2) {
|
jtulach@52
|
87 |
return new Or(input1, new Primitive(input2));
|
jtulach@52
|
88 |
}
|
jtulach@52
|
89 |
|
jtulach@52
|
90 |
@Deprecated
|
jtulach@52
|
91 |
public static Circuit or(Circuit input1, boolean input2) {
|
jtulach@52
|
92 |
return new Or(input1, new Primitive(input2));
|
jtulach@52
|
93 |
}
|
jtulach@52
|
94 |
|
jtulach@52
|
95 |
public static Circuit or(Input input1, Circuit input2) {
|
jtulach@52
|
96 |
return new Or(new Primitive(input1), input2);
|
jtulach@52
|
97 |
}
|
jtulach@52
|
98 |
|
jtulach@52
|
99 |
@Deprecated
|
jtulach@52
|
100 |
public static Circuit or(boolean input1, Circuit input2) {
|
jtulach@52
|
101 |
return new Or(new Primitive(input1), input2);
|
jtulach@52
|
102 |
}
|
jtulach@52
|
103 |
|
jtulach@52
|
104 |
public static Circuit or(Circuit input1, Circuit input2) {
|
jtulach@52
|
105 |
return new Or(input1, input2);
|
jtulach@52
|
106 |
}
|
jtulach@52
|
107 |
|
jtulach@52
|
108 |
|
jtulach@52
|
109 |
public final boolean output() {
|
jtulach@52
|
110 |
double v = value();
|
jtulach@52
|
111 |
if (v > 1 || v < 0) {
|
jtulach@52
|
112 |
throw new IllegalArgumentException();
|
jtulach@52
|
113 |
}
|
jtulach@52
|
114 |
return (v > 0) ? true : false;
|
jtulach@52
|
115 |
}
|
jtulach@52
|
116 |
|
jtulach@52
|
117 |
private static final double check(double v) {
|
jtulach@52
|
118 |
if (v > 1 || v < 0) {
|
jtulach@52
|
119 |
throw new IllegalArgumentException();
|
jtulach@52
|
120 |
}
|
jtulach@52
|
121 |
return v;
|
jtulach@52
|
122 |
}
|
jtulach@52
|
123 |
|
jtulach@52
|
124 |
|
jtulach@52
|
125 |
public abstract double value();
|
jtulach@52
|
126 |
|
jtulach@52
|
127 |
private final static class Primitive extends Circuit {
|
jtulach@52
|
128 |
private Input input;
|
jtulach@52
|
129 |
|
jtulach@52
|
130 |
Primitive(boolean input) {
|
jtulach@52
|
131 |
this.input = new Input(input);
|
jtulach@52
|
132 |
}
|
jtulach@52
|
133 |
|
jtulach@52
|
134 |
Primitive(Input input) {
|
jtulach@52
|
135 |
this.input = input;
|
jtulach@52
|
136 |
}
|
jtulach@52
|
137 |
|
jtulach@52
|
138 |
|
jtulach@52
|
139 |
|
jtulach@52
|
140 |
public double value() {
|
jtulach@52
|
141 |
return input.getValue();
|
jtulach@52
|
142 |
}
|
jtulach@52
|
143 |
}
|
jtulach@52
|
144 |
|
jtulach@52
|
145 |
private final static class Negation extends Circuit {
|
jtulach@52
|
146 |
private Circuit input;
|
jtulach@52
|
147 |
|
jtulach@52
|
148 |
@Deprecated
|
jtulach@52
|
149 |
Negation(boolean input) {
|
jtulach@52
|
150 |
this.input = new Primitive(input);
|
jtulach@52
|
151 |
}
|
jtulach@52
|
152 |
|
jtulach@52
|
153 |
Negation(Circuit input) {
|
jtulach@52
|
154 |
this.input = input;
|
jtulach@52
|
155 |
}
|
jtulach@52
|
156 |
|
jtulach@52
|
157 |
|
jtulach@52
|
158 |
public double value() {
|
jtulach@52
|
159 |
double x = input.value();
|
jtulach@52
|
160 |
return check(1-x);
|
jtulach@52
|
161 |
}
|
jtulach@52
|
162 |
}
|
jtulach@52
|
163 |
|
jtulach@52
|
164 |
private static class And extends Circuit {
|
jtulach@52
|
165 |
Circuit input1;
|
jtulach@52
|
166 |
Circuit input2;
|
jtulach@52
|
167 |
|
jtulach@52
|
168 |
And(boolean input1, boolean input2) {
|
jtulach@52
|
169 |
this.input1 = new Primitive(input1);
|
jtulach@52
|
170 |
this.input2 = new Primitive(input2);
|
jtulach@52
|
171 |
}
|
jtulach@52
|
172 |
|
jtulach@52
|
173 |
And(Circuit input1, Circuit input2) {
|
jtulach@52
|
174 |
this.input1 = input1;
|
jtulach@52
|
175 |
this.input2 = input2;
|
jtulach@52
|
176 |
}
|
jtulach@52
|
177 |
|
jtulach@52
|
178 |
public double value() {
|
jtulach@52
|
179 |
double x = input1.value();
|
jtulach@52
|
180 |
double y = input2.value();
|
jtulach@52
|
181 |
return check(x * y);
|
jtulach@52
|
182 |
}
|
jtulach@52
|
183 |
}
|
jtulach@52
|
184 |
|
jtulach@52
|
185 |
private final static class Or extends And {
|
jtulach@52
|
186 |
Or(boolean input1, boolean input2) {
|
jtulach@52
|
187 |
super(input1, input2);
|
jtulach@52
|
188 |
}
|
jtulach@52
|
189 |
|
jtulach@52
|
190 |
Or(Circuit input1, Circuit input2) {
|
jtulach@52
|
191 |
super(input1, input2);
|
jtulach@52
|
192 |
}
|
jtulach@52
|
193 |
|
jtulach@52
|
194 |
public double value() {
|
jtulach@52
|
195 |
double x = input1.value();
|
jtulach@52
|
196 |
double y = input2.value();
|
jtulach@52
|
197 |
|
jtulach@52
|
198 |
return check(1 - (1 - x) * (1 - y));
|
jtulach@52
|
199 |
}
|
jtulach@52
|
200 |
}
|
jtulach@52
|
201 |
|
jtulach@52
|
202 |
public static class Input {
|
jtulach@52
|
203 |
private double value;
|
jtulach@52
|
204 |
|
jtulach@52
|
205 |
public static Input valueOf(boolean initValue) {
|
jtulach@52
|
206 |
return new Input(initValue);
|
jtulach@52
|
207 |
}
|
jtulach@52
|
208 |
|
jtulach@52
|
209 |
public static Input valueOf(double initValue) {
|
jtulach@52
|
210 |
return new Input(initValue);
|
jtulach@52
|
211 |
}
|
jtulach@52
|
212 |
|
jtulach@52
|
213 |
/** Creates a new instance of Input */
|
jtulach@52
|
214 |
private Input(boolean input) {
|
jtulach@52
|
215 |
setValue(input);
|
jtulach@52
|
216 |
}
|
jtulach@52
|
217 |
|
jtulach@52
|
218 |
private Input(double input) {
|
jtulach@52
|
219 |
setValue(input);
|
jtulach@52
|
220 |
}
|
jtulach@52
|
221 |
|
jtulach@52
|
222 |
public double getValue() {
|
jtulach@52
|
223 |
return value;
|
jtulach@52
|
224 |
}
|
jtulach@52
|
225 |
|
jtulach@52
|
226 |
public void setValue(double input) {
|
jtulach@52
|
227 |
value = input;
|
jtulach@52
|
228 |
}
|
jtulach@52
|
229 |
|
jtulach@52
|
230 |
public void setValue(boolean input) {
|
jtulach@52
|
231 |
value = (input) ? 1 : 0;
|
jtulach@52
|
232 |
}
|
jtulach@52
|
233 |
}
|
jtulach@52
|
234 |
}
|