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