[Avida-SVN] r1395 - in branches/uml/source: cpu main

hjg at myxo.css.msu.edu hjg at myxo.css.msu.edu
Sat Mar 10 12:22:41 PST 2007


Author: hjg
Date: 2007-03-10 15:22:41 -0500 (Sat, 10 Mar 2007)
New Revision: 1395

Modified:
   branches/uml/source/cpu/cHardwareCPU.cc
   branches/uml/source/main/cOrganism.cc
   branches/uml/source/main/cOrganism.h
Log:
template function refactor



Modified: branches/uml/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/uml/source/cpu/cHardwareCPU.cc	2007-03-09 17:48:15 UTC (rev 1394)
+++ branches/uml/source/cpu/cHardwareCPU.cc	2007-03-10 20:22:41 UTC (rev 1395)
@@ -3387,35 +3387,34 @@
 	
 	int reg_used = FindModifiedRegister(REG_AX);
 	
+	int jump_amount = 1;
+	
 	switch (reg_used){
 	case 0:
-		// increment the triggers vector index
-		organism->nextTrigger();
+		// decrement the triggers vector index
+		organism->jumpTrigger(jump_amount);
 		break;
 	case 1:
-		// increment the guards vector index
-		organism->nextGuard();
+		// decrement the guards vector index
+		organism->jumpGuard(jump_amount);
 		break;
 	case 2:
-		// increment the actions vector index
-		organism->nextAction();
+		// decrement the actions vector index
+		organism->jumpAction(jump_amount);
 		break;
 	case 3:
-		// increment the transition labels index
-		organism->nextTransitionLabel(); 
-		break;
+		// decrement the transition labels index
+		organism->jumpTransitionLabel(jump_amount);
+		break;	
 	case 4:
-		// increment the origin state index
-		organism->nextOriginState();
+		// decrement the original state index
+		organism->jumpOriginState(jump_amount);
 		break;
 	case 5:
-		// increment the destination state index
-		organism->nextDestinationState();
+		// decement the destination state index
+		organism->jumpDestinationState(jump_amount);
 		break;
-   // default:
-		// we should never get here...
 	}
-	
 	return true;
 }
  
@@ -3423,33 +3422,33 @@
 {
 	int reg_used = FindModifiedRegister(REG_AX);
 	
+	int jump_amount = -1;
+		
 	switch (reg_used){
 	case 0:
 		// decrement the triggers vector index
-		organism->prevTrigger();
+		organism->jumpTrigger(jump_amount);
 		break;
 	case 1:
 		// decrement the guards vector index
-		organism->prevGuard();
+		organism->jumpGuard(jump_amount);
 		break;
 	case 2:
 		// decrement the actions vector index
-		organism->prevAction();
+		organism->jumpAction(jump_amount);
 		break;
 	case 3:
 		// decrement the transition labels index
-		organism->prevTransitionLabel();
+		organism->jumpTransitionLabel(jump_amount);
 		break;	
 	case 4:
 		// decrement the original state index
-		organism->prevOriginState();
+		organism->jumpOriginState(jump_amount);
 		break;
 	case 5:
 		// decement the destination state index
-		organism->prevDestinationState();
+		organism->jumpDestinationState(jump_amount);
 		break;
-   // default:
-		// we should never get here...
 	}
 	return true;
 }
@@ -3460,7 +3459,6 @@
 	const int reg_jump = FindModifiedRegister(REG_BX);
 	int jump_amount = GetRegister(reg_jump);
 
-//	int jump_amount = 
 	
 	switch (reg_used){
 	case 0:
@@ -3487,8 +3485,6 @@
 		// decement the destination state index
 		organism->jumpDestinationState(jump_amount);
 		break;
-   // default:
-		// we should never get here...
 	}
 	return true;
 }

Modified: branches/uml/source/main/cOrganism.cc
===================================================================
--- branches/uml/source/main/cOrganism.cc	2007-03-09 17:48:15 UTC (rev 1394)
+++ branches/uml/source/main/cOrganism.cc	2007-03-10 20:22:41 UTC (rev 1395)
@@ -128,9 +128,9 @@
   action_index = 0;
   guard_index = 0;
   trans_label_index = 0;
-  orig_state_index = -1;
-  dest_state_index = -1;
-  total_states = 0;
+  orig_state_index = 0;
+  dest_state_index = 0;
+//  total_states = 0;
 }
 
 
