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