samples/apifest1/day2/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/apifest1/day2/pinbasedsolution/src/org/netbeans/apifest/boolcircuit/Element.java Sat Jun 14 09:52:45 2008 +0200
1.3 @@ -0,0 +1,119 @@
1.4 +/*
1.5 + * Element.java
1.6 + *
1.7 + * Created on 12. Ĩervenec 2006, 14:14
1.8 + *
1.9 + * To change this template, choose Tools | Template Manager
1.10 + * and open the template in the editor.
1.11 + */
1.12 +
1.13 +package org.netbeans.apifest.boolcircuit;
1.14 +
1.15 +/**
1.16 + * Representation of an element in the circuit.
1.17 + * The internal behaviour of the element is opaque to the API user, it can only
1.18 + * be used as a building block for logical equations, using primitive operation
1.19 + * factories and a factory for input pin representation.
1.20 + * Elements are chained to create the logical net. The inputs to the net are
1.21 + * represented by the elements created by {@link #createInput(boolean[])}
1.22 + * factory method.
1.23 + */
1.24 +public abstract class Element {
1.25 +
1.26 + /** Creates a new instance of Element */
1.27 + private Element() {
1.28 + }
1.29 +
1.30 + /**
1.31 + */
1.32 + abstract double evaluate(double[] inputs);
1.33 +
1.34 + abstract int maxInput();
1.35 +
1.36 + /**
1.37 + * Creates an Element representing 2-input AND function.
1.38 + *
1.39 + */
1.40 + public static Element createAnd(final Element source1, final Element source2) {
1.41 + return new Element() {
1.42 + double evaluate(double[] inputs) {
1.43 + return source1.evaluate(inputs) * source2.evaluate(inputs);
1.44 + }
1.45 +
1.46 + int maxInput() {
1.47 + return Math.max(source1.maxInput(), source2.maxInput());
1.48 + }
1.49 + };
1.50 + }
1.51 +
1.52 + /**
1.53 + * Creates an Element representing 2-input OR function.
1.54 + *
1.55 + */
1.56 + public static Element createOr(final Element source1, final Element source2) {
1.57 + return new Element() {
1.58 + double evaluate(double[] inputs) {
1.59 + double x = source1.evaluate(inputs);
1.60 + double y = source2.evaluate(inputs);
1.61 + return 1 - (1 - x)*(1-y);
1.62 + }
1.63 +
1.64 + int maxInput() {
1.65 + return Math.max(source1.maxInput(), source2.maxInput());
1.66 + }
1.67 + };
1.68 + }
1.69 +
1.70 + /**
1.71 + * Creates an Element representing negation.
1.72 + *
1.73 + */
1.74 + public static Element createNot(final Element source1) {
1.75 + return new Element() {
1.76 + double evaluate(double[] inputs) {
1.77 + return 1 - source1.evaluate(inputs);
1.78 + }
1.79 +
1.80 + int maxInput() {
1.81 + return source1.maxInput();
1.82 + }
1.83 + };
1.84 + }
1.85 +
1.86 + /**
1.87 + * Creates an Element representing input to the logical net.
1.88 + *
1.89 + */
1.90 + public static Element createInput(final int pin) {
1.91 + return new Element() {
1.92 + double evaluate(double[] inputs) {
1.93 + return inputs[pin];
1.94 + }
1.95 +
1.96 + int maxInput() {
1.97 + return pin;
1.98 + }
1.99 + };
1.100 + }
1.101 +
1.102 + /**
1.103 + * Creates an Element with user-defined transfer function.
1.104 + *
1.105 + */
1.106 + public static Element createGate(final Element source1, final Element source2, final Function function) {
1.107 + return new Element() {
1.108 + double evaluate(double[] inputs) {
1.109 + double x = source1.evaluate(inputs);
1.110 + double y = source2.evaluate(inputs);
1.111 + double result = function.evaluate(x, y);
1.112 + if (result < 0.0 || result > 1.0) throw new InternalError("Illegal gate function");
1.113 + return result;
1.114 + }
1.115 +
1.116 + int maxInput() {
1.117 + return Math.max(source1.maxInput(), source2.maxInput());
1.118 + }
1.119 + };
1.120 + }
1.121 +
1.122 +}