Overview
Comment:Started adding rbg2abg functionality to ibg2abg.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: ebd768f4424ba3c1ffcc6d96d7a60892088d7cfbbd1c709994c02e303263a59e
User & Date: geraint@users.sourceforge.net on 2003-01-29 23:31:58
Other Links: branch diff | manifest | tags
Context
2003-01-31
11:49:22
Added aliasing and causality (vectorisation is still to do). check-in: 75fde5922d user: geraint@users.sourceforge.net tags: origin/master, trunk
2003-01-29
23:31:58
Started adding rbg2abg functionality to ibg2abg. check-in: ebd768f442 user: geraint@users.sourceforge.net tags: origin/master, trunk
2003-01-24
21:05:30
Moved creation time to top of README. check-in: eb0321dc23 user: geraint@users.sourceforge.net tags: origin/master, trunk
Changes

Added mttroot/mtt/bin/trans/ibg2abg_m version [9cef41324f].





















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /bin/sh

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

# Bourne shell script: ibg2abg_m
# Based on rbg2abg_m

while [ -n "`echo $1 | grep '^-'`" ]; do
    case $1 in
	-I )
	    info=info ;;
	-nounits )
	    units=no ;;
	*)
	    echo "$1 is an invalid argument - ignoring" ;;
    esac
    shift
done

# Set up some vars
sys=$1
lbl_file=${sys}_lbl.txt
abg_file=${sys}_abg.m
err=mtt_error.txt
log=ibg2abg_m.log

# Remove the old log file
rm -f ibg2abg_m.log
rm -f ${abg_file}

# Inform user
echo Creating ${abg_file}

# Use matrix manipulation to accomplish the transformation
${MATRIX} > ${log} 2> ${err} <<EOF
  name = '$1'
  infofile = fopen('mtt_info.txt', 'w');
  errorfile = fopen('mtt_error.txt', 'w');
  
  ## Interpret data from the ibg representation
  ibonds = $1_ibg;
  bonds = ibonds.bonds;

  [bonds,components,n_vector_bonds] = ibg2abg(name,bonds, \
					      infofile,errorfile)

  ## Write the acausal bond graph m-file
  write_abg(name,bonds,components,n_vector_bonds);
EOF

# Set the units for the abg file
test_units()
{
      grep '^[\s]*[#|%]UNITS' < ${lbl_file} >/dev/null
  if [ $? = "0" ]; then 
    grep '^[\s]*[#|%]UNITS' < ${lbl_file} |\
    gawk '{
      printf("mtt_units.sh %s %s %s %s %s\n", sys, $2, $3, $4, $5)
    }' sys=${sys}  | sh | grep ERROR
    if [ $? = "0" ]; then
      echo "    " *MTT_ERRROR: domains and units are not OK - exiting
      exit 1  
    else
      echo "    " domains and units are OK 
    fi
  else
    echo "  no domains or units declared"
  fi
} 

check_ports_exist()
{
 declared_ports=`grep '^[\s]*[#|%]UNITS' < ${lbl_file} | gawk '{print $2}'`
  for declared_port in $declared_ports; do
    grep "${sys}\.ports\.${declared_port}\.type" ${abg_file} >/dev/null
    if [ $? = "1" ]; then
      echo "*MTT_ERROR: Units declared for non-existent port ${declared_port}"
      exit 1
    fi
  done
}

set_units()
{ 
  grep '^[\s]*[#|%]UNITS' < ${lbl_file} |\
  gawk '{
    printf("  %s.ports.%s.domain = \"%s\";\n", sys, $2, $3);
    printf("  %s.ports.%s.units.effort = \"%s\";\n", sys, $2, $4);
    printf("  %s.ports.%s.units.flow = \"%s\";\n", sys, $2, $5);
  }' sys=${sys}
} 

if [ -z "$units" ]; then
  echo Checking port domains and units
  check_ports_exist;
  test_units;
  echo "## Port domain and units" >> ${abg_file}
  set_units >> ${abg_file}
fi

# Append any VAR declarations
if [ -f "$1_lbl.txt" ]; then
  echo "## Explicit variable declarations" >> ${abg_file}
  grep '^[\s]*[%|#][V|P]AR' $1_lbl.txt | tr '%' '#' >> ${abg_file}    
  grep '^[\s]*[%|#]NOT[V|P]AR' $1_lbl.txt | tr '%' '#' >> ${abg_file}    
fi

# Close off the function
echo "endfunction" >> ${abg_file}

# Errors and info
if [ "$info" = "info" ]; then
    cat mtt_info.txt
fi

if mtt_error mtt_error.txt; then
    exit 0
else
    exit 1
fi

Added mttroot/mtt/bin/trans/m/ibg2abg.m version [55f6b30f06].























































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
## -*-octave-*-

