[Avida-SVN] r1392 - branches/uml/source/main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Thu Mar 8 12:31:03 PST 2007
Author: hjg
Date: 2007-03-08 15:31:02 -0500 (Thu, 08 Mar 2007)
New Revision: 1392
Modified:
branches/uml/source/main/cOrganism.cc
branches/uml/source/main/cOrganism.h
branches/uml/source/main/cStats.cc
branches/uml/source/main/cStats.h
branches/uml/source/main/cTaskLib.cc
branches/uml/source/main/cTaskLib.h
Log:
Modified logging to work w/ refactoring. Modified addtrans and addTransLabel to ignore dupes.
Modified: branches/uml/source/main/cOrganism.cc
===================================================================
--- branches/uml/source/main/cOrganism.cc 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cOrganism.cc 2007-03-08 20:31:02 UTC (rev 1392)
@@ -458,7 +458,7 @@
void cOrganism::modelCheck(cAvidaContext& ctx)
{
- printXMI(ctx);
+ printXMI();
assert(m_interface);
@@ -514,9 +514,22 @@
}
// m_world->GetStats().UpdateModelStats(uml_state_diagram);
+ m_world->GetStats().addState(total_states);
+ m_world->GetStats().addTrans(transitions.size());
+ m_world->GetStats().addTransLabel(transition_labels.size());
}
+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)) {
+// if (i->trigger == t.trigger) {
+ return true;
+ }
+ }
+ return false;
+}
+
bool cOrganism::findTrans(int orig, int dest)
{
for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
@@ -544,6 +557,22 @@
return false;
}
+bool cOrganism::findTrans(transition t)
+{
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if((i->orig_state == t.orig_state) && (i->dest_state == t.dest_state) &&
+ (i->trans.trigger == t.trans.trigger) && (i->trans.guard == t.trans.guard) &&
+ (i->trans.action == t.trans.action)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+
+
// 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.
@@ -899,8 +928,11 @@
t.guard = getGuard();
t.action = getAction();
+ if (findTransLabel(t)){
+ return false;
+ }
+
transition_labels.push_back(t);
-
return true;
}
@@ -915,6 +947,10 @@
t.orig_state = getOrigState();
t.dest_state = getDestState();
t.trans = getTransLabel();
+
+ if (findTrans(t)) {
+ return false;
+ }
transitions.push_back(t);
return true;
@@ -931,8 +967,164 @@
return transitions.size();
}
-void cOrganism::printXMI(cAvidaContext& ctx)
+// print the label. Change - signs to _
+std::string cOrganism::StringifyAnInt(int x) {
+
+ std::ostringstream o;
+
+ if (x < 0) {
+ x = abs(x);
+ o << "_";
+ }
+
+ o << x;
+ return o.str();
+}
+
+std::string cOrganism::getXMI()
{
+ return (xmi_begin + xmi + xmi_end);
+}
+
+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";
+ xmi += temp + "\" isSpecification=\"false\"/>\n";
+ ++s_count;
+ }
+
+
+ for (; s_count < numStates(); ++s_count) {
+ temp = "s" + StringifyAnInt(s_count);
+ xmi+="<UML:CompositeState xmi.id=\"";
+ xmi+=temp;
+ xmi+= "\" isConcurrent=\"false\" name=\"";
+ xmi+= temp;
+ xmi+= "\" isSpecification=\"false\"/>\n";
+ }
+
+ // end the set of states....
+ xmi+= "</UML:CompositeState.subvertex>\n";
+ xmi+= "</UML:CompositeState>\n";
+ xmi+= "</UML:StateMachine.top>\n";
+
+ // start the set of transitions...
+ xmi+="<UML:StateMachine.transitions>\n";
+
+
+
+ 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);
+ temp = temp + temp1 + temp2;
+
+ 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;
+ temp1 = transitions[t_count].trans.guard;
+ temp2 = transitions[t_count].trans.action;
+
+ // print trigger, if any
+ if (temp != "") {
+ xmi+= "<UML:Transition.trigger> <UML:Event> <UML:ModelElement.namespace> <UML:Namespace> ";
+ xmi+= "<UML:Namespace.ownedElement> <UML:CallEvent xmi.id=\"\" operation=\"\" ";
+ xmi+= "name=\"" + temp + "isSpecification=\"false\"/> ";
+ 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, '<' => '<'
+ if (temp1 != ""){
+ xmi+= "<UML:Transition.guard> <UML:Guard> <UML:Guard.expression> ";
+ xmi+= "<UML:BooleanExpression body=\"" + temp1 + "\" language=\"\"/> ";
+ xmi+= "</UML:Guard.expression> </UML:Guard> </UML:Transition.guard> ";
+ }
+
+ /* // Guard stuff
+ <UML:Transition.guard>
+ <UML:Guard>
+ <UML:Guard.expression>
+ <UML:BooleanExpression body="brightnessValue<0" language=""/>
+ </UML:Guard.expression>
+ </UML:Guard>
+ </UML:Transition.guard>
+
+
+ */
+
+ // print action, if any
+ if (temp2 != "") {
+ xmi+= "<UML:Transition.effect> <UML:UninterpretedAction xmi.id=\"\" ";
+ xmi+= "isAsynchronous=\"false\" name=\"\" isSpecification=\"false\"> ";
+ xmi+= "<UML:Action.script> <UML:ActionExpression language=\"\" body=\" ";
+ 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";
+
+
+ }
+
return;
}
Modified: branches/uml/source/main/cOrganism.h
===================================================================
--- branches/uml/source/main/cOrganism.h 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cOrganism.h 2007-03-08 20:31:02 UTC (rev 1392)
@@ -148,9 +148,7 @@
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::map<int, int> PosToStateLabel; // a map that relates the number in which the state was inserted
- // to the label the organism assigns it.
-
+
std::vector<std::string> triggers;
std::vector<std::string> guards;
std::vector<std::string> actions;
@@ -234,9 +232,13 @@
// UML Stuff
void modelCheck(cAvidaContext& ctx); // evaluate the model
- void printXMI(cAvidaContext& ctx); // print the XMI version of the model
+ void printXMI(); // print the XMI version of the model
+ std::string getXMI ();
+ std::string StringifyAnInt(int);
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();
Modified: branches/uml/source/main/cStats.cc
===================================================================
--- branches/uml/source/main/cStats.cc 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cStats.cc 2007-03-08 20:31:02 UTC (rev 1392)
@@ -859,6 +859,7 @@
df.Write( GetUpdate(), "update" );
df.Write( av_number_of_states.Average(), "av num states");
df.Write( av_number_of_trans.Average(), "av num trans");
+ df.Write( av_number_of_trans_lab.Average(), "av num of trans lab");
df.Write( m_hydraAttempt.Sum(), "total number of hydra attempts" );
df.Write( m_hydraPassed.Sum(), "total number of hydra passes" );
df.Write( m_spinAttempt.Sum(), "total number of spin attempts" );
@@ -868,6 +869,7 @@
av_number_of_states.Clear();
av_number_of_trans.Clear();
+ av_number_of_trans_lab.Clear();
m_hydraAttempt.Clear();
m_hydraPassed.Clear();
Modified: branches/uml/source/main/cStats.h
===================================================================
--- branches/uml/source/main/cStats.h 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cStats.h 2007-03-08 20:31:02 UTC (rev 1392)
@@ -221,7 +221,7 @@
// Stats for UML state diagrams
cDoubleSum av_number_of_states;
cDoubleSum av_number_of_trans;
-
+ cDoubleSum av_number_of_trans_lab;
cDoubleSum m_hydraAttempt;
cDoubleSum m_hydraPassed;
cDoubleSum m_spinAttempt;
@@ -542,7 +542,10 @@
// UML Data Function
-// void UpdateModelStats (cOrganism::Graph& g);
+// void UpdateModelStats ();
+ void addState(int x) { av_number_of_states.Add(x); }
+ void addTrans(int x) { av_number_of_trans.Add(x); }
+ void addTransLabel(int x) { av_number_of_trans_lab.Add(x); }
void HydraAttempt() { ++m_hydraAttempt; }
void HydraPassed() { ++m_hydraPassed; }
void SpinAttempt() { ++m_spinAttempt; }
Modified: branches/uml/source/main/cTaskLib.cc
===================================================================
--- branches/uml/source/main/cTaskLib.cc 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cTaskLib.cc 2007-03-08 20:31:02 UTC (rev 1392)
@@ -344,10 +344,12 @@
else if (name == "numStates") //
NewTask(name, "Successfully created 5 states", &cTaskLib::Task_NumStates);
else if (name == "numTrans") //
- NewTask(name, "Successfully created 5 transitions", &cTaskLib::Task_NumTrans);
-
-
-
+ NewTask(name, "Successfully created 5 transitions", &cTaskLib::Task_NumTrans);
+ else if (name == "hydra") //
+ NewTask(name, "Successfully ran hydra", &cTaskLib::Task_Hydra);
+ else if (name == "spin1") //
+ NewTask(name, "Successfully ran Spin", &cTaskLib::Task_SpinN1);
+
// Make sure we have actually found a task
if (task_array.GetSize() == start_size) {
cerr << "Unknown task entry '" << name << "'." << endl;
@@ -1854,34 +1856,60 @@
double cTaskLib::Task_Trans1(cTaskContext* ctx) const
{
- if (ctx->organism->findTrans(1,2, "tagaaa")) return 1.0;
- return 0.0;
+ double bonus = 0.0;
+ if (ctx->organism->findTrans(1,2, "tagaaa")) {
+ bonus = 1.0;
+ }
- // check for a transition between 2 states
-
+ ctx->task_failed = ctx->task_failed && bonus;
+ return bonus;
}
double cTaskLib::Task_Trans2(cTaskContext* ctx) const
{
- return ctx->organism->findTrans(2,3, "tagdab");
+ double bonus = 0.0;
+ if (ctx->organism->findTrans(2,3, "tagdab")){
+ bonus = 1.0;
+ }
+
+ ctx->task_failed = ctx->task_failed && bonus;
+ return bonus;
}
double cTaskLib::Task_Trans3(cTaskContext* ctx) const
{
- return ctx->organism->findTrans(3,4, "tcgbac");
+ double bonus = 0.0;
+ if (ctx->organism->findTrans(3,4, "tcgbac")){
+ bonus = 1.0;
+ }
+
+ ctx->task_failed = ctx->task_failed && bonus;
+ return bonus;
}
double cTaskLib::Task_Trans4(cTaskContext* ctx) const
{
- return ctx->organism->findTrans(0,1, "tbgcad");
+ double bonus = 0.0;
+ if (ctx->organism->findTrans(0,1, "tbgcad")){
+ bonus = 1.0;
+ }
+
+ ctx->task_failed = ctx->task_failed && bonus;
+ return bonus;
}
double cTaskLib::Task_Trans5(cTaskContext* ctx) const
{
- return ctx->organism->findTrans(4,0, "tdgaac");
+ double bonus = 0.0;
+ if (ctx->organism->findTrans(4,0, "tdgaac")){
+ bonus = 1.0;
+ }
+
+ ctx->task_failed = ctx->task_failed && bonus;
+ return bonus;
}
@@ -1910,3 +1938,116 @@
+double cTaskLib::Task_Hydra(cTaskContext* ctx) const
+{
+ cOrganism* organism = ctx->organism;
+
+// Check for task success...
+ if (ctx->task_failed == 0) {
+ return 0;
+ }
+
+ m_world->GetStats().HydraAttempt();
+
+ double bonus = 0.0;
+ std::string temp;
+ unsigned int status_total = 0;
+ int status=0;
+
+ int to_subavida[2]={0};
+ int from_subavida[2]={0};
+
+ pipe(to_subavida); //write to 1, read from 0
+ pipe(from_subavida);
+
+ pid_t subavida = fork();
+ if(subavida == 0) {
+ //child
+ close(to_subavida[1]);
+ close(from_subavida[0]);
+ dup2(to_subavida[0], STDIN_FILENO); //oldd, newd
+ dup2(from_subavida[1], STDOUT_FILENO);
+ execl("/usr/bin/java", "-cp .", "-jar", "./hydraulic.jar", NULL);
+ // We don't ever get here.
+ }
+ //parent
+ close(to_subavida[0]);
+ close(from_subavida[1]);
+
+ // At this point, forget about subavida - It's running.
+ // Write the model to to_subavida[1]. Close to_subavida[1] (which wakes up subavida).
+ // Then, read from from_subavida[0] as long as is possible, after which point subavida will die.
+
+ // Write the model to STDIN of subavida (be careful; write may not write all that you ask!)
+ temp = organism->getXMI();
+ do {
+ status = write(to_subavida[1], temp.c_str()+status_total, temp.size());
+ if (status < 0) {
+ break;
+ } else {
+ status_total += status;
+ }
+ } while (status_total < temp.size());
+ close(to_subavida[1]); // Wakes up subavida.
+
+ // Time passes...
+
+ // Read the output from subavida. Keep reading until subavida closes the pipe.
+ const int read_size=128; // The number of bytes that we're going to try to read from subavida.
+ std::string subavida_output;
+ char line[read_size]={0};
+ do {
+ status = read(from_subavida[0], line, read_size-1);
+ if(status > 0) {
+ subavida_output += line;
+ memset(line, 0, read_size);
+ }
+ } while(((status==-1) && (errno == EINTR)) || (status>0));
+ // Done with subavida.
+ close(from_subavida[0]);
+ // Make sure that subavida dies.
+ pid_t done=0;
+ while((done=waitpid(subavida, &status, 0))==-1 && (errno == EINTR));
+ assert(done==subavida);
+
+ // if there are no errors, return 0 from hydraulic. otherwise, return non-zero.
+ if(status != 0) {
+ ctx->task_failed = 0;
+ return 0.0;
+ } else {
+ ctx->task_failed = ctx->task_failed && 1;
+ m_world->GetStats().HydraPassed();
+ return 1.0;
+ }
+}
+
+double cTaskLib::SpinCoprocess(cTaskContext* ctx, const std::string& neverclaimFile) const {
+ cOrganism* organism = ctx->organism;
+ m_world->GetStats().SpinAttempt();
+ int status=0;
+ std::string cmd = "cat " + neverclaimFile + " >> tmp.pr && ./spin -a tmp.pr &> /dev/null";
+ if(system(cmd.c_str())!=0) return 0.0;
+ m_world->GetStats().SpinPassed();
+ m_world->GetStats().PanAttempt();
+
+ if(system("/usr/bin/gcc -DMEMLIM=512 pan.c -o pan &> /dev/null")!=0) return 0.0;
+ if(system("./pan -a &> ./pan.out")!=0) return 0.0;
+ if(system("cat pan.out | perl -e 'while(<STDIN>) { if(/errors:\\s(\\d+)/) {exit($1);}}'")!=0) return 0.0;
+
+ std::ostringstream strstrm;
+ strstrm << "cp tmp.xmi " << m_world->GetStats().GetUpdate() << "." << organism->GetID();
+ strstrm << ".xml";
+ if(system(strstrm.str().c_str())!=0) return 0.0;
+
+ m_world->GetStats().PanPassed();
+ return 3.0;
+}
+
+double cTaskLib::Task_SpinN1(cTaskContext* ctx) const {
+ if (ctx->task_failed) {
+ return SpinCoprocess(ctx, "N1");
+ }
+ return 0.0;
+}
+
+
Modified: branches/uml/source/main/cTaskLib.h
===================================================================
--- branches/uml/source/main/cTaskLib.h 2007-03-06 17:54:20 UTC (rev 1391)
+++ branches/uml/source/main/cTaskLib.h 2007-03-08 20:31:02 UTC (rev 1392)
@@ -238,7 +238,11 @@
double Task_Trans5(cTaskContext* ctx) const;
double Task_NumStates(cTaskContext* ctx) const;
double Task_NumTrans(cTaskContext* ctx) const;
+ double Task_Hydra(cTaskContext* ctx) const;
+ double SpinCoprocess(cTaskContext* ctx, const std::string& neverclaimFile) const;
+ double Task_SpinN1(cTaskContext* ctx) const;
+
};
More information about the Avida-cvs
mailing list