END_TEST


START_TEST (test_unitdefinition_convert_SI2)
{
  UnitDefinition *ud = new UnitDefinition(1, 1);
  UnitDefinition *ud1;
  
  Unit * u = ud->createUnit();
  u->setKind(UNIT_KIND_FARAD);

  ud1 = UnitDefinition::convertToSI(ud);

  fail_unless( ud1->getNumUnits() == 4);
  fail_unless( ud1->getLevel() == 1);
  fail_unless( ud1->getVersion() == 1);
  fail_unless( ud1->getUnit(0)->getKind() == UNIT_KIND_AMPERE );
  fail_unless( ud1->getUnit(0)->getExponent() == 2);
  fail_unless( ud1->getUnit(1)->getKind() == UNIT_KIND_KILOGRAM );
  fail_unless( ud1->getUnit(1)->getExponent() == -1);
  fail_unless( ud1->getUnit(2)->getKind() == UNIT_KIND_METRE );
  fail_unless( ud1->getUnit(2)->getExponent() == -2);
  fail_unless( ud1->getUnit(3)->getKind() == UNIT_KIND_SECOND );
  fail_unless( ud1->getUnit(3)->getExponent() == 4);


  UnitDefinition_free(ud);
}
END_TEST

START_TEST(test_unitdefinition_convert_SI)
{
  UnitDefinition* ud  = new UnitDefinition(2, 4);
  UnitDefinition* ud1 = new UnitDefinition(2, 4);

  Unit* u  = new Unit(2, 4);
  u->setKind(UNIT_KIND_JOULE);
  Unit* u1 = new Unit(2, 4);
  u1->setKind(UNIT_KIND_NEWTON);

  u1->setExponent(-1);

  ud->addUnit(u);
  ud->addUnit(u1);

  ud1 = UnitDefinition::convertToSI(ud);

  fail_unless(ud1->getNumUnits() == 1);
  fail_unless(ud1->getUnit(0)->getKind() == UNIT_KIND_METRE);

  /* NULL case*/
  ud = NULL;

  ud1 = UnitDefinition::convertToSI(ud);
  
  fail_unless(ud1 == NULL);

  delete u;
  delete u1;
  delete ud;
  delete ud1;
}
END_TEST

#if (0)
START_TEST (test_getUnitDefinition_power_minus_double_exponent)
{
  ASTNode * node = new ASTNode(AST_POWER);
  ASTNode * c = new ASTNode(AST_NAME);
  c->setName("k");
  ASTNode * c1 = new ASTNode(AST_REAL);
  c1->setValue(0.3);
  ASTNode * c2 = new ASTNode(AST_MINUS);
  c2->addChild(c1);
  node->addChild(c);
  node->addChild(c2);

  UnitDefinition * ud = NULL;
    
  ud = uff->getUnitDefinition(node);
  
  fail_unless(uff->getContainsUndeclaredUnits() == false);
  fail_unless(uff->canIgnoreUndeclaredUnits() == false);

  fail_unless(ud != NULL);
  fail_unless(ud->getNumUnits() == 1);

  fail_unless(ud->getUnit(0)->getKind() == UNIT_KIND_METRE);
  fail_unless(util_isEqual(ud->getUnit(0)->getExponentAsDouble(), -0.3));

  delete node;
  delete ud;

}
Ejemplo n.º 4
0
/*
 * Converts a Watt Unit into the corresponding UnitDefinition
 * consisting only of SI units.
 * If the given unit does not have the correct kind, a NULL pointer is
 * returned.
 * It is up to the receiver to free the memory of the returned
 * UnitDefinition.
 * @param const Unit& unit
 * @return UnitDefinition* result
 */
LIBSBML_EXTERN
UnitDefinition* UnitConversionFactory::convertWattToSI(const Unit& unit)
{
    UnitKind_t uKind = unit.getKind();

    if (uKind != UNIT_KIND_WATT) return NULL;

    UnitDefinition* pUdef = new UnitDefinition(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION);
    Unit* pU = new Unit(unit);
    pU->setOffset(0.0);
    pU->setKind(UNIT_KIND_KILOGRAM);
    pU->setExponent(unit.getExponent());
    pUdef->addUnit(pU);
    delete pU;
    pU = new Unit(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION);
    pU->setKind(UNIT_KIND_METER);
    pU->setExponent(2*unit.getExponent());
    pUdef->addUnit(pU);
    delete pU;
    pU = new Unit(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION);
    pU->setKind(UNIT_KIND_SECOND);
    pU->setExponent(-3*unit.getExponent());
    pUdef->addUnit(pU);
    delete pU;
    return pUdef;
}
END_TEST


