samples/apifest1/day2/welltestedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java
changeset 52 4257f4cf226b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day2/welltestedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,177 @@
     1.4 +/*
     1.5 + * The contents of this file are subject to the terms of the Common Development
     1.6 + * and Distribution License (the License). You may not use this file except in
     1.7 + * compliance with the License.
     1.8 + *
     1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    1.10 + * or http://www.netbeans.org/cddl.txt.
    1.11 + *
    1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    1.14 + * If applicable, add the following below the CDDL Header, with the fields
    1.15 + * enclosed by brackets [] replaced by your own identifying information:
    1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.17 + *
    1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.20 + * Microsystems, Inc. All Rights Reserved.
    1.21 + */
    1.22 +
    1.23 +package org.netbeans.apifest.boolcircuit;
    1.24 +
    1.25 +/**
    1.26 + */
    1.27 +public abstract class Circuit {
    1.28 +    
    1.29 +    /** Creates a simple Circuit that is associated with given input pin.
    1.30 +     * @since day2
    1.31 +     */
    1.32 +    public static Circuit input(int pin) {
    1.33 +        return new Input(pin);
    1.34 +    }
    1.35 +    
    1.36 +    /** Create circuit that inverts results of input circuit.
    1.37 +     */
    1.38 +    public static Circuit createNotCircuit(Circuit in) {
    1.39 +        return new Not(in);
    1.40 +    }
    1.41 +    
    1.42 +    public static Circuit createOrCircuit(Circuit in1, Circuit in2) {
    1.43 +        return new Binary(in1, in2, true);
    1.44 +    }
    1.45 +    
    1.46 +    public static Circuit createAndCircuit(Circuit in1, Circuit in2) {
    1.47 +        return new Binary(in1, in2, false);
    1.48 +    }
    1.49 +    
    1.50 +    public Circuit() {
    1.51 +        
    1.52 +    }
    1.53 +    
    1.54 +    public final boolean evaluate(Boolean ... inputs) {
    1.55 +        if (inputs == null) 
    1.56 +            throw new IllegalArgumentException();
    1.57 +        double[] vals = new double [inputs.length];
    1.58 +        for (int i = 0; i < inputs.length; i++) 
    1.59 +            vals[i] = inputs[i]? 1.0: 0.0;
    1.60 +        return evaluateFuzzy(vals) == 1.0;
    1.61 +    }
    1.62 +    
    1.63 +    public final double evaluateFuzzy(double ... inputs) {
    1.64 +        if (inputs == null) 
    1.65 +            throw new IllegalArgumentException();
    1.66 +        return doEvaluate(inputs);
    1.67 +    }
    1.68 +    
    1.69 +    protected abstract double doEvaluate(double ... inputs);
    1.70 +    
    1.71 +    public abstract int maxInputs();
    1.72 +    
    1.73 +    private interface OldEval {
    1.74 +        
    1.75 +        double doEvaluate1(int offset, double ... inputs);
    1.76 +    }
    1.77 +    
    1.78 +    private static class Input extends Circuit {
    1.79 +        
    1.80 +        int pin;
    1.81 +        
    1.82 +        Input(int pin) {
    1.83 +            this.pin = pin;
    1.84 +        }
    1.85 +        
    1.86 +        protected double doEvaluate(double ... inputs) {
    1.87 +            if (inputs == null || inputs.length < maxInputs()) 
    1.88 +                throw new IllegalArgumentException();
    1.89 +            
    1.90 +            return inputs[pin];
    1.91 +        }
    1.92 +
    1.93 +        public  int maxInputs() {
    1.94 +            return pin + 1;
    1.95 +        }
    1.96 +        
    1.97 +    }
    1.98 +    
    1.99 +    private static class Not extends Circuit implements OldEval {
   1.100 +
   1.101 +        private Circuit in;
   1.102 +        
   1.103 +        Not (Circuit in) {
   1.104 +            this.in = in;
   1.105 +        }
   1.106 +        
   1.107 +        public double doEvaluate(double ... inputs) {
   1.108 +            return doEvaluate1(0, inputs);
   1.109 +        }
   1.110 +
   1.111 +        public double doEvaluate1(int i, double ... inputs) {
   1.112 +            if (in == null) {
   1.113 +                if (inputs == null || inputs.length < i + 1)
   1.114 +                    throw new IllegalArgumentException();
   1.115 +                return 1 - inputs[i];
   1.116 +            }
   1.117 +            
   1.118 +            if (in instanceof OldEval) {
   1.119 +                return 1 - ((OldEval)in).doEvaluate1(i, inputs[i]);
   1.120 +            }
   1.121 +            else {
   1.122 +                return 1 - in.evaluateFuzzy(inputs);
   1.123 +            }
   1.124 +        }
   1.125 +
   1.126 +        public  int maxInputs() {
   1.127 +            return in != null? in.maxInputs(): 1;
   1.128 +        }
   1.129 +    }
   1.130 +    
   1.131 +    private static class Binary extends Circuit implements OldEval {
   1.132 +
   1.133 +        private Circuit in1, in2;
   1.134 +        boolean or;
   1.135 +        
   1.136 +        Binary (Circuit in1, Circuit in2, boolean or) {
   1.137 +            this.in1 = in1;
   1.138 +            this.in2 = in2;
   1.139 +            this.or = or;
   1.140 +        }
   1.141 +        
   1.142 +        public double doEvaluate(double ... inputs) {
   1.143 +            return doEvaluate1(0, inputs);
   1.144 +        }
   1.145 +
   1.146 +        public double doEvaluate1(int i, double ... inputs) {
   1.147 +            double x1, x2;
   1.148 +            if (in1 == null) {
   1.149 +                if (inputs == null || inputs.length < i + 1)
   1.150 +                    throw new IllegalArgumentException();
   1.151 +                x1 = inputs[i];
   1.152 +            } else {
   1.153 +                if (in1 instanceof OldEval) {
   1.154 +                    x1 = ((OldEval)in1).doEvaluate1(i, inputs);
   1.155 +                }
   1.156 +                else {
   1.157 +                    x1 = in1.evaluateFuzzy(inputs);
   1.158 +                }
   1.159 +            }
   1.160 +            if (in2 == null) {
   1.161 +                if (inputs == null || inputs.length < i + (in1 != null? in1.maxInputs(): 1) + 1)
   1.162 +                    throw new IllegalArgumentException();
   1.163 +                x2 = inputs[i + (in1 != null? in1.maxInputs(): 1)];
   1.164 +            } else {
   1.165 +                if (in2 instanceof OldEval) {
   1.166 +                    x2 = ((OldEval)in2).doEvaluate1(i + (in1 != null? in1.maxInputs(): 1), inputs);
   1.167 +                }
   1.168 +                else {
   1.169 +                    x2 = in2.evaluateFuzzy(inputs);
   1.170 +                }
   1.171 +            }
   1.172 +            return or? 1 - (1 - x1) * (1 - x2) : x1 * x2;
   1.173 +        }
   1.174 +        
   1.175 +        public  int maxInputs() {
   1.176 +            return (in1 != null? in1.maxInputs(): 1) + 
   1.177 +                    (in2 != null? in2.maxInputs(): 1) ;
   1.178 +        }
   1.179 +    }
   1.180 +}