#!/bin/sh
######################################
##### Model Transformation Tools #####
######################################
# Bourne shell script: abg2lbl_fig2txt
#
# Acausal bond graph to causal bond graph: mfile format
###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.27 2002/05/10 09:08:55 gawthrop
## Added EMTF and INTF
## Fixed bug in writing out component headings
##
## Revision 1.26 2002/04/28 18:41:26 geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.25 2001/10/15 14:27:00 gawthrop
## Now handles [1:N] style port labels
##
## Revision 1.24 2001/08/02 03:24:48 geraint
## Replaced mtt_version.sh with mtt_banner.sh - I think this was the intent.
##
## Revision 1.23 2001/07/26 04:08:35 gawthrop
## Removed lines deleting _type.sh and cbg.m
## -- how did they get there ??
##
## Revision 1.22 2001/07/08 03:28:11 gawthrop
## Fixed a bug: abg2sympar_m2txt neads a _lbl.txt file when using
## "usinglabel" option
##
## Revision 1.21 2001/07/06 00:46:50 gawthrop
## Added -cr option -- forces cr to be loaded before the ese.r file
## This avoids causality problems when using multi-port Rs to represent
## arbitary equations
##
## Revision 1.20 2001/07/04 05:19:57 gawthrop
## Added RT & CT to list
##
## Revision 1.19 2001/07/03 23:51:10 gawthrop
## Now puts outline #Summary and #Description lines at top of lbl file
##
## Revision 1.18 2001/06/13 18:24:10 gawthrop
## Made "lin" default in place of "none"
## Still needs arg and cr alias clashes to be sorted ....
##
## Revision 1.17 2001/06/13 10:41:06 gawthrop
## Further changes towards aouto creation of lbl files.
## Prettified lbl files
##
## Revision 1.16 2001/06/11 19:43:49 gawthrop
## MTT is now much more sophisticated in generating lbl files
## Labels can contain maths
## Repetative components are now broken
##
## Revision 1.15 2001/06/11 16:53:49 gawthrop
## Now uses abg2sympar_m2txt to get the arguments
##
## Revision 1.14 2001/06/11 15:06:58 gawthrop
## Now handles user-defined components
##
## Revision 1.13 2001/06/04 08:13:37 gawthrop
## Various changes to support PPP
##
## Revision 1.12 2001/05/08 15:18:10 gawthrop
## Added trig and hyperbolic functions to argument exclusion list
##
## Revision 1.11 2000/09/19 11:14:30 peterg
## Now writes the first component type header correctely
##
## Revision 1.10 2000/01/26 10:11:10 peterg
## Added I component
##
## Revision 1.9 1999/11/10 00:47:08 peterg
## Replaced ifs by a table of cr/arg information
##
## Revision 1.8 1999/11/09 22:32:41 peterg
## Under RCS ready for using arrays to contaain defaults.
##
## Revision 1.7 1998/07/04 10:37:21 peterg
## Major revision to include:
## aliases
## new Style
## prettyfied
##
## Revision 1.6 1998/03/05 10:09:47 peterg
## Corrected bug in writing "other" components
##
## Revision 1.5 1998/03/02 09:26:18 peterg
## Now does default CR and args for the basic components only
## SS,I,R,C,GY,TF
## C now has a default of effort input.
##
## Revision 1.4 1998/02/23 16:20:33 peterg
## Summary line just contains the model name
##
## Revision 1.3 1997/05/09 14:21:35 peterg
## Default to flow,component_name
##
# Revision 1.2 1997/03/19 12:08:01 peterg
# No longer writes out non-unique names - now done in rbg_fig2m
#
# Revision 1.1 1997/03/18 13:55:01 peterg
# Initial revision
#
###############################################################
# P.J.Gawthrop March 1997
# Copyright (c) P.J.Gawthrop, 1997
infofile='mtt_info.txt'
typefile="$1_type.sh"
sensitivity=$2;
system=$1
outfile=$1_lbl.txt
if [ -n "$sensitivity" ]; then
sys="-s ${system}"
else
sys=${system}
fi
# Remove unwanted files
rm -f abg2lbl_fig2txt.log
rm -f $infofile
rm -f mtt_junk*
## Create lbl files beneath this one
mtt -q -u -l 1 ${sys} sub sh # Create the list
sh $1_sub.sh "abg2lbl_fig2txt " " $sensitivity"
if [ -f "$1_lbl.txt" ]; then
##echo "$1_lbl.txt exists in `pwd` - no action taken"
exit
##else
##echo "$1_lbl.txt dosn not exist in `pwd` "
fi
#Inform user
if [ -n "$sensitivity" ]; then
echo "Creating ${outfile} (sensitivity version)"
else
echo "Creating ${outfile}"
fi
write_header()
{
cat<<EOF
#SUMMARY ${system}
#DESCRIPTION Detailed description here
## System ${system}, representation lbl, language txt
## File ${system}_lbl.txt
## Generated by MTT on `date`
EOF
cat $MTT_DOC/mtt_banner.sh
}
write_blurb()
{
cat <<EOF
## Each line should be of one of the following forms:
## a comment (ie starting with #)
## component-name cr_name arg1,arg2,..argn
## blank
## ---- Component labels ----
EOF
}
create_lbl_body()
{
## Find names of all components
grep -v '\[[0-9]*:[0-9]*\]' $1_abg.fig |\
gawk '/:/ {print $NF}' | \
sed 's/\\001//' | \
sort | \
tee $1_raw_list | \
sort -u> $1_unique_raw_list
#Write out non-unique names
#echo 'Non-unique names (if any):'
#diff $1_unique_raw_list $1_raw_list | grep '>' | sed 's/>/ /'
#Write out the outline lbl file
grep -v '\[[0-9]*\]' $1_unique_raw_list | \
gawk --field-separator ':' '
BEGIN {
OldComponent="None";
default_cr = "lin";
}
{
Component = $1;
Name = $2;
Arg = $3;
##Aliases
if (Component == "SS") {
port_alias[++j] = Name;
}
else {
alias[++i] = Name;
if (length(sensitivity)>0) {
s_arg = sprintf("%ss",Name);
alias[++i] = s_arg;
s_arg = sprintf(";%s",s_arg);
Component_type = Component;
Component = substr(Component,2);
lin = "slin"
}
else {
Component_type = Component;
lin = "lin";
}
}
## Table of components
cr["SS"] = "SS"; arg["SS"] = "external,external";
cr["Se"] = "SS"; arg["Se"] = sprintf("external%s", s_arg);
cr["Sf"] = "SS"; arg["Sf"] = sprintf("external%s", s_arg);
cr["De"] = "SS"; arg["De"] = "external";
cr["Df"] = "SS"; arg["Df"] = "external";
cr["R"] = lin; arg["R"] = sprintf("flow,%s%s", Name, s_arg);
cr["C"] = lin; arg["C"] = sprintf("effort,%s%s", Name, s_arg);
cr["I"] = lin; arg["I"] = sprintf("flow,%s%s", Name, s_arg);
cr["INTF"] = lin; arg["INTF"] = "";
cr["CS"] = "lin"; arg["CS"] = sprintf("effort,%s;%s_x0%s", Name, Name, s_arg);
cr["IS"] = "lin"; arg["IS"] = sprintf("flow,%s;%s_x0%s", Name, Name, s_arg);
cr["INTFS"] = "lin"; arg["INTFS"] = sprintf("%s_x0%s", Name, s_arg);
cr["TF"] = "lin"; arg["TF"] = sprintf("flow,%s%s", Name, s_arg);
cr["GY"] = "lin"; arg["GY"] = sprintf("flow,%s%s", Name, s_arg);
##cr["EMTF"] = sprintf("%s", Name); arg["EMTF"] = sprintf("l%s", s_arg);
cr["EMTF"] = "lin"; arg["EMTF"] = sprintf("flow,%s%s", Name, s_arg);
cr["AE"] = "lin"; arg["AE"] = sprintf("%s%s", Name, s_arg);
cr["AF"] = "lin"; arg["AF"] = sprintf("%s%s", Name, s_arg);
cr["AEf"] = "lin"; arg["AEf"] = sprintf("%s%s", Name, s_arg);
cr["AFe"] = "lin"; arg["AFe"] = sprintf("%s%s", Name, s_arg);
cr["CDx"] = "lin"; arg["CDx"] = sprintf("%s%s", Name, s_arg);
cr["FMR"] = lin; arg["FMR"] = sprintf("effort,%s%s", Name, s_arg);
cr["RS"] = lin; arg["RS"] = sprintf("flow,%s%s", Name, s_arg);
cr["RT"] = lin; arg["RT"] = sprintf("flow,%s%s", Name, s_arg);
cr["CT"] = lin; arg["CT"] = sprintf("effort,%s%s", Name, s_arg);
## Put in the explicit arguments
if (length(Arg)>0) {
arg[Component] = Arg;
if (match(Arg,"=")>0) # Its an equation type cr
cr[Component] = "cr"
else # assume a linear cr
cr[Component] = "lin";
}
## Print component header
if (Component != OldComponent) print "\n## Component type", Component_type
## Print Component
printf("\t%s\t", Name);
if (Component in cr)
printf("%s\t\t", cr[Component])
else
printf("%s\t\t", default_cr);
if (Component in arg)
printf("%s\t\t\n", arg[Component])
else {
print "lbl2args_txt2out", Component_type | "/bin/sh"; close("/bin/sh")
}
OldComponent=Component;
delete arg[Component]; # Zap the element
delete cr[Component]; # Zap the element
}
END{
print "\n## Port aliases" >> "mtt_junk_alias.txt";
for (k=1;k<=j;k++){
if (port_alias[k] ~ "\\["){ # Only do ports
port_name = substr(port_alias[k],2,length(port_alias[k])-2);
if (length(sensitivity)==0)
print "#ALIAS\t" port_name "\t" port_name >> "mtt_junk_alias.txt";
else
print "#ALIAS\t" port_name "\t" port_name "_1," port_name "_2" >> "mtt_junk_alias.txt";
}
}
# print "\n% Argument aliases" >> "mtt_junk_alias.txt";
# for (k=1;k<=i;k++){
# print "%ALIAS\t\$" k "\t" alias[k] >> "mtt_junk_alias.txt";
# }
}' sensitivity=$sensitivity
}
create_arg_aliases()
{
cat <<EOF
## Argument aliases
EOF
abg2sympar_m2txt ${system} ${system} use_label_file |\
gawk '{printf("#ALIAS\t$%i\t%s\n", ++i, $1)}'
}
## Main
create_lbl_body $1> $1_lbl.txt
create_arg_aliases > mtt_args.txt # using $1_lbl.txt
mv $1_lbl.txt mtt_junk_lbl.txt
write_header > ${outfile}
cat mtt_junk_alias.txt >> ${outfile}
cat mtt_args.txt >> ${outfile}
write_blurb >> ${outfile}
cat mtt_junk_lbl.txt >> ${outfile}
## Put a blank line at end
cat >> ${outfile} <<EOF
EOF
## Clean up
rm -f mtt_junk*