Overview
Comment:Attempting to add common intermediate representation between rbg and abg
so that a common method can be used to interpret dia and xfig diagrams
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: 8212047a2c33ebcc6d2d65633744ef90750379d0ef7ef68677a07c5134071a2b
User & Date: geraint@users.sourceforge.net on 2003-01-07 00:43:29
Other Links: branch diff | manifest | tags
Context
2003-01-07
09:11:53
Octavised check-in: 2a91d53c00 user: gawthrop@users.sourceforge.net tags: origin/master, trunk
00:43:29
Attempting to add common intermediate representation between rbg and abg
so that a common method can be used to interpret dia and xfig diagrams
check-in: 8212047a2c user: geraint@users.sourceforge.net tags: origin/master, trunk
2003-01-06
21:22:53
Updated SS entries. check-in: f972704606 user: geraint@users.sourceforge.net tags: origin/master, trunk
Changes

Added mttroot/mtt/bin/trans/m/rbg2ibg.m version [5558cbb136].





































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
## -*-octave-*-

function [bonds] = \
      rbg2ibg(name,rbonds,rstrokes,rcomponents,port_coord,port_name, \
	      infofile, errorfile)

  rbonds
  rstrokes
  rcomponents
  port_coord
  port_name

  ## Default to no components
  components = [0];

  ## Xfig scaling factor
  scale = 1200.0/2.54546;

  ## Rotation matrix
  rot = [0, -1; 1, 0];

  ## Find number of strokes
  [n_strokes,columns] = size(rstrokes);
  if ((columns ~= 4) & (n_strokes > 0))
    error('Incorrect rstrokes matrix: must have 4 columns');
  endif

  ## Find number of bonds
  [n_bonds,columns] = size(rbonds);
  if ((columns ~= 6) & (n_bonds > 0))
    error('Incorrect rbonds matrix: must have 6 columns');
  endif

  ## Find number of components
  [n_components,columns] = size(rcomponents);

  ## Find number of ports referred to
  [n_ports,columns] = size(port_coord);
  
  ## If port_name empty, make it empty string
  if (length(port_name) == 0)
    port_name="";
  endif
  
  ## Determine coordinates of arrow end of bond and other end
  other_end_1	= rbonds(:,1:2);
  arrow_end	= rbonds(:,3:4);
  other_end_2	= rbonds(:,5:6);

  distance_1	= length2d(other_end_1 - arrow_end);
  distance_2	= length2d(other_end_2 - arrow_end);
  which_end	= (distance_1 > distance_2) * [1, 1];
  one		= ones(size(which_end));
  other_end	= which_end .* other_end_1 + (one - which_end) .* \
      other_end_2;
  arrow_vector	= (which_end .* other_end_2 + (one - which_end) .* \
		   other_end_1) - arrow_end;
  
  ## Locate bond end nearest each port
  ## col 1 of port_near_bond contains a signed bond number (+ for arrow
  ## end)
  ## col 2 of port_near_bond contains the corresponding port index
  port_bond = [];
  for i = 1:n_ports
    near_bond = adjbond(port_coord(i,1:2), arrow_end, other_end);
    [rows,cols] = size(near_bond);
    if (rows > 1)
      error(sprintf ...
	    ("A port is near to more than one bond at coordinates \
	  %g,%g %s\n", ...
	     port_coord(i,1)/scale, port_coord(i,2)/scale, \
	     deblank(port_name(i,:))));
    endif
    
    ## The (signed) bond corresponding to the ith port label
    port_bond(i) = near_bond(1) * sign(1.5 - near_bond(2));
  endfor
  
  port_bond
  ## Now have (signed) bond (port_bond(i)) corresponding to the ith port
  ## Create inverse mapping
  for i = 1:n_bonds
    eval(sprintf('bond_port_head%i = "[]"', i))
    eval(sprintf('bond_port_tail%i = "[]"', i))
  endfor  
  for i = 1:n_ports
    if (port_bond(i) > 0)
      eval(sprintf('bond_port_head%i = "%s"', port_bond(i), \
		   deblank(port_name(i,:))))
    else
      eval(sprintf('bond_port_tail%i = "%s"', -port_bond(i), \
		   deblank(port_name(i,:))))
    endif
  endfor
  
  ## Locate the components at the ends of each bond
  ## col 1 of comp_near_bond contains component nearest to the arrow end
  ## col 2 of comp_near_bond contains component nearest other end
  for i = 1:n_bonds
    comp_near_bond(i,:) = adjcomp(arrow_end(i,:), other_end(i,:), \
				  rcomponents);
    [hc_type, hc_name] = eval([name, '_cmp(comp_near_bond(i,1))']);
    [tc_type, tc_name] = eval([name, '_cmp(comp_near_bond(i,2))']);
  endfor

  ## Deduce causality from the strokes (if any)
  causality = zeros(n_bonds,2);
  if (n_strokes > 0)
    ## Find location of centre and ends of stroke
    stroke_end_1	= [rstrokes(:,1), rstrokes(:,2)];
    stroke_end_2	= [rstrokes(:,3), rstrokes(:,4)];

    stroke_centre	= (stroke_end_1 + stroke_end_2)/2;
    stroke_vector	= (stroke_end_1 - stroke_end_2);
    stroke_length	= length2d(stroke_vector);

    for i = 1:n_strokes
      stroke = [stroke_centre(i,:)
		stroke_end_1(i,:)
		stroke_end_2(i,:)];


      ## Find the nearest bond end
      [index,distance] = adjbond(stroke(1,:),arrow_end,other_end);
      if (distance > (2 * stroke_length(i)))
	info = sprintf('Stroke at (%4.3f,%4.3f) is %4.3f away from the nearest bond\n', ...
		       stroke(1,1)/scale, stroke(1,2)/scale, \
		       distance/scale);
      endif

      ## Bond end coordinates
      j = index(1,1);
      which_end = (index(1,2) == 1);
      bond_end = arrow_end(j,:) * which_end + other_end(j,:) * (1 - \
								which_end);
      
      ## Now decide which bit of the stroke is nearest
      stroke_index = adjbond(bond_end, stroke, zeros(size(stroke)));

      if (stroke_index(1) == 1)	# uni-causal stroke
	causality(j,1:2) = (2 * which_end - 1) * [1, 1];
      else			# bi-causal stroke
	stroke_direction = stroke(1,:) - stroke(stroke_index(1),:);
	flow_side = stroke_direction * arrow_vector(j,:)' > 0;
	causality(j,1+flow_side) = 2 * which_end - 1;
      endif
    endfor
  endif

  ## Return data
  for i = 1:n_bonds
    eval(sprintf("bonds.bond%i.head.component\t= '%s:%s'", i, hc_type, \
		 hc_name));
    eval(sprintf("bonds.bond%i.tail.component\t= '%s:%s'", i, tc_type, \
		 tc_name));
    eval(sprintf("bonds.bond%i.head.ports\t= bond_port_head%i", i, i));
    eval(sprintf("bonds.bond%i.tail.ports\t= bond_port_tail%i", i, i));
  endfor
  
  
  
