File mttroot/mtt/bin/trans/mtt_update artifact 12b2c25398 part of check-in 4be02de99d


#!/bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: mtt_update
# Generates Updates core representations
# Copyright (C) 2000 by Peter J. Gawthrop

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.21  2003/08/14 10:33:46  gawthrop
## Ignore if and endif
##
## Revision 1.20  2003/08/13 15:49:39  gawthrop
## Don't sort the states when defaulting - leave in implied order
##
## Revision 1.19  2003/08/05 15:29:36  gawthrop
## Now handles ss.r files with implicit zero values
##
## Revision 1.18  2003/08/04 09:10:30  gawthrop
## Now handles steady-state computation for _state.txt.
##
## Revision 1.17  2002/04/28 18:41:27  geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.16  2000/10/17 13:45:18  peterg
## split up state and input - different defaults
##
## Revision 1.15  2000/10/17 09:53:05  peterg
## More on logic rep
##
## Revision 1.14  2000/10/17 08:36:56  peterg
## Included logic rep.
##
## Revision 1.13  2000/10/15 09:27:13  peterg
## Better way of generating $infofile
##
## Revision 1.12  2000/10/14 16:11:00  peterg
## Made all variables, states etc lower case
##
## Revision 1.11  2000/10/13 11:07:00  peterg
## Added initialisation for state and input
##
## Revision 1.10  2000/10/13 10:56:07  peterg
## Now uses the full name from the struc file - no need to recreate.
##
## Revision 1.9  2000/10/13 09:55:09  peterg
## Changed state/input name to $4_$3
##
## Revision 1.8  2000/10/13 09:28:16  peterg
## Changed regexp to /\(.*$old.*=\)/
##
## Revision 1.7  2000/10/13 08:56:51  peterg
## Output variables as column, not row.
##
## Revision 1.6  2000/10/13 08:07:24  peterg
## Added state and input - it checks against sympar as well so that
## defined variables may be used here.
##
## Revision 1.5  2000/10/12 19:24:55  peterg
## Corrected output message
##
## Revision 1.4  2000/10/12 17:57:34  peterg
## Fixed header typos
##
## Revision 1.3  2000/10/12 15:11:30  peterg
## Added the update switch
##
## Revision 1.2  2000/10/12 13:45:13  peterg
## Put in the no-file version
##
## Revision 1.1  2000/10/12 12:32:23  peterg
## Initial revision
##
##
###############################################################


sys=$1 # System name
rep=$2 # System representation
update=$3 # Update or not
date=`date`

if [ -z "$2" ]; then
  echo Usage mtt_update system representation [update]    
  exit
fi

## Extract initial value from steady-state information
initial_value() {
    i=$1
    default=$2
    ssfile=${sys}_ss.r
    if [ "${rep}" = "state" ]; then
       if [ -f "${ssfile}" ]; then
	count=`grep -c "^MTTx($i" ${ssfile}`; # See if its there
	if [ $count = "1" ]; then
	    value=`grep "^MTTx($i" ${ssfile}| sed 's/ //g' | gawk -F '=' '{print substr($2,1,length($2)-2)}'`	    
	else
	    value="0.0"
	fi
       else
	value=${default}   
       fi 
    else
	value=${default}
    fi
}

