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 |
/**
|
jtulach@52
|
23 |
*/
|
jtulach@52
|
24 |
public abstract class Circuit {
|
jtulach@52
|
25 |
|
jtulach@52
|
26 |
/** Creates a simple Circuit that is associated with given input pin.
|
jtulach@52
|
27 |
* @since day2
|
jtulach@52
|
28 |
*/
|
jtulach@52
|
29 |
public static Circuit input(int pin) {
|
jtulach@52
|
30 |
return new Input(pin);
|
jtulach@52
|
31 |
}
|
jtulach@52
|
32 |
|
jtulach@52
|
33 |
/** Create circuit that inverts results of input circuit.
|
jtulach@52
|
34 |
*/
|
jtulach@52
|
35 |
public static Circuit createNotCircuit(Circuit in) {
|
jtulach@52
|
36 |
return new Not(in);
|
jtulach@52
|
37 |
}
|
jtulach@52
|
38 |
|
jtulach@52
|
39 |
public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
|
jtulach@52
|
40 |
return new Binary(in1, in2, true);
|
jtulach@52
|
41 |
}
|
jtulach@52
|
42 |
|
jtulach@52
|
43 |
public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
|
jtulach@52
|
44 |
return new Binary(in1, in2, false);
|
jtulach@52
|
45 |
}
|
jtulach@52
|
46 |
|
jtulach@52
|
47 |
public Circuit() {
|
jtulach@52
|
48 |
|
jtulach@52
|
49 |
}
|
jtulach@52
|
50 |
|
jtulach@52
|
51 |
public final boolean evaluate(Boolean ... inputs) {
|
jtulach@52
|
52 |
if (inputs == null)
|
jtulach@52
|
53 |
throw new IllegalArgumentException();
|
jtulach@52
|
54 |
double[] vals = new double [inputs.length];
|
jtulach@52
|
55 |
for (int i = 0; i < inputs.length; i++)
|
jtulach@52
|
56 |
vals[i] = inputs[i]? 1.0: 0.0;
|
jtulach@52
|
57 |
return evaluateFuzzy(vals) == 1.0;
|
jtulach@52
|
58 |
}
|
jtulach@52
|
59 |
|
jtulach@52
|
60 |
public final double evaluateFuzzy(double ... inputs) {
|
jtulach@52
|
61 |
if (inputs == null)
|
jtulach@52
|
62 |
throw new IllegalArgumentException();
|
jtulach@52
|
63 |
return doEvaluate(inputs);
|
jtulach@52
|
64 |
}
|
jtulach@52
|
65 |
|
jtulach@52
|
66 |
protected abstract double doEvaluate(double ... inputs);
|
jtulach@52
|
67 |
|
jtulach@52
|
68 |
public abstract int maxInputs();
|
jtulach@52
|
69 |
|
jtulach@52
|
70 |
private interface OldEval {
|
jtulach@52
|
71 |
|
jtulach@52
|
72 |
double doEvaluate1(int offset, double ... inputs);
|
jtulach@52
|
73 |
}
|
jtulach@52
|
74 |
|
jtulach@52
|
75 |
private static class Input extends Circuit {
|
jtulach@52
|
76 |
|
jtulach@52
|
77 |
int pin;
|
jtulach@52
|
78 |
|
jtulach@52
|
79 |
Input(int pin) {
|
jtulach@52
|
80 |
this.pin = pin;
|
jtulach@52
|
81 |
}
|
jtulach@52
|
82 |
|
jtulach@52
|
83 |
protected double doEvaluate(double ... inputs) {
|
jtulach@52
|
84 |
if (inputs == null || inputs.length < maxInputs())
|
jtulach@52
|
85 |
throw new IllegalArgumentException();
|
jtulach@52
|
86 |
|
jtulach@52
|
87 |
return inputs[pin];
|
jtulach@52
|
88 |
}
|
jtulach@52
|
89 |
|
jtulach@52
|
90 |
public int maxInputs() {
|
jtulach@52
|
91 |
return pin + 1;
|
jtulach@52
|
92 |
}
|
jtulach@52
|
93 |
|
jtulach@52
|
94 |
}
|
jtulach@52
|
95 |
|
jtulach@52
|
96 |
private static class Not extends Circuit implements OldEval {
|
jtulach@52
|
97 |
|
jtulach@52
|
98 |
private Circuit in;
|
jtulach@52
|
99 |
|
jtulach@52
|
100 |
Not (Circuit in) {
|
jtulach@52
|
101 |
this.in = in;
|
jtulach@52
|
102 |
}
|
jtulach@52
|
103 |
|
jtulach@52
|
104 |
public double doEvaluate(double ... inputs) {
|
jtulach@52
|
105 |
return doEvaluate1(0, inputs);
|
jtulach@52
|
106 |
}
|
jtulach@52
|
107 |
|
jtulach@52
|
108 |
public double doEvaluate1(int i, double ... inputs) {
|
jtulach@52
|
109 |
if (in == null) {
|
jtulach@52
|
110 |
if (inputs == null || inputs.length < i + 1)
|
jtulach@52
|
111 |
throw new IllegalArgumentException();
|
jtulach@52
|
112 |
return 1 - inputs[i];
|
jtulach@52
|
113 |
}
|
jtulach@52
|
114 |
|
jtulach@52
|
115 |
if (in instanceof OldEval) {
|
jtulach@52
|
116 |
return 1 - ((OldEval)in).doEvaluate1(i, inputs[i]);
|
jtulach@52
|
117 |
}
|
jtulach@52
|
118 |
else {
|
jtulach@52
|
119 |
return 1 - in.evaluateFuzzy(inputs);
|
jtulach@52
|
120 |
}
|
jtulach@52
|
121 |
}
|
jtulach@52
|
122 |
|
jtulach@52
|
123 |
public int maxInputs() {
|
jtulach@52
|
124 |
return in != null? in.maxInputs(): 1;
|
jtulach@52
|
125 |
}
|
jtulach@52
|
126 |
}
|
jtulach@52
|
127 |
|
jtulach@52
|
128 |
private static class Binary extends Circuit implements OldEval {
|
jtulach@52
|
129 |
|
jtulach@52
|
130 |
private Circuit in1, in2;
|
jtulach@52
|
131 |
boolean or;
|
jtulach@52
|
132 |
|
jtulach@52
|
133 |
Binary (Circuit in1, Circuit in2, boolean or) {
|
jtulach@52
|
134 |
this.in1 = in1;
|
jtulach@52
|
135 |
this.in2 = in2;
|
jtulach@52
|
136 |
this.or = or;
|
jtulach@52
|
137 |
}
|
jtulach@52
|
138 |
|
jtulach@52
|
139 |
public double doEvaluate(double ... inputs) {
|
jtulach@52
|
140 |
return doEvaluate1(0, inputs);
|
jtulach@52
|
141 |
}
|
jtulach@52
|
142 |
|
jtulach@52
|
143 |
public double doEvaluate1(int i, double ... inputs) {
|
jtulach@52
|
144 |
double x1, x2;
|
jtulach@52
|
145 |
if (in1 == null) {
|
jtulach@52
|
146 |
if (inputs == null || inputs.length < i + 1)
|
jtulach@52
|
147 |
throw new IllegalArgumentException();
|
jtulach@52
|
148 |
x1 = inputs[i];
|
jtulach@52
|
149 |
} else {
|
jtulach@52
|
150 |
if (in1 instanceof OldEval) {
|
jtulach@52
|
151 |
x1 = ((OldEval)in1).doEvaluate1(i, inputs);
|
jtulach@52
|
152 |
}
|
jtulach@52
|
153 |
else {
|
jtulach@52
|
154 |
x1 = in1.evaluateFuzzy(inputs);
|
jtulach@52
|
155 |
}
|
jtulach@52
|
156 |
}
|
jtulach@52
|
157 |
if (in2 == null) {
|
jtulach@52
|
158 |
if (inputs == null || inputs.length < i + (in1 != null? in1.maxInputs(): 1) + 1)
|
jtulach@52
|
159 |
throw new IllegalArgumentException();
|
jtulach@52
|
160 |
x2 = inputs[i + (in1 != null? in1.maxInputs(): 1)];
|
jtulach@52
|
161 |
} else {
|
jtulach@52
|
162 |
if (in2 instanceof OldEval) {
|
jtulach@52
|
163 |
x2 = ((OldEval)in2).doEvaluate1(i + (in1 != null? in1.maxInputs(): 1), inputs);
|
jtulach@52
|
164 |
}
|
jtulach@52
|
165 |
else {
|
jtulach@52
|
166 |
x2 = in2.evaluateFuzzy(inputs);
|
jtulach@52
|
167 |
}
|
jtulach@52
|
168 |
}
|
jtulach@52
|
169 |
return or? 1 - (1 - x1) * (1 - x2) : x1 * x2;
|
jtulach@52
|
170 |
}
|
jtulach@52
|
171 |
|
jtulach@52
|
172 |
public int maxInputs() {
|
jtulach@52
|
173 |
return (in1 != null? in1.maxInputs(): 1) +
|
jtulach@52
|
174 |
(in2 != null? in2.maxInputs(): 1) ;
|
jtulach@52
|
175 |
}
|
jtulach@52
|
176 |
}
|
jtulach@52
|
177 |
}
|