[Avida-SVN] r1529 - branches/uml/source/main
hjg at myxo.css.msu.edu
hjg at myxo.css.msu.edu
Fri May 4 13:19:05 PDT 2007
Author: hjg
Date: 2007-05-04 16:19:05 -0400 (Fri, 04 May 2007)
New Revision: 1529
Added:
branches/uml/source/main/cUMLModel.cc
branches/uml/source/main/cUMLModel.h
branches/uml/source/main/cUMLStateDiagram.cc
branches/uml/source/main/cUMLStateDiagram.h
Log:
New cUMLModel.* and cUMLStateDiagram.* files.
Added: branches/uml/source/main/cUMLModel.cc
===================================================================
--- branches/uml/source/main/cUMLModel.cc (rev 0)
+++ branches/uml/source/main/cUMLModel.cc 2007-05-04 20:19:05 UTC (rev 1529)
@@ -0,0 +1,46 @@
+#include "cUMLModel.h"
+
+
+#include <iomanip>
+
+
+using namespace std;
+
+std::string loadFile(const char* filename) {
+ std::string data, line; // or maybe stringstream? (strstream?)
+ std::ifstream infile;
+ infile.open(filename);
+ 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;
+}
+
+std::string cUMLModel::xmi_begin = loadFile("xmi_begin");
+std::string cUMLModel::xmi_end = loadFile("xmi_end");
+
+std::string cUMLModel::getXMI()
+{
+ std::string x;
+ int v;
+
+ x = xmi_begin;
+
+ // get the xmi for each state diagram
+ for (v = 0; v < state_diagrams.size(); ++v) {
+ state_diagrams[v].printXMI();
+ x+=state_diagrams[v].getXMI();
+ }
+
+ x += xmi_end;
+
+}
+
+
Added: branches/uml/source/main/cUMLModel.h
===================================================================
--- branches/uml/source/main/cUMLModel.h (rev 0)
+++ branches/uml/source/main/cUMLModel.h 2007-05-04 20:19:05 UTC (rev 1529)
@@ -0,0 +1,28 @@
+#ifndef _C_UMLMODEL_H_
+#define _C_UMLMODEL_H_
+
+#include "cUMLStateDiagram.h"
+
+#include <string>
+#include <iostream>
+#include <map>
+#include <utility>
+#include <set>
+#include <vector>
+#include <fstream>
+
+
+class cUMLModel {
+protected:
+ static std::string xmi_begin;
+ std::string xmi; // the XMI created by the model
+ static std::string xmi_end;
+ std::vector<cUMLStateDiagram> state_diagrams;
+
+public:
+
+ std::string getXMI(); // get the XMI version of the model
+
+};
+
+#endif
Added: branches/uml/source/main/cUMLStateDiagram.cc
===================================================================
--- branches/uml/source/main/cUMLStateDiagram.cc (rev 0)
+++ branches/uml/source/main/cUMLStateDiagram.cc 2007-05-04 20:19:05 UTC (rev 1529)
@@ -0,0 +1,394 @@
+#include "cUMLStateDiagram.h"
+
+bool cUMLStateDiagram::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 cUMLStateDiagram::findTrans(int orig, int dest, int trig, std::string gu, std::string act)
+{
+ // the wild cards for there are
+ // -1 for orig, dest & trigger
+ // "*" for guard, and action
+
+ for(std::vector<transition>::iterator i=transitions.begin(); i!=transitions.end(); ++i){
+ if (((orig == -1) || (orig == i->orig_state)) &&
+ ((dest == -1) || (dest == i->dest_state)) &&
+ ((trig == -1) || (trig == i->trans.trigger)) &&
+ ((gu == "*") || (gu == i->trans.guard)) &&
+ ((act == "*") || (act == i->trans.action))) {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+template <typename T>
+bool cUMLStateDiagram::absoluteMoveIndex (T x, int &index, int amount )
+{
+ index = 0;
+ return relativeMoveIndex(x, index, amount);
+}
+
+template <typename T>
+bool cUMLStateDiagram::relativeMoveIndex (T x, int &index, int amount )
+{
+ if (x.size() == 0) {
+ return false;
+ }
+
+ if (amount > 0) {
+// index += (amount % x.size()); // this provides relative jumping
+ 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 cUMLStateDiagram::absoluteJumpTrigger(int jump_amount)
+{
+ return absoluteMoveIndex(triggers, trigger_index, jump_amount);
+}
+
+bool cUMLStateDiagram::absoluteJumpGuard(int jump_amount)
+{
+ return absoluteMoveIndex(guards, guard_index, jump_amount);
+}
+
+bool cUMLStateDiagram::absoluteJumpAction(int jump_amount)
+{
+ return absoluteMoveIndex(actions, action_index, jump_amount);
+}
+
+bool cUMLStateDiagram::absoluteJumpTransitionLabel(int jump_amount)
+{
+ return absoluteMoveIndex(transition_labels, trans_label_index, jump_amount);
+}
+
+bool cUMLStateDiagram::absoluteJumpOriginState(int jump_amount)
+{
+ return absoluteMoveIndex(states, orig_state_index, jump_amount);
+}
+
+bool cUMLStateDiagram::absoluteJumpDestinationState(int jump_amount)
+{
+ return absoluteMoveIndex(states, dest_state_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpTrigger(int jump_amount)
+{
+ return relativeMoveIndex(triggers, trigger_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpGuard(int jump_amount)
+{
+ return relativeMoveIndex(guards, guard_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpAction(int jump_amount)
+{
+ return relativeMoveIndex(actions, action_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpTransitionLabel(int jump_amount)
+{
+ return relativeMoveIndex(transition_labels, trans_label_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpOriginState(int jump_amount)
+{
+ return relativeMoveIndex(states, orig_state_index, jump_amount);
+}
+
+bool cUMLStateDiagram::relativeJumpDestinationState(int jump_amount)
+{
+ return relativeMoveIndex(states, dest_state_index, jump_amount);
+}
+
+int cUMLStateDiagram::getTriggerIndex()
+{
+ /*if (triggers.size() == 0) {
+ return 0;
+ } else {*/
+
+ return trigger_index;
+ //}
+}
+
+std::string cUMLStateDiagram::getGuard()
+{
+ if (guards.size() == 0) {
+ return "";
+ } else {
+ return guards[guard_index];
+ }
+}
+
+std::string cUMLStateDiagram::getAction()
+{
+ if (actions.size() == 0) {
+ return "";
+ } else {
+ return actions[action_index];
+ }
+}
+
+int cUMLStateDiagram::getOrigStateIndex()
+{
+ return orig_state_index;
+}
+
+int cUMLStateDiagram::getDestStateIndex()
+{
+ return dest_state_index;
+}
+
+transition_label cUMLStateDiagram::getTransLabel()
+{
+ return transition_labels[trans_label_index];
+}
+
+bool cUMLStateDiagram::addState()
+{
+ state s;
+ s.identifier = states.size();
+ s.num_incoming = 0;
+ s.num_outgoing = 0;
+ states.push_back(s);
+
+ return true;
+}
+
+bool cUMLStateDiagram::addTransitionLabel()
+{
+ transition_label t;
+ t.trigger = getTriggerIndex();
+ t.guard = getGuard();
+ t.action = getAction();
+
+ // no dupes
+ if (findTransLabel(t)){
+ return false;
+ }
+
+ transition_labels.push_back(t);
+
+ // Move the transition label index to the most recently created
+ trans_label_index = transition_labels.size() - 1;
+
+ return true;
+}
+
+
+bool cUMLStateDiagram::addTransition()
+{
+ if ((states.size() == 0) || (transition_labels.size() == 0)) {
+
+ return false;
+ }
+
+ transition t;
+ t.orig_state = getOrigStateIndex();
+ t.dest_state = getDestStateIndex();
+ // increment number of edges for a state
+ states[getOrigStateIndex()].num_outgoing += 1;
+ states[getDestStateIndex()].num_incoming += 1;
+
+ t.trans = getTransLabel();
+
+ // no dupes
+ if (findTrans(t.orig_state, t.dest_state, t.trans.trigger, t.trans.guard, t.trans.action)) {
+ return false;
+ }
+
+ transitions.push_back(t);
+
+ return true;
+
+}
+
+
+bool cUMLStateDiagram::addTransitionTotal()
+{
+ if ((states.size() == 0)) {
+
+ return false;
+ }
+
+ transition t;
+ t.orig_state = getOrigStateIndex();
+ t.dest_state = getDestStateIndex();
+
+
+ // Do not create transition if the origin state is unreachable.
+ if ((t.orig_state != 0) && (states[t.orig_state].num_incoming == 0)) {
+ return false;
+ }
+
+ // increment number of edges for a state
+ states[getOrigStateIndex()].num_outgoing += 1;
+ states[getDestStateIndex()].num_incoming += 1;
+
+
+ transition_label tl;
+ tl.trigger = getTriggerIndex();
+ tl.guard = getGuard();
+ tl.action = getAction();
+ t.trans = tl;
+
+
+ // no dupes
+ if (findTrans(t.orig_state, t.dest_state, t.trans.trigger, t.trans.guard, t.trans.action)) {
+ return false;
+ }
+
+ transitions.push_back(t);
+
+ // reset all indices
+ orig_state_index = 0;
+ dest_state_index = 0;
+ trigger_index = 0;
+ action_index = 0;
+ guard_index = 0;
+
+ return true;
+
+}
+
+
+int cUMLStateDiagram::numStates()
+{
+ return states.size();
+}
+
+int cUMLStateDiagram::numTrans()
+{
+ return transitions.size();
+}
+
+// print the label. Change - signs to _
+std::string cUMLStateDiagram::StringifyAnInt(int x) {
+
+ std::ostringstream o;
+
+ if (x < 0) {
+ x = abs(x);
+ o << "_";
+ }
+
+ o << x;
+ return o.str();
+}
+
+std::string cUMLStateDiagram::getXMI()
+{
+ return (xmi);
+}
+
+void cUMLStateDiagram::printXMI()
+{
+ std::string temp, temp1, temp2, temp3;
+ std::string trig_label, trig_op_label;
+
+ int s_count = 0;
+ int t_count = 0;
+ xmi = "";
+
+ // This state is the initial state; thus it should be printed regardless of whether it has an incoming
+ // edge or not.
+ 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) {
+
+ // only print if this state has an incoming edge.
+ if ((states[s_count]).num_incoming > 0) {
+ 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....
+ temp = "t" + StringifyAnInt(t_count);
+ temp1 = "s" + StringifyAnInt(transitions[t_count].orig_state);
+ temp2 = "s" + StringifyAnInt(transitions[t_count].dest_state);
+ temp3 = temp + temp1 + temp2;
+
+ xmi+= "<UML:Transition xmi.id=\"" + temp3 + "\"";
+ xmi+= " source=\"" + temp1 + "\"";
+ xmi += " target=\"" + temp2 + "\" name=\"\" isSpecification=\"false\">\n";
+
+ // Get guard, trigger, and action
+// temp = transitions[t_count].trans.trigger;
+ temp1 = transitions[t_count].trans.guard;
+ temp2 = transitions[t_count].trans.action;
+ trig_label = triggers[transitions[t_count].trans.trigger].label;
+ trig_op_label = triggers[transitions[t_count].trans.trigger].operation_id;
+
+
+ // print trigger, if any
+ if (trig_label != "<null>") {
+ xmi+= "<UML:Transition.trigger> <UML:Event> <UML:ModelElement.namespace> <UML:Namespace> ";
+ xmi+= "<UML:Namespace.ownedElement> <UML:CallEvent xmi.id=\"" + temp3;
+ xmi+= "tt\" operation=\""+ trig_op_label + "\" ";
+ xmi+= "name=\"" + trig_label + "\" isSpecification=\"false\"/> ";
+ xmi+= "</UML:Namespace.ownedElement> </UML:Namespace> </UML:ModelElement.namespace> ";
+ xmi+= "</UML:Event> </UML:Transition.trigger> ";
+ }
+
+ // print guard, if any
+ // Note: for guard to work, '<' => '<'
+ if (temp1 != "<null>"){
+ xmi+= "<UML:Transition.guard> <UML:Guard> <UML:Guard.expression> ";
+ xmi+= "<UML:BooleanExpression body=\"" + temp1 + "\" language=\"\"/> ";
+ xmi+= "</UML:Guard.expression> </UML:Guard> </UML:Transition.guard> ";
+ }
+
+ // print action, if any
+ if (temp2 != "<null>") {
+ xmi+= "<UML:Transition.effect> <UML:UninterpretedAction xmi.id=\"" + temp3 + "ui\" ";
+ xmi+= "isAsynchronous=\"false\" name=\"\" isSpecification=\"false\"> ";
+ xmi+= "<UML:Action.script> <UML:ActionExpression language=\"\" body=\"";
+ xmi+= temp2 + "\"/> </UML:Action.script> </UML:UninterpretedAction> </UML:Transition.effect> ";
+ }
+
+ xmi += "</UML:Transition>\n";
+
+
+ }
+
+ return;
+}
Added: branches/uml/source/main/cUMLStateDiagram.h
===================================================================
--- branches/uml/source/main/cUMLStateDiagram.h (rev 0)
+++ branches/uml/source/main/cUMLStateDiagram.h 2007-05-04 20:19:05 UTC (rev 1529)
@@ -0,0 +1,120 @@
+#ifndef _C_UMLSTATEDIAGRAM_H_
+#define _C_UMLSTATEDIAGRAM_H_
+
+#include <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+
+
+struct transition_label {
+ int trigger;
+ std::string guard;
+ std::string action;
+};
+
+struct transition {
+ int orig_state;
+ int dest_state;
+ transition_label trans;
+};
+
+struct state {
+ int identifier;
+ int num_incoming;
+ int num_outgoing;
+};
+
+struct trigger_info {
+ std::string operation_id;
+ std::string label;
+};
+
+
+class cUMLStateDiagram {
+protected:
+ std::vector<state> states;
+ std::vector<trigger_info> triggers;
+ std::vector<std::string> guards;
+ std::vector<std::string> actions;
+ std::vector<transition> transitions;
+ std::vector<transition_label> transition_labels;
+
+ int orig_state_index;
+ int dest_state_index;
+ int trans_label_index;
+ int trigger_index;
+ int guard_index;
+ int action_index;
+
+ std::string xmi;
+
+
+public:
+
+ void printXMI(); // print the XMI version of the model
+ std::string getXMI (); // get the xmi string (including beginning & end read from file.)
+ std::string StringifyAnInt(int);
+ int numStates();
+ int numTrans();
+
+ bool findTrans(int, int, int, std::string, std::string) ;
+ bool findTransLabel(transition_label); // find a specific transition label
+
+ template <typename T>
+ bool absoluteMoveIndex (T x, int &y, int z);
+
+ template <typename T>
+ bool relativeMoveIndex (T x, int &y, int z);
+
+// The jump functions jump the index of the various vectors either forward (+ int) or backwards (- int)
+ bool absoluteJumpGuard(int);
+ bool absoluteJumpAction(int);
+ bool absoluteJumpTrigger(int);
+ bool absoluteJumpTransitionLabel(int);
+ bool absoluteJumpOriginState(int);
+ bool absoluteJumpDestinationState(int);
+ bool relativeJumpGuard(int);
+ bool relativeJumpAction(int);
+ bool relativeJumpTrigger(int);
+ bool relativeJumpTransitionLabel(int);
+ bool relativeJumpOriginState(int);
+ bool relativeJumpDestinationState(int);
+
+
+// The first functions jump the index to the beginning of various vectors
+ bool firstGuard() {guard_index = 0;}
+ bool firstAction() {action_index = 0;}
+ bool firstTrigger() {trigger_index = 0;}
+ bool firstTransitionLabel() {trans_label_index = 0;}
+ bool firstOriginState() {orig_state_index = 0;}
+ bool firstDestinationState() {dest_state_index = 0;}
+
+// The last functions jump the index to the end of various vectors
+ bool lastGuard() {guard_index = (guards.size()-1);}
+ bool lastAction() {action_index = (actions.size()-1);}
+ bool lastTrigger() {trigger_index = (triggers.size()-1);}
+ bool lastTransitionLabel() {trans_label_index = (transition_labels.size()-1);}
+ bool lastOriginState() {orig_state_index = (states.size()-1);}
+ bool lastDestinationState() {dest_state_index = (states.size()-1);}
+
+
+// The get functions get the value of the index of various vectors
+// std::string getTriggerIndex();
+ int getTriggerIndex();
+ std::string getGuard();
+ std::string getAction();
+ transition_label getTransLabel();
+ int getOrigStateIndex();
+ int getDestStateIndex();
+
+// Add functions
+ bool addState();
+ bool addTransitionLabel();
+ bool addTransition();
+ bool addTransitionTotal();
+ // END UML functions
+
+};
+
+#endif
More information about the Avida-cvs
mailing list