jtulach@52
|
1 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
2 |
|
jtulach@52
|
3 |
/**
|
jtulach@52
|
4 |
* Representation of an element in the circuit.
|
jtulach@154
|
5 |
* The internal behaviour of the element is opaque to the API user,
|
jtulach@154
|
6 |
* it can only be used as a building block for logical equations,
|
jtulach@154
|
7 |
* using primitive operation factories and a factory for input
|
jtulach@154
|
8 |
* pin representation.
|
jtulach@154
|
9 |
* Elements are chained to create the logical net.
|
jtulach@154
|
10 |
* The inputs to the net are represented by the elements created
|
jtulach@154
|
11 |
* by {@link #createInput(boolean[])} factory method.
|
jtulach@52
|
12 |
*/
|
jtulach@52
|
13 |
public abstract class Element {
|
jtulach@52
|
14 |
|
jtulach@52
|
15 |
/** Creates a new instance of Element */
|
jtulach@52
|
16 |
private Element() {
|
jtulach@52
|
17 |
}
|
jtulach@52
|
18 |
|
jtulach@52
|
19 |
/**
|
jtulach@52
|
20 |
*/
|
jtulach@52
|
21 |
abstract double evaluate(double[] inputs);
|
jtulach@52
|
22 |
|
jtulach@52
|
23 |
abstract int maxInput();
|
jtulach@52
|
24 |
|
jtulach@52
|
25 |
/**
|
jtulach@52
|
26 |
* Creates an Element representing 2-input AND function.
|
jtulach@52
|
27 |
*
|
jtulach@52
|
28 |
*/
|
jtulach@154
|
29 |
public static Element createAnd(
|
jtulach@154
|
30 |
final Element source1, final Element source2
|
jtulach@154
|
31 |
) {
|
jtulach@52
|
32 |
return new Element() {
|
jtulach@52
|
33 |
double evaluate(double[] inputs) {
|
jtulach@52
|
34 |
return source1.evaluate(inputs) * source2.evaluate(inputs);
|
jtulach@52
|
35 |
}
|
jtulach@52
|
36 |
|
jtulach@52
|
37 |
int maxInput() {
|
jtulach@52
|
38 |
return Math.max(source1.maxInput(), source2.maxInput());
|
jtulach@52
|
39 |
}
|
jtulach@52
|
40 |
};
|
jtulach@52
|
41 |
}
|
jtulach@52
|
42 |
|
jtulach@52
|
43 |
/**
|
jtulach@52
|
44 |
* Creates an Element representing 2-input OR function.
|
jtulach@52
|
45 |
*
|
jtulach@52
|
46 |
*/
|
jtulach@154
|
47 |
public static Element createOr(
|
jtulach@154
|
48 |
final Element source1, final Element source2
|
jtulach@154
|
49 |
) {
|
jtulach@52
|
50 |
return new Element() {
|
jtulach@52
|
51 |
double evaluate(double[] inputs) {
|
jtulach@52
|
52 |
double x = source1.evaluate(inputs);
|
jtulach@52
|
53 |
double y = source2.evaluate(inputs);
|
jtulach@52
|
54 |
return 1 - (1 - x)*(1-y);
|
jtulach@52
|
55 |
}
|
jtulach@52
|
56 |
|
jtulach@52
|
57 |
int maxInput() {
|
jtulach@52
|
58 |
return Math.max(source1.maxInput(), source2.maxInput());
|
jtulach@52
|
59 |
}
|
jtulach@52
|
60 |
};
|
jtulach@52
|
61 |
}
|
jtulach@52
|
62 |
|
jtulach@52
|
63 |
/**
|
jtulach@52
|
64 |
* Creates an Element representing negation.
|
jtulach@52
|
65 |
*
|
jtulach@52
|
66 |
*/
|
jtulach@52
|
67 |
public static Element createNot(final Element source1) {
|
jtulach@52
|
68 |
return new Element() {
|
jtulach@52
|
69 |
double evaluate(double[] inputs) {
|
jtulach@52
|
70 |
return 1 - source1.evaluate(inputs);
|
jtulach@52
|
71 |
}
|
jtulach@52
|
72 |
|
jtulach@52
|
73 |
int maxInput() {
|
jtulach@52
|
74 |
return source1.maxInput();
|
jtulach@52
|
75 |
}
|
jtulach@52
|
76 |
};
|
jtulach@52
|
77 |
}
|
jtulach@52
|
78 |
|
jtulach@52
|
79 |
/**
|
jtulach@52
|
80 |
* Creates an Element representing input to the logical net.
|
jtulach@52
|
81 |
*
|
jtulach@52
|
82 |
*/
|
jtulach@52
|
83 |
public static Element createInput(final int pin) {
|
jtulach@52
|
84 |
return new Element() {
|
jtulach@52
|
85 |
double evaluate(double[] inputs) {
|
jtulach@52
|
86 |
return inputs[pin];
|
jtulach@52
|
87 |
}
|
jtulach@52
|
88 |
|
jtulach@52
|
89 |
int maxInput() {
|
jtulach@52
|
90 |
return pin;
|
jtulach@52
|
91 |
}
|
jtulach@52
|
92 |
};
|
jtulach@52
|
93 |
}
|
jtulach@52
|
94 |
|
jtulach@59
|
95 |
// BEGIN: apifest.day2.pinbasedsolution.FunctionFactory
|
jtulach@52
|
96 |
/**
|
jtulach@52
|
97 |
* Creates an Element with user-defined transfer function.
|
jtulach@52
|
98 |
*/
|
jtulach@154
|
99 |
public static Element createGate(
|
jtulach@154
|
100 |
final Element source1, final Element source2, final Function function
|
jtulach@154
|
101 |
) {
|
jtulach@52
|
102 |
return new Element() {
|
jtulach@52
|
103 |
double evaluate(double[] inputs) {
|
jtulach@52
|
104 |
double x = source1.evaluate(inputs);
|
jtulach@52
|
105 |
double y = source2.evaluate(inputs);
|
jtulach@52
|
106 |
double result = function.evaluate(x, y);
|
jtulach@154
|
107 |
if (result < 0.0 || result > 1.0) {
|
jtulach@154
|
108 |
throw new InternalError("Illegal gate function");
|
jtulach@154
|
109 |
}
|
jtulach@52
|
110 |
return result;
|
jtulach@52
|
111 |
}
|
jtulach@52
|
112 |
|
jtulach@52
|
113 |
int maxInput() {
|
jtulach@52
|
114 |
return Math.max(source1.maxInput(), source2.maxInput());
|
jtulach@52
|
115 |
}
|
jtulach@52
|
116 |
};
|
jtulach@52
|
117 |
}
|
jtulach@59
|
118 |
// END: apifest.day2.pinbasedsolution.FunctionFactory
|
jtulach@52
|
119 |
}
|