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.security.CodeSource;
|
jtulach@52
|
23 |
import java.security.Permission;
|
jtulach@52
|
24 |
import java.security.PermissionCollection;
|
jtulach@52
|
25 |
import java.security.Policy;
|
jtulach@52
|
26 |
import java.util.Collection;
|
jtulach@52
|
27 |
import java.util.Collections;
|
jtulach@52
|
28 |
import java.util.Enumeration;
|
jtulach@52
|
29 |
import junit.framework.TestCase;
|
jtulach@52
|
30 |
import junit.framework.*;
|
jtulach@52
|
31 |
|
jtulach@52
|
32 |
/** This file contains the APIFest quest for day 2. Simply, turn the
|
jtulach@52
|
33 |
* boolean circuit into circuit that can compute with double values from 0 to 1.
|
jtulach@52
|
34 |
* <p>
|
jtulach@52
|
35 |
* This means that where ever a boolean was used to represent input or
|
jtulach@52
|
36 |
* output values, one can now use any double number from >= 0 and <= 1.
|
jtulach@52
|
37 |
* Still, to support backward compatibility, the operations with booleans
|
jtulach@52
|
38 |
* has to be kept available and have to work. In fact False shall be
|
jtulach@52
|
39 |
* treated as 0 and True as 1.
|
jtulach@52
|
40 |
* <p>
|
jtulach@52
|
41 |
* The basic elements has to be modified to work on doubles in the following
|
jtulach@52
|
42 |
* way:
|
jtulach@52
|
43 |
* <ul>
|
jtulach@52
|
44 |
* <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
|
jtulach@52
|
45 |
* <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
|
jtulach@52
|
46 |
* and(false,true)=0*1=0=false
|
jtulach@52
|
47 |
* <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
|
jtulach@52
|
48 |
* or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
|
jtulach@52
|
49 |
* or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
|
jtulach@52
|
50 |
* </ul>
|
jtulach@52
|
51 |
* <p>
|
jtulach@52
|
52 |
* However as the circuits with doubles are more rich than plain boolean circuits,
|
jtulach@52
|
53 |
* there is additional requirement to allow any user of your API to write its
|
jtulach@52
|
54 |
* own "element" type. This is all going to be exercise in the tests bellow
|
jtulach@52
|
55 |
* which you are supposed to implement.
|
jtulach@52
|
56 |
*/
|
jtulach@52
|
57 |
public class RealTest extends TestCase {
|
jtulach@52
|
58 |
static {
|
jtulach@52
|
59 |
// your code shall run without any permissions
|
jtulach@52
|
60 |
}
|
jtulach@52
|
61 |
|
jtulach@52
|
62 |
public RealTest(String testName) {
|
jtulach@52
|
63 |
super(testName);
|
jtulach@52
|
64 |
}
|
jtulach@52
|
65 |
|
jtulach@52
|
66 |
protected void setUp() throws Exception {
|
jtulach@52
|
67 |
}
|
jtulach@52
|
68 |
|
jtulach@52
|
69 |
protected void tearDown() throws Exception {
|
jtulach@52
|
70 |
}
|
jtulach@52
|
71 |
|
jtulach@52
|
72 |
|
jtulach@52
|
73 |
|
jtulach@52
|
74 |
/** First of all create a circuit which will evaluate
|
jtulach@52
|
75 |
* expression (X1 and X2) or not(x1). Hold the circuit
|
jtulach@52
|
76 |
* in some variable.
|
jtulach@52
|
77 |
*
|
jtulach@52
|
78 |
* Feed this circuit with x1=true, x2=false, assert result is false
|
jtulach@52
|
79 |
*
|
jtulach@52
|
80 |
* Feed the same circuit with x1=false, x2=true, assert result is true
|
jtulach@52
|
81 |
*
|
jtulach@52
|
82 |
* Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
|
jtulach@52
|
83 |
*
|
jtulach@52
|
84 |
* Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
|
jtulach@52
|
85 |
*
|
jtulach@52
|
86 |
* Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
|
jtulach@52
|
87 |
*/
|
jtulach@52
|
88 |
public void testX1andX2orNotX1() {
|
jtulach@52
|
89 |
FuzzyCircuit c = new CircuitSupport( 2 ) {
|
jtulach@52
|
90 |
|
jtulach@52
|
91 |
@Override
|
jtulach@52
|
92 |
public boolean ev(boolean[] in) {
|
jtulach@52
|
93 |
return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
|
jtulach@52
|
94 |
}
|
jtulach@52
|
95 |
|
jtulach@52
|
96 |
@Override
|
jtulach@52
|
97 |
public double ev(double[] in) {
|
jtulach@52
|
98 |
return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
|
jtulach@52
|
99 |
}
|
jtulach@52
|
100 |
|
jtulach@52
|
101 |
};
|
jtulach@52
|
102 |
|
jtulach@52
|
103 |
assertFalse( c.evaluate(true, false) );
|
jtulach@52
|
104 |
assertTrue( c.evaluate(false, true) );
|
jtulach@52
|
105 |
assertEquals(c.evaluate(0.0, 1.0), 1.0, 0.0001);
|
jtulach@52
|
106 |
assertEquals(c.evaluate(0.5, 0.5), 0.625, 0.0001);
|
jtulach@52
|
107 |
try {
|
jtulach@52
|
108 |
c.evaluate(0.0, 2.0);
|
jtulach@52
|
109 |
}
|
jtulach@52
|
110 |
catch ( IllegalArgumentException e ) {
|
jtulach@52
|
111 |
return;
|
jtulach@52
|
112 |
}
|
jtulach@52
|
113 |
fail();
|
jtulach@52
|
114 |
}
|
jtulach@52
|
115 |
|
jtulach@52
|
116 |
/** Ensure that one variable cannot be filled with two different values.
|
jtulach@52
|
117 |
* Create a circuit for x1 and x1. Make sure that for any usage of your
|
jtulach@52
|
118 |
* API that would not lead to x1 * x1 result, an exception is thrown.
|
jtulach@52
|
119 |
* For example if there was a way to feed the circuit with two different
|
jtulach@52
|
120 |
* values 0.3 and 0.5 an exception is thrown indicating that this is
|
jtulach@52
|
121 |
* improper use of the circuit.
|
jtulach@52
|
122 |
*/
|
jtulach@52
|
123 |
public void testImproperUseOfTheCircuit() {
|
jtulach@52
|
124 |
FuzzyCircuit c = new CircuitSupport( 1 ) {
|
jtulach@52
|
125 |
|
jtulach@52
|
126 |
@Override
|
jtulach@52
|
127 |
public boolean ev(boolean[] in) {
|
jtulach@52
|
128 |
return AND.evaluate( in[0], in[0] );
|
jtulach@52
|
129 |
}
|
jtulach@52
|
130 |
|
jtulach@52
|
131 |
@Override
|
jtulach@52
|
132 |
public double ev(double[] in) {
|
jtulach@52
|
133 |
return AND.evaluate( in[0], in[0] );
|
jtulach@52
|
134 |
}
|
jtulach@52
|
135 |
|
jtulach@52
|
136 |
};
|
jtulach@52
|
137 |
|
jtulach@52
|
138 |
// Empty array
|
jtulach@52
|
139 |
IllegalArgumentException ex = null;
|
jtulach@52
|
140 |
try {
|
jtulach@52
|
141 |
c.evaluate(new boolean[]{} );
|
jtulach@52
|
142 |
} catch ( IllegalArgumentException e) {
|
jtulach@52
|
143 |
ex = e ;
|
jtulach@52
|
144 |
}
|
jtulach@52
|
145 |
if ( ex == null ) {
|
jtulach@52
|
146 |
fail();
|
jtulach@52
|
147 |
}
|
jtulach@52
|
148 |
|
jtulach@52
|
149 |
// Other sizes
|
jtulach@52
|
150 |
int MAX = 4; // :-) This test obviously requires a nonsese.
|
jtulach@52
|
151 |
// For testing nonsese Integer.MAX_VALUE
|
jtulach@52
|
152 |
for( int i = 2; i < MAX ; i++ ) {
|
jtulach@52
|
153 |
double a[] = new double[i];
|
jtulach@52
|
154 |
ex = null;
|
jtulach@52
|
155 |
try {
|
jtulach@52
|
156 |
c.evaluate( a );
|
jtulach@52
|
157 |
}
|
jtulach@52
|
158 |
catch ( IllegalArgumentException e) {
|
jtulach@52
|
159 |
ex = e ;
|
jtulach@52
|
160 |
}
|
jtulach@52
|
161 |
if ( ex == null ) {
|
jtulach@52
|
162 |
fail();
|
jtulach@52
|
163 |
}
|
jtulach@52
|
164 |
}
|
jtulach@52
|
165 |
|
jtulach@52
|
166 |
}
|
jtulach@52
|
167 |
|
jtulach@52
|
168 |
/** Write your own element type called "gte" that will have two inputs and one output.
|
jtulach@52
|
169 |
* The output value will be 1 if x1 >= x2 and 0 otherwise.
|
jtulach@52
|
170 |
*
|
jtulach@52
|
171 |
* Create
|
jtulach@52
|
172 |
* circuit for following expression: (x1 and not(x1)) gte x1
|
jtulach@52
|
173 |
*
|
jtulach@52
|
174 |
* Feed the circuit with 0.5 and verify the result is 0
|
jtulach@52
|
175 |
*
|
jtulach@52
|
176 |
* Feed the same circuit with 1 and verify the result is 0
|
jtulach@52
|
177 |
*
|
jtulach@52
|
178 |
* Feed the same circuit with 0 and verify the result is 1
|
jtulach@52
|
179 |
*/
|
jtulach@52
|
180 |
public void testGreaterThanEqualElement() {
|
jtulach@52
|
181 |
final FuzzyCircuit gte = new CircuitSupport( 2 ) {
|
jtulach@52
|
182 |
|
jtulach@52
|
183 |
// Assumes true > false
|
jtulach@52
|
184 |
@Override
|
jtulach@52
|
185 |
public boolean ev(boolean[] in) {
|
jtulach@52
|
186 |
return in[0] || ( !in[0] && in[1] ); // May be
|
jtulach@52
|
187 |
}
|
jtulach@52
|
188 |
|
jtulach@52
|
189 |
@Override
|
jtulach@52
|
190 |
public double ev(double[] in) {
|
jtulach@52
|
191 |
return in[0] >= in[1] ? 1.0 : 0.0;
|
jtulach@52
|
192 |
}
|
jtulach@52
|
193 |
|
jtulach@52
|
194 |
};
|
jtulach@52
|
195 |
|
jtulach@52
|
196 |
FuzzyCircuit c = new CircuitSupport( 1 ) {
|
jtulach@52
|
197 |
|
jtulach@52
|
198 |
// Assumes true > false
|
jtulach@52
|
199 |
@Override
|
jtulach@52
|
200 |
public boolean ev(boolean[] in) {
|
jtulach@52
|
201 |
return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
|
jtulach@52
|
202 |
}
|
jtulach@52
|
203 |
|
jtulach@52
|
204 |
@Override
|
jtulach@52
|
205 |
public double ev(double[] in) {
|
jtulach@52
|
206 |
return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
|
jtulach@52
|
207 |
}
|
jtulach@52
|
208 |
|
jtulach@52
|
209 |
};
|
jtulach@52
|
210 |
|
jtulach@52
|
211 |
|
jtulach@52
|
212 |
assertEquals(c.evaluate(0.5), 0.0, 0.0001);
|
jtulach@52
|
213 |
assertEquals(c.evaluate(1.0), 0.0, 0.0001);
|
jtulach@52
|
214 |
assertEquals(c.evaluate(0.0), 1.0, 0.0001);
|
jtulach@52
|
215 |
|
jtulach@52
|
216 |
}
|
jtulach@52
|
217 |
|
jtulach@52
|
218 |
|
jtulach@52
|
219 |
private abstract static class CircuitSupport extends FuzzyCircuit {
|
jtulach@52
|
220 |
|
jtulach@52
|
221 |
private int paramCount;
|
jtulach@52
|
222 |
|
jtulach@52
|
223 |
CircuitSupport( int paramCount ) {
|
jtulach@52
|
224 |
this.paramCount = paramCount;
|
jtulach@52
|
225 |
}
|
jtulach@52
|
226 |
|
jtulach@52
|
227 |
abstract boolean ev( boolean in[] );
|
jtulach@52
|
228 |
abstract double ev( double in[] );
|
jtulach@52
|
229 |
|
jtulach@52
|
230 |
public boolean evaluate(boolean[] in) {
|
jtulach@52
|
231 |
checkParams( paramCount, in );
|
jtulach@52
|
232 |
return ev( in );
|
jtulach@52
|
233 |
}
|
jtulach@52
|
234 |
|
jtulach@52
|
235 |
public double evaluate(double[] in) {
|
jtulach@52
|
236 |
checkParams( paramCount, in );
|
jtulach@52
|
237 |
return ev( in );
|
jtulach@52
|
238 |
}
|
jtulach@52
|
239 |
|
jtulach@52
|
240 |
}
|
jtulach@52
|
241 |
|
jtulach@52
|
242 |
}
|