## Remove if, end and endif from list
remove_if() {
    file=$1
    if [ -f ${file} ]; then
      tmpfile="$1_tmp"
    
      mv ${file} ${tmpfile}
      grep -v 'if[ (]\|endif;' <${tmpfile} > ${file} 

      ## Delete empty file
      size=`ls -s ${file} | awk '{print $1}'`
      if [ "${size}" = "0" ]; then
	  rm $file
      fi
    fi
 }
 rm -f mtt_empty
 touch mtt_empty

 case $rep in
     numpar)
	     lang=txt
	     textfile=$1_$2.$lang
	     infofile=$1_sympar.$lang
	     moreinfofile=$infofile
	     default='1.0'
	 ;;
     input)
	     lang=txt
	     textfile=$1_$2.$lang
	     infofile=mtt_list.$lang
	     moreinfofile=mtt_list_numpar.$lang
	     gawk '{if ($1==rep) printf("%s\n", $4)}' rep=$2 \
	     <$1_struc.txt >$infofile
	     cat $infofile $1_sympar.$lang> $moreinfofile  
	     default='1.0'
	  ;;
     state)
	     lang=txt
	     textfile=$1_$2.$lang
	     infofile=mtt_list.$lang
	     moreinfofile=mtt_list_numpar.$lang
	     gawk '{if ($1==rep) printf("%s\n", $4)}' rep=$2 \
	     <$1_struc.txt >$infofile
	     cat $infofile $1_sympar.$lang> $moreinfofile  
	     default='0.0'
	  ;;
     logic) 
	     lang=txt
	     textfile=$1_$2.$lang
	     infofile=mtt_list.$lang
	     moreinfofile=mtt_list_numpar.$lang
	     gawk '{if ($3=="MTT_SWITCH") printf("%s_logic\n", $4)}' rep=$2 \
	     <$1_struc.txt >$infofile
	     cat $infofile $1_sympar.$lang> $moreinfofile  
	     default='1.0'
	 ;;
     *)
	     echo Representation $rep not implemented
	     exit
 esac

 # Create a string containing the variables
 if [ -f "$textfile" ]; then
   # Inform User
   echo Checking $textfile
 else
   echo Creating $textfile
   mtt_header $sys $rep $lang > $textfile 
     if [ "${rep}" = "state" ]; then
	 info=`cut -f1 $infofile` # No sort
     else
	 info=`cut -f1 $infofile | sort` # Sort
     fi
   i=0;
   for new in $info; do
     let i=$i+1;
     initial_value $i ${default}
     echo $new $value  |\
     gawk '{printf("%s\t= %s; # Default\n",$1,$2)}' >> $textfile
   done
   exit    
 fi

 # Remove tmp files
 rm -f mtt_in_text mtt_in_info

 # Strip files
 strip_comments <$textfile | tr 'A-Z' 'a-z' >mtt_text
 strip_comments <$infofile | tr 'A-Z' 'a-z' >mtt_info
 strip_comments <$moreinfofile  | tr 'A-Z' 'a-z' >mtt_moreinfo

 # Use awk to check file
 gawk '{
   if (FILENAME=="mtt_text") {
     gsub("[\t ]" ,""); # Remove whitespace
     split($0,a,"=");
     text[a[1]] = a[2];
   }
   if (FILENAME=="mtt_info"){
      info[$1] = default;
   }
   if (FILENAME=="mtt_moreinfo"){
      moreinfo[$1] = default;
   }
   }
 END{
   # for (iname in info) print iname
   for (tname in text) {
     if (!(tname in moreinfo)) print tname >> "mtt_in_text"
   }
   for (iname in info) {
     if (!(iname in text)) print iname >> "mtt_in_info"
   }
   }'  default=$default mtt_text mtt_info mtt_moreinfo

 ## Remove if, end and endif from list
 remove_if mtt_in_text

if [ -z "$update" ]; then
  if [ -f "mtt_in_text" ]; then
    echo "The following variables are defined in $textfile, but do not exist:"
    sort mtt_in_text | gawk '{printf("\t%s\n",$1)}'
    echo Use mtt $sys $rep $lang to update
  fi
  if [ -f "mtt_in_info" ]; then
    echo "The following variables exist, but are not defined in $textfile:"
    sort mtt_in_info | gawk '{printf("\t%s\n",$1)}'
    echo Use mtt $sys $rep $lang to update
  fi
  exit    
fi
answered=''
if [ -f "mtt_in_text" ]; then
  in_text=`sort mtt_in_text`
  echo "The following variables are defined in $textfile, but do not exist:"
  sort mtt_in_text | gawk '{printf("\t%s\n",$1)}'


  while [ -z "$answered" ]; do
    echo "Update $textfile (y/n)?"
    read answer < /dev/tty
    case $answer in
	y)
	    answered=yes
 	    for old in $in_text; do
              echo Commenting out $old
	      sed "s/\(.*$old.*=\)/## Removed by MTT on $date: \1/I"\
               <$textfile > mtt_tmp
              mv mtt_tmp $textfile
              changed=yes
	    done
	    ;;
	n)
	    answered=yes
	    ;;
	*)
    esac
  done      
fi

answered=''
if [ -f "mtt_in_info" ]; then
  echo "The following variables exist, but are not defined in $textfile:"
  sort mtt_in_info | gawk '{printf("\t%s\n",$1)}'

  while [ -z "$answered" ]; do
    echo "Update $textfile (y/n)?"
    read answer < /dev/tty
    case $answer in
	y)
	    answered=yes
            in_info=`sort mtt_in_info`
 	    for new in $in_info; do
                echo Adding $new
                echo $new $default  |\
                gawk '{printf("%s\t= %s; # Added by MTT on %s\n",$1,$2,strftime())}'\
                >> $textfile
                changed=yes
	    done
	    ;;
	n)
	    answered=yes
	    ;;
	*)
    esac
  done      
fi


MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]