/* SCCS %W%---%G%--%U% */ // Copyright: Copyright (c) 1997 University of Southern California Brain Project. // Copyright: This software may be freely copied provided the toplevel // Copyright: COPYRIGHT file is included with each such copy. // Copyright: Email nsl@java.usc.edu. // $Log: s_Nsl.java,v $ // Revision 1.2 1998/02/03 00:23:51 erhan // adapted new nsls structure // package nslj.src.nsls.struct; import tcl.lang.*; import java.util.*; import nslj.src.lang.*; import nslj.src.system.*; import nslj.src.display.*; import nslj.src.math.*; import nslj.src.cmd.*; public class s_Nsl implements Command { private Interp tclInterp; private NslData num, var1, var2; // nsl commands static final private String opts[] = { "source", // 0 "makeInst", // 1 "makeConn", // 2 "initSys", // 3 "initRun", // 4 "simRun", // 5 "stepRun", // 6 "contRun", // 7 "endRun", // 8 "run", // 9 "doRunEpochTimes", // 10 "initTrain", // 11 "simTrain", // 12 "stepTrain", // 13 "contTrain", // 14 "endTrain", // 15 "train", // 16 "doTrainEpochTimes", // 17 "step", // 18 "cont", // 19 "endSys", // 20 "break", // 21 "get", // 22 "set", // 23 "create", // 24 "exit", // 25 "quit", // 26 "initModule", // 27 "endModule", // 28 "call", // 29 "getObject", // 30 "initRunEpoch", // 31 "endRunEpoch", // 32 "initTrainEpoch", // 33 "endTrainEpoch", // 34 "breakModules", // 35 "breakCycles", // 36 "breakEpochs", // 37 "contModule", // 38 "contCycle", // 39 "contEpoch", // 40 "stepModule", // 41 "stepCycle", // 42 "stepEpoch", // 43 "trainAndRunAll", // 44 "trans", //45 "product", //46 "dotProduct", //47 "add", //48 "sub", //49 "elemMult", //50 "elemDiv", //51 "conv", //52 "convZero", //53 "abs", //54 "arcCos", //55 "arcSin", //56 "arcTan", //57 "cos", //58 "distance", //59 "exp", //60 "gausian", //61 "log", //62 "maxMerge", //63 "minMerge", //64 "pow", //65 "random", //66 "rint", //67 "sin", //68 "sqrt", //69 "tan", //70 "maxElem", //71 "maxValue", //72 "minElem", //73 "minValue", //74 "sum", //75 "fillRows", //76 "fillColumns", //77 "equ", //78 "geq", //79 "gtr", //80 "leq", //81 "les", //82 "neq", //83 "and", //84 "or", //85 "none", //86 "some", //87 "all", //88 }; // nsl get options static final private String getOpts[] = { "-dim" }; // nsl create options static final private String createOpts[] = { "NslOutFrame", "NslOutCanvas", }; // nsl create NslOutputFrame switches static final private String createSw[] = { "-title", "-rows", "-columns", "-x0", "-y0", "-width", "-height", "-font", "-background", "-foreground", "-freq", }; // nsl create NslOutputCanvas switches static final private String createSwCanvas[] = { "-title", "-var", "-graph", "-position", "-xlabel", "-ylabel", "-wymin", "-wymax", "-wxmin", "-wxmax", "-freq", "-drawcolor", "-drawstyle", "-option", }; // nsl create NslOutputCanvas graph switches static final private String graphSw[] = { "Area", "Bar", "Dot", "Image", "Spatial3d", "String", "Temporal", }; // nsl create NslOutputCanvas option switches static final private String optionSw[] = { "rescale", "shift", }; // nsl create NslOutputCanvas position switches static final private String positionSw[] = { "first", "next", "previous", "last", }; private void exec(String cmd, String param) { NslThreadCommands cmdExec = new NslThreadCommands( cmd, param, Executive.system, Executive.system.getInterpreter()); if (Executive.readingFile) { cmdExec.run(); } else { cmdExec.start(); } } private void exec(String cmd) { NslThreadCommands cmdExec = new NslThreadCommands( cmd, Executive.system, Executive.system.getInterpreter()); if (Executive.readingFile) { cmdExec.run(); } else { cmdExec.start(); } } public void cmdProc(Interp interp, TclObject argv[]) throws TclException { tclInterp = interp; if (argv.length < 2) { echo("wrong # args: should be \"nsl\" nsl_command ?opts?"); } else { String variable; int index; index = TclIndex.get(tclInterp, argv[1], opts, "option", 0); switch (index) { case 0: /* * the following code implements the source command */ //Executive.readingFile=true; //String filename = argv[2].toString(); //interp.evalFile(argv[2].toString()); exec("Source",argv[2].toString()); //Executive.readingFile=false; break; case 1: /* * the following code implements the makeInst command */ echo("makeinst"); break; case 2: /* * the following code implements the makeConn command */ echo("makenslConn"); break; case 3: /* * the following code implements the initSys command */ Executive.interpreter.execute("initsys"); // Executive.system.init_displays(); break; case 4: /* * the following code implements the initRun command */ exec("InitRun"); break; case 5: /* * the following code implements the simRun command */ if (Executive.system.isSchedulerInTrainMode() || !Executive.initMade) { echo("You are not in run mode"); } if (argv.length < 3) { exec("SimRun"); } else { exec("SimRun",""+TclDouble.get(tclInterp, argv[2])); } break; case 6: /* * the following code implements the stepRun command */ if (Executive.system.isSchedulerInTrainMode() || !Executive.initMade) { echo("You are not in run mode"); } if (argv.length < 3) { exec("1"); } else { // We have to check if is number or not exec(""+TclInteger.get(tclInterp, argv[2])); } break; case 7: /* * the following code implements the contRun command */ if (Executive.system.isSchedulerInTrainMode() || !Executive.initMade) { echo("You are not in run mode"); } if (argv.length < 3) { exec("Continue"); } else { exec("Continue",""+TclDouble.get(tclInterp, argv[2])); } break; case 8: /* * the following code implements the endRun command */ if (Executive.system.isSchedulerInTrainMode() || !Executive.initMade) { echo("You are not in run mode"); } exec("EndRun"); break; case 9: /* * the following code implements the run command */ Executive.initMade = true; if (argv.length < 3) { exec("Run"); } else { exec("Run",""+TclDouble.get(tclInterp, argv[2])); } //Executive.initMade = false; break; case 10: /* * the following code implements the DoRunEpochTimes command */ Executive.initMade = true; if (argv.length < 3) { exec("DoRunEpochTimes"); } else { exec("DoRunEpochTimes",""+TclInteger.get(tclInterp, argv[2])); } //Executive.initMade = false; break; case 11: /* * the following code implements the initTrain command */ exec("InitTrain"); break; case 12: /* * the following code implements the simTrain command */ if (Executive.system.isSchedulerInRunMode() || !Executive.initMade) { echo("You are not in train mode"); } exec("SimTrain"); break; case 13: /* * the following code implements the stepTrain command */ if (Executive.system.isSchedulerInRunMode() || !Executive.initMade) { echo("You are not in train mode"); } if (argv.length < 3) { exec("1"); } else { // We have to check if is number or not exec(""+TclInteger.get(tclInterp, argv[2])); } break; case 14: /* * the following code implements the contTrain command */ if (Executive.system.isSchedulerInRunMode() || !Executive.initMade) { echo("You are not in train mode"); } if (argv.length < 3) { exec("Continue"); } else { exec("Continue",""+TclDouble.get(tclInterp, argv[2])); } break; case 15: /* * the following code implements the endTrain command */ if (Executive.system.isSchedulerInRunMode() || !Executive.initMade) { echo("You are not in train mode"); } exec("EndTrain"); break; case 16: /* * the following code implements the train command */ Executive.initMade = true; if (argv.length < 3) { exec("Train"); } else { exec("Train",""+TclDouble.get(tclInterp, argv[2])); } //Executive.initMade = false; break; case 17: /* * the following code implements the DoTrainEpochTimes command */ Executive.initMade = true; if (argv.length < 3) { exec("DoTrainEpochTimes"); } else { exec("DoTrainEpochTimes",""+TclInteger.get(tclInterp, argv[2])); } //Executive.initMade = false; break; case 18: /* * the following code implements the step command */ if (!Executive.initMade) { echo("Mode has not been set"); } if (argv.length < 3) { exec("StepCycle"); } else { // We have to check if is number or not exec("StepCycle",""+TclInteger.get(tclInterp, argv[2])); } break; case 19: /* * the following code implements the cont command */ if (!Executive.initMade) { echo("Mode has not been set"); } if (argv.length < 3) { exec("Continue"); } else { exec("Continue",""+TclDouble.get(tclInterp, argv[2])); } break; case 20: /* * the following code implements the endsys command */ Executive.interpreter.execute("endSys"); break; case 21: /* * the following code implements the break command */ exec("Break"); break; case 22: /* * the following code implements the get command */ if (argv.length < 3) { echo("wrong # args: should be \"nsl get\" variable ?opts?"); } variable = argv[2].toString(); if (hasParenthesis(variable)) { //System.out.println("Detecting Parenthesis"); String indices; try { indices = getVariableIndex(variable); //System.out.println("Index: "+indices); variable = getVariableName(variable); //System.out.println("Variable Name: "+variable); Vector v = getIndices(indices); //System.out.println("Number of indices: "+v.size()); int l=v.size(); /*for (int i=0; i4) { echo("Incorrect number of dimensions: should be less than 4"); } if (num instanceof NslNumeric0) { echo("Incorrect number of dimensions: should be 0"); } else if (num instanceof NslNumeric1) { if (l!=1) { echo("Incorrect number of dimensions: should be 1"); } size = ((NslNumeric1)num).getSize(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } tclInterp.setResult(""+ ((NslNumeric1)num).getdouble(elem1.intValue())); } else if (num instanceof NslNumeric2) { if (l!=2) { echo("Incorrect number of dimensions: should be 2"); } size = ((NslNumeric2)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric2)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } tclInterp.setResult(""+ ((NslNumeric2)num).getdouble(elem1.intValue(), elem2.intValue())); } else if (num instanceof NslNumeric3) { if (l!=3) { echo("Incorrect number of dimensions: should be 3"); } size = ((NslNumeric3)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric3)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric3)num).getSize3(); elem3 = (Integer)v.elementAt(2); if (elem3.intValue()>size || elem3.intValue()<0) { echo("Second dimension out of range"); } tclInterp.setResult(""+ ((NslNumeric3)num).getdouble(elem1.intValue(), elem2.intValue(),elem3.intValue())); } else if (num instanceof NslNumeric4) { if (l!=4) { echo("Incorrect number of dimensions: should be 4"); } size = ((NslNumeric4)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric4)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric4)num).getSize3(); elem3 = (Integer)v.elementAt(2); if (elem3.intValue()>size || elem3.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric4)num).getSize4(); elem4 = (Integer)v.elementAt(3); if (elem4.intValue()>size || elem4.intValue()<0) { echo("Second dimension out of range"); } tclInterp.setResult(""+((NslNumeric4)num).getdouble(elem1.intValue(), elem2.intValue(),elem3.intValue(), elem4.intValue())); } } Executive.system.init_displays(); } catch (NoSuchMethodException e) { echo("Sintax error in variable "+variable); } break; } num = resolve_var(argv[2].toString()); if (num==null) { // We have to check if its a systema variable if (hasSystem(variable)) { tclInterp.eval("nsl call system get"+nslGetSystemVariable(variable)); } else { echo("can't read \"" +argv[2].toString() +"\": no such nsl variable"); } } else { if (argv.length == 4) { index = TclIndex.get(tclInterp, argv[3], getOpts, "sw", 0); switch (index) { case 0: TclObject result = TclList.newInstance(); String s; if (num instanceof NslNumeric1) { s = ((NslNumeric1)num).getSize() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); tclInterp.setResult(result); } else if (num instanceof NslNumeric2) { s = ((NslNumeric2)num).getSize1() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric2)num).getSize2() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); tclInterp.setResult(result); } else if (num instanceof NslNumeric3) { s = ((NslNumeric3)num).getSize1() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric3)num).getSize2() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric3)num).getSize3() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); tclInterp.setResult(result); } else if (num instanceof NslNumeric4) { s = ((NslNumeric4)num).getSize1() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric4)num).getSize2() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric4)num).getSize3() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); s = ((NslNumeric4)num).getSize4() + ""; TclList.append(tclInterp, result, TclString.newInstance(s)); tclInterp.setResult(result); } else { tclInterp.setResult(""); } break; } } else { tclInterp.setResult(num.toString()); } } break; case 23: /* * the following code implements the set command */ if (argv.length < 3) { echo("wrong # args: should be \"nsl set\" variable values_in_list_form"); } variable = argv[2].toString(); if (hasParenthesis(variable)) { //System.out.println("Detecting Parenthesis"); String indices; try { indices = getVariableIndex(variable); //System.out.println("Index: "+indices); variable = getVariableName(variable); //System.out.println("Variable Name: "+variable); Vector v = getIndices(indices); //System.out.println("Number of indices: "+v.size()); int l=v.size(); /*for (int i=0; i4) { echo("Incorrect number of dimensions: should be less than 4"); } if (num instanceof NslNumeric0) { echo("Incorrect number of dimensions: should be 0"); } else if (num instanceof NslNumeric1) { if (l!=1) { echo("Incorrect number of dimensions: should be 1"); } size = ((NslNumeric1)num).getSize(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } if (argv.length >= 4) { //System.out.println("Assigning "+TclDouble.get(tclInterp,argv[3])+ // " to the element in "+elem1.intValue()); ((NslNumeric1)num).set(elem1.intValue(), TclDouble.get(tclInterp,argv[3])); } else { echo(""+((NslNumeric1)num).getdouble(elem1.intValue())); } } else if (num instanceof NslNumeric2) { if (l!=2) { echo("Incorrect number of dimensions: should be 2"); } size = ((NslNumeric2)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric2)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } if (argv.length >= 4) { //System.out.println("Assigning "+TclDouble.get(tclInterp,argv[3])+ // " to the element in "+elem1.intValue()+","+elem2.intValue()); ((NslNumeric2)num).set(elem1.intValue(), elem2.intValue(), TclDouble.get(tclInterp,argv[3])); } else { echo(""+((NslNumeric2)num).getdouble(elem1.intValue(), elem2.intValue())); } } else if (num instanceof NslNumeric3) { if (l!=3) { echo("Incorrect number of dimensions: should be 3"); } size = ((NslNumeric3)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric3)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric3)num).getSize3(); elem3 = (Integer)v.elementAt(2); if (elem3.intValue()>size || elem3.intValue()<0) { echo("Second dimension out of range"); } if (argv.length >= 4) { ((NslNumeric3)num).set(elem1.intValue(), elem2.intValue(), elem3.intValue(), TclDouble.get(tclInterp,argv[3])); } else { echo(""+((NslNumeric3)num).getdouble(elem1.intValue(), elem2.intValue(),elem3.intValue())); } } else if (num instanceof NslNumeric4) { if (l!=4) { echo("Incorrect number of dimensions: should be 4"); } size = ((NslNumeric4)num).getSize1(); elem1 = (Integer)v.elementAt(0); if (elem1.intValue()>size || elem1.intValue()<0) { echo("First dimension out of range"); } size = ((NslNumeric4)num).getSize2(); elem2 = (Integer)v.elementAt(1); if (elem2.intValue()>size || elem2.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric4)num).getSize3(); elem3 = (Integer)v.elementAt(2); if (elem3.intValue()>size || elem3.intValue()<0) { echo("Second dimension out of range"); } size = ((NslNumeric4)num).getSize4(); elem4 = (Integer)v.elementAt(3); if (elem4.intValue()>size || elem4.intValue()<0) { echo("Second dimension out of range"); } if (argv.length >= 4) { ((NslNumeric4)num).set(elem1.intValue(), elem2.intValue(), elem3.intValue(), elem4.intValue(), TclDouble.get(tclInterp,argv[3])); } else { echo(""+((NslNumeric4)num).getdouble(elem1.intValue(), elem2.intValue(),elem3.intValue(), elem4.intValue())); } } } Executive.system.init_displays(); } catch (NoSuchMethodException e) { echo("Sintax error in variable "+variable); } break; } NslData num=resolve_var(argv[2].toString()); if (num==null) { // We have to check if its a systema variable if (hasSystem(variable)) { //String functionName = nslGetSystemVariable(variable); if (argv.length < 4) { tclInterp.eval("nsl call system get"+ nslGetSystemVariable(variable)); } else { tclInterp.eval("nsl call system set"+ nslGetSystemVariable(variable)+" "+argv[3]); } } else { echo("can't read \"" +argv[2].toString() +"\": no such nsl variable"); } } else { if (argv.length < 4) { echo(num.toString()); } else { if (argv.length == 4) { int size1, size2, size3, size4; if (num instanceof NslNumeric0) { ((NslNumeric0)num).set(TclDouble.get(tclInterp,argv[3])); } else if (num instanceof NslNumeric1) { size1= ((NslNumeric1)num).getSize(); int limit1 = TclList.getLength(tclInterp,argv[3]); if ( limit1 <= size1 ) { for (int i=0; i"+temp.toString()); last = temp.toString(); temp = TclList.index(tclInterp,temp,0); } while(i<5 && hasBrackets(last)); size = i-1; if (size==0 && dim[0]==1) size=i-1; else size=i; //System.out.println("It is a "+size+" dimension variable"); //for (int j=0; j5) { return name.substring(0,i); } else { return null; } } private String getCanvasName(String name) { int i = lastdot(name); if (i>5) { int l = name.length(); return name.substring(i+1,l); } else { return null; } } static private String getVariableName(String variable) throws NoSuchMethodException { int o = openParenthesis(variable); if (o>0) { return variable.substring(0,o); } else { throw new NoSuchMethodException(); } } /* * This is to solve the hierarchy - chetan */ static private String root(String s) { int i; //System.out.println("root:"+s); i=lastdot(s); if (i!=-1) return s.substring(0,lastdot(s)); else return s; } static private String tail(String s) { //System.out.println("tail:"+s); return(s.substring(lastdot(s)+1,s.length())); } static private boolean hasBrackets(String variable) { int o = openBrackets(variable); return o!=-1; } static private boolean hasParenthesis(String variable) { int o = openParenthesis(variable); return o!=-1; } static private boolean hasSystem(String variable) { return variable.startsWith("system."); } static private String nslGetSystemVariable(String variable) { int pos = 7; // system. <- it has 7 characters String firstCharacter = "" + variable.charAt(pos); firstCharacter = firstCharacter.toUpperCase(); return firstCharacter + variable.substring(pos+1); } private Vector getIndices(String indices) throws NoSuchMethodException, TclException { Vector result = new Vector(); StringBuffer strbuf = new StringBuffer(); char state = 's'; // 's' = Same Numbre, 'n' = New Number int l=indices.length(); char element; for(int i=0;i=0;i--) if (s.charAt(i)=='.' ) return i; return -1; } }