/** * Implements update() method * * @throws DAVEException */ public void update() throws DAVEException { int numInputs; Iterator<Signal> theInputs; Signal theInput; double[] iwv; // index and weights vector int[] iv; // index vector int index = 0; boolean ready = true; boolean verbose = this.isVerbose(); if (verbose) { System.out.println(); System.out.println("Entering update method for function '" + this.getName() + "'"); } // sanity check to see if number of inputs matches our dimensionality numInputs = this.inputs.size(); if (numInputs != this.functionTableDef.numDim()) throw new DAVEException( "Number of inputs doesn't match function dimensions in '" + this.getName() + "'"); // see if each input variable is ready theInputs = this.inputs.iterator(); iwv = new double[numInputs]; // index and weights vector iv = new int[numInputs]; // index vector if (verbose) System.out.println(" Allocated index-and-weights vector of size " + this.inputs.size()); // Here to do table lookup while (theInputs.hasNext()) { theInput = theInputs.next(); if (!theInput.sourceReady()) { ready = false; if (verbose) System.out.println(" Upstream signal '" + theInput.getName() + "' is not ready."); iwv[index] = 0.0; iv[index] = 0; } else { iwv[index] = theInput.sourceValue(); iv[index] = (int) theInput.sourceValue(); if (verbose) System.out.println(" Input # " + index + " value is " + iwv[index]); } index++; } if (!ready) return; // At this point we have the index-and-weights vector in iwv. // Call recursive interpolation routine this.value = this.interpolate(iwv, iv, numInputs); if (verbose) System.out.println(" Interpolate returned value " + this.value); // record current cycle counter resultsCycleCount = ourModel.getCycleCounter(); }
/** * Using specified output (dependent) variable name, look for such a declared variable. If not * found, create appropriate one. * * @param function JDOM function Element */ protected String hookUpOutput(Element function) { // Parse and discover dependent variable ID Element depVar = function.getChild("dependentVarRef", this.ns); if (depVar == null) depVar = function.getChild("dependentVarPts", this.ns); // simple table String depVarID = depVar.getAttributeValue("varID"); Iterator<Signal> sigIterator = this.ourModel.getSignals().iterator(); boolean depVarSignalFound = false; Signal dVsig = null; // Look for existing variable definition (signal) while (sigIterator.hasNext()) { // look for matching explicit signal dVsig = sigIterator.next(); if (depVarID.equals(dVsig.getVarID())) { depVarSignalFound = true; break; } } /* // if not found, make our own if (!depVarSignalFound) { // create and connect to new signal dVsig = new Signal( depVarName, depVarName, "unkn", 10, m ); } */ // if not found, complain if (!depVarSignalFound) { System.err.println( "Unable to locate output signal with ID '" + depVarID + "' for Function block '" + this.getName() + "'."); System.exit(0); } try { this.addOutput(dVsig); // connect to new or existing signal } catch (DAVEException e) { System.err.println( "Unexpected error: new Function block '" + this.getName() + "' is unable to hook up to output signal ID '" + depVarID + "':"); System.err.println(e.getMessage()); System.exit(0); } return dVsig.getName(); }
/** * Create a new breakpoint block and associated index-and-weight signal to serve as an input to * this block. */ protected void createAndHookUpIWPath(String bpID, String varID, String iwSignalID, int portNum) { Signal connector = null; // signal wire to join Breakpoint to Function // Look at predeclared signals to match breakpoint input varID // to get name & units if (this.isVerbose()) System.out.print("Looking for input signal named '" + varID + "'..."); Signal theBPInputSignal = ourModel.getSignals().findByID(varID); if (theBPInputSignal != null) { if (this.isVerbose()) System.out.println(" found it."); // create and connect to new intermediate signal String connectorName = theBPInputSignal.getName() + "_by_" + bpID; String units = theBPInputSignal.getUnits(); if (this.isVerbose()) System.out.println( "Creating new index-and-weights signal named '" + connectorName + "' with a varID of '" + iwSignalID + "' and units of '" + units + "'"); connector = new Signal(connectorName, iwSignalID, units, 2, ourModel); connector.setAutoFlag(); // flag as an automatic variable connector.addSink(this, portNum + 1); // hook up to new signal } else { // else block - error if (this.isVerbose()) System.out.println(" DIDN'T FIND IT!! - ERROR!"); System.err.println( "Error: in BlockFuncTable.createAndHookUpIWPath() for Function block '" + this.getName() + "', can't find independent (input) variable with ID '" + iwSignalID + "'."); System.exit(0); } // Create new breakpoint block to generate the index-and-weights signal try { new BlockBP(bpID, bpID, theBPInputSignal, connector, ourModel); } catch (DAVEException e) { System.err.println( "BlockFuncTable.createAndHookUpIWPath: in hooking up Function block '" + this.getName() + "':"); System.err.println(e.getMessage()); System.exit(0); } }
/** * Implements update() method * * <p>Passes input 0 if input 1 > 0.; otherwise passes input 2 to output * * @throws DAVEException */ public void update() throws DAVEException { int numInputs; Iterator<Signal> theInputs; Signal theInput; double[] theInputValue; int index = 0; int requiredInputs = 3; boolean verbose = this.isVerbose(); if (verbose) { System.out.println(); System.out.println("Entering update method for switch '" + this.getName() + "'"); } // sanity check to see if we have exact number of inputs numInputs = this.inputs.size(); if (numInputs != requiredInputs) { throw new DAVEException( "Number of inputs to '" + this.getName() + "' wrong - should be " + requiredInputs + "."); } // allocate memory for the input values theInputValue = new double[requiredInputs]; // see if each input variable is ready theInputs = this.inputs.iterator(); while (theInputs.hasNext()) { theInput = theInputs.next(); if (!theInput.sourceReady()) { if (verbose) { System.out.println(" Upstream signal '" + theInput.getName() + "' is not ready."); } return; } else { theInputValue[index] = theInput.sourceValue(); } index++; } // Calculate our output this.value = theInputValue[2]; if (Math.abs(theInputValue[1]) > 0.0001) // choose input 3 if input 2 non-zero { this.value = theInputValue[0]; } // record current cycle counter resultsCycleCount = ourModel.getCycleCounter(); }
/** * Implements update() method * * @throws DAVEException */ public void update() throws DAVEException { int numInputs; Iterator<Signal> theInputs; Signal theInput; double[] theInputValue; int index = 0; int requiredInputs = 2; boolean verbose = this.isVerbose(); if (verbose) { System.out.println(); System.out.println("Entering update method for function '" + this.getName() + "'"); } // sanity check to see if we have exact number of inputs numInputs = this.inputs.size(); if (numInputs != requiredInputs) throw new DAVEException( "Number of inputs to '" + this.getName() + "' wrong - should be " + requiredInputs + "."); // allocate memory for the input values theInputValue = new double[requiredInputs]; // see if each input variable is ready theInputs = this.inputs.iterator(); while (theInputs.hasNext()) { theInput = theInputs.next(); if (!theInput.sourceReady()) { if (verbose) System.out.println(" Upstream signal '" + theInput.getName() + "' is not ready."); return; } else { theInputValue[index] = theInput.sourceValue(); } index++; } // Calculate our output this.value = Double.NaN; switch (this.relation) { case LT: if (theInputValue[0] < theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; case LEQ: if (theInputValue[0] <= theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; case EQ: if (theInputValue[0] == theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; case GEQ: if (theInputValue[0] >= theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; case GT: if (theInputValue[0] > theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; case NEQ: if (theInputValue[0] != theInputValue[1]) this.value = 1.0; else this.value = 0.0; break; } if (this.value == Double.NaN) throw new DAVEException( "Unrecognized operator " + this.relationOp + " in block " + this.getName()); // record current cycle counter resultsCycleCount = ourModel.getCycleCounter(); }