samples/apifest1/day2/welltestedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
permissions -rw-r--r--
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
     1 /*
     2  * The contents of this file are subject to the terms of the Common Development
     3  * and Distribution License (the License). You may not use this file except in
     4  * compliance with the License.
     5  *
     6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
     7  * or http://www.netbeans.org/cddl.txt.
     8  *
     9  * When distributing Covered Code, include this CDDL Header Notice in each file
    10  * and include the License file at http://www.netbeans.org/cddl.txt.
    11  * If applicable, add the following below the CDDL Header, with the fields
    12  * enclosed by brackets [] replaced by your own identifying information:
    13  * "Portions Copyrighted [year] [name of copyright owner]"
    14  *
    15  * The Original Software is NetBeans. The Initial Developer of the Original
    16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    17  * Microsystems, Inc. All Rights Reserved.
    18  */
    19 
    20 package org.netbeans.apifest.boolcircuit;
    21 
    22 /**
    23  */
    24 public abstract class Circuit {
    25     
    26     /** Creates a simple Circuit that is associated with given input pin.
    27      * @since day2
    28      */
    29     public static Circuit input(int pin) {
    30         return new Input(pin);
    31     }
    32     
    33     /** Create circuit that inverts results of input circuit.
    34      */
    35     public static Circuit createNotCircuit(Circuit in) {
    36         return new Not(in);
    37     }
    38     
    39     public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
    40         return new Binary(in1, in2, true);
    41     }
    42     
    43     public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
    44         return new Binary(in1, in2, false);
    45     }
    46     
    47     public Circuit() {
    48         
    49     }
    50     
    51     public final boolean evaluate(Boolean ... inputs) {
    52         if (inputs == null) 
    53             throw new IllegalArgumentException();
    54         double[] vals = new double [inputs.length];
    55         for (int i = 0; i < inputs.length; i++) 
    56             vals[i] = inputs[i]? 1.0: 0.0;
    57         return evaluateFuzzy(vals) == 1.0;
    58     }
    59     
    60     public final double evaluateFuzzy(double ... inputs) {
    61         if (inputs == null) 
    62             throw new IllegalArgumentException();
    63         return doEvaluate(inputs);
    64     }
    65     
    66     protected abstract double doEvaluate(double ... inputs);
    67     
    68     public abstract int maxInputs();
    69     
    70     private interface OldEval {
    71         
    72         double doEvaluate1(int offset, double ... inputs);
    73     }
    74     
    75     private static class Input extends Circuit {
    76         
    77         int pin;
    78         
    79         Input(int pin) {
    80             this.pin = pin;
    81         }
    82         
    83         protected double doEvaluate(double ... inputs) {
    84             if (inputs == null || inputs.length < maxInputs()) 
    85                 throw new IllegalArgumentException();
    86             
    87             return inputs[pin];
    88         }
    89 
    90         public  int maxInputs() {
    91             return pin + 1;
    92         }
    93         
    94     }
    95     
    96     private static class Not extends Circuit implements OldEval {
    97 
    98         private Circuit in;
    99         
   100         Not (Circuit in) {
   101             this.in = in;
   102         }
   103         
   104         public double doEvaluate(double ... inputs) {
   105             return doEvaluate1(0, inputs);
   106         }
   107 
   108         public double doEvaluate1(int i, double ... inputs) {
   109             if (in == null) {
   110                 if (inputs == null || inputs.length < i + 1)
   111                     throw new IllegalArgumentException();
   112                 return 1 - inputs[i];
   113             }
   114             
   115             if (in instanceof OldEval) {
   116                 return 1 - ((OldEval)in).doEvaluate1(i, inputs[i]);
   117             }
   118             else {
   119                 return 1 - in.evaluateFuzzy(inputs);
   120             }
   121         }
   122 
   123         public  int maxInputs() {
   124             return in != null? in.maxInputs(): 1;
   125         }
   126     }
   127     
   128     private static class Binary extends Circuit implements OldEval {
   129 
   130         private Circuit in1, in2;
   131         boolean or;
   132         
   133         Binary (Circuit in1, Circuit in2, boolean or) {
   134             this.in1 = in1;
   135             this.in2 = in2;
   136             this.or = or;
   137         }
   138         
   139         public double doEvaluate(double ... inputs) {
   140             return doEvaluate1(0, inputs);
   141         }
   142 
   143         public double doEvaluate1(int i, double ... inputs) {
   144             double x1, x2;
   145             if (in1 == null) {
   146                 if (inputs == null || inputs.length < i + 1)
   147                     throw new IllegalArgumentException();
   148                 x1 = inputs[i];
   149             } else {
   150                 if (in1 instanceof OldEval) {
   151                     x1 = ((OldEval)in1).doEvaluate1(i, inputs);
   152                 }
   153                 else {
   154                     x1 = in1.evaluateFuzzy(inputs);
   155                 }
   156             }
   157             if (in2 == null) {
   158                 if (inputs == null || inputs.length < i + (in1 != null? in1.maxInputs(): 1) + 1)
   159                     throw new IllegalArgumentException();
   160                 x2 = inputs[i + (in1 != null? in1.maxInputs(): 1)];
   161             } else {
   162                 if (in2 instanceof OldEval) {
   163                     x2 = ((OldEval)in2).doEvaluate1(i + (in1 != null? in1.maxInputs(): 1), inputs);
   164                 }
   165                 else {
   166                     x2 = in2.evaluateFuzzy(inputs);
   167                 }
   168             }
   169             return or? 1 - (1 - x1) * (1 - x2) : x1 * x2;
   170         }
   171         
   172         public  int maxInputs() {
   173             return (in1 != null? in1.maxInputs(): 1) + 
   174                     (in2 != null? in2.maxInputs(): 1) ;
   175         }
   176     }
   177 }