function [bonds,components,n_vector_bonds] = ibg2abg(name,bonds, \
						     infofile, \
						     errorfile)

  ## Provide useful data for log
  struct_levels_to_print = 5;

  ## Find number of bonds
  [n_bonds, junk] = size(struct_elements(bonds));

  ## Get components
  max_bonds_on_component = 0;

  for i = 1:n_bonds
    eval(sprintf("bond = bonds.b%i", i));

    ## heads
    head_type_name = bond.head.component;
    head_type_name = deblank(split(head_type_name, ":"));
    head_type = head_type_name(1,:);
    head_name = head_type_name(2,:);
    if (exist("component_struct"))
      if (struct_contains(component_struct, head_name))
	## create a copy to work on - clearer than eval(sprintf(...))
	## (pointers would be better still!)
	eval(sprintf("head = component_struct.%s;", head_name));
      endif
    endif
    head.type = head_type;
    if (!struct_contains(head,"bonds"))
      head.n_bonds = 1;
      head.bonds = [+i];
      head.named_ports = !strcmp(bond.head.ports, "[]");
    else
      head.n_bonds = head.n_bonds + 1;
      head.bonds = [head.bonds, +i];
      head.named_ports = head.named_ports + !strcmp(bond.head.ports, \
						    "[]");
    endif
    eval(sprintf("head.ports.bond%i.name = '%s'", i, bond.head.ports));
    eval(sprintf("head.ports.bond%i.sign = '[in]'", i));
    max_bonds_on_component = max(max_bonds_on_component, \
				 head.n_bonds)
    eval(sprintf("component_struct.%s = head", head_name));
    clear head;

    ## tails
    tail_type_name = bond.tail.component;
    tail_type_name = deblank(split(tail_type_name, ":"));
    tail_type = tail_type_name(1,:);
    tail_name = tail_type_name(2,:);
    if (exist("component_struct"))
      if (struct_contains(component_struct, tail_name))
	## create a copy to work on - clearer than eval(sprintf(...))
	tail = eval(sprintf("tail = component_struct.%s", tail_name));
      endif
    endif
    tail.type = tail_type;
    if (!struct_contains(tail,"bonds"))
      tail.n_bonds = 1;
      tail.bonds = [-i];
      tail.named_ports = !strcmp(bond.tail.ports, "[]");
    else
      tail.n_bonds = tail.n_bonds + 1
      tail.bonds = [tail.bonds, -i];
      tail.named_ports = tail.named_ports + !strcmp(bond.tail.ports, \
						    "[]");
    endif
    eval(sprintf("tail.ports.bond%i.name = '%s'", i, bond.tail.ports));
    eval(sprintf("tail.ports.bond%i.sign = '[out]'", i));
    max_bonds_on_component = max(max_bonds_on_component, \
				 tail.n_bonds);
    eval(sprintf("component_struct.%s = tail", tail_name));
    clear tail;

  endfor
  
  ## Get component indices from sys_cmp.m
  [n_components, junk] = size(struct_elements(component_struct));
  for i = 1:n_components
    [comp_type, comp_name] = eval(sprintf("%s_cmp(%i)", name, i));
    eval(sprintf("component_struct.%s.index = %i", comp_name, i));
  endfor

  ## Create the connections matrix (components)
  components = zeros(n_components, max_bonds_on_component);
  for [comp, comp_name] = component_struct
    components(comp.index,1:comp.n_bonds) = comp.bonds;
  endfor
  
  for [comp, comp_name] = component_struct
    ## Unalias and/or default all ports on this component
    if ((strcmp(deblank(comp.type), "0")) || (strcmp(deblank(comp.type), "1")))
      disp("---- default junctions ----");
      if (comp.named_ports == 1) # one named port
	for [port, bond_number] = comp.ports
	  if (!strcmp(port.name,"[]"))
	    junction_port_name = port.name;
	  endif
	endfor
	mtt_info(sprintf("Defaulting all ports on junction %s to %s", \
			 comp_name, junction_port_name));
	for [port, bond_number] = comp.ports
	  port.name = junction_port_name;
	endfor	
      elseif ((comp.named_ports != 0) && (comp.named_ports != \
					  comp.n_bonds)) # not allowed
	mtt_error(sprintf("Junction %s must have 0, 1 or %i port labels", \
			  comp_name, comp.n_bonds));
      endif
    else			# Not a junction
      unlabelled_ports = comp.n_bonds - comp.named_ports;
      if (unlabelled_ports == 1)
	## find unlabelled ports
	for [port, bond_number] = comp.ports
	  if (strcmp(deblank(port.name), "[]"))
	    ## found it - default to "in" or "out"
	    eval(sprintf("comp.ports.%s.name = port.sign", bond_number));
	    mtt_info(["Defaulting port name [" port.sign "]\t on component " \
		      comp_name " (" deblank(comp.type) ")"], infofile);
	  endif
	endfor
      elseif (unlabelled_ports == 2)
	## find unlabelled ports
	for [port, bond_number] = comp.ports
	  if (strcmp(deblank(port.name), "[]"))
	    ## got one
	    if (exist("unlabelled_port1"))
	      unlabelled_port2 = port;
	      bond_number2 = bond_number;
	    else
	      unlabelled_port1 = port;
	      bond_number1 = bond_number;
	    endif
	  endif
	endfor
	if (strcmp(unlabelled_port1.sign, "[in]") && \
	    strcmp(unlabelled_port2.sign, "[in]"))
	  mtt_error(["More than one unlabelled INport on component " \
		     comp_name " (" deblank(comp.type) ")"], errorfile);
	elseif (strcmp(unlabelled_port1.sign, "[out]") && \
		strcmp(unlabelled_port2.sign, "[out]"))
	  mtt_error(["More than one unlabelled OUTport on component " \
		     comp_name " (" deblank(comp.type) ")"], errorfile);
	else
	  eval(sprintf("comp.ports.%s.name = \
	  unlabelled_port1.sign", bond_number1));
	  mtt_info(["Defaulting port name [" unlabelled_port1.sign
		    "]\t on component "  comp_name " (" \
		    deblank(comp.type) ")"], infofile);
	  eval(sprintf("comp.ports.%s.name = \
	  unlabelled_port2.sign", bond_number2));
	  mtt_info(["Defaulting port name [" unlabelled_port2.sign
		    "]\t on component "  comp_name " (" \
		    deblank(comp.type) ")"], infofile);
	endif
	## ???	
      endif
    ## ???	
    endif
    ## ???
    eval(sprintf("component_struct.%s = comp", comp_name));    
  endfor
component_struct;
  ## ???
  bonds;
  components;
  n_vector_bonds = 1;
endfunction


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