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