Search in sources :

Example 76 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class XmlHelper method XMLToSimTask.

public static SimulationTask XMLToSimTask(String xmlString) throws XmlParseException, ExpressionException {
    Namespace ns = Namespace.getNamespace(XMLTags.VCML_NS);
    try {
        if (xmlString == null || xmlString.length() == 0) {
            throw new XmlParseException("Invalid xml for Simulation: " + xmlString);
        }
        // default parser and no validation
        Element root = (XmlUtil.stringToXML(xmlString, null)).getRootElement();
        if (!root.getName().equals(SimulationTask_tag)) {
            throw new RuntimeException("expecting top level element to be " + SimulationTask_tag);
        }
        int taskId = Integer.parseInt(root.getAttributeValue(TaskId_attr));
        int jobIndex = Integer.parseInt(root.getAttributeValue(JobIndex_attr));
        String computeResource = root.getChildTextTrim(ComputeResource_tag, ns);
        List<?> children = root.getChildren(FieldFunctionIdentifierSpec_tag, ns);
        ArrayList<FieldDataIdentifierSpec> fdisArrayList = new ArrayList<FieldDataIdentifierSpec>();
        for (Object child : children) {
            if (child instanceof Element) {
                String fdisText = ((Element) child).getTextTrim();
                FieldDataIdentifierSpec fdis = FieldDataIdentifierSpec.fromCSVString(fdisText);
                fdisArrayList.add(fdis);
            }
        }
        FieldDataIdentifierSpec[] fdisArray = fdisArrayList.toArray(new FieldDataIdentifierSpec[0]);
        Element simElement = root.getChild(XMLTags.SimulationTag, ns);
        Element mdElement = root.getChild(XMLTags.MathDescriptionTag, ns);
        Element geomElement = root.getChild(XMLTags.GeometryTag, ns);
        XmlReader reader = new XmlReader(true, ns);
        Geometry geom = null;
        if (geomElement != null) {
            geom = reader.getGeometry(geomElement);
        }
        MathDescription md = reader.getMathDescription(mdElement, geom);
        Simulation sim = reader.getSimulation(simElement, md);
        sim.refreshDependencies();
        SimulationJob simJob = new SimulationJob(sim, jobIndex, fdisArray);
        SimulationTask simTask = new SimulationTask(simJob, taskId, computeResource);
        return simTask;
    } catch (Exception pve) {
        pve.printStackTrace();
        throw new XmlParseException("Unable to parse simulation string.", pve);
    }
}
Also used : SimulationTask(cbit.vcell.messaging.server.SimulationTask) MathDescription(cbit.vcell.math.MathDescription) Element(org.jdom.Element) ArrayList(java.util.ArrayList) Namespace(org.jdom.Namespace) XMLStreamException(javax.xml.stream.XMLStreamException) SbmlException(org.vcell.sbml.SbmlException) SBMLException(org.sbml.jsbml.SBMLException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) SimulationJob(cbit.vcell.solver.SimulationJob)

Example 77 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class XmlHelper method exportSBML.

/**
 * Exports VCML format to another supported format (currently: SBML or CellML). It allows
 *   choosing a specific Simulation Spec to export.
 * Creation date: (4/8/2003 12:30:27 PM)
 * @return java.lang.String
 */
