Differences From Artifact [d1b6aaad6f]:

To Artifact [ec747e75b4]:


1
2
3
4
5
6
7
8
9
10
11
## -*-octave-*-

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

  rbonds
  rstrokes
  rcomponents
  port_coord
  port_name


|
|







1
2
3
4
5
6
7
8
9
10
11
## -*-octave-*-

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

  rbonds
  rstrokes
  rcomponents
  port_coord
  port_name
︙︙
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
  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,1) = 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);
  endfor
  comp_near_bond

  ## Deduce causality from the strokes (if any)
  causality = zeros(n_bonds,2);
  if (n_strokes > 0)







|

|












|

|
















|


|








|







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
  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,1) = 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);
  endfor
  comp_near_bond

  ## Deduce causality from the strokes (if any)
  causality = zeros(n_bonds,2);
  if (n_strokes > 0)
︙︙
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		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];







|






|







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		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];
︙︙
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  causality

  ## Write data
  for i = 1:n_bonds
    [hc_type, hc_name] = eval([name, '_cmp(comp_near_bond(i,1))']);
    [tc_type, tc_name] = eval([name, '_cmp(comp_near_bond(i,2))']);
    ## components
    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));
    ## ports
    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));
    ## causality
    if (causality(i,1) == 1)
      effort_causality = "head"







|

|







148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  causality

  ## Write data
  for i = 1:n_bonds
    [hc_type, hc_name] = eval([name, '_cmp(comp_near_bond(i,1))']);
    [tc_type, tc_name] = eval([name, '_cmp(comp_near_bond(i,2))']);
    ## components
    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));
    ## ports
    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));
    ## causality
    if (causality(i,1) == 1)
      effort_causality = "head"
︙︙

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