IBM
Contents Index Previous Next



Solutions to the Examples


This section contains fully implemented solutions to each example given in the Examples. It will also contain the expected output when executing each TTCN Access application with the example test suite as input.

Makefile

This generic Makefile has been used for compilation and linking:

#----------------------------------------------------------
# Name: Makefile
#
# Destcription:
# Generic Makefile for Access applications
# Note that the makefile requires the variable 
# itexaccess to be set.
#----------------------------------------------------------

ACCESS-INCL = $(itexaccess)/include
ACCESS-LIB = $(itexaccess)/lib

TARGET = the name of the target 
OBJS = $(TARGET).o

#------------------------------------------------------

$(TARGET) :	 $(OBJS)
	 $(CCC) -L$(ACCESS-LIB) -Bstatic -o $@ $(OBJS) -laccess
	 strip $@

#------------------------------------------------------

clean :
	 /bin/rm $(TARGET) $(OBJS)

#----------------------------------------------------------

.SUFFIXES: .cc .o

.cc.o :
	 $(CCC) -I$(ACCESS-INCL) -c -o $@ $<

Solution 1

Code

#include <ITEXAccessClasses.hh>
#include <ITEXAccessVisitor.hh>

class Trav : public AccessVisitor
{
public:
  void VisitIdentifier( const Identifier& id );
};


void Trav::VisitIdentifier( const Identifier& Me )
{
  printf( "Identifier %s\n", (const char*) Me );
}


int main( int /* argc */, char **argv )
{
  AccessSuite suite;

  if( suite.open( argv[ 1 ] ) )   // OK open database?           
   {
      Trav visitor;

      visitor.Visit( suite );

      if( suite.close( ) )      // OK close database?
        return 1;     
      else                      // Error close database!
        return 0;
   }

  else                          // Error open database!
    return -1; 
}

Output

> ./print-ids ../Example_Suite_A.itex
Identifier Example_Suite_A
Identifier Example_Suite_A
Identifier L
Identifier LOWER_PCO
Identifier SEND
Identifier LOWER_PCO
Identifier Field_1
Identifier Field_2
Identifier RECEIVE
Identifier LOWER_PCO
Identifier Field_1
Identifier Field_2
Identifier S1
Identifier SEND
Identifier Field_1
Identifier Field_2
Identifier R1
Identifier RECEIVE
Identifier Field_1
Identifier Field_2
Identifier TEST_CASE_1
Identifier L
Identifier SEND
Identifier S1
Identifier TEST_STEP_1
Identifier TEST_STEP_1
Identifier L
Identifier RECEIVE
Identifier R1
Identifier L
>

Solution 2

Code

#include <ITEXAccessClasses.hh>
#include <ITEXAccessVisitor.hh>

class Trav : public AccessVisitor
{
public:
  void VisitIdentifier( const Identifier& Me );
};


int main( int /* argc */, char **argv )
{
  AccessSuite suite;

  if( suite.open( argv[ 1 ] ) )   // OK open database?           
   {
      Trav visitor;

      visitor.Visit( suite );

      if( suite.close( ) )      // OK close database?
        return 1;     
      else                      // Error close database!
        return 0;
   }

  else                          // Error open database!
    return -1; 
}


void Trav::VisitIdentifier( const Identifier& Me )
{
  switch( Me.choice( ) ) 
    {
      case Choices::c_TTCN_PDU:
        printf( "Identifier `%s' is a TTCN PDU type\n",
                (const char*) Me );
        break;

      case Choices::c_TTCN_PDU_Cons:
        printf( "Identifier `%s' is a TTCN PDU constraint\n",
                (const char*) Me );
        break;

      default:
        break;
    }
}

Output

> ./print-ids ../Example_Suite_A.itex

Identifier `SEND' is a TTCN PDU type
Identifier `RECEIVE' is a TTCN PDU type
Identifier `S1' is a TTCN PDU constraint
Identifier `SEND' is a TTCN PDU type
Identifier `R1' is a TTCN PDU constraint
Identifier `RECEIVE' is a TTCN PDU type
Identifier `SEND' is a TTCN PDU type
Identifier `S1' is a TTCN PDU constraint
Identifier `RECEIVE' is a TTCN PDU type
Identifier `R1' is a TTCN PDU constraint
>

Solution 3

Code

#include <access.hh>

void id_pre_TTCN_PDU_TypeDef(const TTCN_PDU_TypeDef& Me);
void id_pre_TTCN_PDU_Constraint(const TTCN_PDU_Constraint& 
Me);

