Search in sources :

Example 1 with SimpleSymbolTableEntry

use of cbit.vcell.parser.SimpleSymbolTable.SimpleSymbolTableEntry in project vcell by virtualcell.

the class FunctionFileGenerator method readFunctionsFile.

/**
 * This method was created in VisualAge.
 * @param logFile java.io.File
 */
public static synchronized Vector<AnnotatedFunction> readFunctionsFile(File functionsFile, String simJobID) throws java.io.FileNotFoundException, java.io.IOException {
    // Check if file exists
    if (!functionsFile.exists()) {
        throw new java.io.FileNotFoundException("functions file " + functionsFile.getPath() + " not found");
    }
    // 
    // Read characters from functionFile into character array and transfer into string buffer.
    // 
    Vector<AnnotatedFunction> annotatedFunctionsVector = new Vector<AnnotatedFunction>();
    long fnFileLength = functionsFile.length();
    StringBuffer stringBuffer = new StringBuffer();
    FileInputStream is = null;
    try {
        is = new FileInputStream(functionsFile);
        InputStreamReader reader = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(reader);
        char[] charArray = new char[10000];
        while (true) {
            int numRead = br.read(charArray, 0, charArray.length);
            if (numRead > 0) {
                stringBuffer.append(charArray, 0, numRead);
            } else if (numRead == -1) {
                break;
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
    if (stringBuffer.length() != fnFileLength) {
        System.out.println("<<<SYSOUT ALERT>>>SimulationData.readFunctionFile(), read " + stringBuffer.length() + " of " + fnFileLength + " bytes of input file");
    }
    String newLineDelimiters = "\n\r";
    StringTokenizer lineTokenizer = new StringTokenizer(stringBuffer.toString(), newLineDelimiters);
    String token1 = new String("");
    int j = 0;
    // 
    // Each token is a line representing a function name and function expression,
    // separated by a semicolon
    // 
    HashSet<String> allSymbols = new HashSet<String>();
    while (lineTokenizer.hasMoreTokens()) {
        token1 = lineTokenizer.nextToken();
        FunctionFileGenerator.FuncFileLineInfo funcFileLineInfo = readFunctionLine(token1);
        if (funcFileLineInfo != null && funcFileLineInfo.functionName != null && funcFileLineInfo.functionExpr != null && funcFileLineInfo.funcVarType != null) {
            Expression functionExpr = null;
            try {
                functionExpr = new Expression(funcFileLineInfo.functionExpr);
                functionExpr = MathFunctionDefinitions.fixFunctionSyntax(functionExpr);
            } catch (cbit.vcell.parser.ExpressionException e) {
                throw new RuntimeException("Error in reading expression '" + funcFileLineInfo.functionExpr + "' for function \"" + funcFileLineInfo.functionName + "\"");
            }
            Domain domain = Variable.getDomainFromCombinedIdentifier(funcFileLineInfo.functionName);
            String funcName = Variable.getNameFromCombinedIdentifier(funcFileLineInfo.functionName);
            AnnotatedFunction annotatedFunc = new AnnotatedFunction(funcName, functionExpr, domain, funcFileLineInfo.errorString, funcFileLineInfo.funcVarType, funcFileLineInfo.funcIsUserDefined ? FunctionCategory.OLDUSERDEFINED : FunctionCategory.PREDEFINED);
            allSymbols.add(annotatedFunc.getName());
            String[] symbols = annotatedFunc.getExpression().getSymbols();
            if (symbols != null) {
                allSymbols.addAll(Arrays.asList(symbols));
            }
            annotatedFunctionsVector.addElement(annotatedFunc);
        }
        j++;
    }
    if (simJobID != null && simJobID.trim().length() > 0) {
        SimpleSymbolTable simpleSymbolTable = new SimpleSymbolTable(allSymbols.toArray(new String[0]));
        // bind
        for (AnnotatedFunction func : annotatedFunctionsVector) {
            if (func.isOldUserDefined()) {
                try {
                    func.bind(simpleSymbolTable);
                } catch (ExpressionBindingException e) {
                    e.printStackTrace();
                }
            }
        }
        // rename symbol table entries
        for (int i = 0; i < annotatedFunctionsVector.size(); i++) {
            AnnotatedFunction func = annotatedFunctionsVector.get(i);
            if (func.isOldUserDefined()) {
                SimpleSymbolTableEntry ste = (SimpleSymbolTableEntry) simpleSymbolTable.getEntry(func.getName());
                ste.setName(simJobID + "_" + func.getName());
            }
        }
        // rename in the expressions
        for (int i = 0; i < annotatedFunctionsVector.size(); i++) {
            AnnotatedFunction func = annotatedFunctionsVector.get(i);
            if (func.isOldUserDefined()) {
                try {
                    Expression exp = func.getExpression().renameBoundSymbols(simpleSymbolTable.getNameScope());
                    AnnotatedFunction newfunc = new AnnotatedFunction(simJobID + "_" + func.getName(), exp, func.getDomain(), func.getName(), func.getErrorString(), func.getFunctionType(), FunctionCategory.OLDUSERDEFINED);
                    annotatedFunctionsVector.set(i, newfunc);
                } catch (ExpressionBindingException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return annotatedFunctionsVector;
}
Also used : SimpleSymbolTableEntry(cbit.vcell.parser.SimpleSymbolTable.SimpleSymbolTableEntry) Vector(java.util.Vector) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) HashSet(java.util.HashSet) InputStreamReader(java.io.InputStreamReader) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) FileInputStream(java.io.FileInputStream) StringTokenizer(java.util.StringTokenizer) SimpleSymbolTable(cbit.vcell.parser.SimpleSymbolTable) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) Domain(cbit.vcell.math.Variable.Domain)

Aggregations

Domain (cbit.vcell.math.Variable.Domain)1 Expression (cbit.vcell.parser.Expression)1 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)1 SimpleSymbolTable (cbit.vcell.parser.SimpleSymbolTable)1 SimpleSymbolTableEntry (cbit.vcell.parser.SimpleSymbolTable.SimpleSymbolTableEntry)1 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 Vector (java.util.Vector)1