START_TEST (test_getUnitDefinition_power_one_child)
{
  ASTNode * node = new ASTNode(AST_POWER);
  ASTNode * c = new ASTNode(AST_NAME);
  c->setName("k");
  node->addChild(c);

  UnitDefinition * ud = NULL;
    
  ud = uff->getUnitDefinition(node);
  
  fail_unless(uff->getContainsUndeclaredUnits() == true);
  fail_unless(uff->canIgnoreUndeclaredUnits() == false);

  fail_unless(ud != NULL);
  fail_unless(ud->getNumUnits() == 1);

  fail_unless(ud->getUnit(0)->getKind() == UNIT_KIND_METRE);
  fail_unless(util_isEqual(ud->getUnit(0)->getExponent(), 1));


  delete node;
  delete ud;

}
END_TEST


START_TEST ( test_UnitDefinition_parent_NULL )
{
    SBMLDocument *d = new SBMLDocument();
    Model *m = d->createModel();
    UnitDefinition *c = m->createUnitDefinition();
    Unit *u = c->createUnit();

    fail_unless(u->getAncestorOfType(SBML_UNIT_DEFINITION) == c);

    UnitDefinition *c1 = c->clone();
    delete d;

    fail_unless(c1->getAncestorOfType(SBML_MODEL) == NULL);
    fail_unless(c1->getParentSBMLObject() == NULL);
    fail_unless (c1->getSBMLDocument() == NULL);

    fail_unless(c1->getUnit(0)->getAncestorOfType(SBML_UNIT_DEFINITION) == c1);
    fail_unless(c1->getUnit(0)->getParentSBMLObject() != NULL);
    fail_unless (c1->getUnit(0)->getSBMLDocument() == NULL);

    delete c1;
}
Ejemplo n.º 7
0
END_TEST


START_TEST (test_WriteL3SBML_UnitDefinition)
{

  const char* expected = 
    "<unitDefinition id=\"myUnit\">\n"
    "  <listOfUnits>\n"
    "    <unit kind=\"mole\" exponent=\"1\" scale=\"0\" multiplier=\"1.8\"/>\n"
    "  </listOfUnits>\n"
    "</unitDefinition>";

  UnitDefinition* ud = D->createModel()->createUnitDefinition();
  ud->setId("myUnit");

  Unit* u1 = ud->createUnit();
  u1->setKind(UnitKind_forName("mole"));
  u1->setMultiplier(1.8);
  u1->setScale(0);
  u1->setExponent(1);

  char* sbml = ud->toSBML();
  fail_unless( equals(expected, sbml) );
  safe_free(sbml);
}
/**
  * Checks that the units of the arguments 
  * of the function are dimensionless
  * and that there is only one argument
  *
  * If inconsistent units are found, an error message is logged.
  */
void 
ArgumentsUnitsCheckWarnings::checkDimensionlessArgs (const Model& m, 
                                           const ASTNode& node, 
                                           const SBase & sb, 
                                           bool inKL, int reactNo)
{
  /* check that node has children */
  if (node.getNumChildren() == 0)
  {
    return;
  }

  UnitDefinition *dim = new UnitDefinition(m.getSBMLNamespaces());
  Unit *unit = new Unit(m.getSBMLNamespaces());
  unit->setKind(UNIT_KIND_DIMENSIONLESS);
  unit->initDefaults();
  UnitDefinition * tempUD;
  dim->addUnit(unit);
  
  UnitFormulaFormatter *unitFormat = new UnitFormulaFormatter(&m);

  tempUD = unitFormat->getUnitDefinition(node.getChild(0), inKL, reactNo);
  
  if (tempUD->getNumUnits() != 0 && 
    !UnitDefinition::areEquivalent(dim, tempUD)) 
  {
    logInconsistentDimensionless(node, sb);
  }

  delete tempUD;
  delete dim;
  delete unit;
  delete unitFormat;

}
END_TEST
#endif

