const Variable* ReactantList::GetNthReactant(size_t n) const
{
  if (n>=m_components.size()) return NULL;
  Module* module = g_registry.GetModule(m_module);
  assert(module != NULL);
  return module->GetVariable(m_components[n].second);
}
void ReactantList::SetComponentCompartments(Variable* compartment, var_type supercomp)
{
  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) {
      var->SetSuperCompartment(compartment, supercomp);
    }
  }
}
bool ReactantList::SetComponentFormulasTo(Formula form)
{
  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->SetFormula(&form)) return true;
    }
  }
  return false;
}
double ReactantList::GetStoichiometryFor(const Variable* var) const
{
  double stoich = 0;
  for (size_t component=0; component<m_components.size(); component++) {
    Module* module = g_registry.GetModule(m_module);
    assert(module != NULL);
    Variable* compvar = module->GetVariable(m_components[component].second);
    assert(compvar != NULL);
    if (compvar->GetIsEquivalentTo(var)) {
      stoich += m_components[component].first;
    }
  }
  return stoich;
}
Beispiel #5
0
string AntimonyEvent::ToStringDelimitedBy(std::string cc) const
{
  if (IsEmpty()) return "";
  string retval;

  Module* module = g_registry.GetModule(m_module);
  assert(module != NULL);
  Variable* actualvar = module->GetVariable(m_name);
  if (actualvar == NULL) {
    //um what?
    assert(false);
    return "";
  }
  retval += actualvar->GetNameDelimitedBy(cc) + ": at ";
  if (!m_delay.IsEmpty()) {
    retval += m_delay.ToDelimitedStringWithEllipses(cc) + " after ";
  }
  retval += m_trigger.ToDelimitedStringWithEllipses(cc);
  if (!m_priority.IsEmpty()) {
    retval += ", priority = " + m_priority.ToDelimitedStringWithEllipses(cc);
  }
  if (m_initialValue == false) {
    retval += ", t0=false";
  }
  if (m_persistent == false) {
    retval += ", persistent=false";
  }
  if (m_useValuesFromTriggerTime == false) {
    retval += ", fromTrigger=false";
  }
  retval += ": ";
  for (size_t result=0; result<m_varresults.size(); result++) {
    if (result>0) {
      retval += ", ";
    }
    Variable* resultvar = g_registry.GetModule(m_module)->GetVariable(m_varresults[result]);
    if (resultvar == NULL) {
      assert(false);
      return "";
    }
    retval += resultvar->GetNameDelimitedBy(cc);
    retval += " = ";
    retval += m_formresults[result].ToDelimitedStringWithEllipses(cc);
  }
  retval += ";";
  
  return retval;
}
Beispiel #6
0
bool AntimonyEvent::ClearReferencesTo(Variable* deletedvar, set<pair<vector<string>, deletion_type> >* ret)
{
  if (IsEmpty()) return false;
  if (m_trigger.ClearReferencesTo(deletedvar)) {
    //Can't (yet?) have an event with no trigger: delete the event
    return true;
  }
  if (m_delay.ClearReferencesTo(deletedvar)) {
    ret->insert(make_pair(m_name, delEventDelay));
  }
  if (m_priority.ClearReferencesTo(deletedvar)) {
    ret->insert(make_pair(m_name, delEventPriority));
  }
  vector<Formula>::iterator form = m_formresults.begin();
  vector<vector<string> >::iterator varresult = m_varresults.begin();
  Module* module = g_registry.GetModule(m_module);
  assert(module != NULL);
  while (form != m_formresults.end()) {
    if (form->ClearReferencesTo(deletedvar) ||
        module->GetVariable(*varresult)->GetIsEquivalentTo(deletedvar)) {
          vector<string> longername = m_name;
          longername.push_back((*varresult)[varresult->size()-1]);
          ret->insert(make_pair(longername,  delEventAssignment));
          form = m_formresults.erase(form);
          varresult = m_varresults.erase(varresult);
    }
    else {
      form++;
      varresult++;
    }
  }
  if (m_formresults.size() == 0) {
    //Can't (yet?) have an event with no event assignments: delete the event
    return true;
  }
  return false; //Didn't have to delete ourselves.
}
Beispiel #7
0
bool Registry::SynchronizeCellMLConnection(nsCOMPtr<cellml_apiIConnection> connection)
{
  nsString cellmltext;
  string cellmlname;
  nsresult rv;

  nsCOMPtr<cellml_apiIModel> topmodel; //used when the encapsulation parent is null (in GetNameAccordingToEncapsulationParent)
  rv = connection->GetModelElement(getter_AddRefs(topmodel));
  nsCOMPtr<cellml_apiICellMLElement> parentel;
  rv = topmodel->GetParentElement(getter_AddRefs(parentel));
  while (parentel != NULL) {
    rv = parentel->GetModelElement(getter_AddRefs(topmodel));
    rv = topmodel->GetParentElement(getter_AddRefs(parentel));
  }
  //rv = topmodel->GetName(cellmltext);
  //cout << "Top model: " << ToThinString(cellmltext.get()) << endl;
  
  //First we get the list of the component and any/all encapsulation parents
  vector<string> comp1moduleparents, comp2moduleparents;
  vector<string> comp1modulenames, comp2modulenames;
  nsCOMPtr<cellml_apiIMapComponents> compmap;
  rv = connection->GetComponentMapping(getter_AddRefs(compmap));
  nsCOMPtr<cellml_apiICellMLComponent> component;

  //First
  rv = compmap->GetFirstComponent(getter_AddRefs(component));
  while (component != NULL) {
    string modname = GetModuleNameFrom(component);
    comp1moduleparents.insert(comp1moduleparents.begin(), modname);
    modname = GetNameAccordingToEncapsulationParent(component, topmodel);
    FixName(modname);
    comp1modulenames.insert(comp1modulenames.begin(), modname);
    rv = component->GetEncapsulationParent(getter_AddRefs(component));
  }
  comp1moduleparents.insert(comp1moduleparents.begin(), CurrentModule()->GetModuleName());

  //Second
  rv = compmap->GetSecondComponent(getter_AddRefs(component));
  while (component != NULL) {
    string modname = GetModuleNameFrom(component);
    comp2moduleparents.insert(comp2moduleparents.begin(), modname);
    modname = GetNameAccordingToEncapsulationParent(component, topmodel);
    FixName(modname);
    comp2modulenames.insert(comp2modulenames.begin(), modname);
    rv = component->GetEncapsulationParent(getter_AddRefs(component));
  }
  comp2moduleparents.insert(comp2moduleparents.begin(), CurrentModule()->GetModuleName());

  //cout << "First component's parents: " << ToStringFromVecDelimitedBy(comp1moduleparents, '.') << endl << "Second component's parents: " << ToStringFromVecDelimitedBy(comp2moduleparents, '.') << endl;
  //cout << "First component's names: " << ToStringFromVecDelimitedBy(comp1modulenames, '.') << endl << "Second component's names: " << ToStringFromVecDelimitedBy(comp2modulenames, '.') << endl;
  //Now figure out the 'lowest' common parent in the encapsulation tree:
  string commonparent = "";
  assert(comp1moduleparents.size() > 0 && comp2moduleparents.size() > 0 && comp1moduleparents[0] == comp2moduleparents[0]);
  size_t pnum = 0;
  while (comp1modulenames.size() > 0 && comp2modulenames.size() > 0 &&
         comp1modulenames[0] == comp2modulenames[0]) {
    comp1modulenames.erase(comp1modulenames.begin());
    comp2modulenames.erase(comp2modulenames.begin());
    pnum++;
  }
  commonparent = comp1moduleparents[pnum];

  //At this point, we have the name of the module where the encapsulation happens:
  Module* topmod = CurrentModule();
  if (commonparent != "") {
    topmod = GetModule(commonparent);
  }
  assert(topmod != NULL);

  // cout << "Top module: " << commonparent << endl;
  // cout << "first compartment submodule name: " << ToStringFromVecDelimitedBy(comp1modulenames, '.') << endl;
  // cout << "second compartment submodule name: " << ToStringFromVecDelimitedBy(comp2modulenames, '.') << endl;

  //And we have the full names of the submodules whose variables need to be synchronized.  But there might be multiple variables, so we go through them all:
  nsCOMPtr<cellml_apiIMapVariablesSet> mvs;
  rv = connection->GetVariableMappings(getter_AddRefs(mvs));
  nsCOMPtr<cellml_apiIMapVariablesIterator> mvsi;
  rv = mvs->IterateMapVariables(getter_AddRefs(mvsi));
  nsCOMPtr<cellml_apiIMapVariables> mapvars;
  rv = mvsi->NextMapVariable(getter_AddRefs(mapvars));
  bool somefalse = false;
  while (mapvars != NULL) {
    rv = mapvars->GetFirstVariableName(cellmltext);
    cellmlname = ToThinString(cellmltext.get());
    FixName(cellmlname);
    vector<string> fullvarname = comp1modulenames;
    fullvarname.push_back(cellmlname);
    Variable* firstvar = topmod->GetVariable(fullvarname);
    assert(firstvar != NULL);
    firstvar = firstvar->GetSameVariable();
    rv = mapvars->GetSecondVariableName(cellmltext);
    cellmlname = ToThinString(cellmltext.get());
    FixName(cellmlname);
    fullvarname = comp2modulenames;
    fullvarname.push_back(cellmlname);
    Variable* secondvar = topmod->GetVariable(fullvarname);
    assert(secondvar != NULL);
    secondvar = secondvar->GetSameVariable();
    //Now we create a local name for the variables to be synchronized if one doesn't already exist.
    vector<string> firstvarname = firstvar->GetName();
    vector<string> secondvarname = secondvar->GetName();
    if (firstvarname.size() > 1 && secondvarname.size() > 1) {
      //Create a local variable
      vector<string> newvarname;
      newvarname.push_back(secondvarname[secondvarname.size()-1]);
      Variable* newvar = topmod->GetVariable(newvarname);
      if (newvar == NULL) {
        newvar = topmod->AddOrFindVariable(&newvarname[0]);
      }
      else {
        newvar = topmod->AddNewNumberedVariable(newvarname[0]);
      }
      if (firstvar->Synchronize(newvar)) {
        g_registry.AddWarning("In module '" + topmod->GetModuleName() + "', the variables " + firstvar->GetNameDelimitedBy('.') + " and " + newvar->GetNameDelimitedBy('.') + " were unable to be set as equivalent:  " + g_registry.GetError());
        somefalse = true;
      }
      if (secondvar->Synchronize(newvar)) {
        g_registry.AddWarning("In module '" + topmod->GetModuleName() + "', the variables " + secondvar->GetNameDelimitedBy('.') + " and " + newvar->GetNameDelimitedBy('.') + " were unable to be set as equivalent:  " + g_registry.GetError());
        somefalse = true;
      }
    }
    else {
      if (firstvar->Synchronize(secondvar)) {
        g_registry.AddWarning("In module '" + topmod->GetModuleName() + "', the variables " + firstvar->GetNameDelimitedBy('.') + " and " + secondvar->GetNameDelimitedBy('.') + " were unable to be set as equivalent:  " + g_registry.GetError());
        somefalse = true;
      }
    }
    rv = mvsi->NextMapVariable(getter_AddRefs(mapvars));
  }
  return somefalse;
}