void checkComp(CompSBMLDocumentPlugin* compdoc, set<string>& components, set<string>& tests,  const map<string, vector<double> >& results)
{
  SBMLDocument* doc = compdoc->getSBMLDocument();
  List* allElements = doc->getAllElements();
  for (unsigned int e=0; e<allElements->getSize(); e++) {
    SBase* element = static_cast<SBase*>(allElements->get(e));
    ReplacedElement* re;
    Submodel* submod;
    switch(element->getTypeCode()) {
    case SBML_COMP_SUBMODEL:
      components.insert("comp:Submodel");
      submod = static_cast<Submodel*>(element);
      if (submod->isSetExtentConversionFactor()) {
        tests.insert("comp:ExtentConversionFactor");
      }
      if (submod->isSetTimeConversionFactor()) {
        tests.insert("comp:TimeConversionFactor");
      }
      break;
    case SBML_COMP_MODELDEFINITION:
      components.insert("comp:ModelDefinition");
      break;
    case SBML_COMP_EXTERNALMODELDEFINITION:
      components.insert("comp:ExternalModelDefinition");
      break;
    case SBML_COMP_SBASEREF:
      components.insert("comp:SBaseRef");
      break;
    case SBML_COMP_DELETION:
      components.insert("comp:Deletion");
      break;
    case SBML_COMP_REPLACEDELEMENT:
      components.insert("comp:ReplacedElement");
      re = static_cast<ReplacedElement*>(element);
      if (re->isSetConversionFactor()) {
        tests.insert("comp:ConversionFactor");
      }
      break;
    case SBML_COMP_REPLACEDBY:
      components.insert("comp:ReplacedBy");
      break;
    case SBML_COMP_PORT:
      components.insert("comp:Port");
      break;
    default:
      break;
    }
  }
  for (map<string, vector<double> >::const_iterator result=results.begin(); 
    result != results.end(); result++) {
      string id = result->first;
      if (id.find("__") != string::npos) {
        //It is probably a submodel element, renamed
        tests.insert("comp:SubmodelOutput");
      }
  }
}
void 
UnitReplacementCheck::checkReferencedElement(ReplacedElement& repE, 
                                             const Model& m)
{
  // if the deletion attribute is set then it is not a true replacement
  if (repE.isSetDeletion() == true)
  {
    return;
  }

  unsigned int numErrsB4 = repE.getSBMLDocument()->getNumErrors();
  
  SBase* refElem = repE.getReferencedElement();
  
  // if there is an issue with references the getReferencedElement
  // will log errors and possibly fail
  // we dont want to try any further
  unsigned int numErrsAfter = repE.getSBMLDocument()->getNumErrors();

  if (numErrsB4 != numErrsAfter || refElem == NULL)
  {
    return;
  }

  SBase * parent = repE.getParentSBMLObject()->getParentSBMLObject();
  UnitDefinition *parentUnits = parent->getDerivedUnitDefinition();
  
  UnitDefinition *refElemUnits = refElem->getDerivedUnitDefinition();
  bool delrefelem = false;

  bool cfPresent = false;
  /* adjust the refElement units for conversion factor */
  if (repE.isSetConversionFactor() == true && refElemUnits && refElemUnits->getNumUnits() > 0)
  {
    Parameter * p = const_cast<Model *>(&m)
                                   ->getParameter(repE.getConversionFactor());
    UnitDefinition *ud = p->getDerivedUnitDefinition();
    UnitDefinition *newRefElemUnits = UnitDefinition::combine(refElemUnits, ud);
    refElemUnits = newRefElemUnits;
    delrefelem = true;
    cfPresent = true;
  }

  if (parentUnits == NULL)
  {
    if (delrefelem)
    {
      delete refElemUnits;
    }
    return;
  }

  if (refElemUnits == NULL)
  {
    return;
  }


  if (parent->containsUndeclaredUnits() == true ||
    refElem->containsUndeclaredUnits() == true)
  {
    if (delrefelem)
    {
      delete refElemUnits;
    }
    return;
  }

  if (UnitDefinition::areIdentical(parentUnits, refElemUnits) == false)
  {
    logMismatchUnits(repE, refElem, parent, cfPresent);
  }
  else
  {
    // if we have Compartments that have spatialDimensions but no units
    // we can check this 
    if (parent->getTypeCode() == SBML_COMPARTMENT 
      && parentUnits->getNumUnits() == 0
      && refElem->getTypeCode() == SBML_COMPARTMENT
      && refElemUnits->getNumUnits() == 0)
    {
      if (static_cast<Compartment *>(parent)->isSetSpatialDimensions() == true
      && static_cast<Compartment *>(refElem)->isSetSpatialDimensions() == true)
      {
        if (util_isEqual(
          static_cast<Compartment *>(parent)->getSpatialDimensionsAsDouble(),
          static_cast<Compartment *>(refElem)->getSpatialDimensionsAsDouble())
                         == 0)
        {
          logMismatchSpatialDimensions(repE, refElem, parent);
        }
      }
    }
  }
  if (delrefelem)
  {
    delete refElemUnits;
  }
}