samples/apifest1/day2/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 59 02e05defc6a0
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
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@132
     5
 * The internal behaviour of the element is opaque to the API user, 
jtulach@132
     6
 * it can only be used as a building block for logical equations, 
jtulach@132
     7
 * using primitive operation factories and a factory for input 
jtulach@132
     8
 * pin representation.
jtulach@132
     9
 * Elements are chained to create the logical net. 
jtulach@132
    10
 * The inputs to the net are represented by the elements created 
jtulach@132
    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@132
    29
    public static Element createAnd(
jtulach@132
    30
        final Element source1, final Element source2
jtulach@132
    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@132
    47
    public static Element createOr(
jtulach@132
    48
        final Element source1, final Element source2
jtulach@132
    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@132
    99
    public static Element createGate(
jtulach@132
   100
        final Element source1, final Element source2, final Function function
jtulach@132
   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@132
   107
                if (result < 0.0 || result > 1.0) {
jtulach@132
   108
                    throw new InternalError("Illegal gate function");
jtulach@132
   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
}