libFAUDES

Sections

Index

mtc_1_generators.cpp File Reference


Detailed Description

Tutorial, mtcGenerator methods.

/** @file mtc_1_generators.cpp 

Tutorial,  mtcGenerator methods.

@ingroup Tutorials 

@include mtc_1_generators.cpp
*/


#include "libfaudes.h"
#include "mtc_include.h"


// for simplicity we make the faudes namespace available to our program

using namespace faudes;


/////////////////
// main program
/////////////////

int main() {

  /***************************************************************
   * constructor and file io
   *
   ***************************************************************/

  // At first we create an empty mtcGenerator object
  mtcGenerator g1;

  // insert states
  Idx s1 = g1.InsState("up");
  Idx s2 = g1.InsInitState("middle");

  // insert events
  Idx e1, e2;
  e1 = g1.InsEvent("go_up");
  e2 = g1.InsEvent("go_down");

  // set transitions - by name or index
  // prefer working with indices instead of names,
  // it's faster, as no name look-up is necessary
  g1.SetTransition("middle", "go_up", "up");
  g1.SetTransition(s1, e2, s2);

  // generate further mtcGenerator objects by reading a generator from a file...
  mtcGenerator g2("data/1_mtc_methods_1.gen");

  mtcGenerator g3;
  g3.Read("data/1_mtc_methods_1.gen");

  // ...or by copying a generator
  mtcGenerator g4;
  g1.Copy(g4);

  // Output of mtcGenerator to file
  g3.Write("results/1_mtc_methods_output_1.gen");
  g1.Write("results/1_mtc_methods_output_2.gen");

  // Write mtcGenerator to console (for debugging)
  std::cout << "##########################################" << std::endl;
  std::cout << "# mtcgenerators - constructors and file io" << std::endl;
  std::cout << std::endl << "# mtcGenerator without colors:" << std::endl << std::endl;
  g1.DWrite();
  std::cout << std::endl << "# mtcGenerator with colors:" << std::endl << std::endl;
  g2.DWrite();
  std::cout << std::endl << "# same mtcGenerator, but read from file using the Read() method:" << std::endl << std::endl;
  g3.DWrite();
  std::cout << "##########################################" << std::endl << std::endl;

  
  /***************************************************************
   * output to .dot-file for Graphviz visualization
   *
   ***************************************************************/

   // Generator .dot-file for Graphviz, a tool for graphical output of our automata
  // run mtc2dot.sh from console afterwards to get .png-images of the automata

  // output of the mtcGenerator to the dot format, where states are colored
  g1.DotWrite("results/1_mtc_methods_output_1.dot");

  // Graphical output of the mtcGenerator
  g2.GraphWrite("results/1_mtc_methods_output_2.png");


  /***************************************************************
   * handling state attributes - colored states
   *
   ***************************************************************/

  // insert a new colored state
  Idx s3 = g1.InsColoredState("down", "low");

  // set further transitions
  g1.SetTransition(s2, e2, s3);
  g1.SetTransition(s3, e1, s2);

  // set a color for an existing state
  Idx c1 = g1.InsColor(s1, "high");
  g1.InsColor(s2, c1);

          // test output
          g1.Write("results/1_mtc_methods_output_3.gen");
          g1.GraphWrite("results/1_mtc_methods_output_3.png");


  // the color label of the second state is wrong
  // ==> delete it
  g1.DelColor(s2, "high");

          // test output
          g1.Write("results/1_mtc_methods_output_4.gen");
          g1.GraphWrite("results/1_mtc_methods_output_4.png");

  // find out index of a color
  Idx c3 = g1.ColorIndex("high");
  std::cout << "Index of color \"high\": " << c3 << std::endl;

  // find out name of color
  std::string color3 = g1.ColorName(c3);
  std::cout << "Color name for index " << c3 << ": " << color3 << std::endl;

  // delete one color from all states
  // g1.DelColor("high");
  g1.DelColor(c3);

          // test output
          g1.Write("results/1_mtc_methods_output_5.gen");
          g1.GraphWrite("results/1_mtc_methods_output_5.png");

  // delete all colors from all states
  g1.ClrStateAttributes();

          // test output
          g1.Write("results/1_mtc_methods_output_6.gen");
          g1.GraphWrite("results/1_mtc_methods_output_6.png");

  // reinsert color "high"
  g1.InsColor(s1, c3);

          // test output
          g1.Write("results/1_mtc_methods_output_7.gen");
          g1.GraphWrite("results/1_mtc_methods_output_7.png");

  // delete all colors from a particular state
  g1.ClrStateAttribute(s1);

          // test output
          g1.Write("results/1_mtc_methods_output_8.gen");
          g1.GraphWrite("results/1_mtc_methods_output_8.png");

  // reinsert color
  g1.InsColor(s1, c1);

          // test output
          g1.Write("results/1_mtc_methods_output_9.gen");
          g1.GraphWrite("results/1_mtc_methods_output_9.png");

  // lookup name of reinserted color
  std::string name = g1.ColorName(c1);
  // Color names are not deleted from symbol table when the global
  // color symbol table is used. If there is a local symbol table, color
  // names will be deleted when they are removed from one state and are
  // not used anywhere else in the generator
  std::cout << "Color name for index " << c1 << ": " << name << std::endl;
  
  // check if color exists in generator
  // (therefore iterate over all states -> expensive method)
  if(!g1.ExistsColor(c1)) 
    std::cout << "Color c1 does not exist in g1 anymore" << std::endl;
  else 
    std::cout << "Color c1 exists in g1" << std::endl;

    // check if a color exists in a particular state
  if(!g1.ExistsColor(s2, c1))
    std::cout << "State s2 is not colored by color c1" << std::endl;
  if(g1.ExistsColor(s1, c1))
    std::cout << "State s1 is colored by color " << g1.CStr(c1) << std::endl;

  // Collect all colors of the generator
  // directly inserts colors into the specified reference of a color set
  ColorSet allColors1;
  g1.Colors(allColors1);
  
  // creates temporary color set, inserts all colors,
  // and copies the set to allcolors2 afterwards
  ColorSet allColors2 = g1.Colors();

  // Get all colors of a particular state
  ColorSet allStateColors1;
  g1.StateColors(s1, allStateColors1);

  // or, same as above
  ColorSet allStateColors2 = g1.StateColors(s1);

  // print color set allStateColors2 to console
  allStateColors2.DWrite();
  
  // clear mtcGenerator g1
  g1.Clear();


/////////////////////////////////////////////////////
// Local color symbol table
/////////////////////////////////////////////////////

{
  mtcGenerator gen, copygen;

  Idx st1 = gen.InsInitState("1");
  Idx st2 = gen.InsState("2");

  Idx eva = gen.InsEvent("a");
  Idx evb = gen.InsEvent("b");
  Idx evc = gen.InsEvent("c");

  Idx c1 = gen.InsColor(st1, "first");
  Idx c2 = gen.InsColor(st2, "second");

  gen.SetTransition(st1, eva, st2);
  gen.SetTransition(st2, evb, st2);
  gen.SetTransition(st2, evc, st1);

  gen.Write("results/1_mtc_methods_output_locSymTable_1.gen");
  gen.GraphWrite("results/1_mtc_methods_output_locSymTable_1.png");

  std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
  std::cout << "gen: Color index of first = " << gen.ColorIndex("first") << std::endl;

  // generate new color symbol table for gen,
  // all color labels already contained in gen are copied
  gen.NewColorSymbolTable();

  std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
  std::cout << "gen: Color index of first = " << gen.ColorIndex("first") << std::endl;

  gen.Copy(copygen);

  gen.DelColor(c1);

  gen.Write("results/1_mtc_methods_output_locSymTable_2.gen");
  gen.GraphWrite("results/1_mtc_methods_output_locSymTable_2.png");

  std::cout << "copygen: Color name of c1 = " << copygen.ColorName(c1) << std::endl;
  std::cout << "copygen: Color index of first = " << copygen.ColorIndex("first") << std::endl;

  Idx c3 = copygen.InsColor(st1, "New_Color");

  copygen.Write("results/1_mtc_methods_output_locSymTable_3.gen");
  copygen.GraphWrite("results/1_mtc_methods_output_locSymTable_3.png");


  if (!gen.ExistsColor(c1)) std::cout << "gen: Index c1 does not exist!" << std::endl;
    else std::cout << "gen: Index c1 exists!" << std::endl;
  try {
    std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
  }
  catch (faudes::Exception& exception){
    std::cout << "gen: Color name of c1 does not exist any more" << std::endl;
  }

  if (!gen.ExistsColor(c3)) std::cout << "gen: Index c3 does not exist!" << std::endl;
    else std::cout << "gen: Index c3 exists!" << std::endl;
  try {
    std::cout << "gen: Color name of c3 = " << gen.ColorName(c1) << std::endl;
  }
  catch (faudes::Exception& exception){
    std::cout << "gen: Color name of c3 does not exist any more" << std::endl;
  }


  if (!copygen.ExistsColor(c1)) std::cout << "copygen: Index c1 does not exist!" << std::endl;
    else std::cout << "copygen: Index c1 exists!" << std::endl;
  try {
    std::cout << "copygen: Color name of c1 = " << copygen.ColorName(c1) << std::endl;
  }
  catch (faudes::Exception& exception){
    std::cout << "copygen: Color name of c1 does not exist any more" << std::endl;
  }

  if (!copygen.ExistsColor(c3)) std::cout << "copygen: Index c3 does not exist!" << std::endl;
    else std::cout << "copygen: Index c3 exists!" << std::endl;
  try {
    std::cout << "copygen: Color name of c3 = " << copygen.ColorName(c1) << std::endl;
  }
  catch (faudes::Exception& exception){
    std::cout << "copygen: Color name of c3 does not exist any more" << std::endl;
  }

}

  return 0;
}

#include "libfaudes.h"
#include "mtc_include.h"

Functions

int main ()


Function Documentation

int main (  ) 

libFAUDES 2.13a c++ source docu by doxygen 1.5.6