[Avida-SVN] r1244 - in development/source: main tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Feb 1 08:48:32 PST 2007


Author: brysonda
Date: 2007-02-01 11:48:32 -0500 (Thu, 01 Feb 2007)
New Revision: 1244

Modified:
   development/source/main/cEventList.cc
   development/source/main/cEventList.h
   development/source/main/cInstUtil.cc
   development/source/main/cInstUtil.h
   development/source/main/cPopulation.cc
   development/source/main/cWorld.cc
   development/source/tools/cFile.cc
Log:
Add check for success in loading event file, report and exit on failure.

Add check for success in loading a start creature file, reports and exits with error.  Warns if the supplied genome is zero length.   Start creature now also accepts '-' and empty string values, warning that no start creature was specified.   This can be useful in experiments where all organisms are inserted through events.

Modified: development/source/main/cEventList.cc
===================================================================
--- development/source/main/cEventList.cc	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cEventList.cc	2007-02-01 16:48:32 UTC (rev 1244)
@@ -68,18 +68,23 @@
   return false;
 }
 
-void cEventList::LoadEventFile(const cString& filename)
+bool cEventList::LoadEventFile(const cString& filename)
 {
   cInitFile event_file(filename);
   
+  if (!event_file.IsOpen()) return false;
+
   // Load in the proper event list and set it up.
   event_file.Load();
   event_file.Compress();
+  event_file.Close();
   
   // Loop through the line_list and change the lines to events.
   for (int line_id = 0; line_id < event_file.GetNumLines(); line_id++) {
     AddEventFileFormat(event_file.GetLine(line_id));
   }
+  
+  return true;
 }
 
 

Modified: development/source/main/cEventList.h
===================================================================
--- development/source/main/cEventList.h	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cEventList.h	2007-02-01 16:48:32 UTC (rev 1244)
@@ -156,7 +156,7 @@
   bool AddEventFileFormat(const cString& line);
 
 
-  void LoadEventFile(const cString& filename);
+  bool LoadEventFile(const cString& filename);
 
   void Process(cAvidaContext& ctx);   // Go through list executing appropriate events.
   void Sync(); // Get all events caught up.

Modified: development/source/main/cInstUtil.cc
===================================================================
--- development/source/main/cInstUtil.cc	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cInstUtil.cc	2007-02-01 16:48:32 UTC (rev 1244)
@@ -26,11 +26,21 @@
 
 cGenome cInstUtil::LoadGenome(const cString& filename, const cInstSet& inst_set)
 {
-    cInitFile input_file(filename);
-  if (!input_file.IsOpen()) {
-    cerr << "Cannot open file: " << filename << endl;
-    return cGenome(0);
+  cGenome new_genome(0);
+  if (!LoadGenome(filename, inst_set, new_genome)) {
+    cerr << "Error: Unable to load genome" << endl;
+    exit(1);
   }
+  return new_genome;
+}
+
+bool cInstUtil::LoadGenome(const cString& filename, const cInstSet& inst_set, cGenome& out_genome)
+{
+  cInitFile input_file(filename);
+  bool success = true;
+
+  if (!input_file.IsOpen()) return false;
+  
   input_file.Load();
   input_file.Compress();
   input_file.Close();
@@ -44,18 +54,17 @@
 
     if (new_genome[line_num] == cInstSet::GetInstError()) {
       // You're using the wrong instruction set!  YOU FOOL!
-      cerr << "Cannot load organism '" << filename << "'" << endl
-	   << "       Unknown line: " << cur_line
-	   << " (best match is '" << inst_set.FindBestMatch(cur_line) << "')"
-	   << endl;
-      exit(1);
+      if (success) {
+        cerr << "Error: Cannot load organism '" << filename << "'" << endl;
+        success = false;
+      }
+      cerr << "       Unknown line: " << cur_line << " (best match is '" << inst_set.FindBestMatch(cur_line) << "')" << endl;
     }
   }
 
-  if(new_genome.GetSize()==0)
-    cerr << "Warning: Genome size is 0!" << endl;
-
-  return new_genome;
+  if (new_genome.GetSize() == 0) cerr << "Warning: Genome size is 0!" << endl;
+  if (success) out_genome = new_genome;
+  return success;
 }
 
 cGenome cInstUtil::LoadInternalGenome(istream& fp, const cInstSet& inst_set)

