use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class Kinetics method resolveUndefinedUnits.
/**
* Insert the method's description here.
* Creation date: (4/13/2004 3:09:21 PM)
*/
public void resolveUndefinedUnits() {
//
if (!bResolvingUnits) {
bResolvingUnits = true;
try {
if (getReactionStep().getModel() != null) {
ModelUnitSystem modelUnitSystem = getReactionStep().getModel().getUnitSystem();
boolean bAnyTBDUnits = false;
for (int i = 0; i < fieldKineticsParameters.length; i++) {
if (fieldKineticsParameters[i].getUnitDefinition() == null) {
// not ready to resolve units yet
return;
} else if (fieldKineticsParameters[i].getUnitDefinition().isTBD()) {
bAnyTBDUnits = true;
}
}
//
if (bAnyTBDUnits) {
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(modelUnitSystem);
VCUnitDefinition[] vcUnitDefinitions = unitEvaluator.suggestUnitDefinitions(fieldKineticsParameters);
for (int i = 0; i < fieldKineticsParameters.length; i++) {
if (!fieldKineticsParameters[i].getUnitDefinition().isEquivalent(vcUnitDefinitions[i])) {
fieldKineticsParameters[i].setUnitDefinition(vcUnitDefinitions[i]);
}
}
// System.out.println("successfully completed Kinetics.resolveUndefinedUnits() for ReactionStep '"+getReactionStep()+"'");
}
}
} catch (Exception e) {
if (lg.isEnabledFor(Level.WARN)) {
lg.warn("Kinetics.resolveUndefinedUnits(): EXCEPTION: " + e.getMessage());
}
} finally {
bResolvingUnits = false;
}
}
}
use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class Model method gatherIssues.
/**
* Insert the method's description here.
* Creation date: (5/12/2004 10:38:12 PM)
* @param issueList java.util.Vector
*/
public void gatherIssues(IssueContext issueContext, List<Issue> issueList) {
issueContext = issueContext.newChildContext(ContextType.Model, this);
//
try {
for (ModelParameter modelParameter : fieldModelParameters) {
Expression exp = modelParameter.getExpression();
String[] symbols = exp.getSymbols();
if (symbols != null) {
String issueMsgPrefix = "Global parameter '" + modelParameter.getName() + "' ";
for (int j = 0; j < symbols.length; j++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
if (ste == null) {
issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined symbol '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
} else if (ste instanceof SpeciesContext) {
if (!contains((SpeciesContext) ste)) {
issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined species '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
}
} else if (ste instanceof ModelParameter) {
if (!contains((ModelParameter) ste)) {
issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined global parameter '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
}
}
}
}
}
//
for (int i = 0; i < fieldModelParameters.length; i++) {
try {
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(getUnitSystem());
VCUnitDefinition paramUnitDef = fieldModelParameters[i].getUnitDefinition();
VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldModelParameters[i].getExpression());
if (paramUnitDef == null) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
} else if (expUnitDef == null) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
} else if (paramUnitDef.isTBD()) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "unit is undefined (" + unitSystem.getInstance_TBD().getSymbol() + ")", Issue.SEVERITY_WARNING));
} else if (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD()) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "unit mismatch, computed = [" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
}
} catch (VCUnitException e) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "units inconsistent: " + e.getMessage(), Issue.SEVERITY_WARNING));
} catch (ExpressionException e) {
issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "units inconsistent: " + e.getMessage(), Issue.SEVERITY_WARNING));
}
}
} catch (Throwable e) {
e.printStackTrace(System.out);
issueList.add(new Issue(this, issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_WARNING));
}
//
for (Structure struct : fieldStructures) {
struct.gatherIssues(issueContext, issueList);
}
//
for (int i = 0; i < fieldReactionSteps.length; i++) {
fieldReactionSteps[i].gatherIssues(issueContext, issueList);
}
//
// get issues from species contexts (species patterns)
//
int seedSpeciesCount = 0;
for (int i = 0; i < fieldSpeciesContexts.length; i++) {
if (fieldSpeciesContexts[i].hasSpeciesPattern()) {
seedSpeciesCount++;
}
fieldSpeciesContexts[i].gatherIssues(issueContext, issueList);
}
if (seedSpeciesCount == 0 && !rbmModelContainer.getReactionRuleList().isEmpty()) {
String msg = "At least one seed species is required.";
issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, msg, Issue.Severity.WARNING));
}
//
// get issues for symbol name clashes (specifically structures with same voltage names or structure names)
//
HashSet<SymbolTableEntry> steHashSet = new HashSet<SymbolTableEntry>();
gatherLocalEntries(steHashSet);
Iterator<SymbolTableEntry> iter = steHashSet.iterator();
Hashtable<String, SymbolTableEntry> symbolHashtable = new Hashtable<String, SymbolTableEntry>();
while (iter.hasNext()) {
SymbolTableEntry ste = iter.next();
SymbolTableEntry existingSTE = symbolHashtable.get(ste.getName());
if (existingSTE != null) {
issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, "model symbol \"" + ste.getName() + "\" is used within \"" + ste.getNameScope().getName() + "\" and \"" + existingSTE.getNameScope().getName() + "\"", Issue.SEVERITY_ERROR));
} else {
symbolHashtable.put(ste.getName(), ste);
}
}
//
// gather issues for electrical topology (unspecified +ve or -ve features, or +ve feature == -ve feature
//
getElectricalTopology().gatherIssues(issueContext, issueList);
//
if (rbmModelContainer == null) {
issueList.add(new Issue(this, issueContext, IssueCategory.InternalError, "Rbm Model Container is null", Issue.SEVERITY_WARNING));
} else {
rbmModelContainer.gatherIssues(issueContext, issueList);
}
}
use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class ParameterContext method gatherIssues.
public void gatherIssues(IssueContext issueContext, List<Issue> issueList, ParameterRoleEnum userDefinedRole) {
//
for (int i = 0; fieldUnresolvedParameters != null && i < fieldUnresolvedParameters.length; i++) {
issueList.add(new Issue(fieldUnresolvedParameters[i], issueContext, IssueCategory.UnresolvedParameter, "Unresolved parameter '" + fieldUnresolvedParameters[i].getName(), Issue.SEVERITY_ERROR));
}
//
for (int i = 0; fieldParameters != null && i < fieldParameters.length; i++) {
if (fieldParameters[i].getRole() == userDefinedRole) {
try {
if (!isReferenced(fieldParameters[i], 0)) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.KineticsUnreferencedParameter, "Unreferenced Kinetic Parameter '" + fieldParameters[i].getName(), Issue.SEVERITY_WARNING));
}
} catch (ExpressionException e) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.KineticsExpressionError, "error resolving expression " + e.getMessage(), Issue.SEVERITY_WARNING));
}
}
}
//
if (fieldParameters != null) {
for (LocalParameter parameter : this.fieldParameters) {
if (parameter.getExpression() == null) {
issueList.add(new Issue(parameter, issueContext, IssueCategory.KineticsExpressionMissing, "expression is missing", Issue.SEVERITY_INFO));
} else {
Expression exp = parameter.getExpression();
String[] symbols = exp.getSymbols();
String issueMessagePrefix = "parameter '" + parameter.getName() + "' ";
if (symbols != null) {
for (int j = 0; j < symbols.length; j++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
if (ste instanceof LocalProxyParameter) {
ste = ((LocalProxyParameter) ste).getTarget();
}
if (ste == null) {
issueList.add(new Issue(parameter, 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(parameter,issueContext,IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined species '"+symbols[j]+"'",Issue.SEVERITY_ERROR));
// }
// if (reactionStep.countNumReactionParticipants((SpeciesContext)ste) == 0){
// issueList.add(new Issue(parameter,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(parameter,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 (LocalParameter parameter : fieldParameters) {
String name = parameter.getName();
SymbolTableEntry ste = nameScope.getExternalEntry(name, this);
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;
issueList.add(new Issue(parameter, issueContext, IssueCategory.Identifiers, msg, Issue.SEVERITY_WARNING));
}
}
}
try {
//
// determine unit consistency for each expression
//
VCUnitSystem unitSystem = unitSystemProvider.getUnitSystem();
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(unitSystem);
for (int i = 0; i < fieldParameters.length; i++) {
if (fieldParameters[i].getExpression() == null) {
continue;
}
try {
VCUnitDefinition paramUnitDef = fieldParameters[i].getUnitDefinition();
VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldParameters[i].getExpression());
if (paramUnitDef == null) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
} else if (paramUnitDef.isTBD()) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "undefined unit " + unitSystem.getInstance_TBD().getSymbol(), Issue.SEVERITY_WARNING));
} else if (expUnitDef == null) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
} else if (paramUnitDef.isTBD() || (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD())) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "inconsistent units, defined=[" + fieldParameters[i].getUnitDefinition().getSymbol() + "], computed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
}
} catch (VCUnitException e) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
} catch (ExpressionException e) {
issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
}
}
} catch (Throwable e) {
issueList.add(new Issue(parameterPolicy.getIssueSource(), issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_INFO));
}
//
for (int i = 0; i < fieldParameters.length; i++) {
RealInterval simpleBounds = parameterPolicy.getConstraintBounds(fieldParameters[i].getRole());
if (simpleBounds != null) {
String parmName = fieldParameters[i].getName();
issueList.add(new SimpleBoundsIssue(fieldParameters[i], issueContext, simpleBounds, "parameter " + parmName + ": must be within " + simpleBounds.toString()));
}
}
}
use of cbit.vcell.parser.VCUnitEvaluator in project vcell by virtualcell.
the class MathMapping_4_8 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, StructureMapping structureMapping) 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);
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, structureMapping);
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 ParameterContext method resolveUndefinedUnits.
public void resolveUndefinedUnits() {
//
if (!bResolvingUnits) {
VCUnitSystem unitSystem = unitSystemProvider.getUnitSystem();
try {
bResolvingUnits = true;
boolean bAnyTBDUnits = false;
for (int i = 0; i < fieldParameters.length; i++) {
if (fieldParameters[i].getUnitDefinition() == null) {
// not ready to resolve units yet
return;
} else if (fieldParameters[i].getUnitDefinition().isTBD()) {
bAnyTBDUnits = true;
}
}
//
if (bAnyTBDUnits) {
VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(unitSystem);
VCUnitDefinition[] vcUnitDefinitions = unitEvaluator.suggestUnitDefinitions(fieldParameters);
for (int i = 0; i < fieldParameters.length; i++) {
if (!fieldParameters[i].getUnitDefinition().isEquivalent(vcUnitDefinitions[i])) {
fieldParameters[i].setUnitDefinition(vcUnitDefinitions[i]);
}
}
// System.out.println("successfully completed Kinetics.resolveUndefinedUnits() for ReactionStep '"+getReactionStep()+"'");
}
} catch (ExpressionBindingException e) {
System.out.println("ParameterContext.resolveUndefinedUnits(): EXCEPTION: " + e.getMessage());
} catch (Exception e) {
System.out.println("ParameterContext.resolveUndefinedUnits(): EXCEPTION: " + e.getMessage());
} finally {
bResolvingUnits = false;
}
}
}
Aggregations