START_TEST (test_getUnitDefinition_power_dim_param_exponent)
{
  ASTNode * node = new ASTNode(AST_POWER);
  ASTNode * c = new ASTNode(AST_NAME);
  c->setName("k");
  ASTNode * c1 = new ASTNode(AST_NAME);
  c1->setName("a");
  node->addChild(c);
  node->addChild(c1);

  UnitDefinition * ud = NULL;
    
  ud = uff->getUnitDefinition(node);
  
  fail_unless(uff->getContainsUndeclaredUnits() == false);
  fail_unless(uff->canIgnoreUndeclaredUnits() == false);

  fail_unless(ud != NULL);
  fail_unless(ud->getNumUnits() == 1);

  fail_unless(ud->getUnit(0)->getKind() == UNIT_KIND_METRE);
  fail_unless(util_isEqual(ud->getUnit(0)->getExponentAsDouble(), 3.5));

  delete node;
  delete ud;

}
END_TEST

START_TEST(test_unitdefinition_areIdentical2)
{
  UnitDefinition* ud  = new UnitDefinition(2, 2);
  UnitDefinition* ud1 = new UnitDefinition(2, 2);

  Unit* u  = new Unit(2, 2);
  u->setKind(UNIT_KIND_JOULE);
  Unit* u1 = new Unit(2, 2);
  u1->setKind(UNIT_KIND_NEWTON);
  
  ud->addUnit(u);
  ud->addUnit(u1);

  ud1->addUnit(u);
  ud1->addUnit(u1);

  int identical = UnitDefinition::areIdentical(ud, ud1);

  fail_unless(identical == 1);

  delete u;
  delete u1;
  delete ud;
  delete ud1;

}
END_TEST


START_TEST (test_getUnitDefinition_power_three_children)
{
  ASTNode * node = new ASTNode(AST_POWER);
  ASTNode * c = new ASTNode(AST_NAME);
  c->setName("k");
  ASTNode * c1 = new ASTNode(AST_INTEGER);
  c1->setValue(1);
  ASTNode * c2 = new ASTNode(AST_INTEGER);
  c2->setValue(2);
  node->addChild(c);
  node->addChild(c1);
  node->addChild(c2);

  UnitDefinition * ud = NULL;
    
  ud = uff->getUnitDefinition(node);
  
  fail_unless(uff->getContainsUndeclaredUnits() == true);
  fail_unless(uff->canIgnoreUndeclaredUnits() == false);

  fail_unless(ud != NULL);
  fail_unless(ud->getNumUnits() == 0);

  delete node;
  delete ud;

}
END_TEST


START_TEST(test_unitdefinition_divide1)
{
  UnitDefinition* ud = new UnitDefinition(2, 1);
  UnitDefinition* ud1 = new UnitDefinition(2, 2);
  UnitDefinition* udTemp;

  Unit* u  = new Unit(2, 1);
  u->setKind(UNIT_KIND_METRE);
  Unit* u1 = new Unit(2, 2);
  u1->setKind(UNIT_KIND_MOLE);
 
  ud->addUnit(u);
  ud1->addUnit(u1);
  
  udTemp = UnitDefinition::divide(ud, ud1);

  fail_unless(udTemp == 0);

  delete u;
  delete ud1;
  delete u1;
  delete ud;
 }
Ejemplo n.º 13
0
END_TEST

START_TEST ( test_UnitDefinition_L1 )
{
  UnitDefinition* ud = new UnitDefinition(1, 2);
  
  fail_unless (ud->hasRequiredElements());

  delete ud;
}
END_TEST