@@ -513,13 +513,31 @@
     m_hardware->ProcessBonusInst(ctx, cInstruction(cur_inst) );
   }
   
-//  m_world->GetStats().UpdateModelStats(uml_state_diagram);
-	m_world->GetStats().addState(total_states);
+	m_world->GetStats().addState(states.size());
 	m_world->GetStats().addTrans(transitions.size());
 	m_world->GetStats().addTransLabel(transition_labels.size());
   
 }
 
+void cOrganism::seedModel() {
+	std::string data, line; 
+	std::ifstream infile;
+	infile.open("instinctModel");
+	assert(infile.is_open());
+	
+	while (getline (infile, line))
+	{
+		data.append(line);
+		line.erase();
+	}
+	
+	//read from file; load into string/strstream, and return it.
+	
+	//return data;
+	return;
+
+}
+
 bool cOrganism::findTransLabel(transition_label t) { 
 	for(std::vector<transition_label>::iterator i=transition_labels.begin(); i!=transition_labels.end(); ++i){
 		if ((i->trigger == t.trigger) && (i->guard == t.guard) && (i->action == t.action)) {
@@ -571,299 +589,55 @@
 }
 
 
-
-
-// For all of the next* functions
-// increment the index. If the index points to the end of the vector, it should then point to 
-// the beginning of the vector.
-
-
-bool cOrganism::nextTrigger()
+template <typename T>
+bool cOrganism::moveIndex (T x, int &index, int amount )
 {
-	if (triggers.size() == 0) {
+	if (x.size() == 0) {
 		return false;
 	}
-
-	trigger_index++;
-	if (trigger_index >= triggers.size()) {
-		trigger_index = 0;
-	}
-	return true;
-
-}
-
-bool cOrganism::nextGuard()
-{
-	if (guards.size() == 0) {
-		return false;
-	}
 	
-	guard_index++;
-	if (guard_index >= guards.size()) {
-		guard_index = 0;
-	}
-	
+	if (amount > 0) { 
+		index += (amount % x.size());
+		// index is greater than vector
+		if (index >= x.size()) { 
+			index -= x.size();
+		} else if(index < 0) { 
+			index += x.size();
+		}
+	}	
+		
 	return true;
 }
 
-bool cOrganism::nextAction()
-{	
-	if (actions.size() == 0) {
-		return false;
-	}
-	
-	action_index++;
-	if (action_index >= actions.size()) {
-		action_index = 0;
-	}
-	
-	return true;
-}
 
-/*  int orig_state_index;
-  int dest_state_index;
-  int trans_label_index;*/
-  
-bool cOrganism::nextTransitionLabel()
-{
-	if (transition_labels.size() == 0) {
-		return false;
-	}
-	trans_label_index++;
-	if (trans_label_index >= transition_labels.size()) {
-		trans_label_index = 0;
-	}
-	
-	return true;
-}
-
-bool cOrganism::nextOriginState()
-{
-	if (total_states == 0) {
-		return false;
-	}
-	orig_state_index++;
-	if (orig_state_index >= total_states) {
-		orig_state_index = 0;
-	}
-	
-	return true;
-}
-
-
-bool cOrganism::nextDestinationState()
-{
-	if (total_states == 0) {
-		return false;
-	}
-	dest_state_index++;
-	if (dest_state_index >= total_states) {
-		dest_state_index = 0;
-	}
-	return true;
-}
-
-
-bool cOrganism::prevTrigger()
-{
-	if (triggers.size() == 0) {
-		return false;
-	}
-	
-	if (trigger_index <= 0) {
-		trigger_index = triggers.size();
-	}
-	trigger_index--;
-	
-	return true;
-}
-
-
-
-bool cOrganism::prevGuard()
-{
-	if (guards.size() == 0) {
-		return false;
-	}
-	
-	if (guard_index <= 0) {
-		guard_index = guards.size();
-	}
-	guard_index--;
-	
-	return true;
-}
-
-bool cOrganism::prevAction()
-{
-	if (actions.size() == 0) {
-		return false;
-	}
-	if (action_index <= 0) {
-		action_index = actions.size();
-	}
-	action_index--;
-	
-	return true;
-}
-
-bool cOrganism::prevTransitionLabel()
-{
-	if (transition_labels.size() == 0) {
-		return false;
-	}
-	if (trans_label_index <= 0) {
-		trans_label_index = transition_labels.size();
-	}
-	trans_label_index--;
-	
-	return true;
-}
-
-
-bool cOrganism::prevOriginState() 
-{
-	if (total_states == 0) {
-		return false;
-	}
-	if (orig_state_index <= 0) {
-		orig_state_index = total_states;
-	} 
-	orig_state_index--;
-	
-	return true;
-}
-
-bool cOrganism::prevDestinationState() 
-{
-	if (total_states == 0) {
-		return false;
-	}
-	if (dest_state_index <= 0) {
-		dest_state_index = total_states;
-	} 
-	dest_state_index--;
-	
-	return true;
-}
-
-
 bool cOrganism::jumpTrigger(int jump_amount)
 {
-	if (triggers.size() == 0) {
-		return false;
-	}
-	
-	if (jump_amount > 0) { 
-		trigger_index = trigger_index + (jump_amount % triggers.size());
-		// index is greater than vector
-		if (trigger_index >= triggers.size()) { 
-			trigger_index -= triggers.size();
-		} else if(trigger_index < 0) { 
-			trigger_index += triggers.size();
-		}
-	}	
-	
-/*	
-	if (trigger_index <= 0) {
-		trigger_index = triggers.size();
-	}
-	trigger_index--;*/
-	
-	return true;
+	return moveIndex(triggers, trigger_index, jump_amount);
 }
 
-
-
 bool cOrganism::jumpGuard(int jump_amount)
 {
-	if (guards.size() == 0) {
-		return false;
-	}
-	
-	if (jump_amount > 0) { 
-		guard_index = guard_index + (jump_amount % guards.size());
-		// index is greater than vector
-		if (guard_index >= guards.size()) { 
-			guard_index -= guards.size();
-		} else if(guard_index < 0) { 
-			guard_index += guards.size();
-		}
-	}	
-	
-	return true;
+	return moveIndex(guards, guard_index, jump_amount);	
 }
 
 bool cOrganism::jumpAction(int jump_amount)
 {
-	int act_size = actions.size();
-	if (actions.size() == 0) {
-		return false;
-	}
-	if (jump_amount > 0) { 
-		action_index = action_index + (jump_amount % actions.size());
-		// index is greater than vector
-		if (action_index >= actions.size()) { 
-			action_index -= actions.size();
-		} else if(action_index < 0) { 
-			action_index += actions.size();
-		}
-	}		
-	return true;
+	return moveIndex(actions, action_index, jump_amount);
 }
 
 bool cOrganism::jumpTransitionLabel(int jump_amount)
 {
-	if (transition_labels.size() == 0) {
-		return false;
-	}
-	if (jump_amount > 0) { 
-		trans_label_index = trans_label_index + (jump_amount % transition_labels.size());
-		// index is greater than vector
-		if (trans_label_index >= transition_labels.size()) { 
-			trans_label_index -= transition_labels.size();
-		} else if(trans_label_index < 0) { 
-			trans_label_index += transition_labels.size();
-		}
-	}	
-	
-	return true;
+	return moveIndex(transition_labels, trans_label_index, jump_amount);
 }
 
-
 bool cOrganism::jumpOriginState(int jump_amount) 
 {
-	if (total_states == 0) {
-		return false;
-	}
-	if (jump_amount > 0) { 
-		orig_state_index = orig_state_index + (jump_amount % total_states);
-		// index is greater than vector
-		if (orig_state_index >= total_states) { 
-			orig_state_index -= total_states;
-		} else if(orig_state_index < 0) { 
-			orig_state_index += total_states;
-		}
-	}		
-	return true;
+	return moveIndex(states, orig_state_index, jump_amount);
 }
 
 bool cOrganism::jumpDestinationState(int jump_amount) 
 {
-	if (total_states == 0) {
-		return false;
-	}
-	if (jump_amount > 0) { 
-		dest_state_index = dest_state_index + (jump_amount % total_states);
-		// index is greater than vector
-		if (dest_state_index >= total_states) { 
-			dest_state_index -= total_states;
-		} else if(dest_state_index < 0) { 
-			dest_state_index += total_states;
-		}
-	}	
-	
-	return true;
+	return moveIndex(states, dest_state_index, jump_amount);
 }
 
 std::string cOrganism::getTrigger()
@@ -911,16 +685,9 @@
 // State manipulation
 bool cOrganism::addState()
 {
-	total_states++;
-	// if this is the first state, point the orig and dest state indices to it
-	if (total_states == 1) {
-		orig_state_index = 0;
-		dest_state_index = 0;
-	}
-	
-	// Move the destination state to the most recently created.
-	dest_state_index = total_states - 1;
-	
+	states.push_back(states.size());
+	dest_state_index = states.size() - 1;
+		
 	return true;
 }
 
@@ -933,9 +700,9 @@
 	t.action = getAction();
 	
 	// no dupes
-/*	if (findTransLabel(t)){
+	if (findTransLabel(t)){
 		return false;
-	 }*/
+	 }
 	
 	transition_labels.push_back(t);
 	
@@ -948,7 +715,8 @@
 
 bool cOrganism::addTransition()
 {
-	if ((total_states == 0) || (transition_labels.size() == 0)) {
+	if ((states.size() == 0) || (transition_labels.size() == 0)) {
+
 		return false;
 	} 
 
@@ -958,9 +726,9 @@
 	t.trans = getTransLabel();
 	
 	// no dupes
-/*    if (findTrans(t)) {
+    if (findTrans(t)) {
 		return false;
-	}*/
+	}
 
 	transitions.push_back(t);
 		
@@ -970,7 +738,7 @@
 
 int cOrganism::numStates()
 {
-	return total_states;
+	return states.size();
 }
 
 int cOrganism::numTrans()
@@ -999,27 +767,12 @@
 
 void cOrganism::printXMI()
 {
-/*	Graph::vertex_iterator i, iend;
-	Graph::edge_iterator e, eend;
-	
-	int trans_label;
-	int dest_state;
 	std::string temp, temp1, temp2;
-	int tempint;
-	
-	InitTransForMSXMI();
-*/
 
-	std::string temp, temp1, temp2;
-
 	int s_count = 0;
 	int t_count = 0;
 	xmi = "";
-	// loop through all states
-	// print initial information for the init state.
 
-//	tie(i, iend) = vertices(uml_state_diagram);
-	
 	if (numStates() > 0) {
 		temp = StringifyAnInt(s_count);
 		xmi += "<UML:Pseudostate xmi.id=\"s" + temp + "\" kind=\"initial\" outgoing=\"\" name=\"s";
@@ -1049,7 +802,6 @@
 
 	for (t_count = 0; t_count < numTrans(); ++t_count) { 
 		// info determined from the trans itself....
-//		trans_label = ;
 		temp = "t" + StringifyAnInt(t_count);
 		temp1 = "s" + StringifyAnInt(transitions[t_count].orig_state);
 		temp2 = "s" + StringifyAnInt(transitions[t_count].dest_state);
@@ -1058,10 +810,6 @@
 		xmi+= "<UML:Transition xmi.id=\"" + temp + "\"";
 		xmi+= " source=\"" + temp1 + "\"";
 		xmi += " target=\"" + temp2 + "\" name=\"\" isSpecification=\"false\">\n";
-		
-		// Generate transition guts here...
-		//temp = transGuardActionInfo[trans_label];
-		//xmi += temp;
 
 		// Get guard, trigger, and action
 		temp = transitions[t_count].trans.trigger;
@@ -1076,22 +824,7 @@
 			xmi+= "</UML:Namespace.ownedElement> </UML:Namespace> </UML:ModelElement.namespace> ";
 			xmi+= "</UML:Event>  </UML:Transition.trigger> ";
 		}
-
-		/* // Trigger stuff 
-				<UML:Transition.trigger>                                        
-                                                      <UML:Event>                                            
-                                                      <UML:ModelElement.namespace>                                                
-                                                      <UML:Namespace>                                                    
-                                                      <UML:Namespace.ownedElement>                                                        
-                                                      <UML:CallEvent xmi.id="XDE-592D2425-ABF8-44DD-A306-97EC22A45B69" operation="XDE-7C41CD1F-6E52-4E32-9C8E-999BA1919EC6" name="getTempData" isSpecification="false"/>                                                   
-                                                       </UML:Namespace.ownedElement>                                                
-                                                       </UML:Namespace>                                            
-                                                       </UML:ModelElement.namespace>                                        
-                                                       </UML:Event>                                    
-                                                       </UML:Transition.trigger>             
 		
-		*/
-
 		// print guard, if any
 		// Note: for guard to work, '<' => '&lt'
 		if (temp1 != ""){
@@ -1100,18 +833,6 @@
 			xmi+= "</UML:Guard.expression> </UML:Guard> </UML:Transition.guard> ";
 		}
 		
-		/* // Guard stuff
-		<UML:Transition.guard>
-                                        <UML:Guard>
-                                            <UML:Guard.expression>
-                                                <UML:BooleanExpression body="brightnessValue&lt;0" language=""/>
-                                            </UML:Guard.expression>
-                                        </UML:Guard>
-                                    </UML:Transition.guard>
-
-		
-		*/
-		
 		// print action, if any
 		if (temp2 != "") { 
 			xmi+= "<UML:Transition.effect> <UML:UninterpretedAction xmi.id=\"\" ";
@@ -1120,17 +841,6 @@
 			xmi+= temp2 + "\"/> </UML:Action.script> </UML:UninterpretedAction> </UML:Transition.effect> ";		
 		}
 		
-		/* // Action stuff
-		<UML:Transition.effect>
-                                        <UML:UninterpretedAction xmi.id="XDE-0B7A10EB-A9FC-4DE8-BBF1-AF1C9A970E7F" isAsynchronous="false" name="" isSpecification="false">
-                                            <UML:Action.script>
-                                                <UML:ActionExpression language="" body="correctedBrightnessValue:=0"/>
-                                            </UML:Action.script>
-                                        </UML:UninterpretedAction>
-                                    </UML:Transition.effect>
-		
-		*/
-
 		xmi += "</UML:Transition>\n";
 
 	

Modified: branches/uml/source/main/cOrganism.h
===================================================================
--- branches/uml/source/main/cOrganism.h	2007-03-09 17:48:15 UTC (rev 1394)
+++ branches/uml/source/main/cOrganism.h	2007-03-10 20:22:41 UTC (rev 1395)
@@ -17,16 +17,8 @@
 #include <set>
 #include <string>
 #include <vector>
-//#include <boost/config.hpp>
-//#include <boost/graph/edge_list.hpp>
-//#include <boost/graph/adjacency_list.hpp>
-//#include <boost/graph/graph_utility.hpp>
-//#include <boost/graph/filtered_graph.hpp>
-//#include <boost/graph/connected_components.hpp>
-//using namespace boost;
 
 
-
 #ifndef cCPUMemory_h
 #include "cCPUMemory.h"
 #endif
@@ -86,8 +78,6 @@
 class cEnvironment;
 class cCodeLabel;
 
-// 
-
 struct transition_label { 
 	std::string trigger;
 	std::string guard;
@@ -148,14 +138,14 @@
   static std::string xmi_begin; // what comes before the organism generated xmi
   std::string xmi; // the xmi created by pretty-printing the diagram generated by the organism
   static std::string xmi_end; // what comes after the organism generated xmi
-  										
+  									
+  std::vector<int> states;											
   std::vector<std::string> triggers;
   std::vector<std::string> guards;
   std::vector<std::string> actions;
   std::vector<transition> transitions;
   std::vector<transition_label> transition_labels;
   
-  int total_states;
   int orig_state_index;
   int dest_state_index;
   int trans_label_index;
@@ -233,31 +223,21 @@
   // UML Stuff
   void modelCheck(cAvidaContext& ctx);   // evaluate the model
   void printXMI();	 // print the XMI version of the model
-  std::string getXMI ();
+  std::string getXMI (); // get the xmi string (including beginning & end read from file.)
   std::string StringifyAnInt(int);
+  int numStates();
+  int numTrans();
+  void seedModel();
+  
+  // find functions
   bool findTrans(int, int); // find a transition between two states
   bool findTrans(int, int, std::string); // find a transition between two states with a specific label.
   bool findTrans(transition); // find a transition between two states with a specific label.
   bool findTransLabel(transition_label); // find a specific transition label
-  int numStates();
-  int numTrans();
 
-// The next functions increment the index of the various vectors.
-  bool nextTrigger();
-  bool prevTrigger();
-  bool nextGuard();
-  bool nextTransitionLabel();
-  bool nextOriginState();
-  bool nextDestinationState();
-  
-// The previous functions decrement the index of the various vectors.
-  bool prevGuard();
-  bool nextAction();
-  bool prevAction();
-  bool prevTransitionLabel();
-  bool prevOriginState();
-  bool prevDestinationState();
-  
+  template <typename T>
+  bool moveIndex (T x, int &y, int z);
+ 
 // The jump functions jump the index of the various vectors either forward (+ int) or backwards (- int)
   bool jumpGuard(int);
   bool jumpAction(int);




More information about the Avida-cvs mailing list