boost::optional<model::ModelObject> ReverseTranslator::translateShadowCalculation(
    const WorkspaceObject& workspaceObject)
{
  OS_ASSERT(workspaceObject.iddObject().type() == IddObjectType::ShadowCalculation);

  ShadowCalculation shadowCalculation = m_model.getUniqueModelObject<ShadowCalculation>();

  OptionalInt i = workspaceObject.getInt(ShadowCalculationFields::CalculationFrequency);
  if (i) {
    shadowCalculation.setCalculationFrequency(*i);
  }

  i = workspaceObject.getInt(ShadowCalculationFields::MaximumFiguresinShadowOverlapCalculations);
  if (i) {
    shadowCalculation.setMaximumFiguresInShadowOverlapCalculations(*i);
  }

  OptionalString s = workspaceObject.getString(ShadowCalculationFields::PolygonClippingAlgorithm);
  if (s && !s->empty()) {
    shadowCalculation.setPolygonClippingAlgorithm(*s);
  }

  s = workspaceObject.getString(ShadowCalculationFields::SkyDiffuseModelingAlgorithm);
  if (s && !s->empty()) {
    shadowCalculation.setSkyDiffuseModelingAlgorithm(*s);
  }

  return shadowCalculation.cast<ModelObject>();
}
TEST_F(IdfFixture, IdfObject_NameGetterWithReturnDefaultOption) {
  // OBJECT WITH DEFAULT NAME
  std::stringstream text;
  text << "Building," << std::endl
    << "," << std::endl
    << "," << std::endl
    << "," << std::endl
    << "," << std::endl
    << "," << std::endl
    << "," << std::endl
    << ";";
  OptionalIdfObject oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  IdfObject object = *oObj;
  OptionalString name = object.name();
  ASSERT_TRUE(name);
  EXPECT_TRUE(name->empty());
  name = object.name(true);
  ASSERT_TRUE(name);
  EXPECT_EQ("NONE",*name);
  object.setName("MyBuilding");
  name = object.name();
  ASSERT_TRUE(name);
  EXPECT_EQ("MyBuilding",*name);
  OptionalString name2 = object.name(true);
  ASSERT_TRUE(name2);
  EXPECT_EQ(*name,*name2);
}
  void WeatherFileFinder::extractDetails(
      const IdfFile &t_idffile,
      ToolVersion &t_version,
      boost::optional<std::string> &t_filelocationname,
      boost::optional<std::string> &t_weatherfilename)
  {
    IdfObjectVector versionObjects = t_idffile.getObjectsByType(IddObjectType::Version);
    if(versionObjects.size() == 1){
      OptionalString version = versionObjects[0].getString(VersionFields::VersionIdentifier, true);
      if (version){
        boost::regex majorMinorBuildRegex("^(\\d*?)\\.(\\d*?)\\.(\\d*?)$");
        boost::regex majorMinorRegex("^(\\d*?)\\.(\\d*?)$");
        boost::regex majorRegex("^(\\d*?)$");

        boost::smatch matches;
        if (boost::regex_search(*version, matches, majorMinorBuildRegex)){
          unsigned major = boost::lexical_cast<unsigned>(std::string(matches[1].first, matches[1].second));
          unsigned minor = boost::lexical_cast<unsigned>(std::string(matches[2].first, matches[2].second));
          unsigned build = boost::lexical_cast<unsigned>(std::string(matches[3].first, matches[3].second));
          t_version = ToolVersion(major, minor, build);
        }else if(boost::regex_search(*version, matches, majorMinorRegex)){
          unsigned major = boost::lexical_cast<unsigned>(std::string(matches[1].first, matches[1].second));
          unsigned minor = boost::lexical_cast<unsigned>(std::string(matches[2].first, matches[2].second));
          t_version = ToolVersion(major, minor);
        }else if(boost::regex_search(*version, matches, majorRegex)){
          unsigned major = boost::lexical_cast<unsigned>(std::string(matches[1].first, matches[1].second));
          t_version = ToolVersion(major);
        }
      }
    }

    IdfObjectVector locationObjects = t_idffile.getObjectsByType(IddObjectType::Site_Location);
    if(locationObjects.size() == 1){
      OptionalString locationname = locationObjects[0].getString(Site_LocationFields::Name, true);
      if (locationname && !locationname->empty()){
        LOG(Debug, "Location name field from IDF: " << *locationname);
        t_filelocationname = locationname;
      }
    }

    std::string header = t_idffile.header();
    std::vector<std::string> headerlines;
    boost::split(headerlines, header, boost::is_any_of("\n\r"), boost::algorithm::token_compress_on);
    for (std::vector<std::string>::const_iterator itr = headerlines.begin();
         itr != headerlines.end();
         ++itr)
    {
      boost::smatch matches;
     
      if (boost::regex_match(*itr, matches, boost::regex(".*!\\s+WeatherFileName=(.*)"),boost::regex_constants::match_all))
      {
        std::string match = matches[1];
        t_weatherfilename = match;
        break;
      }
    }
  }