START_TEST(test_unitdefinition_areIdentical)
{
  UnitDefinition* ud  = new UnitDefinition(2, 4);
  UnitDefinition* ud1 = new UnitDefinition(2, 4);

  Unit* u  = new Unit(2, 4);
  u->setKind(UNIT_KIND_JOULE);
  Unit* u1 = new Unit(2, 4);
  u1->setKind(UNIT_KIND_NEWTON);
  Unit* u2 = new Unit(2, 4);
  u2->setKind(UNIT_KIND_METRE);
  
  ud->addUnit(u);
  ud->addUnit(u1);

  ud1->addUnit(u);
  ud1->addUnit(u1);

  int identical = UnitDefinition::areIdentical(ud, ud1);

  fail_unless(identical == 1);

  ud->addUnit(u2);

  identical = UnitDefinition::areIdentical(ud, ud1);
 
  fail_unless(identical == 0);

  /* NULL case*/
  ud = NULL;

  identical = UnitDefinition::areIdentical(ud, ud1);;
  
  fail_unless(identical == 0);
 
  identical = UnitDefinition::areIdentical(ud1, ud);;
  
  fail_unless(identical == 0);
  
  ud1 = NULL;

  identical = UnitDefinition::areIdentical(ud, ud1);;
  
  fail_unless(identical == 1);

  delete u;
  delete u1;
  delete u2;
  delete ud;
  delete ud1;

}
END_TEST


START_TEST (test_CalcUnitDefinition_local1)
{
  UnitDefinition *fud = m->getReaction("R5")->getKineticLaw()
    ->getLocalParameter(0)->getDerivedUnitDefinition();

  fail_unless(fud->getNumUnits() == 0);

  m->getReaction("R5")->getKineticLaw()
    ->getLocalParameter(0)->setCalculatingUnits(true);
  fud = m->getReaction("R5")->getKineticLaw()
    ->getLocalParameter(0)->getDerivedUnitDefinition();
  m->getReaction("R5")->getKineticLaw()
    ->getLocalParameter(0)->setCalculatingUnits(false);

  fail_unless(fud->getNumUnits() == 1);
  fail_unless(!strcmp(fud->getId().c_str(), ""), NULL);

  fail_unless(fud->getUnit(0)->getMultiplier() == 1);
  fail_unless(fud->getUnit(0)->getScale() == 0);
  fail_unless(fud->getUnit(0)->getExponent() == 1);
  fail_unless(fud->getUnit(0)->getOffset() == 0.0);
  fail_unless(fud->getUnit(0)->getKind() == UNIT_KIND_SECOND);
}
END_TEST


START_TEST(test_unitdefinition_areEquivalent)
{
  UnitDefinition* ud  = new UnitDefinition(2, 4);
  UnitDefinition* ud1 = new UnitDefinition(2, 4);

  Unit* u  = new Unit(2, 4);
  u->setKind(UNIT_KIND_JOULE);
  Unit* u1 = new Unit(2, 4);
  u1->setKind(UNIT_KIND_NEWTON);
  Unit* u2 = new Unit(2, 4);
  u->setKind(UNIT_KIND_METRE);

  u1->setExponent(-1);

  ud->addUnit(u);
  ud->addUnit(u1);

  ud1->addUnit(u2);

  int equivalent = UnitDefinition::areEquivalent(ud, ud1);

  //fail_unless(equivalent == 1);

  ud->addUnit(u2);

  equivalent = UnitDefinition::areEquivalent(ud, ud1);
 
  fail_unless(equivalent == 0);

  /* NULL case*/
  ud = NULL;

  equivalent = UnitDefinition::areEquivalent(ud, ud1);
  
  fail_unless(equivalent == 0);
 
  equivalent = UnitDefinition::areEquivalent(ud1, ud);
  
  fail_unless(equivalent == 0);
 
  ud1 = NULL;

  equivalent = UnitDefinition::areEquivalent(ud, ud1);
  
  fail_unless(equivalent == 1);

  delete u;
  delete u1;
  delete u2;
  delete ud;
  delete ud1;
}
Ejemplo n.º 17
0
/*
 * Returns a string representation of the given Unit. THis function is
 * only for debugging purposes.
 * @param const UnitDefinition& uDef
 * @return std::string unitDefinitionRepresentation
 */
LIBSBML_EXTERN
std::string UnitConversionFactory::toString(const UnitDefinition& uDef)
{
    unsigned int maxUnits = uDef.getNumUnits();
    unsigned int i;
    std::stringstream ss;

    for (i = 0; i < maxUnits; ++i)
    {
        ss << "(" << UnitConversionFactory::toString(*(uDef.getUnit(i))) << ") ";
    }

    return ss.str();
}
END_TEST