int main( int /* argc */, char **argv )
{
  AccessSuite suite;

  if( suite->open( argv[ 1 ] ) )   // OK open database?
    {
      // Setup the id writing functions
      pre_TTCN_PDU_TypeDef = id_pre_TTCN_PDU_TypeDef;
      pre_TTCN_PDU_Constraint = id_pre_TTCN_PDU_Constraint;

      // Start traversing from root.
      if( !suite->trav_root() )
        return 1;
      
      if( suite->close() )         // OK close database?
        return 0;
       else                        // Error close database!
        return 2;
    }
  
  else                             // Error open database!
   return -1; 
}


void id_pre_TTCN_PDU_TypeDef(const TTCN_PDU_TypeDef& Me)
{
  printf( "Identifier `%s' is a TTCN PDU type\n",
           (const char*) Me->pdu_Id()->pdu_IdAndFullId()
           ->pdu_Identifier() );
}


void id_pre_TTCN_PDU_Constraint(const TTCN_PDU_Constraint& Me)
{
  printf( "Identifier `%s' is a TTCN PDU constraint\n",
          (const char*) Me->consId()->consIdAndParList()
          ->constraintIdentifier() );
}

Output

> print-ids ../Example_Suite_A.itex

Identifier `SEND' is a TTCN PDU type
Identifier `RECEIVE' is a TTCN PDU type
Identifier `S1' is a TTCN PDU constraint
Identifier `R1' is a TTCN PDU constraint
>

Solution 4

Code

#include <ITEXAccessClasses.hh>
#include <ITEXAccessVisitor.hh>

class Trav : public AccessVisitor
{
public:
  void VisitTTCN_PDU_TypeDef( const TTCN_PDU_TypeDef& Me );
  void VisitPDU_FieldDcl( const PDU_FieldDcl& Me );
};

Astring get_type_str(const AccessNode& n);
Astring get_id(const AccessNode& n);

int main( int /* argc */, char **argv )
{
  AccessSuite suite;

  if( suite.open( argv[ 1 ] ) )   // OK open database?
    {
      Trav visitor;

      visitor.Visit( suite );

      if( suite.close() )         // OK close database?
        return 0;
      else                        // Error close database!
        return 2;
    }
  
  else                            // Error open database!
    return -1;
}


void Trav::VisitTTCN_PDU_TypeDef( const TTCN_PDU_TypeDef& Me )
{
  // Generate the header part

  cout << Me.pdu_Id( ) << "_encode( const ";
  cout << Me.pdu_Id( ) << "& me, char* enc_buf )";
  cout << "\n{" << endl;

  // Traverse fields using default traverser

  AccessVisitor::VisitPDU_FieldDcls( Me.pdu_FieldDcls( ) );

  // Generate the footer part

  cout << "}" << endl;
}


void Trav::VisitPDU_FieldDcl( const PDU_FieldDcl& Me )
{
  printf( "  %s %s;\n", 
          (const char*) get_type_str( Me.pdu_FieldType( ), 
          (const char*) get_id( Me.pdu_FieldId( ) );
}

// Help functions

Astring get_type_str(const AccessNode& n)
{
  switch ( n.choice( ) )
    {
    case Choices::c_PDU_FieldType:
      return n.PDU_FieldType().content();

    default:
      fprintf( stderr, "Bad case in get_type %d\n",
               (int) n.choice( ) );
      abort();
    }
}


Astring get_id(const AccessNode& n)
{
  switch ( n.choice( ) )
    {
      case Choices::c_PDU_FieldId:
           if ( n.PDU_FieldId( ).pdu_FieldIdOrMacro( )
                .choice( )
                == Choices::c_MacroSymbol )
           return n.PDU_FieldId( ).pdu_FieldIdOrMacro( )
                  .macroSymbol( );
        else
           return n.PDU_FieldId( ).pdu_FieldIdOrMacro( )
                  .pdu_FieldIdAndFullId( )    
                  .pdu_FieldIdentifier( );
      
      case Choices::c_PDU_Id:
        return n.PDU_Id( ).pdu_IdAndFullId( )
               .pdu_Identifier( );

      default:
        fprintf( stderr, "Bad case in get_id %d\n",
                 (int) n.choice( ) );
        abort( );
    }
}

Output

> ./pdu2c ../Example_Suite_A.itex

typedef struct {
  INTEGER Field_1;
  BOOLEAN Field_2;
} SEND;

typedef struct {
  INTEGER Field_1;
  BOOLEAN Field_2;
} RECEIVE;
>


http://www.ibm.com/rational
Contents Index Previous Next