bool ReactantList::SetComponentTypesTo(var_type vtype) { for (size_t component=0; component<m_components.size(); component++) { Module* module = g_registry.GetModule(m_module); assert(module != NULL); Variable* var = module->GetVariable(m_components[component].second); if (var != NULL) { if (var->SetType(vtype)) return true; } } return false; }
void CodeGenerator::ProcessVarTerm(int flags) { infunc(CodeGenerator::ProcessVarTerm); char name[MAX_LABEL_SIZE]; VarType type; if ((flags & FLAGS_IN_STATE) && !(flags & FLAGS_IN_FUNCTION)) throw CompileError("(Line %d) Not allowed variable declarations inside states, but outside of functions", CUR_TOKEN.line); // Grab the type GetTypeInfo(type, 0); // Loop until the declaration has ended while (CUR_TOKEN.type != TOKEN_END_OF_LINE) { Variable *var; if (CUR_TOKEN.type != TOKEN_NAME) throw CompileError("(Line %d) No name after type declaration", CUR_TOKEN.line); // Get the current variable name IsolateTokenString(CUR_TOKEN); strcpy(name, token_string); INC_TOKEN; // Grab the array dimensions if it's an array if (CUR_TOKEN.type == TOKEN_ARRAY_OPEN) GetArrayInfo(type); if (g_Object == NULL) { if (flags & FLAGS_IN_FUNCTION) { var = cur_class->cur_function->AddLocal(type, name); } else { // Create a new variable if ((var = new Variable) == NULL) throw CError("Couldn't allocate variable structure"); var->SetType(type); var->SetScope(VARIABLE_SCOPE_MEMBER); if (type.array) { var->SetAddress(cur_class->NewAddress(4 + (type.elements << 2), 0)); var->SetArrayElements(type.elements); } else var->SetAddress(cur_class->NewAddress(4, 1)); // Add it to the class list of variables cur_class->variables.Add(name, var); } } else { // Variable already defined, get it from the class var = cur_class->GetVariable(name, flags); // Check for invalid custom variable types if (var->GetType().id == VARIABLE_TYPEID_CUSTOM) if (g_Env->GetClass(var->GetType().name) == NULL) throw CompileError("(Line %d) Undefined variable type", CUR_TOKEN.line); // Local variables that are arrays need to be prepared if (flags & FLAGS_IN_FUNCTION && var->GetType().array) g_Object->WriteOp(OPCODE_ARRAYP, var->GetAddress()); } // Check for initial values if (CUR_TOKEN.type == TOKEN_ASSIGN) { if (type.id == VARIABLE_TYPEID_CUSTOM) throw CompileError("(Line %d) Not allowed immediate assignments on custom variable types", CUR_TOKEN.line); INC_TOKEN; // Is this an array? if (var->GetArrayElements()) { int x = 0, neg; dynamic num; neg = 0; if (CUR_TOKEN.type != TOKEN_BLOCK_OPEN) throw CompileError("(Line %d) Expecting block open for array default value definition", CUR_TOKEN.line); INC_TOKEN; while (1) { // Check for exit if (CUR_TOKEN.type == TOKEN_BLOCK_CLOSE) { INC_TOKEN; break; } // Check for too many variables if (x == var->GetArrayElements()) throw CompileError("(Line %d) Too many initialisers for array", CUR_TOKEN.line); // Read the value ReadConstant(&num, type, TOKEN_COMMA, TOKEN_BLOCK_CLOSE); if (g_Object == NULL) var->SetElementValue(x, &num); x++; // Check for the comma if (CUR_TOKEN.type != TOKEN_BLOCK_CLOSE) { if (CUR_TOKEN.type != TOKEN_COMMA) throw CompileError("(Line %d) Expecting comma seperator", CUR_TOKEN.line); INC_TOKEN; // Check for situations like = { 4, }; if (CUR_TOKEN.type == TOKEN_BLOCK_CLOSE) throw CompileError("(Line %d) Expecting array element", CUR_TOKEN.line); } } } // No else { dynamic num; ReadConstant(&num, type, TOKEN_END_OF_LINE, TOKEN_COMMA); if (g_Object == NULL) var->SetDefaultValue(&num); } } // Check for multiple variable declarations if (CUR_TOKEN.type == TOKEN_COMMA) { INC_TOKEN; if (CUR_TOKEN.type == TOKEN_END_OF_LINE) throw CompileError("(Line %d) Unexpected end of line", CUR_TOKEN.line); } else if (CUR_TOKEN.type != TOKEN_END_OF_LINE) throw CompileError("(Line %d) Illegal token after variable declaration", CUR_TOKEN.line); } outfunc; }
void Module::LoadSBML(const SBMLDocument* sbmldoc) { //m_sbml = *sbmldoc; const Model* sbml = sbmldoc->getModel(); string sbmlname = ""; //Function Definitions //This is a bit weird, since functions exist outside of modules, since they can be used in any model. So we have to go to the registry to save them. for (unsigned int func=0; func<sbml->getNumFunctionDefinitions(); func++) { const FunctionDefinition* function = sbml->getFunctionDefinition(func); sbmlname = getNameFromSBMLObject(function, "_F"); g_registry.NewUserFunction(&sbmlname); for (unsigned int arg=0; arg<function->getNumArguments(); arg++) { string argument(parseASTNodeToString(function->getArgument(arg))); Variable* expvar = g_registry.AddVariableToCurrent(&argument); g_registry.AddVariableToCurrentExportList(expvar); } string formulastring(parseASTNodeToString(function->getBody())); Formula* formula = g_registry.NewBlankFormula(); setFormulaWithString(formulastring, formula, this); g_registry.SetUserFunction(formula); g_registry.GetNthUserFunction(g_registry.GetNumUserFunctions()-1)->FixNames(); } set<string> defaultcompartments; //Compartments for (unsigned int comp=0; comp<sbml->getNumCompartments(); comp++) { const Compartment* compartment = sbml->getCompartment(comp); sbmlname = getNameFromSBMLObject(compartment, "_C"); if (compartment->getSBOTerm() == 410) { //The 'implicit compartment' defaultcompartments.insert(sbmlname); continue; } if (sbmlname == DEFAULTCOMP && compartment->getConstant() && compartment->isSetSize() && compartment->getSize() == 1.0) { defaultcompartments.insert(sbmlname); continue; //LS NOTE: we assume this was created with Antimony, and ignore the auto-generated 'default compartment' // Later versions of antimony now set the SBO terms to 410, so we might not need this code very long. } Variable* var = AddOrFindVariable(&sbmlname); if (compartment->isSetName()) { var->SetDisplayName(compartment->getName()); } var->SetType(varCompartment); Formula* formula = g_registry.NewBlankFormula(); if (compartment->isSetSize()) { formula->AddNum(compartment->getSize()); var->SetFormula(formula); } if (compartment->isSetUnits()) { var->SetUnits(compartment->getUnits()); } } //Species for (unsigned int spec=0; spec<sbml->getNumSpecies(); spec++) { const Species* species = sbml->getSpecies(spec); sbmlname = getNameFromSBMLObject(species, "_S"); Variable* var = AddOrFindVariable(&sbmlname); if (species->isSetName()) { var->SetDisplayName(species->getName()); } var->SetType(varSpeciesUndef); //Setting the formula Formula* formula = g_registry.NewBlankFormula(); if (species->isSetInitialAmount()) { double amount = species->getInitialAmount(); formula->AddNum(amount); if (amount != 0 && defaultcompartments.find(species->getCompartment()) == defaultcompartments.end()) { Variable* compartment = AddOrFindVariable(&(species->getCompartment())); Formula* compform = compartment->GetFormula(); if (!compform->IsOne()) { formula->AddMathThing('/'); formula->AddVariable(compartment); } } var->SetFormula(formula); } else if (species->isSetInitialConcentration()) { formula->AddNum(species->getInitialConcentration()); var->SetFormula(formula); } //Anything more complicated is set in a Rule, which we'll get to later. if (species->getConstant() || species->getBoundaryCondition()) { //Since all species are variable by default, we only set this explicitly if true. var->SetIsConst(true); } if (defaultcompartments.find(species->getCompartment()) == defaultcompartments.end()) { Variable* compartment = AddOrFindVariable(&(species->getCompartment())); compartment->SetType(varCompartment); var->SetCompartment(compartment); } if (species->isSetUnits()) { var->SetUnits(species->getUnits()); } } //Events: for (unsigned int ev=0; ev<sbml->getNumEvents(); ev++) { const Event* event = sbml->getEvent(ev); sbmlname = getNameFromSBMLObject(event, "_E"); Variable* var = AddOrFindVariable(&sbmlname); if (event->isSetName()) { var->SetDisplayName(event->getName()); } var->SetType(varEvent); //Set the trigger: string triggerstring(parseASTNodeToString(event->getTrigger()->getMath())); Formula trigger; setFormulaWithString(triggerstring, &trigger, this); Formula delay; const Delay* sbmldelay = event->getDelay(); if (sbmldelay != NULL) { string delaystring(parseASTNodeToString(sbmldelay->getMath())); setFormulaWithString(delaystring, &delay, this); } AntimonyEvent antevent(delay, trigger,var); var->SetEvent(&antevent); //Set the assignments: for (unsigned int asnt=0; asnt<event->getNumEventAssignments(); asnt++) { const EventAssignment* assignment = event->getEventAssignment(asnt); string name = assignment->getVariable(); Variable* asntvar = AddOrFindVariable(&name); Formula* asntform = g_registry.NewBlankFormula(); setFormulaWithString(parseASTNodeToString(assignment->getMath()), asntform, this); var->GetEvent()->AddResult(asntvar, asntform); } } //LS DEBUG: Add constraints? //Parameters for (unsigned int param=0; param<sbml->getNumParameters(); param++) { const Parameter* parameter = sbml->getParameter(param); sbmlname = getNameFromSBMLObject(parameter, "_P"); Variable* var = AddOrFindVariable(&sbmlname); if (parameter->isSetName()) { var->SetDisplayName(parameter->getName()); } if (parameter->isSetValue()) { Formula* formula = g_registry.NewBlankFormula(); formula->AddNum(parameter->getValue()); var->SetFormula(formula); //LS NOTE: If a parameter has both a value and an 'initial assignment', the initial assignment will override the value. } if (parameter->isSetUnits()) { var->SetUnits(parameter->getUnits()); } } //Initial Assignments: can override 'getValue' values. for (unsigned int ia=0; ia<sbml->getNumInitialAssignments(); ia++) { const InitialAssignment* initasnt = sbml->getInitialAssignment(ia); if (initasnt->isSetSymbol()) { sbmlname = initasnt->getSymbol(); Variable* var = AddOrFindVariable(&sbmlname); if (initasnt->isSetName()) { var->SetDisplayName(initasnt->getName()); } Formula* formula = g_registry.NewBlankFormula(); string formulastring(parseASTNodeToString(initasnt->getMath())); setFormulaWithString(formulastring, formula, this); var->SetFormula(formula); } else { //LS DEBUG: error? The 'symbol' is supposed to be required. } } //Rules: for (unsigned int rulen=0; rulen<sbml->getNumRules(); rulen++) { const Rule* rule = sbml->getRule(rulen); if (rule->isAlgebraic()) { //LS DEBUG: error message? Unable to process algebraic rules continue; } sbmlname = rule->getVariable(); assert(sbmlname != ""); if (sbmlname == "") { sbmlname = getNameFromSBMLObject(rule, "_R"); } Variable* var = AddOrFindVariable(&sbmlname); if (rule->isSetName()) { var->SetDisplayName(rule->getName()); } Formula* formula = g_registry.NewBlankFormula(); string formulastring(parseASTNodeToString(rule->getMath())); setFormulaWithString(formulastring, formula, this); if (IsSpecies(var->GetType())) { //Any species in any rule must be 'const' (in Antimony), because this means it's a 'boundary species' var->SetIsConst(true); } else { //For other parameters, assignment and rate rules always mean the variable in question is not constant. var->SetIsConst(false); } if (rule->isAssignment()) { var->SetAssignmentRule(formula); } else if (rule->isRate()) { var->SetRateRule(formula); } else { assert(false); //should be caught above } } //Reactions for (unsigned int rxn=0; rxn<sbml->getNumReactions(); rxn++) { const Reaction* reaction = sbml->getReaction(rxn); sbmlname = getNameFromSBMLObject(reaction, "_J"); Variable* var = AddOrFindVariable(&sbmlname); if (reaction->isSetName()) { var->SetDisplayName(reaction->getName()); } //reactants ReactantList reactants; for (unsigned int react=0; react<reaction->getNumReactants(); react++) { const SpeciesReference* reactant = reaction->getReactant(react); double stoichiometry = 1; if (reactant->isSetStoichiometryMath()) { //LS DEBUG: error message? } else { stoichiometry = reactant->getStoichiometry(); } sbmlname = reactant->getSpecies(); if (sbmlname == "") { sbmlname = getNameFromSBMLObject(reactant, "_S"); } Variable* rvar = AddOrFindVariable(&sbmlname); reactants.AddReactant(rvar, stoichiometry); } //products ReactantList products; for (unsigned int react=0; react<reaction->getNumProducts(); react++) { const SpeciesReference* product = reaction->getProduct(react); double stoichiometry = 1; if (product->isSetStoichiometryMath()) { //LS DEBUG: error message? } else { stoichiometry = product->getStoichiometry(); } sbmlname = product->getSpecies(); if (sbmlname == "") { sbmlname = getNameFromSBMLObject(product, "_S"); } Variable* rvar = AddOrFindVariable(&sbmlname); products.AddReactant(rvar, stoichiometry); } //formula string formulastring = ""; Formula formula; if (reaction->isSetKineticLaw()) { const KineticLaw* kl = reaction->getKineticLaw(); var->SetUnits(kl->getSubstanceUnits() + "/(" + kl->getTimeUnits() + ")"); formulastring = parseASTNodeToString(kl->getMath()); setFormulaWithString(formulastring, &formula, this); for (unsigned int localp=0; localp<kl->getNumParameters(); localp++) { const Parameter* localparam = kl->getParameter(localp); vector<string> fullname; //Find the variable with the original name: string origname = getNameFromSBMLObject(localparam, "_P"); fullname.push_back(origname); Variable* origvar = GetVariable(fullname); //Create a new variable with a new name: fullname.clear(); sbmlname = var->GetNameDelimitedBy('_') + "_" + origname; fullname.push_back(sbmlname); Variable* foundvar = GetVariable(fullname); while (foundvar != NULL) { //Just in case something weird happened and there was another one of *this* name, too. sbmlname = var->GetNameDelimitedBy('_') + "_" + sbmlname; fullname.clear(); fullname.push_back(sbmlname); foundvar = GetVariable(fullname); } Variable* localvar = AddOrFindVariable(&sbmlname); //Replace the variable in the formula: if(origvar != NULL) { formula.ReplaceWith(origvar, localvar); } else { //If origvar is NULL, nothing needs to be replaced: if the original formula had included the parameter, the earlier setFormulaWithString would have created one. But since there wasn't one, this means the original formula didn't include the parameter at all! Meaning this local parameter has no use whatsoever! What the heck, dude. Oh, well. //cout << "Unused local variable for reaction " << var->GetNameDelimitedBy('.') << ": " << origname << endl; } //Set the value for the new variable: Formula localformula; localformula.AddNum(localparam->getValue()); localvar->SetFormula(&localformula); } } else if (reaction->getNumModifiers() > 0) { //If the kinetic law is empty, we can set some interactions, if there are any Modifiers. ReactantList right; right.AddReactant(var); ReactantList left; for (unsigned int mod=0; mod<reaction->getNumModifiers(); mod++) { const ModifierSpeciesReference* msr = reaction->getModifier(mod); string species = msr->getSpecies(); Variable* specvar = AddOrFindVariable(&species); left.AddReactant(specvar); sbmlname = getNameFromSBMLObject(msr, "_I"); } Variable* interaction = AddOrFindVariable(&sbmlname); Formula blankform; AddNewReaction(&left, rdInfluences, &right, &blankform, interaction); } rd_type rxntype = rdBecomes; if (!reaction->getReversible()) { rxntype = rdBecomesIrreversibly; } //Put reactants, products, and the formula together: AddNewReaction(&reactants, rxntype, &products, &formula, var); } //Finally, fix the fact that 'time' used to be OK in functions (l2v1), but is no longer (l2v2). g_registry.FixTimeInFunctions(); //And that some SBML-OK names are not OK in Antimony FixNames(); }