START_TEST ( test_UnitDefinition )
{
  UnitDefinition* ud = new UnitDefinition(2, 4);
  
  fail_unless (!(ud->hasRequiredAttributes()));

  ud->setId("ud");

  fail_unless (ud->hasRequiredAttributes());

  delete ud;
}
END_TEST


START_TEST ( test_UnitDefinition_parent_create )
{
    Model *m = new Model(2, 4);
    UnitDefinition *ud = m->createUnitDefinition();

    ListOf *lo = m->getListOfUnitDefinitions();

    fail_unless(lo == m->getUnitDefinition(0)->getParentSBMLObject());
    fail_unless(lo == ud->getParentSBMLObject());
    fail_unless(m == lo->getParentSBMLObject());
}
Ejemplo n.º 20
0
/*
 * Converts a Second Unit into the corresponding UnitDefinition
 * consisting only of SI units.
 * If the given unit does not have the correct kind, a NULL pointer is
 * returned.
 * It is up to the receiver to free the memory of the returned
 * UnitDefinition.
 * @param const Unit& unit
 * @return UnitDefinition* result
 */
LIBSBML_EXTERN
UnitDefinition* UnitConversionFactory::convertSecondToSI(const Unit& unit)
{
    UnitKind_t uKind = unit.getKind();

    if (uKind != UNIT_KIND_SECOND) return NULL;

    UnitDefinition* pUdef = new UnitDefinition(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION);
    Unit* pU = new Unit(unit);
    pU->setOffset(0.0);
    pUdef->addUnit(pU);
    delete pU;
    return pUdef;
}
END_TEST



START_TEST(test_unitdefinition_divide2)
{
  UnitDefinition* ud = new UnitDefinition(2, 2);
  UnitDefinition* ud1 = new UnitDefinition(2, 2);
  UnitDefinition* udTemp;

  Unit* u  = ud->createUnit();
  u->setKind(UNIT_KIND_METRE);
  Unit* u1 = ud1->createUnit();
  u1->setKind(UNIT_KIND_MOLE);
  
  udTemp = UnitDefinition::divide(ud, ud1);

  fail_unless(udTemp->getNumUnits() == 2);
  fail_unless(udTemp->getUnit(0)->getKind() == UNIT_KIND_METRE);
  fail_unless(udTemp->getUnit(1)->getKind() == UNIT_KIND_MOLE);
  fail_unless(udTemp->getUnit(1)->getExponent() == -1);
  fail_unless(udTemp->getLevel() == 2);
  fail_unless(udTemp->getVersion() == 2);

  delete ud1;
  delete ud;
  delete udTemp;
 }
std::string Expression2PresentationMMLUnits::getMathML(const UnitDefinition & ud) const
{
  std::string tmp;

  /*tmp += "<mrow>";
  unsigned int i, imax = ud.getNumUnits();
  for (i=0; i<imax; ++i)
  {
    if (i) tmp += "<mo>&CenterDot;</mo>";
    tmp += getMathML(ud.getUnit(i), false);
  }
  tmp += "</mrow>";*/

  tmp += "<mrow>";
  bool isFirst = true;
  unsigned int i, imax = ud.getNumUnits();

  for (i = 0; i < imax; ++i)
    {
      if (ud.getUnit(i)->getExponent() >= 0)
        {
          if (!isFirst) tmp += "<mo>&CenterDot;</mo>";

          tmp += getMathML(ud.getUnit(i), true);
          isFirst = false;
        }
    }

  if (isFirst) tmp += "<mn>1</mn>"; //there are no units with pos. exponent

  std::string tmp2;
  isFirst = true;

  for (i = 0; i < imax; ++i)
    {
      if (ud.getUnit(i)->getExponent() < 0)
        {
          if (!isFirst) tmp2 += "<mo>&CenterDot;</mo>";

          tmp2 += getMathML(ud.getUnit(i), true);
          isFirst = false;
        }
    }

  if (!isFirst) tmp += "<mo>/</mo>" + tmp2; //only create fraction when there is a denominator

  tmp += "</mrow>";

  return tmp;
}
Ejemplo n.º 23
0
END_TEST

