use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class AbstractMathMapping method getIdentifierSubstitutions.
/**
* Substitutes appropriate variables for speciesContext bindings
*
* @return cbit.vcell.parser.Expression
* @param origExp cbit.vcell.parser.Expression
* @param structureMapping cbit.vcell.mapping.StructureMapping
*/
protected Expression getIdentifierSubstitutions(Expression origExp, VCUnitDefinition desiredExpUnitDef, GeometryClass geometryClass) throws ExpressionException, MappingException {
String[] symbols = origExp.getSymbols();
if (symbols == null) {
return origExp;
}
VCUnitDefinition expUnitDef = null;
try {
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(simContext.getModel().getUnitSystem());
expUnitDef = unitEvaluator.getUnitDefinition(origExp);
if (desiredExpUnitDef == null) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
System.out.println("...........exp='" + expStr + "', desiredUnits are null");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[null], observed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
} else if (expUnitDef == null) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
System.out.println("...........exp='" + expStr + "', evaluated Units are null");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[null]", Issue.SEVERITY_WARNING));
} else if (desiredExpUnitDef.isTBD()) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
// System.out.println("...........exp='"+expStr+"', desiredUnits are ["+desiredExpUnitDef.getSymbol()+"] and expression units are ["+expUnitDef.getSymbol()+"]");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
} else if (!desiredExpUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD()) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
// System.out.println("...........exp='"+expStr+"', desiredUnits are ["+desiredExpUnitDef.getSymbol()+"] and expression units are ["+expUnitDef.getSymbol()+"]");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + desiredExpUnitDef.getSymbol() + "], observed=[" + expUnitDef.getSymbol() + "] for exp = " + expStr, Issue.SEVERITY_WARNING));
}
} catch (VCUnitException e) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
} catch (ExpressionException e) {
String expStr = origExp.renameBoundSymbols(getNameScope()).infix();
System.out.println(".........exp='" + expStr + "' exception='" + e.getMessage() + "'");
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
} catch (Exception e) {
e.printStackTrace(System.out);
localIssueList.add(new Issue(origExp, issueContext, IssueCategory.Units, "expected=[" + ((desiredExpUnitDef != null) ? (desiredExpUnitDef.getSymbol()) : ("null")) + "], exception=" + e.getMessage(), Issue.SEVERITY_WARNING));
}
Expression newExp = new Expression(origExp);
//
// flatten user-defined functions
//
FunctionInvocation[] functionInvocations = newExp.getFunctionInvocations(new FunctionFilter() {
@Override
public boolean accept(String functionName, FunctionType functionType) {
return functionType == FunctionType.USERDEFINED;
}
});
for (FunctionInvocation functionInvocation : functionInvocations) {
if (functionInvocation.getSymbolTableFunctionEntry() instanceof Model.ModelFunction) {
ModelFunction modelFunction = (ModelFunction) functionInvocation.getSymbolTableFunctionEntry();
newExp.substituteInPlace(functionInvocation.getFunctionExpression(), modelFunction.getFlattenedExpression(functionInvocation));
}
}
//
// then substitute Math symbols for Biological symbols.
//
newExp.bindExpression(null);
for (int i = 0; i < symbols.length; i++) {
SymbolTableEntry ste = origExp.getSymbolBinding(symbols[i]);
if (ste == null) {
throw new ExpressionBindingException("symbol '" + symbols[i] + "' not bound");
// ste = simContext.getGeometryContext().getModel().getSpeciesContext(symbols[i]);
}
if (ste != null) {
String newName = getMathSymbol(ste, geometryClass);
if (!newName.equals(symbols[i])) {
newExp.substituteInPlace(new Expression(symbols[i]), new Expression(newName));
}
}
}
return newExp;
}
use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class Kinetics method gatherIssues.
/**
* Insert the method's description here.
* Creation date: (5/12/2004 2:53:13 PM)
*/
public void gatherIssues(IssueContext issueContext, List<Issue> issueList) {
issueContext = issueContext.newChildContext(ContextType.ModelProcessDynamics, this);
//
for (int i = 0; fieldUnresolvedParameters != null && i < fieldUnresolvedParameters.length; i++) {
issueList.add(new Issue(fieldUnresolvedParameters[i], issueContext, IssueCategory.UnresolvedParameter, "Unresolved parameter '" + fieldUnresolvedParameters[i].getName() + "' in reaction '" + reactionStep.getName() + "'", Issue.SEVERITY_ERROR));
}
//
for (int i = 0; fieldKineticsParameters != null && i < fieldKineticsParameters.length; i++) {
if (fieldKineticsParameters[i].getRole() == ROLE_UserDefined) {
try {
if (!isReferenced(fieldKineticsParameters[i], 0)) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.KineticsUnreferencedParameter, "Unreferenced Kinetic Parameter '" + fieldKineticsParameters[i].getName() + "' in reaction '" + reactionStep.getName() + "'", Issue.SEVERITY_WARNING));
}
} catch (ExpressionException e) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.KineticsExpressionError, "error resolving expression " + e.getMessage(), Issue.SEVERITY_WARNING));
} catch (ModelException e) {
issueList.add(new Issue(getReactionStep(), issueContext, IssueCategory.CyclicDependency, "cyclic dependency in the parameter definitions", Issue.SEVERITY_ERROR));
}
}
}
//
if (fieldKineticsParameters != null) {
for (KineticsParameter kineticsParameter : fieldKineticsParameters) {
if (kineticsParameter.getExpression() == null) {
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionMissing, "expression is missing", Issue.SEVERITY_INFO));
} else {
Expression exp = kineticsParameter.getExpression();
String[] symbols = exp.getSymbols();
String issueMessagePrefix = "Kinetic parameter '" + kineticsParameter.getName() + "' in reaction '" + getReactionStep().getName() + "' ";
if (symbols != null) {
for (int j = 0; j < symbols.length; j++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
if (ste instanceof KineticsProxyParameter) {
ste = ((KineticsProxyParameter) ste).getTarget();
}
if (ste == null) {
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined symbol '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
} else if (ste instanceof SpeciesContext) {
if (!getReactionStep().getModel().contains((SpeciesContext) ste)) {
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined species '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
}
if (reactionStep.countNumReactionParticipants((SpeciesContext) ste) == 0) {
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionNonParticipantSymbol, issueMessagePrefix + "references species context '" + symbols[j] + "', but it is not a reactant/product/catalyst of this reaction", Issue.SEVERITY_WARNING));
}
} else if (ste instanceof ModelParameter) {
if (!getReactionStep().getModel().contains((ModelParameter) ste)) {
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined global parameter '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
}
}
}
}
}
}
// looking for local param which masks a global and issueing a warning
for (KineticsParameter kineticsParameter : fieldKineticsParameters) {
String name = kineticsParameter.getName();
SymbolTableEntry ste = getReactionStep().getNameScope().getExternalEntry(name, getReactionStep());
String steName;
if (ste != null) {
if (ste instanceof Displayable) {
steName = ((Displayable) ste).getDisplayType() + " " + ste.getName();
} else {
steName = ste.getClass().getSimpleName() + " " + ste.getName();
}
String msg = steName + " is overriden by a local parameter " + name + " in reaction " + getReactionStep().getName();
issueList.add(new Issue(kineticsParameter, issueContext, IssueCategory.Identifiers, msg, Issue.SEVERITY_WARNING));
}
}
}
try {
//
// determine unit consistency for each expression
//
ModelUnitSystem modelUnitSystem = getReactionStep().getModel().getUnitSystem();
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(modelUnitSystem);
for (int i = 0; i < fieldKineticsParameters.length; i++) {
try {
VCUnitDefinition paramUnitDef = fieldKineticsParameters[i].getUnitDefinition();
VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldKineticsParameters[i].getExpression());
if (paramUnitDef == null) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
} else if (paramUnitDef.isTBD()) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "undefined unit " + modelUnitSystem.getInstance_TBD().getSymbol(), Issue.SEVERITY_WARNING));
} else if (expUnitDef == null) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
} else if (paramUnitDef.isTBD() || (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD())) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, "inconsistent units, defined=[" + fieldKineticsParameters[i].getUnitDefinition().getSymbol() + "], computed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
}
} catch (VCUnitException e) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
} catch (ExpressionException e) {
issueList.add(new Issue(fieldKineticsParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
}
}
} catch (Throwable e) {
issueList.add(new Issue(getReactionStep(), issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_INFO));
}
//
for (int i = 0; i < fieldKineticsParameters.length; i++) {
RealInterval simpleBounds = bounds[fieldKineticsParameters[i].getRole()];
if (simpleBounds != null) {
String parmName = reactionStep.getNameScope().getName() + "." + fieldKineticsParameters[i].getName();
issueList.add(new SimpleBoundsIssue(fieldKineticsParameters[i], issueContext, simpleBounds, "parameter " + parmName + ": must be within " + simpleBounds.toString()));
}
}
}
Aggregations