Example #4
0
TEST_F(ModelFixture, ComponentWatcher_BadComponentDataFromWorkspace) {
  Workspace ws;
  OptionalWorkspaceObject owo = ws.addObject(IdfObject(IddObjectType::OS_ComponentData));
  ASSERT_TRUE(owo);
  // make component data ok except points to non-existent object
  WorkspaceObject cd = *owo;
  OptionalString oName = cd.name(); // should have been set by constructor
  ASSERT_TRUE(oName);
  EXPECT_FALSE(oName->empty());
  cd.setString(OS_ComponentDataFields::UUID,toString(createUUID()));
  cd.setString(OS_ComponentDataFields::VersionUUID,toString(createUUID()));
  StringVector values;
  values.push_back("My Material");
  IdfExtensibleGroup eg = cd.pushExtensibleGroup(values);
  EXPECT_TRUE(eg.empty()); // Cannot register a bad pointer.

  EXPECT_EQ(1u,ws.numObjects());
  Model model(ws);
  // expect ComponentWatcher creation to kick out ComponentData
  EXPECT_EQ(0u,model.numObjects());
}
TEST_F(IdfFixture, IdfObject_StringFieldGetterWithReturnDefaultOption) {
  // NON-EXTENSIBLE OBJECT
  std::stringstream text;
  text << "Refrigeration:Condenser:AirCooled," << std::endl
       << "  MyCondenser," << std::endl
       << "  ," << std::endl
       << "  ," << std::endl // default is 0.0
       << "  ," << std::endl // default is "Fixed"
       << "  125.0;";        // default is 250.0
                             // default is 0.2
                             //
                             // default is "General"
                             // default is 0.0
                             // default is 0.0
                             // default is 0.0
  OptionalIdfObject oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  IdfObject object = *oObj;

  // returns set values
  OptionalString idfField = object.getString(0,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("MyCondenser",*idfField);
  idfField = object.getString(1,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("",*idfField);
  idfField = object.getString(4,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("125.0",*idfField);

  // returns default for fields behind fields with set values
  idfField = object.getString(2,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("0.0",*idfField);
  idfField = object.getString(3,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("Fixed",*idfField);

  // returns default for non-existent fields
  idfField = object.getString(6,true);
  EXPECT_FALSE(idfField);
  idfField = object.getString(7,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("General",*idfField);
  idfField = object.getString(8,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("0.0",*idfField);
  idfField = object.getString(10,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("0.0",*idfField);
  idfField = object.getString(11,true);
  EXPECT_FALSE(idfField);

  // EXTENSIBLE OBJECT
  text.str("");
  text << "DaylightingDevice:Tubular," << std::endl
       << "  MyTDD," << std::endl
       << "  MyDome," << std::endl
       << "  MyDiffuser," << std::endl
       << "  MyConstruction," << std::endl
       << "  1.0," << std::endl
       << "  2.0;";
       // \default 0.28
       // Transition Zone 1 Name
       // Transition Zone 1 Length
       // ... (extensible 2)
  oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  object = *oObj;
  EXPECT_EQ(6u,object.numFields());

  // returns set values
  idfField = object.getString(0,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("MyTDD",*idfField);
  idfField = object.getString(5,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("2.0",*idfField);

  // returns default for non-existent, non-extensible fields
  idfField = object.getString(6,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("0.28",*idfField);
  EXPECT_EQ(6u,object.numFields());
  idfField = object.getString(6);
  EXPECT_FALSE(idfField);

  StringVector newGroup;
  newGroup.push_back("MyFirstTransistionZone");
  newGroup.push_back("1.5");
  ASSERT_FALSE(object.pushExtensibleGroup(newGroup).empty());

  // returns default for fields behind fields with set values
  idfField = object.getString(6,true);
  ASSERT_TRUE(idfField);
  EXPECT_EQ("0.28",*idfField);
  idfField = object.getString(6);
  ASSERT_TRUE(idfField);
  EXPECT_TRUE(idfField->empty());

  // return evaluates to false for extensible fields that do not exist
  idfField = object.getString(10);
  EXPECT_FALSE(idfField);
}
TEST_F(IdfFixture, IdfObject_DefaultFieldComments) {
  // OBJECT WITH NO FIELD COMMENTS
  std::stringstream text;
  text << "  Schedule:Day:Interval," << std::endl
       << "    A Schedule," << std::endl
       << "    Any Number," << std::endl
       << "    ," << std::endl
       << "    09:00," << std::endl
       << "    0," << std::endl
       << "    22:00," << std::endl
       << "    1," << std::endl
       << "    24:00," << std::endl
       << "    0;";
  OptionalIdfObject oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  IdfObject object = *oObj;
  // non-extensible fields
  OptionalString fc = object.fieldComment(0,true);
  OptionalIddField iddField = object.iddObject().getField(0);
  ASSERT_TRUE(fc); ASSERT_TRUE(iddField);
  EXPECT_EQ(makeIdfEditorComment(iddField->name()),*fc);
  fc = object.fieldComment(0);
  ASSERT_TRUE(fc); EXPECT_TRUE(fc->empty());

  fc = object.fieldComment(2,true);
  iddField = object.iddObject().getField(2);
  ASSERT_TRUE(fc); ASSERT_TRUE(iddField);
  EXPECT_EQ(makeIdfEditorComment(iddField->name()),*fc);
  fc = object.fieldComment(2);
  ASSERT_TRUE(fc); EXPECT_TRUE(fc->empty());

  // extensible fields
  fc = object.fieldComment(6,true);
  iddField = object.iddObject().getField(6);
  ASSERT_TRUE(fc); ASSERT_TRUE(iddField);
  EXPECT_EQ(makeIdfEditorComment(iddField->name()) + " 2",*fc);
  fc = object.fieldComment(6);
  ASSERT_TRUE(fc); EXPECT_TRUE(fc->empty());

  // non-existant fields
  fc = object.fieldComment(14,true);
  EXPECT_FALSE(fc);

  // OBJECT WITH SOME FIELD COMMENTS
  text.str("");
  text << "  Schedule:Day:Interval," << std::endl
       << "    A Schedule," << std::endl
       << "    Any Number," << std::endl
       << "    ," << std::endl
       << "    09:00, ! opening time" << std::endl
       << "    0," << std::endl
       << "    22:00, ! closing time" << std::endl
       << "    1," << std::endl
       << "    24:00," << std::endl
       << "    0;";
  oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  object = *oObj;
  // returns set values
  fc = object.fieldComment(3,true);
  ASSERT_TRUE(fc);
  EXPECT_EQ("! opening time",*fc);
  fc = object.fieldComment(5,true);
  ASSERT_TRUE(fc);
  EXPECT_EQ("! closing time",*fc);

  // returns default for fields behind fields with set values
  fc = object.fieldComment(4,true);
  iddField = object.iddObject().getField(4);
  ASSERT_TRUE(fc); ASSERT_TRUE(iddField);
  EXPECT_EQ(makeIdfEditorComment(iddField->name()) + " 1",*fc);
  fc = object.fieldComment(6);
  ASSERT_TRUE(fc); EXPECT_TRUE(fc->empty());

  // returns default for fields past fields with set values
  fc = object.fieldComment(6,true);
  iddField = object.iddObject().getField(6);
  ASSERT_TRUE(fc); ASSERT_TRUE(iddField);
  EXPECT_EQ(makeIdfEditorComment(iddField->name()) + " 2",*fc);
  fc = object.fieldComment(6);
  ASSERT_TRUE(fc); EXPECT_TRUE(fc->empty());

}
TEST_F(IdfFixture, IdfObject_CommentGettersAndSetters) {
  // DEFAULT OBJECT COMMENTS
  IdfObject object(IddObjectType::Zone);
  // exist? if so, are comments according to regex?
  std::string str = object.comment();
  if (!str.empty()) { EXPECT_EQ(str,makeComment(str)); }
  unsigned n = object.numFields();
  for (unsigned i = 0; i < n; ++i) {
    OptionalString oStr = object.fieldComment(i);
    if (oStr && !oStr->empty()) {
      EXPECT_EQ(*oStr,makeComment(*oStr));
    }
  }

  // comment setter works (already comments, and needs comment character prepended)
  str = "! New object comment";
  object.setComment(str);
  EXPECT_EQ(str,object.comment());
  str = "\n\nThis is my object.\n";
  object.setComment(str);
  str = "\n\n! This is my object.\n";
  EXPECT_EQ(str,object.comment());

  // field comment setter works for valid indices
  for (int i = n-1; i >= 0; --i) {
    std::stringstream ss;
    ss << "Field " << i; str = ss.str(); ss.str("");
    EXPECT_TRUE(object.setFieldComment(i,str));
    ss << "! " << str;
    EXPECT_EQ(ss.str(),object.fieldComment(i));
  }

  // field comment setter returns false, does not crash for invalid indices
  EXPECT_FALSE(object.setFieldComment(n+3,str));

  // TEXT OBJECT COMMENTS
  std::stringstream text;
  text << "  Branch," << std::endl
       << "    Central Chiller Branch," << std::endl
       << "    0,                       !- Maximum Flow Rate {m3/s}" << std::endl
       << "    ," << std::endl
       << "    Chiller:Electric, ! i think we have an electric chiller" << std::endl
       << "    Central Chiller," << std::endl
       << "    Central Chiller Inlet Node," << std::endl
       << "    Central Chiller Outlet Node," << std::endl
       << "    Active;";
  OptionalIdfObject oObj = IdfObject::load(text.str());
  ASSERT_TRUE(oObj);
  object = *oObj;
  // field comments setter properly extends field comments vector as needed
  OptionalString optStr = object.fieldComment(0); 
  ASSERT_TRUE(optStr);
  EXPECT_EQ("",*optStr);
  optStr = object.fieldComment(1); ASSERT_TRUE(optStr);
  EXPECT_EQ("",*optStr); // auto-generated comments are stripped
  optStr = object.fieldComment(3);
  ASSERT_TRUE(optStr);
  EXPECT_EQ("! i think we have an electric chiller",*optStr);
  EXPECT_TRUE(object.setFieldComment(5,"my comment"));
  optStr = object.fieldComment(5); ASSERT_TRUE(optStr);
  EXPECT_EQ("! my comment",*optStr);
  optStr = object.fieldComment(4); ASSERT_TRUE(optStr);
  EXPECT_EQ("",*optStr);
  optStr = object.fieldComment(6); ASSERT_TRUE(optStr); EXPECT_TRUE(optStr->empty());

  // field comments setter returns false, does not crash if exceed number of fields.
  EXPECT_FALSE(object.setFieldComment(10,"hi"));

}