START_TEST ( test_UnitDefinition )
{
  UnitDefinition* ud = new UnitDefinition(2, 4);
  
  fail_unless (!(ud->hasRequiredElements()));

  ud->createUnit();

  fail_unless (ud->hasRequiredElements());

  delete ud;
}
END_TEST


START_TEST (test_UnitFormulaFormatter2_getUnitDefinition_model_extent)
{
  UnitDefinition * ud = NULL;

  /* model extent units */
  ud = uff->getExtentUnitDefinition();

  fail_unless(ud->getNumUnits() == 1);

  fail_unless(!strcmp(ud->getId().c_str(), ""), NULL);

  fail_unless(ud->getUnit(0)->getMultiplier() == 1);
  fail_unless(ud->getUnit(0)->getScale() == 0);
  fail_unless(ud->getUnit(0)->getExponent() == 1);
  fail_unless(ud->getUnit(0)->getOffset() == 0.0);
  fail_unless(ud->getUnit(0)->getKind() == UNIT_KIND_MOLE);


  /* check deals with invalid nodes */
  delete ud;
  UnitDefinition * ud1 = NULL;
  
  m->unsetExtentUnits();

  ud1 = uff->getExtentUnitDefinition();

  fail_unless (ud1->getNumUnits() == 0);

  delete ud1;

}
END_TEST

START_TEST (test_UnitFormulaFormatter_getUnitDefinition_plus)
{
  UnitDefinition * ud = new UnitDefinition(2, 4);

  ud = uff->getUnitDefinition(m->getRule(3)->getMath());

  fail_unless(ud->getNumUnits() == 1);

  fail_unless(!strcmp(ud->getId().c_str(), ""), NULL);

  fail_unless(ud->getUnit(0)->getMultiplier() == 1);
  fail_unless(ud->getUnit(0)->getScale() == 0);
  fail_unless(ud->getUnit(0)->getExponent() == 1);
  fail_unless(ud->getUnit(0)->getOffset() == 0.0);
  fail_unless(ud->getUnit(0)->getKind() == UNIT_KIND_METRE);

   /* check an invalid node */
  delete ud;
  UnitDefinition * ud1 = new UnitDefinition(m->getLevel(), m->getVersion());
  ASTNode *node = new ASTNode(AST_PLUS);
  
  ud1 = uff->getUnitDefinition(node);

  fail_unless (ud1->getNumUnits() == 0);

  delete ud1;
  delete node;
}
Ejemplo n.º 26
0
/*
  * Constructs and returns a UnitDefinition that expresses the units of this 
  * LocalParameter.
  */
UnitDefinition *
LocalParameter::getDerivedUnitDefinition()
{
  /* if we have the whole model but it is not in a document
   * it is still possible to determine the units
   */
  Model * m = static_cast <Model *> (getAncestorOfType(SBML_MODEL));

  if (m != NULL)
  {
    if (!m->isPopulatedListFormulaUnitsData())
    {
      m->populateListFormulaUnitsData();
    }

    UnitDefinition *ud = NULL;
    const char * units = getUnits().c_str();
    if (!strcmp(units, ""))
    {
      ud   = new UnitDefinition(getSBMLNamespaces());
      return ud;
    }
    else
    {
      if (UnitKind_isValidUnitKindString(units, 
                                getLevel(), getVersion()))
      {
        Unit * unit = new Unit(getSBMLNamespaces());
        unit->setKind(UnitKind_forName(units));
        unit->initDefaults();
        ud   = new UnitDefinition(getSBMLNamespaces());
        
        ud->addUnit(unit);

        delete unit;
      }
      else
      {
        /* must be a unit definition */
        ud = static_cast <Model *> (getAncestorOfType(SBML_MODEL))->getUnitDefinition(units);
      }
      return ud;
    }
  }
  else
  {
    return NULL;
  }
}
END_TEST


