/* All user scripts should derive from the base "Application" class */ require ("Application"); /* Get the Gamma library functions and methods for ODBC and/or * Windows programming. Uncomment either or both. */ //require ("WindowsSupport"); //require ("ODBCSupport"); /* Applications share the execution thread and the global name * space, so we create a class that contains all of the functions * and variables for the application. This does two things: * 1) creates a private name space for the application, and * 2) allows you to re-load the application to create either * a new unique instance or multiple instances without * damaging an existing running instance. */ class AutoCalculation Application { fileName = "C:\\Program Files\\Cogent\\Cogent DataHub\\scripts\\calculations.csv"; fp; } class Computation { app; // the Application instance defining this computation output; // a symbol inputs; // a list of symbols code; // the code to execute } method Computation.constructor(app, expression) { .app = app; .code = expression; .output = .findOutput(expression); .inputs = .findInputs(expression); datahub_command(format("(create %s 1)", stringc(.output)), 1); with input in .inputs do { datahub_command(format("(create %s 1)", stringc(input)), 1); .app.OnChange(input, `(@self).compute()); } } method Computation.compute() { try { eval(.code); } catch { princ ("Assignment to ", .output, " failed: ", _last_error_, "\n"); } } method Computation.findOutput (expr) { cadr(expr); } method Computation.findInputsRecursive (expr) { local inputs, partial; if (list_p(expr)) { with part in expr do { partial = .findInputsRecursive(part); inputs = nappend(inputs, partial); } } else if (symbol_p(expr) && strchr(string(expr), ":") != -1) { inputs = cons(expr, inputs); } inputs; } method Computation.findInputs (expr) { .findInputsRecursive(cddr(expr)); } method AutoCalculation.readFile() { //we open the file here. If you want to change the path of where the file is you would do that here. if ((.fp = open(.fileName, "r", t)) != nil) { //create some local variables local line; //loop until we have read the entire file. while((line = read(.fp)) != _eof_) { local comp = new Computation(self, line); } //once we have read the entire file we need to close the file. close(.fp); } } /* Write the 'main line' of the program here. */ method AutoCalculation.constructor () { .readFile(); } /* Any code to be run when the program gets shut down. */ method AutoCalculation.destructor () { } /* Start the program by instantiating the class. If your * constructor code does not create a persistent reference to * the instance (self), then it will be destroyed by the * garbage collector soon after creation. If you do not want * this to happen, assign the instance to a global variable, or * create a static data member in your class to which you assign * 'self' during the construction process. ApplicationSingleton() * does this for you automatically. */ ApplicationSingleton (AutoCalculation);