public static String exportSBML(VCDocument vcDoc, int level, int version, int pkgVersion, boolean isSpatial, SimulationContext simContext, SimulationJob simJob) throws XmlParseException {
    if (vcDoc == null) {
        throw new XmlParseException("Invalid arguments for exporting SBML.");
    }
    if (vcDoc instanceof BioModel) {
        try {
            // check if model to be exported to SBML has units compatible with SBML default units (default units in SBML can be assumed only until SBML Level2)
            ModelUnitSystem forcedModelUnitSystem = simContext.getModel().getUnitSystem();
            if (level < 3 && !ModelUnitSystem.isCompatibleWithDefaultSBMLLevel2Units(forcedModelUnitSystem)) {
                forcedModelUnitSystem = ModelUnitSystem.createDefaultSBMLLevel2Units();
            }
            // create new Biomodel with new (SBML compatible)  unit system
            BioModel modifiedBiomodel = ModelUnitConverter.createBioModelWithNewUnitSystem(simContext.getBioModel(), forcedModelUnitSystem);
            // extract the simContext from new Biomodel. Apply overrides to *this* modified simContext
            SimulationContext simContextFromModifiedBioModel = modifiedBiomodel.getSimulationContext(simContext.getName());
            SimulationContext clonedSimContext = applyOverridesForSBML(modifiedBiomodel, simContextFromModifiedBioModel, simJob);
            // extract sim (in simJob) from modified Biomodel, if not null
            SimulationJob modifiedSimJob = null;
            if (simJob != null) {
                Simulation simFromModifiedBiomodel = clonedSimContext.getSimulation(simJob.getSimulation().getName());
                modifiedSimJob = new SimulationJob(simFromModifiedBiomodel, simJob.getJobIndex(), null);
            }
            SBMLExporter sbmlExporter = new SBMLExporter(modifiedBiomodel, level, version, isSpatial);
            sbmlExporter.setSelectedSimContext(simContextFromModifiedBioModel);
            sbmlExporter.setSelectedSimulationJob(modifiedSimJob);
            return sbmlExporter.getSBMLFile();
        } catch (ExpressionException | SbmlException | SBMLException | XMLStreamException e) {
            e.printStackTrace(System.out);
            throw new XmlParseException(e);
        }
    } else if (vcDoc instanceof MathModel) {
        try {
            return MathModel_SBMLExporter.getSBMLString((MathModel) vcDoc, level, version);
        } catch (ExpressionException | IOException | SBMLException | XMLStreamException e) {
            e.printStackTrace(System.out);
            throw new XmlParseException(e);
        }
    } else {
        throw new RuntimeException("unsupported Document Type " + vcDoc.getClass().getName() + " for SBML export");
    }
}
Also used : SBMLException(org.sbml.jsbml.SBMLException) MathModel(cbit.vcell.mathmodel.MathModel) MathModel_SBMLExporter(org.vcell.sbml.vcell.MathModel_SBMLExporter) SBMLExporter(org.vcell.sbml.vcell.SBMLExporter) SimulationContext(cbit.vcell.mapping.SimulationContext) ExpressionException(cbit.vcell.parser.ExpressionException) Simulation(cbit.vcell.solver.Simulation) XMLStreamException(javax.xml.stream.XMLStreamException) BioModel(cbit.vcell.biomodel.BioModel) SbmlException(org.vcell.sbml.SbmlException) SimulationJob(cbit.vcell.solver.SimulationJob) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 78 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class SimulationData method readFunctions.

/**
 * Insert the method's description here.
 * Creation date: (1/15/2004 11:48:25 AM)
 */