START_TEST (test_CalcUnitDefinition_global_with_local_unknown)
{
  UnitDefinition *fud = m->getParameter("r")->getDerivedUnitDefinition();

  fail_unless(fud->getNumUnits() == 0);

  m->getParameter("r")->setCalculatingUnits(true);
  fud = m->getParameter("r")->getDerivedUnitDefinition();
  m->getParameter("r")->setCalculatingUnits(false);

  fail_unless(fud == NULL);
}
Ejemplo n.º 28
0
/*
 * Converts a "Dimensionless" Unit into the corresponding UnitDefinition
 * consisting only of SI units. This would include e.g. the Radian unit.
 * If the given unit does not have the correct kind, a NULL pointer is
 * returned.
 * It is up to the receiver to free the memory of the returned
 * UnitDefinition.
 * @param const Unit& unit
 * @return UnitDefinition* result
 */
LIBSBML_EXTERN
UnitDefinition* UnitConversionFactory::convertDimensionlessToSI(const Unit& unit)
{
    UnitKind_t uKind = unit.getKind();

    if (uKind != UNIT_KIND_DIMENSIONLESS && uKind != UNIT_KIND_ITEM && uKind != UNIT_KIND_RADIAN && uKind != UNIT_KIND_STERADIAN) return NULL;

    UnitDefinition* pUdef = new UnitDefinition(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION);
    Unit* pU = new Unit(unit);
    pU->setKind(UNIT_KIND_DIMENSIONLESS);
    pU->setOffset(0.0);
    pUdef->addUnit(pU);
    delete pU;
    return pUdef;
}
Ejemplo n.º 29
0
/*
  * Checks that the units of the delay function are consistent
  *
  * If inconsistent units are found, an error message is logged.
  */
void 
ArgumentsUnitsCheck::checkUnitsFromDelay (const Model& m, 
                                        const ASTNode& node, 
                                        const SBase & sb, bool inKL, int reactNo)
{
  /* check that node has two children */
  if (node.getNumChildren() != 2)
  {
    return;
  }

  if (!m.getSBMLNamespaces()->getNamespaces())
  {
#if 0
    cout << "[DEBUG] XMLNS IS NULL" << endl;
#endif
  }

  /* delay(x, t) 
   * no restrictions on units of x
   * but t must have units of time
   */
  UnitDefinition *time = new UnitDefinition(m.getSBMLNamespaces());
  Unit *unit = new Unit(m.getSBMLNamespaces());
  unit->setKind(UNIT_KIND_SECOND);
  unit->initDefaults();
  UnitDefinition * tempUD;
  time->addUnit(unit);
  
  UnitFormulaFormatter *unitFormat = new UnitFormulaFormatter(&m);

  tempUD = unitFormat->getUnitDefinition(node.getRightChild(), inKL, reactNo);
  
  if (!unitFormat->getContainsUndeclaredUnits())
  {
    if (!UnitDefinition::areEquivalent(time, tempUD)) 
    {
      logInconsistentDelay(node, sb);
    }
  }

  delete time;
  delete tempUD;
  delete unit;
  delete unitFormat;

  checkUnits(m, *node.getLeftChild(), sb, inKL, reactNo);
}
Ejemplo n.º 30
0
/*
 * The functions determins wether the given UnitDefinition contains only
 * units from the list given as the second argument.
 * @param const UnitDefinition& uDef
 * @param const ListOf& unitList
 * @return bool containsOnlyGivenUnits
 */
LIBSBML_EXTERN
bool UnitConversionFactory::containsOnlyGivenUnits(const UnitDefinition& uDef, const ListOf& unitList)
{
    bool result = true;
    UnitDefinition* pTmpUdef = UnitConversionFactory::convertToSI(uDef);

    if (pTmpUdef)
    {
        unsigned int i;
        unsigned int maxUnits = pTmpUdef->getNumUnits();

        for (i = 0; i < maxUnits; ++i)
        {
            Unit* pU = pTmpUdef->getUnit(i);
            UnitKind_t kind = pU->getKind();
            unsigned int j;
            unsigned int maxUnits2 = unitList.size();
            bool found = false;

            for (j = 0; j < maxUnits2; ++j)
            {
                const Unit* pU2 = dynamic_cast<const Unit*>(unitList.get(j));

                if (!pU2) break;

                if (pU2->getKind() == kind)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                result = false;
                break;
            }
        }

        delete pTmpUdef;
    }
    else
    {
        result = false;
    }

    return result;
}