27 #ifndef FAUDES_MTCGENERATOR_H
28 #define FAUDES_MTCGENERATOR_H
52 template <
class GlobalAttr,
class StateAttr,
class EventAttr,
class TransAttr>
321 void DelColor(
Idx stateIndex,
const std::string& rColorName);
346 void DelColor(
const std::string& rColorName);
476 bool ExistsColor(
const std::string& rColorName)
const;
588 virtual void DotWrite(
const std::string& rFileName)
const;
598 std::string
CStr(
Idx index)
const;
750 #ifdef FAUDES_COMPATIBILITY
755 #define THIS TmtcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
756 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
757 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
800 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem()");
806 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(rOtherGen)");
815 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(rOtherGen)");
821 TEMP THIS::TmtcGenerator(
const std::string& rFileName) :
BASE() {
822 FD_DG(
"MtcSystem(" <<
this <<
")::MtcSystem(" << rFileName <<
")");
832 res->EventSymbolTablep(BASE::mpEventSymbolTable);
833 res->mStateNamesEnabled=BASE::mStateNamesEnabled;
834 res->mReindexOnWrite=BASE::mReindexOnWrite;
836 res->ColorSymbolTable(GlobalColorSymbolTablep());
854 FD_DG(
"MtcSystem(" <<
this <<
")::DoAssign(gen&)");
856 BASE::DoAssign(rSrc);
878 FD_DG(
"TmtcGenerator(" <<
this <<
")::Assign([type] " << &rSrc <<
")");
880 if(&rSrc==static_cast<Type*>(
this))
return *
this;
901 return mpColorSymbolTable;
906 mpColorSymbolTable=pSymTab;
915 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const std::string& rColorName) {
916 FD_DG(
"MtcSystem(" <<
this <<
")::InsColoredState(rStateName, colorName)");
919 #ifdef FAUDES_CHECKED
921 index = BASE::InsState(rStateName);
924 std::stringstream errstr;
925 errstr <<
"Name \"" << rStateName <<
"\" already exists" << std::endl;
926 throw Exception(
"MtcSystem::InsColoredState(rStateName, rColorName)", errstr.str(), 104);
929 attr = BASE::StateAttributep(index);
932 std::stringstream errstr;
933 errstr <<
"Index " << index <<
" not member of set" << std::endl;
934 throw Exception(
"MtcSystem::InsColoredState(stateIndex, colorIndex)", errstr.str(), 200);
937 attr->Colors().Insert(rColorName);
940 std::stringstream errstr;
941 errstr <<
"Name already exists / invalid name: " << rColorName << std::endl;
942 throw Exception(
"MtcSystem::InsColoredState(stateIndex, rColorName)", errstr.str(), 104);
945 index = BASE::InsState(rStateName);
946 attr = Attributep(index);
947 attr->Colors().Insert(rColorName);
953 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const Idx colorIndex) {
954 Idx index = BASE::InsState(rStateName);
955 StateAttr *attr = BASE::StateAttributep(index);
956 #ifdef FAUDES_CHECKED
958 attr->Colors().Insert(colorIndex);
961 std::stringstream errstr;
962 errstr <<
"Color index " << colorIndex <<
" not known to symbol table" << std::endl;
963 throw Exception(
"MtcSystem::InsColoredState(rStateName, colorIndex)", errstr.str(), 200);
966 attr->Colors().Insert(colorIndex);
972 TEMP Idx THIS::InsColoredState(
const std::string& rStateName,
const ColorSet& rColors) {
973 Idx index = BASE::InsState(rStateName);
974 InsColors(index, rColors);
980 Idx index = BASE::InsState();
981 InsColors(index, rColors);
986 TEMP Idx THIS::InsColor(
Idx stateIndex,
const std::string& rColorName) {
989 #ifdef FAUDES_CHECKED
991 attr = BASE::StateAttributep(stateIndex);
994 std::stringstream errstr;
995 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
996 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
999 index = attr->Colors().Insert(rColorName);
1002 std::stringstream errstr;
1003 errstr <<
"Name already exists / invalid name: " << rColorName << std::endl;
1004 throw Exception(
"MtcSystem::InsColor(stateIndex, rColorName)", errstr.str(), 104);
1007 attr = Attributep(stateIndex);
1008 index = attr->Colors().Insert(rColorName);
1016 #ifdef FAUDES_CHECKED
1018 attr = BASE::StateAttributep(stateIndex);
1021 std::stringstream errstr;
1022 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1023 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
1026 attr->Colors().Insert(colorIndex);
1029 std::stringstream errstr;
1030 errstr <<
"Color index " << colorIndex <<
" not known to symbol table" << std::endl;
1031 throw Exception(
"MtcSystem::InsColor(stateIndex, colorIndex)", errstr.str(), 200);
1034 attr = Attributep(stateIndex);
1035 attr->Colors().Insert(colorIndex);
1041 #ifdef FAUDES_CHECKED
1043 std::stringstream errstr;
1044 errstr <<
"Symbol table mismatch" << std::endl;
1045 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 88);
1049 #ifdef FAUDES_CHECKED
1051 attr = BASE::StateAttributep(stateIndex);
1054 std::stringstream errstr;
1055 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1056 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 200);
1059 attr->Colors().InsertSet(rColors);
1062 std::stringstream errstr;
1063 errstr <<
"Symbol table mismach" << std::endl;
1064 throw Exception(
"MtcSystem::InsColors(stateIndex, rColors)", errstr.str(), 200);
1067 attr = Attributep(stateIndex);
1068 attr->Colors().InsertSet(rColors);
1073 TEMP void THIS::DelColor(
Idx stateIndex,
const std::string& rColorName) {
1076 #ifdef FAUDES_CHECKED
1078 attr = BASE::StateAttributep(stateIndex);
1081 std::stringstream errstr;
1082 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1083 throw Exception(
"MtcSystem::DelColor(stateIndex, rColorName)", errstr.str(), 200);
1085 index = ColorIndex(rColorName);
1087 attr->Colors().Erase(index);
1090 std::stringstream errstr;
1091 errstr <<
"Color name \"" << rColorName <<
"\" not found in NameSet" << std::endl;
1092 throw Exception(
"MtcSystem::DelColor(stateIndex, rColorName)", errstr.str(), 202);
1095 attr = Attributep(stateIndex);
1096 index = ColorIndex(rColorName);
1097 attr->Colors().Erase(index);
1099 CheckSymbolTable(index);
1106 #ifdef FAUDES_CHECKED
1108 attr = BASE::StateAttributep(stateIndex);
1111 std::stringstream errstr;
1112 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1113 throw Exception(
"MtcSystem::DelColor(stateIndex, colorIndex)", errstr.str(), 200);
1116 attr = BASE::StateAttributep(stateIndex);
1118 res = attr->Colors().Erase(colorIndex);
1120 std::stringstream errstr;
1121 errstr <<
"Color index " << colorIndex <<
" not found in generator" << std::endl;
1122 throw Exception(
"MtcSystem::DelColor(stateIndex, colorIndex)", errstr.str(), 205);
1124 CheckSymbolTable(colorIndex);
1128 TEMP void THIS::DelColor(
const std::string& rColorName) {
1129 StateSet::Iterator lit;
1132 Idx index = ColorIndex(rColorName);
1134 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1135 attr = BASE::StateAttributep(*lit);
1136 attr->Colors().Erase(index);
1138 CheckSymbolTable(index);
1143 StateSet::Iterator lit;
1145 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1146 attr = BASE::StateAttributep(*lit);
1147 attr->Colors().Erase(colorIndex);
1149 CheckSymbolTable(colorIndex);
1155 ColorSet::Iterator cit;
1156 ColorSet delColors(mpColorSymbolTable);
1157 #ifdef FAUDES_CHECKED
1159 attr = BASE::StateAttributep(stateIndex);
1162 std::stringstream errstr;
1163 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1164 throw Exception(
"MtcSystem::ClrColors(stateIndex)", errstr.str(), 200);
1167 attr = BASE::StateAttributep(stateIndex);
1170 attr->Colors().Clear();
1171 CheckSymbolTable(delColors);
1176 ColorSymbolTablep()->ClrEntry(colorIndex);
1181 #ifdef FAUDES_CHECKED
1183 std::stringstream errstr;
1184 errstr <<
"Symbol table mismatch" << std::endl;
1185 throw Exception(
"MtcSystem::Colors", errstr.str(), 88);
1188 StateSet::Iterator lit;
1189 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1190 const StateAttr& attr = BASE::pStates->Attribute(*lit);
1197 StateSet::Iterator lit;
1198 ColorSet colors(mpColorSymbolTable);
1199 for (lit = BASE::pStates->Begin(); lit != BASE::pStates->End(); lit++) {
1200 const StateAttr& attr = BASE::pStates->Attribute(*lit);
1208 const StateAttr* attrp;
1209 #ifdef FAUDES_CHECKED
1211 attrp = &BASE::pStates->
Attribute(stateIndex);
1214 std::stringstream errstr;
1215 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1216 throw Exception(
"MtcSystem::Colors(stateIndex)", errstr.str(), 200);
1219 attrp = &BASE::pStates->Attribute(stateIndex);
1221 return attrp->Colors();
1225 TEMP void THIS::CheckSymbolTable(
Idx colorIndex) {
1227 ColorSet colors(mpColorSymbolTable);
1229 if (!colors.
Exists(colorIndex))
1230 DelColorName(colorIndex);
1236 #ifdef FAUDES_CHECKED
1238 std::stringstream errstr;
1239 errstr <<
"Symbol table mismatch" << std::endl;
1240 throw Exception(
"MtcSystem::CheckSymbolTable", errstr.str(), 88);
1244 ColorSet colors(mpColorSymbolTable);
1247 ColorSet::Iterator cit;
1248 for(cit = rColors.
Begin(); cit != rColors.
End(); cit++) {
1250 if(!colors.
Exists(*cit)) DelColorName(*cit);
1256 TEMP bool THIS::ExistsColor(
Idx colorIndex)
const {
1257 StateSet::Iterator lit;
1258 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1259 if (ExistsColor(*lit, colorIndex))
return true;
1265 TEMP bool THIS::ExistsColor(
const std::string& rColorName)
const {
1266 Idx colorIndex = ColorIndex(rColorName);
1267 #ifdef FAUDES_CHECKED
1268 if (colorIndex == 0) {
1269 std::stringstream errstr;
1270 errstr <<
"Color name \"" << rColorName <<
"\" not found in symbol table " << std::endl;
1271 throw Exception(
"MtcSystem::ExistsColor(rColorName)", errstr.str(), 202);
1274 return ExistsColor(colorIndex);
1278 TEMP std::string THIS::ColorName(
Idx colorIndex)
const {
1279 return ColorSymbolTablep()->Symbol(colorIndex);
1283 TEMP Idx THIS::ColorIndex(
const std::string& rColorName)
const {
1284 return ColorSymbolTablep()->Index(rColorName);
1289 return ColorName(index);
1293 TEMP bool THIS::ExistsColor(
Idx stateIndex,
Idx colorIndex)
const {
1294 #ifdef FAUDES_CHECKED
1296 return BASE::pStates->Attribute(stateIndex).Colors().Exists(colorIndex);
1299 std::stringstream errstr;
1300 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1301 throw Exception(
"MtcSystem::ExistsColor(stateIndex, colorIndex)", errstr.str(), 200);
1304 return BASE::pStates->Attribute(stateIndex).Colors().Exists(colorIndex);
1309 TEMP void THIS::DotWrite(
const std::string& rFileName)
const {
1310 FD_DG(
"TmtcGenerator(" <<
this <<
")::DotWrite(" << rFileName <<
")");
1312 StateSet::Iterator it;
1313 BASE::SetMinStateIndexMap();
1316 std::ofstream stream;
1317 stream.exceptions(std::ios::badbit|std::ios::failbit);
1318 stream.open(rFileName.c_str());
1319 stream <<
"// dot output generated by libFAUDES TmtcGenerator" << std::endl;
1320 stream <<
"digraph \"" << BASE::Name() <<
"\" {" << std::endl;
1321 stream <<
" rankdir=LR" << std::endl;
1322 stream <<
" node [shape=circle];" << std::endl;
1323 stream << std::endl;
1324 stream <<
" // initial states" << std::endl;
1326 for (it = BASE::InitStatesBegin(); it != BASE::InitStatesEnd(); ++it) {
1327 std::string xname= BASE::StateName(*it);
1328 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1329 stream <<
" dot_dummyinit_" << i <<
" [shape=none, label=\"\" ];" << std::endl;
1330 stream <<
" dot_dummyinit_" << i <<
" -> \"" << xname <<
"\";" << std::endl;
1333 stream << std::endl;
1336 stream <<
" // uncolored states" << std::endl;
1337 for (it = BASE::pStates->Begin(); it != BASE::pStates->End(); ++it) {
1339 const StateAttr& attr = BASE::StateAttribute(*it);
1341 if (!attr.IsDefault()) {
1342 coloredStates.
Insert(*it, attr);
1345 std::string xname=BASE::StateName(*it);
1347 stream <<
" \"" << xname <<
"\";" << std::endl;
1352 std::vector<std::string> ColorVector;
1353 ColorVector.push_back(
"blue");
1354 ColorVector.push_back(
"red");
1355 ColorVector.push_back(
"magenta");
1356 ColorVector.push_back(
"green");
1357 ColorVector.push_back(
"darkorange");
1358 ColorVector.push_back(
"cyan");
1359 ColorVector.push_back(
"navy");
1360 ColorVector.push_back(
"brown");
1361 ColorVector.push_back(
"green4");
1362 ColorVector.push_back(
"yellow");
1363 ColorVector.push_back(
"darkviolet");
1364 ColorVector.push_back(
"firebrick");
1365 ColorVector.push_back(
"greenyellow");
1366 ColorVector.push_back(
"peru");
1367 ColorVector.push_back(
"skyblue");
1368 ColorVector.push_back(
"darkgreen");
1369 ColorVector.push_back(
"violetred");
1370 ColorVector.push_back(
"lightsalmon");
1371 ColorVector.push_back(
"seagreen");
1372 ColorVector.push_back(
"saddlebrown");
1373 ColorVector.push_back(
"slateblue");
1374 ColorVector.push_back(
"thistle");
1375 ColorVector.push_back(
"turquoise4");
1376 ColorVector.push_back(
"gold2");
1377 ColorVector.push_back(
"sandybrown");
1378 ColorVector.push_back(
"aquamarine3");
1379 ColorVector.push_back(
"darkolivegreen");
1380 ColorVector.push_back(
"yellow4");
1382 stream << std::endl;
1383 stream <<
" // colored states" << std::endl;
1385 for (it = coloredStates.
Begin(); it != coloredStates.
End(); it++) {
1387 std::string xname=BASE::StateName(*it);
1388 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1389 const StateAttr& attr = coloredStates.
Attribute(*it);
1390 if (attr.Colors().Size() > 1) {
1391 for(ColorSet::Iterator cit = attr.ColorsBegin(); cit != attr.ColorsEnd(); cit++) {
1392 stream <<
" subgraph cluster_" << clustNr++ <<
" {color=" << ColorVector.at(*cit-1) <<
";" << std::endl;
1395 stream <<
" \"" << xname <<
"\" " << std::endl <<
" ";
1396 for (
int i=0; i<count; i++) {
1399 stream << std::endl;
1401 else if (attr.Colors().Size() == 1) {
1402 ColorSet::Iterator cit;
1403 if ((cit = attr.ColorsBegin()) != attr.ColorsEnd())
1404 stream <<
" \"" << xname <<
"\" " <<
"[color=" << ColorVector.at(*cit-1) <<
"]" << std::endl;
1407 stream << std::endl;
1410 stream <<
" // marked states" << std::endl;
1411 for (it = BASE::MarkedStatesBegin(); it != BASE::MarkedStatesEnd(); ++it) {
1412 std::string xname= BASE::StateName(*it);
1413 if(xname==
"") xname=
ToStringInteger(static_cast<long int>(BASE::MinStateIndex(*it)));
1414 stream <<
" \"" << xname <<
"\";" << std::endl;
1419 stream << std::endl;
1420 stream <<
" // transition relation" << std::endl;
1421 for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
1422 std::string x1name= BASE::StateName(tit->X1);
1424 std::string x2name= BASE::StateName(tit->X2);
1426 stream <<
" \"" << x1name <<
"\" -> \"" << x2name <<
"\" [label=\"" << BASE::EventName(tit->Ev) <<
"\"];" << std::endl;
1428 stream <<
"};" << std::endl;
1431 catch (std::ios::failure&) {
1432 throw Exception(
"TaGenerator::DotWrite",
1433 "Exception opening/writing dotfile \""+rFileName+
"\"", 3);
1435 BASE::ClearMinStateIndexMap();
1440 typename std::map<Idx,ColorSet>
1441 THIS::StateColorMap(
void)
const {
1442 StateSet::Iterator lit;
1443 std::map<Idx,ColorSet> map;
1444 StateSet cstates = ColoredStates();
1445 for (lit = cstates.
Begin(); lit != cstates.
End(); lit++) {
1446 map.insert(std::make_pair(*lit, Colors(*lit)));
1447 FD_DF(
"make_pair(" <<
ToStringInteger(*lit) <<
", " << (THIS::Colors(*lit).ToString()) <<
")");
1454 StateSet::Iterator lit;
1456 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1457 if(ExistsColor(*lit, colorIndex)) coloredStates.
Insert(*lit);
1459 return coloredStates;
1464 Idx colorIndex = ColorIndex(rColorName);
1465 return ColoredStates(colorIndex);
1470 StateSet::Iterator lit;
1472 for(lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1473 if(IsColored(*lit)) coloredStates.
Insert(*lit);
1475 return coloredStates;
1480 StateSet::Iterator lit;
1482 for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1483 if (!IsColored(*lit)) uncoloredStates.
Insert(*lit);
1485 return uncoloredStates;
1489 TEMP bool THIS::IsColored(
void)
const {
1490 StateSet::Iterator lit;
1491 for(lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); lit++) {
1492 if(IsColored(*lit))
return true;
1499 #ifdef FAUDES_CHECKED
1501 return !BASE::pStates->Attribute(stateIndex).Colors().Empty();
1504 std::stringstream errstr;
1505 errstr <<
"State index " << stateIndex <<
" not member of set" << std::endl;
1506 throw Exception(
"MtcSystem::IsColored(stateIndex)", errstr.str(), 200);
1509 return !BASE::pStates->Attribute(stateIndex).Colors().Empty();
1515 StateSet stronglyCoac, notStronglyCoac;
1517 stronglyCoac = BASE::States();
1520 ColorSet::Iterator cit = colors.
Begin();
1521 ColorSet::Iterator cit_end = colors.
End();
1523 for (; cit != cit_end; cit++) {
1525 StateSet states = ColoredStates(*cit);
1528 tmp2 = stronglyCoac * tmp1;
1529 stronglyCoac = tmp2;
1532 return stronglyCoac;
1536 StateSet stronglyCoacSet = StronglyCoaccessibleSet();
1537 StateSet notStronglyCoac = BASE::States() - stronglyCoacSet;
1538 BASE::DelStates(notStronglyCoac);
1539 return (StronglyCoaccessibleSet() == BASE::States());
1543 return (StronglyCoaccessibleSet() == BASE::States());
1548 FD_DF(
"TmtcGenerator::StronglyTrimSet: trim states: "
1549 <<
StateSet(BASE::AccessibleSet() * StronglyCoaccessibleSet()).ToString());
1550 return BASE::AccessibleSet() * StronglyCoaccessibleSet();
1558 FD_DF(
"TmtcGenerator::StronglyTrim: trim states: " << (THIS::mpStates->ToString()));
1559 if (accessiblebool && coaccessiblebool) {
1560 FD_DF(
"TmtcGenerator::StronglyTrim: generator is trim");
1563 FD_DF(
"TmtcGenerator::StronglyTrim: generator is not trim");
1570 FD_DF(
"TmtcGenerator::IsStronglyTrim: generator is strongly trim");
1574 FD_DF(
"TmtcGenerator::IsStronglyTrim: generator is not strongly trim");
1580 TEMP void THIS::NewColorSymbolTable() {
1582 StateSet::Iterator lit;
1585 StateSet coloredStates = ColoredStates();
1586 coloredStates.
Write();
1591 for (lit = coloredStates.
Begin(); lit != coloredStates.
End(); lit++) {
1593 attr = BASE::pStates->Attributep(*lit);
1594 ColorSet::Iterator cit, cit_end;
1595 cit_end = attr->ColorsEnd();
1597 for (cit = attr->ColorsBegin(); cit != cit_end; cit++) {
1598 #ifdef FAUDES_CHECKED
1601 tmp->
InsEntry(*cit, attr->Colors().SymbolicName(*cit));
1604 std::stringstream errstr;
1605 errstr <<
"Could not insert index \"" << *cit <<
"\" and symbol \"" <<
1606 attr->Colors().SymbolicName(*cit) <<
"\" to symbol table" << std::endl;
1607 throw Exception(
"MtcSystem::NewColorSymbolTable()", errstr.str(), 44);
1610 tmp->
InsEntry(*cit, attr->Colors().SymbolicName(*cit));
1614 mpColorSymbolTable = tmp;
1615 attr->ColorSymTab(tmp);
1620 TEMP void THIS::ClearStateAttributes() {
1622 BASE::ClearStateAttributes();
1629 std::set<Idx> colcount;
1630 StateSet::Iterator sit;
1631 for(sit = BASE::StatesBegin(); sit != BASE::StatesEnd(); sit++) {
1632 colcount.insert(Colors(*sit).Size());
1635 std::multiset<Idx>::iterator cit;
1636 std::stringstream countstr;
1637 for(cit = colcount.begin(); cit != colcount.end(); cit++) {
1638 countstr << *cit <<
" ";
1641 BASE::DoSWrite(rTw);