private void readFunctions(OutputContext outputContext) throws FileNotFoundException, IOException {
    File firstJobFunctionsFile = getFirstJobFunctionsFile();
    File jobFunctionsFile = getJobFunctionsFile();
    // only dataset functions
    Vector<AnnotatedFunction> annotatedFuncsVector = FunctionFileGenerator.readFunctionsFile(jobFunctionsFile, vcDataId.getID());
    /* not required as long as we are skipping any legacy user-defined functions from the functions file */
    if (!firstJobFunctionsFile.equals(jobFunctionsFile)) {
        Vector<AnnotatedFunction> f1 = FunctionFileGenerator.readFunctionsFile(firstJobFunctionsFile, vcDataId.getID());
        for (AnnotatedFunction f : f1) {
            if (f.isOldUserDefined()) {
                annotatedFuncsVector.add(f);
            }
        }
    }
    // add user-defined functions from output context, if any
    if (outputContext != null) {
        for (int i = 0; i < outputContext.getOutputFunctions().length; i++) {
            annotatedFuncsVector.add(outputContext.getOutputFunctions()[i]);
        }
    }
    // 
    // Convert this annotatedfunctionsVector into the field annotatedFunctionsList.
    // 
    annotatedFunctionList.clear();
    for (int i = 0; i < annotatedFuncsVector.size(); i++) {
        AnnotatedFunction annotatedFunction = (AnnotatedFunction) annotatedFuncsVector.elementAt(i);
        try {
            addFunctionToList(annotatedFunction);
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Could not add function " + annotatedFunction.getName() + " to annotatedFunctionList");
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) ExpressionException(cbit.vcell.parser.ExpressionException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 79 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class SimulationData method simplifyFunction.

public AnnotatedFunction simplifyFunction(AnnotatedFunction function) throws ExpressionException {
    // attempt to bind function and substitute
    AnnotatedFunction simpleFunction = null;
    try {
        simpleFunction = (AnnotatedFunction) BeanUtils.cloneSerializable(function);
        Expression exp = simpleFunction.getExpression();
        exp = SolverUtilities.substituteSizeAndNormalFunctions(exp, function.getFunctionType().getVariableDomain());
        exp.bindExpression(this);
        String[] symbols = exp.getSymbols();
        if (symbols != null) {
            for (int i = 0; i < symbols.length; i++) {
                Expression oldExp = new Expression(symbols[i]);
                Expression newExp = null;
                SymbolTableEntry ste = getEntry(symbols[i]);
                if (ste != null) {
                    if (!(ste instanceof DataSetIdentifier)) {
                        continue;
                    }
                    DataSetIdentifier dsi = (DataSetIdentifier) ste;
                    if (!dsi.isFunction()) {
                        continue;
                    }
                    for (int j = 0; j < annotatedFunctionList.size(); j++) {
                        AnnotatedFunction mathFunction = (AnnotatedFunction) annotatedFunctionList.elementAt(j);
                        if (mathFunction.getName().equals(symbols[i])) {
                            newExp = mathFunction.getExpression();
                            break;
                        }
                    }
                }
                if (ste == null || newExp == null) {
                    throw new RuntimeException("dependencies for function '" + function + "' not found");
                }
                exp.substituteInPlace(oldExp, newExp);
            }
        }
        exp = exp.flatten();
        exp.bindExpression(this);
        simpleFunction.setExpression(exp);
    } catch (Exception ex) {
        ex.printStackTrace(System.out);
        throw new ExpressionException(ex.getMessage());
    }
    return simpleFunction;
}
Also used : SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) FileNotFoundException(java.io.FileNotFoundException) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 80 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class SimulationData method getVarAndFunctionDataIdentifiers.

/**
 * This method was created in VisualAge.
 * @return java.lang.String[]
 */
public synchronized DataIdentifier[] getVarAndFunctionDataIdentifiers(OutputContext outputContext) throws IOException, DataAccessException {
    // Is this zip format?
    boolean bIsChombo = false;
    try {
        bIsChombo = isChombo();
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.out);
    }
    File zipFile1 = getZipFile(bIsChombo, null);
    File zipFile2 = getZipFile(bIsChombo, 0);
    bZipFormat1 = false;
    bZipFormat2 = false;
    if (zipFile1.exists()) {
        bZipFormat1 = true;
    } else if (zipFile2.exists()) {
        bZipFormat2 = true;
    }
    refreshLogFile();
    if (!isComsol()) {
        try {
            refreshMeshFile();
        } catch (MathException e) {
            e.printStackTrace(System.out);
            throw new DataAccessException(e.getMessage());
        }
    }
    if (!isRulesData && !getIsODEData() && !isComsol() && dataFilenames != null) {
        // read variables only when I have never read the file since variables don't change
        if (dataSetIdentifierList.size() == 0) {
            File file = getPDEDataFile(0.0);
            DataSet dataSet = getPDEDataSet(file, 0.0);
            String[] varNames = dataSet.getDataNames();
            int[] varTypeInts = dataSet.getVariableTypeIntegers();
            if (varNames == null) {
                return null;
            }
            dataSetIdentifierList.clear();
            for (int i = 0; i < varNames.length; i++) {
                VariableType varType = null;
                try {
                    varType = VariableType.getVariableTypeFromInteger(varTypeInts[i]);
                } catch (IllegalArgumentException e) {
                    if (LG.isEnabledFor(Level.WARN)) {
                        LG.warn("Exception typing " + varNames[i] + " has unsupported type " + varTypeInts[i] + ": " + e.getMessage());
                    }
                    varType = SimulationData.getVariableTypeFromLength(mesh, dataSet.getDataLength(varNames[i]));
                }
                Domain domain = Variable.getDomainFromCombinedIdentifier(varNames[i]);
                String varName = Variable.getNameFromCombinedIdentifier(varNames[i]);
                dataSetIdentifierList.addElement(new DataSetIdentifier(varName, varType, domain));
            }
            refreshDataProcessingOutputInfo(outputContext);
            if (dataProcessingOutputInfo != null) {
                for (int i = 0; i < dataProcessingOutputInfo.getVariableNames().length; i++) {
                    if (dataProcessingOutputInfo.getPostProcessDataType(dataProcessingOutputInfo.getVariableNames()[i]).equals(DataProcessingOutputInfo.PostProcessDataType.image)) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(dataProcessingOutputInfo.getVariableNames()[i], VariableType.POSTPROCESSING, null));
                    }
                }
            }
        }
        // always read functions file since functions might change
        getFunctionDataIdentifiers(outputContext);
    }
    if ((isRulesData || getIsODEData()) && dataSetIdentifierList.size() == 0) {
        ODEDataBlock odeDataBlock = getODEDataBlock();
        if (odeDataBlock == null) {
            throw new DataAccessException("Results are not availabe yet. Please try again later.");
        }
        ODESimData odeSimData = odeDataBlock.getODESimData();
        int colCount = odeSimData.getColumnDescriptionsCount();
        // assume index=0 is time "t"
        int DATA_OFFSET = 1;
        dataSetIdentifierList.clear();
        for (int i = 0; i < (colCount - DATA_OFFSET); i++) {
            String varName = odeSimData.getColumnDescriptions(i + DATA_OFFSET).getDisplayName();
            // TODO domain
            Domain domain = null;
            dataSetIdentifierList.addElement(new DataSetIdentifier(varName, VariableType.NONSPATIAL, domain));
        }
    }
    if (isComsol() && dataSetIdentifierList.size() == 0) {
        ComsolSimFiles comsolSimFiles = getComsolSimFiles();
        if (comsolSimFiles.simTaskXMLFile != null) {
            try {
                String xmlString = FileUtils.readFileToString(comsolSimFiles.simTaskXMLFile);
                SimulationTask simTask = XmlHelper.XMLToSimTask(xmlString);
                Enumeration<Variable> variablesEnum = simTask.getSimulation().getMathDescription().getVariables();
                while (variablesEnum.hasMoreElements()) {
                    Variable var = variablesEnum.nextElement();
                    if (var instanceof VolVariable) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), VariableType.VOLUME, var.getDomain()));
                    } else if (var instanceof MemVariable) {
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), VariableType.MEMBRANE, var.getDomain()));
                    } else if (var instanceof Function) {
                        VariableType varType = VariableType.UNKNOWN;
                        if (var.getDomain() != null && var.getDomain().getName() != null) {
                            SubDomain subDomain = simTask.getSimulation().getMathDescription().getSubDomain(var.getDomain().getName());
                            if (subDomain instanceof CompartmentSubDomain) {
                                varType = VariableType.VOLUME;
                            } else if (subDomain instanceof MembraneSubDomain) {
                                varType = VariableType.MEMBRANE;
                            } else if (subDomain instanceof FilamentSubDomain) {
                                throw new RuntimeException("filament subdomains not supported");
                            } else if (subDomain instanceof PointSubDomain) {
                                varType = VariableType.POINT_VARIABLE;
                            }
                        }
                        dataSetIdentifierList.addElement(new DataSetIdentifier(var.getName(), varType, var.getDomain()));
                    } else if (var instanceof Constant) {
                        System.out.println("ignoring Constant " + var.getName());
                    } else if (var instanceof InsideVariable) {
                        System.out.println("ignoring InsideVariable " + var.getName());
                    } else if (var instanceof OutsideVariable) {
                        System.out.println("ignoring OutsideVariable " + var.getName());
                    } else {
                        throw new RuntimeException("unexpected variable " + var.getName() + " of type " + var.getClass().getName());
                    }
                }
            } catch (XmlParseException | ExpressionException e) {
                e.printStackTrace();
                throw new RuntimeException("failed to read sim task file, msg: " + e.getMessage(), e);
            }
        }
    }
    DataIdentifier[] dis = new DataIdentifier[dataSetIdentifierList.size()];
    for (int i = 0; i < dataSetIdentifierList.size(); i++) {
        DataSetIdentifier dsi = (DataSetIdentifier) dataSetIdentifierList.elementAt(i);
        String displayName = dsi.getName();
        if (dsi.isFunction()) {
            AnnotatedFunction f = null;
            for (int j = 0; j < annotatedFunctionList.size(); j++) {
                AnnotatedFunction function = (AnnotatedFunction) annotatedFunctionList.elementAt(j);
                if (function.getName().equals(dsi.getName())) {
                    f = function;
                    break;
                }
            }
            if (f != null) {
                displayName = f.getDisplayName();
            }
        }
        dis[i] = new DataIdentifier(dsi.getName(), dsi.getVariableType(), dsi.getDomain(), dsi.isFunction(), displayName);
    }
    return dis;
}
Also used : MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) SimulationTask(cbit.vcell.messaging.server.SimulationTask) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) MemVariable(cbit.vcell.math.MemVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) Variable(cbit.vcell.math.Variable) VCDataIdentifier(org.vcell.util.document.VCDataIdentifier) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) Constant(cbit.vcell.math.Constant) FileNotFoundException(java.io.FileNotFoundException) PointSubDomain(cbit.vcell.math.PointSubDomain) ODESimData(cbit.vcell.solver.ode.ODESimData) InsideVariable(cbit.vcell.math.InsideVariable) ExpressionException(cbit.vcell.parser.ExpressionException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) MemVariable(cbit.vcell.math.MemVariable) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) ComsolSimFiles(org.vcell.vis.io.ComsolSimFiles) VariableType(cbit.vcell.math.VariableType) VolVariable(cbit.vcell.math.VolVariable) XmlParseException(cbit.vcell.xml.XmlParseException) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) MathException(cbit.vcell.math.MathException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) OutsideVariable(cbit.vcell.math.OutsideVariable) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

ExpressionException (cbit.vcell.parser.ExpressionException)199 Expression (cbit.vcell.parser.Expression)138 MathException (cbit.vcell.math.MathException)58 PropertyVetoException (java.beans.PropertyVetoException)51 DataAccessException (org.vcell.util.DataAccessException)34 ArrayList (java.util.ArrayList)32 Variable (cbit.vcell.math.Variable)30 IOException (java.io.IOException)29 Element (org.jdom.Element)26 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)25 MappingException (cbit.vcell.mapping.MappingException)24 Function (cbit.vcell.math.Function)24 Vector (java.util.Vector)24 ModelException (cbit.vcell.model.ModelException)23 SolverException (cbit.vcell.solver.SolverException)23 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)22 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)21 Constant (cbit.vcell.math.Constant)20 MathDescription (cbit.vcell.math.MathDescription)19 Structure (cbit.vcell.model.Structure)18