samples/apifest1/day2/pinbasedsolution/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
jtulach@52
     1
/*
jtulach@52
     2
 * The contents of this file are subject to the terms of the Common Development
jtulach@52
     3
 * and Distribution License (the License). You may not use this file except in
jtulach@52
     4
 * compliance with the License.
jtulach@52
     5
 *
jtulach@52
     6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
jtulach@52
     7
 * or http://www.netbeans.org/cddl.txt.
jtulach@52
     8
 *
jtulach@52
     9
 * When distributing Covered Code, include this CDDL Header Notice in each file
jtulach@52
    10
 * and include the License file at http://www.netbeans.org/cddl.txt.
jtulach@52
    11
 * If applicable, add the following below the CDDL Header, with the fields
jtulach@52
    12
 * enclosed by brackets [] replaced by your own identifying information:
jtulach@52
    13
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@52
    14
 *
jtulach@52
    15
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@52
    16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@52
    17
 * Microsystems, Inc. All Rights Reserved.
jtulach@52
    18
 */
jtulach@52
    19
jtulach@52
    20
package org.netbeans.apifest.boolcircuit;
jtulach@52
    21
jtulach@52
    22
/**
jtulach@52
    23
 * A representation of a logic circuit.
jtulach@52
    24
 * It can only be constructed from an element net.
jtulach@52
    25
 * Usage:
jtulach@52
    26
 * <pre>
jtulach@52
    27
 *  Circuit c = Circuit.construct(
jtulach@52
    28
 *      Element.createOr(
jtulach@52
    29
 *          Element.createAnd(
jtulach@52
    30
 *              Element.createInput(0),
jtulach@52
    31
 *              Element.createInput(1)
jtulach@52
    32
 *          ),
jtulach@52
    33
 *          Element.createInput(2)
jtulach@52
    34
 *      )
jtulach@52
    35
 *  );
jtulach@52
    36
 *            
jtulach@52
    37
 *  boolean val = c.evaluate(false, true, false));
jtulach@52
    38
 * </pre>
jtulach@52
    39
 */
jtulach@52
    40
public class Circuit {
jtulach@52
    41
    Element root;
jtulach@52
    42
    int pins;
jtulach@52
    43
    
jtulach@52
    44
    private Circuit(Element elem) {
jtulach@52
    45
        root = elem;
jtulach@52
    46
        pins = 1 + root.maxInput();
jtulach@52
    47
    }
jtulach@52
    48
    
jtulach@52
    49
    /**
jtulach@52
    50
     * Evaluate output of the circuit for given inputs.
jtulach@52
    51
     * For general 
jtulach@52
    52
     */
jtulach@52
    53
    public boolean evaluate(Boolean ... inputs) throws UnstableException {
jtulach@52
    54
        if (inputs.length != pins) throw new IllegalArgumentException("Wrong number of inputs, " + pins + " expected.");
jtulach@52
    55
        double[] inp = new double[pins];
jtulach@52
    56
        for (int i=0; i<pins; i++) inp[i] = inputs[i] ? 1 : 0;
jtulach@52
    57
        double res = root.evaluate(inp);
jtulach@52
    58
        return res > 0.5;
jtulach@52
    59
    }
jtulach@52
    60
jtulach@52
    61
    public double evaluate(double ... inputs) throws UnstableException {
jtulach@52
    62
        if (inputs.length != pins) throw new IllegalArgumentException("Wrong number of inputs, " + pins + " expected.");
jtulach@52
    63
        
jtulach@52
    64
        double[] inp = new double[pins]; // defensive copy with a check
jtulach@52
    65
        for (int i=0; i<inputs.length; i++) {
jtulach@52
    66
            if (inputs[i] < 0.0 || inputs[i] > 1.0) {
jtulach@52
    67
                throw new IllegalArgumentException("Out of range, pin " + i + ", value=" + inputs[i]);
jtulach@52
    68
            }
jtulach@52
    69
            inp[i] = inputs[i];
jtulach@52
    70
        }
jtulach@52
    71
        
jtulach@52
    72
        return root.evaluate(inp);
jtulach@52
    73
    }
jtulach@52
    74
    
jtulach@52
    75
    /**
jtulach@52
    76
     * Creates a circuit from a preconstructed element net.
jtulach@52
    77
     *
jtulach@52
    78
     * @param a top-level element representing the logic net.
jtulach@52
    79
     * @return a circuit prepared for evaluating result for given inputs.
jtulach@52
    80
     * 
jtulach@52
    81
     */
jtulach@52
    82
    public static Circuit construct(Element elem) {
jtulach@52
    83
        return new Circuit(elem);
jtulach@52
    84
    }
jtulach@52
    85
    
jtulach@52
    86
}