samples/apifest1/day1/pinbasedsolution/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/day1/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Circuit.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,71 @@
     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 + * A representation of a logic circuit.
    1.27 + * It can only be constructed from an element net.
    1.28 + * Usage:
    1.29 + * <pre>
    1.30 + *  Circuit c = Circuit.construct(
    1.31 + *      Element.createOr(
    1.32 + *          Element.createAnd(
    1.33 + *              Element.createInput(0),
    1.34 + *              Element.createInput(1)
    1.35 + *          ),
    1.36 + *          Element.createInput(2)
    1.37 + *      )
    1.38 + *  );
    1.39 + *            
    1.40 + *  boolean val = c.evaluate(false, true, false));
    1.41 + * </pre>
    1.42 + */
    1.43 +public class Circuit {
    1.44 +    Element root;
    1.45 +    int pins;
    1.46 +    
    1.47 +    private Circuit(Element elem) {
    1.48 +        root = elem;
    1.49 +        pins = 1 + root.maxInput();
    1.50 +    }
    1.51 +    
    1.52 +    /**
    1.53 +     * Evaluate output of the circuit for given inputs.
    1.54 +     * For general 
    1.55 +     */
    1.56 +    public boolean evaluate(Boolean ... inputs) throws UnstableException {
    1.57 +        if (inputs.length != pins) throw new IllegalArgumentException("Wrong number of inputs, " + pins + " expected.");
    1.58 +        boolean[] inp = new boolean[pins];
    1.59 +        for (int i=0; i<pins; i++) inp[i] = inputs[i];
    1.60 +        return root.evaluate(inp);
    1.61 +    }
    1.62 +    
    1.63 +    /**
    1.64 +     * Creates a circuit from a preconstructed element net.
    1.65 +     *
    1.66 +     * @param a top-level element representing the logic net.
    1.67 +     * @return a circuit prepared for evaluating result for given inputs.
    1.68 +     * 
    1.69 +     */
    1.70 +    public static Circuit construct(Element elem) {
    1.71 +        return new Circuit(elem);
    1.72 +    }
    1.73 +    
    1.74 +}