Modified: development/source/main/cInstUtil.h
===================================================================
--- development/source/main/cInstUtil.h	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cInstUtil.h	2007-02-01 16:48:32 UTC (rev 1244)
@@ -34,6 +34,7 @@
   // the only thing in the file unless 'Internal' is in the function name
   // (Internal genomes must begin with a number that indicates genome length)
 
+  static bool LoadGenome(const cString &filename, const cInstSet &inst_set, cGenome& out_genome);
   static cGenome LoadGenome(const cString &filename, const cInstSet &inst_set);
   static cGenome LoadInternalGenome(std::istream & fp, const cInstSet &inst_set);
   static void SaveGenome(std::ostream& fp, const cInstSet & inst_set,

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cPopulation.cc	2007-02-01 16:48:32 UTC (rev 1244)
@@ -160,9 +160,20 @@
   
   // Load a clone if one is provided, otherwise setup start organism.
   if (m_world->GetConfig().CLONE_FILE.Get() == "-" || m_world->GetConfig().CLONE_FILE.Get() == "") {
-    cGenome start_org = cInstUtil::LoadGenome(m_world->GetConfig().START_CREATURE.Get(), world->GetHardwareManager().GetInstSet());
-    if (start_org.GetSize() != 0) Inject(start_org);
-    else cerr << "Warning: Zero length start organism, not injecting into initial population." << endl;
+    cGenome start_org(0);
+    const cString& filename = m_world->GetConfig().START_CREATURE.Get();
+    
+    if (filename != "-" && filename != "") {
+      if (!cInstUtil::LoadGenome(filename, world->GetHardwareManager().GetInstSet(), start_org)) {
+        cerr << "Error: Unable to load start creature" << endl;
+        exit(-1);
+      }
+      
+      if (start_org.GetSize() != 0) Inject(start_org);
+      else cerr << "Warning: Zero length start organism, not injecting into initial population." << endl;
+    } else {
+      cerr << "Warning: No start organism specified." << endl;
+    }
   } else {
     ifstream fp(m_world->GetConfig().CLONE_FILE.Get());
     LoadClone(fp);

Modified: development/source/main/cWorld.cc
===================================================================
--- development/source/main/cWorld.cc	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/main/cWorld.cc	2007-02-01 16:48:32 UTC (rev 1244)
@@ -69,8 +69,8 @@
   m_env = new cEnvironment(this);
   
   // Initialize the default environment...
-  if (m_env->Load(m_conf->ENVIRONMENT_FILE.Get()) == false) {
-    cerr << "Unable to load environment... aborting!" << endl;
+  if (!m_env->Load(m_conf->ENVIRONMENT_FILE.Get())) {
+    cerr << "Error: Unable to load environment" << endl;
     ExitAvida(-1);
   }
   
@@ -89,7 +89,10 @@
   
   // Setup Event List
   m_event_list = new cEventList(this);
-  m_event_list->LoadEventFile(m_conf->EVENT_FILE.Get());
+  if (!m_event_list->LoadEventFile(m_conf->EVENT_FILE.Get())) {
+    cerr << "Error: Unable to load events" << endl;
+    ExitAvida(-1);
+  }
   
   
   const bool revert_fatal = m_conf->REVERT_FATAL.Get() > 0.0;

Modified: development/source/tools/cFile.cc
===================================================================
--- development/source/tools/cFile.cc	2007-02-01 16:16:09 UTC (rev 1243)
+++ development/source/tools/cFile.cc	2007-02-01 16:48:32 UTC (rev 1244)
@@ -43,7 +43,7 @@
     else if (err_id == ENOENT) error_desc = "File or path not found";
 
     // Print the error.
-    cerr << "Unable to open file '" << _fname << "' : " << error_desc << endl;
+    cerr << "Error: Unable to open file '" << _fname << "' : " << error_desc << endl;
     return false;
   }
 




More information about the Avida-cvs mailing list