endfunction

Added mttroot/mtt/bin/trans/m/write_ibg.m version [8fcc691ebd].













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
## -*-octave-*-

function write_ibg(system_name,bonds);
  
  fid = fopen([system_name,"_ibg.m"], "w");
  [nbonds, junk] = size(struct_elements(bonds));
  format_hc = "  %s.bonds.b%i.head.component\t= \"%s\";\n";
  format_tc = "  %s.bonds.b%i.tail.component\t= \"%s\";\n";
  format_hp = "  %s.bonds.b%i.head.ports\t= \"%s\";\n";
  format_tp = "  %s.bonds.b%i.tail.ports\t= \"%s\";\n";
  format_ce = "  %s.bonds.b%i.causality.effort\t= \"%s\";\n";
  format_cf = "  %s.bonds.b%i.causality.flow\t= \"%s\";\n";

  fprintf(fid, "## -*-octave-*-\n\n");
  fprintf(fid, "function [%s] = %s_ibg\n\n", system_name, system_name);
  fprintf(fid, "## Intermediate bond graph representation of %s\n", \
	  system_name);

  fprintf(fid, "## Generated by MTT on %s\n", ctime(time));

  for i=1:nbonds
    fprintf(fid, "\n  ## bond %i\n", i);
    fprintf(fid, format_hc, system_name, i, \
	    eval(sprintf("bonds.bond%i.head.component", i)));
    fprintf(fid, format_tc, system_name, i, \
	    eval(sprintf("bonds.bond%i.tail.component", i)));
    fprintf(fid, format_hp, system_name, i, \
	    eval(sprintf("bonds.bond%i.head.ports", i)));
    fprintf(fid, format_tp, system_name, i, \
	    eval(sprintf("bonds.bond%i.tail.ports", i)));
    fprintf(fid, format_ce, system_name, i, "FIXME");
    fprintf(fid, format_cf, system_name, i, "FIXME");
  endfor;

  fprintf(fid, "\nendfunction\n");
  fclose(fid);

endfunction;

Added mttroot/mtt/bin/trans/rbg2ibg_m version [ac5dd715a8].

































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#! /bin/sh


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

# Bourne shell script: rbg2ibg_m
# Based on rbg2abg_m

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

# Set up some vars
sys=$1
ibg=${sys}_ibg.m
err=mtt_error.txt
log=rbg2ibg_m.log

# Remove the old log file
rm -f ${ibg}
rm -f ${log}

# Inform user
echo Creating ${ibg}

# 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 the connections from the fig version
	[rbonds,rstrokes,rcomponents,port_coord,port_name,port_list] = $1_rbg;
	[n_ports, junk] = size(port_list);

	## Strip the directional information
	rbonds = rbonds(:,1:6);

	[bonds] = \
	    rbg2ibg(name,rbonds,rstrokes,rcomponents,port_coord,port_name, \
		    infofile,errorfile);

	## Write the intermediate bond graph m-file
	write_ibg(name,bonds);
EOF

# 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


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