Overview
Comment:This function now expects bond graphs to be written using the
new dia 'MTT port' and 'MTT bond' objects.
Causal completion can be requested with command-line options.
A new .dia file is created with the requested causality changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: 53f8c2af762122e91579f4ad8af5bb1fd4dd25f2e3278367ed20c7ec0a69a054
User & Date: david-hoover@users.sourceforge.net on 2002-12-09 16:16:09
Other Links: branch diff | manifest | tags
Context
2002-12-11
12:36:12
Added R and P arguments so that ppp_quad_lin can be used here check-in: a736355170 user: gawthrop@users.sourceforge.net tags: origin/master, trunk
2002-12-09
16:16:09
This function now expects bond graphs to be written using the
new dia 'MTT port' and 'MTT bond' objects.
Causal completion can be requested with command-line options.
A new .dia file is created with the requested causality changes.
check-in: 53f8c2af76 user: david-hoover@users.sourceforge.net tags: origin/master, trunk
09:22:04
Updated name of input variable. check-in: bf214bfc84 user: geraint@users.sourceforge.net tags: origin/master, trunk
Changes

Modified mttroot/mtt/bin/trans/dia2abg.pl from [96410e2cea] to [d1ad1c13b1].

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
19
20
21
22
23
24
25

26


27




28
29



30


31
32
33
34
35
36
37
38







-
+
-
-
+
-
-
-
-
+
+
-
-
-
+
-
-
+







# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# The GNU General Public License should be found in the file license.txt.
# For more information about free software, visit http://www.fsf.org/
#----------------------------------------------------------------------------

#############################################################################
# Given a DIA diagram, and a mtt label file, this script writes an
# Given a DIA diagram, the script has functions that perform the following
# acausal bond graph in octave/matlab (.m) form, suitable for input to
# mtt, the model transformation tools.
# MTT (model transformation tools) functions:
#
# This script does not manage half-causal strokes.
#
# The dia arrow code is not quite adapted to bond graphs yet.  A
# 1. Write a _cmp.txt file containing component types:names
# 2. Write a _abg.m file containing an acausal bond graph suitable for input
# half-head arrow exists to indicate power flow.  Nothing appropriate
# for causal strokes is implemented, however.  The closest
# approximations are:
#    to Octave.
#	'slashed cross' = arrow with half head and causal stroke.
#	'cross' = arrow with causal stroke.
# 3. Modify a diagram by changing causality as desired.
#############################################################################

#----------------------------------------------------------------------------
# Dia uses a unique id for each object.
# get_component_data and get_bond_data read the xml file and collect
# important information about component and bond id's, component
# names, and bond connectivity.  These data structures are described here:
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
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







+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+


-
+

















-
+



-
+
+






+
+

-
+
+






+
+





-
+
+








-
+












+

-
+
-









+
+
+
+
+
-
+
-








# The %bond_id_start_id and %bond_id_end_id hashes provides
# connectivity info for line start points and end points:
#
# key=bond_id
# value=component_id

# The %bond_id_arrow_on_start hash is a boolean that indicates whether
# the power arrow (half head) is on the dia line start point.
#
# key=bond_id
# value=boolean arrow_on_start
# The %bond_id_start_arrow and %bond_id_end_arrow hashes provide arrow
# ending info.

# The %bond_id_effort_causality hash is a boolean that provides the
# effort arrow-oriented causality.
#
# key=bond_id
# value=arrow-oriented effort causality.

# The %bond_id_flow_causality hash is a boolean that provides the
# flow arrow-oriented causality.
#
# key=bond_id
# value=dia arrow number
# value=arrow-oriented flow causality.

# %mtt_bond_id_index provides a unique positive integer index for each
# Dia bond ID.  The index is written to the abg.m file for mtt.

# The %component_label_data hash is a hash of arrays.

# key=column 1 of label file
# value=list(order in label file, col1 of lbl file, col2 of lbl file, ...)
#----------------------------------------------------------------------------

#----------------------------MAIN PROGRAM------------------------------------
use strict;
use Getopt::Long;
use XML::DOM;

my (%component_id_tag, %bond_id_start_id, %bond_id_end_id,
    %component_label_data, $objects, %mtt_bond_id_index,
    %bond_id_start_arrow, %bond_id_end_arrow);
    %bond_id_arrow_on_start, %bond_id_flow_causality, %bond_id_effort_causality);

# Parse user options:
my $diagram_name = '';
my $dia_file = '';
my $dia_input_file = '';
my $dia_output_file = '';
my $label_file = '';
my $component_list_file = '';
my $debug = 0;
my $create_component_list = 0;
my $create_abg = 0;
my $abg_file = '';
my $change_flow_causality = '';
my $change_effort_causality = '';
GetOptions ('diagram_name=s' => \$diagram_name,
	    'dia_file=s' => \$dia_file,
	    'dia_input_file=s' => \$dia_input_file,
	    'dia_output_file=s' => \$dia_output_file,
	    'label_file=s' => \$label_file,
	    'component_list_file=s' => \$component_list_file,
	    'debug' => \$debug,
	    'create_component_list' => \$create_component_list,
	    'create_abg' => \$create_abg,
	    'abg_file=s' => \$abg_file,
	    'change_flow_causality=s' => \$change_flow_causality,
	    'change_effort_causality=s' => \$change_effort_causality,
	    );

die usage() if $diagram_name eq '';

# Use defaults if necessary:
$dia_file = $diagram_name . "_abg.dia" if ($dia_file eq '');
$dia_input_file = $diagram_name . "_abg.dia" if ($dia_input_file eq '');
$dia_output_file = $diagram_name . "_abg.dia" if ($dia_output_file eq '');
$label_file = $diagram_name . "_lbl.txt" if ($label_file eq '');
$abg_file = $diagram_name . "_abg.m" if ($abg_file eq '');
$component_list_file = $diagram_name . "_cmp.txt" if ($component_list_file eq '');

# Start Parsing XML, and creating files:
my $dom = new XML::DOM::Parser;
my ($doc);

$doc = $dom->parsefile($dia_file);
$doc = $dom->parsefile($dia_input_file);
$objects = get_objects_node($doc,"Bond Graph");

get_component_data($objects);
get_bond_data($objects);

create_component_list() if ($create_component_list);
if ($create_abg) {
    open (OUT,">$abg_file") ||
	die "Cannot open $abg_file for writing.\n";

    # Don't update the label file unless we are creating component list and abg simultaneously...
    if ($create_component_list) {
	print STDERR "WARNING: Label file may be stale.\n" 
	#system("abg2lbl_fig2txt -c $component_list_file $diagram_name") &&
	system("abg2lbl_fig2txt -x $diagram_name") &&
	#system("abg2lbl_fig2txt -x $diagram_name") && die "abg2lbl_fig2txt failed.";
	    die "abg2lbl_fig2txt failed.";
    }

    get_label_data();
    output_abg();
    output_bond_causality();
    parse_aliases();
    print OUT "endfunction\n";
}

if ($change_flow_causality ne '' || $change_effort_causality ne '') {
    open (DIA_OUT,">$dia_output_file") ||
	die "Cannot open $dia_output_file for writing.\n";
    print DIA_OUT $doc->toString;
    close DIA_OUT;

}
#print $doc->toString;

exit 0;


#----------------------------SUBROUTINES-------------------------------------
sub create_component_list {
    my ($name,@line,$i);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
248
249
250
251
252
253
254



































255
256
257
258
259
260
261







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







	$layer_node->getAttributeNode("name")->getValue eq $layer_name;
    
    $objects = $layer_node->getElementsByTagName('dia:object');

    return $objects;
}

# Return 1 if a half-stroke is on bond 'end', -1 on bond 'start', 0 otherwise.
sub get_sign_of_power {
    my ( $bond_id )= @_;
    my ($on_start,$on_end);

    $on_end = $bond_id_end_arrow{$bond_id}==6 || $bond_id_end_arrow{$bond_id}==7;
    $on_start = $bond_id_start_arrow{$bond_id}==6 || $bond_id_start_arrow{$bond_id}==7;

    die "On bond $bond_id, power flows in both directions!\n"
	if ($on_start && $on_end);

    return 1 if $on_end;
    return -1 if $on_start;

    die "No power direction on bond $bond_id\n";
    return 0;
}

# Return 1 if a causal-stroke is on bond 'end', -1 on bond 'start', 0 otherwise.
sub get_sign_of_causality {
    my ( $bond_id )= @_;

    my ($on_start,$on_end);

    $on_end = $bond_id_end_arrow{$bond_id}==7 || $bond_id_end_arrow{$bond_id}==21;
    $on_start = $bond_id_start_arrow{$bond_id}==7 || $bond_id_start_arrow{$bond_id}==21;

    die "On bond $bond_id, causality is defined in both directions!\n"
	if ($on_start && $on_end);

    return 1 if $on_end;
    return -1 if $on_start;
    return 0;
}

sub output_abg_header {
    my ($date);
    $date = `date`; chomp($date);

    print OUT <<"EOF";
function [${diagram_name}] =  ${diagram_name}_abg
# This function is the acausal bond graph representation of $diagram_name
334
335
336
337
338
339
340
341
342


343
344
345
346
347


348
349
350
351
352
353
354
355
316
317
318
319
320
321
322


323
324
325
326
327


328
329

330
331
332
333
334
335
336







-
-
+
+



-
-
+
+
-







	$type = id_to_type($id);

	$cr = "" unless defined($cr = $component_label_data{$NM}[1]);
	$arg = "" unless defined($arg = $component_label_data{$NM}[2]);

	@clist = ();
	while (($bond_id,$start) = each(%bond_id_start_id)) {
	    push(@clist, -get_sign_of_power($bond_id) * $mtt_bond_id_index{$bond_id})
		if $start eq $id;
	    push(@clist, ($bond_id_arrow_on_start{$bond_id} ? -1 : 1) *
		 $mtt_bond_id_index{$bond_id}) if $start eq $id;
	}
	while (($bond_id,$end) = each(%bond_id_end_id)) {
	    
	    push(@clist, get_sign_of_power($bond_id) * $mtt_bond_id_index{$bond_id})
		if $end eq $id;
	    push(@clist, ($bond_id_arrow_on_start{$bond_id} ? 1 : -1) *
		 $mtt_bond_id_index{$bond_id}) if $end eq $id;

	}
	$connections = join(" ",@clist);

	output_component($NM,$type,$cr,$arg,$rep,$stat,$connections,$subsys_or_port);
    }
    
    # order component id's so that entries found in _lbl.txt file are
388
389
390
391
392
393
394

395

396
397
398
399
400
401
402
403
404


405
406

407
408
409
410
411
412
413
414
415

416
417
418

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

436
437

438
439
440
441


442
443
444




445
446
447
448
449


450
451
452
453
454

455
456
457
458



459
460
461
462
463
464



















465
466
467
468
469
470
471
472

473
474
475
476

477
478
479
480
481
482
483
484
485




486
487
488
489
490
491
492
369
370
371
372
373
374
375
376

377
378
379
380
381
382
383
384


385
386
387

388
389
390
391
392
393
394
395
396

397
398
399

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

417
418

419
420
421
422
423
424
425



426
427
428
429





430
431

432
433
434

435
436



437
438
439
440





441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466

467
468
469
470

471
472
473
474
475
476
477



478
479
480
481
482
483
484
485
486
487
488







+
-
+







-
-
+
+

-
+








-
+


-
+
















-
+

-
+




+
+
-
-
-
+
+
+
+
-
-
-
-
-
+
+
-



-
+

-
-
-
+
+
+

-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







-
+



-
+






-
-
-
+
+
+
+







	    $i++;
	}
    }
    print OUT "\n";
}

sub output_bond_causality {
    my ($mtt_bond_id,$dia_bond_id,$mtt_flow_causality,$mtt_effort_causality,
    my ($mtt_bond_id,$dia_bond_id,$mtt_causality,%reverse_mtt_bond_id_index);
	%reverse_mtt_bond_id_index);

    print OUT "# Bonds\n";
    print OUT "  $diagram_name.bonds = [\n";

    %reverse_mtt_bond_id_index = reverse(%mtt_bond_id_index);

    while (($mtt_bond_id,$dia_bond_id) = each(%reverse_mtt_bond_id_index)) {
	$mtt_causality =
	    get_sign_of_power($dia_bond_id) * get_sign_of_causality($dia_bond_id);
	$mtt_flow_causality = $bond_id_flow_causality{$dia_bond_id};
	$mtt_effort_causality = $bond_id_effort_causality{$dia_bond_id};

	print OUT "      $mtt_causality $mtt_causality\n";
	print OUT "      $mtt_effort_causality $mtt_flow_causality\n";
    }
    print OUT "      ];\n\n";
}

sub get_component_data {
    my ( $objects_node )= @_;
    my($obj,$id,$attr,$comp,$strattr,$str_elem,$string);

    print_debug("READING COMPONENTS FROM $dia_file...\n");
    print_debug("READING COMPONENTS FROM $dia_input_file...\n");
    for my $i (0..$objects_node->getLength-1) {
      $obj = $objects_node->item($i);
      next if ($obj->getAttributeNode("type")->getValue ne "Flowchart - Box");
      next if ($obj->getAttributeNode("type")->getValue ne "BondGraph - MTT port");
      
      $id = $obj->getAttributeNode("id")->getValue;
      print_debug($id . "\n");
      
      $attr = get_first_subnode_by_nodename_attribute(0,$obj,"dia:attribute","name","text");
      $comp = get_first_subnode_by_nodename_attribute(0,$attr,"dia:composite","type","text");
      $strattr = get_first_subnode_by_nodename_attribute(0,$comp,"dia:attribute","name","string");
      $str_elem = get_first_element_subnode($strattr);
      $string = get_first_text_subnode($str_elem);
      $component_id_tag{$id} = $string->getData;
  }
    die "There are no components!\n" unless keys(%component_id_tag) > 0;
}

# Dia stores its attributes in a strange way, not using typical xml attributes.
sub get_dia_attribute_value {
    my ( $attribute_node )= @_;
    my ($type, $attribute_node )= @_;
    my ($subnode);
    $subnode = get_first_subnode_by_nodename_attribute(0,$attribute_node,"dia:enum");
    $subnode = get_first_subnode_by_nodename_attribute(0,$attribute_node,$type);

    return $subnode->getAttributeNode("val")->getValue;
}

# Dia stores its attributes in a strange way, not using typical xml attributes.
sub set_dia_attribute_value {
sub check_arrow_values {
    my ($arrow_number) = @_;
    
    my ($type, $attribute_node, $new_value )= @_;
    my ($subnode);
    $subnode = get_first_subnode_by_nodename_attribute(0,$attribute_node,$type);

    die "Lines can have the following endings: none, half-head (power),
	cross (causality), slashed-cross (power+causality).\n" if
	    ($arrow_number != 0 &&
	     $arrow_number != 6 &&
	     $arrow_number != 7 &&
    $subnode->setAttribute(val => $new_value);
#    return $subnode->getAttributeNode("val")->getValue;
	     $arrow_number != 21);
}

sub get_arrow_info {
    my ( $object_node, $id )= @_;
    my ( $object_node, $id, $id_index )= @_;
    my($attribute,$attributes);
    
    $attribute = get_first_subnode_by_nodename_attribute(1,$object_node, "dia:attribute", "name", "end_arrow");
    $bond_id_end_arrow{$id} = defined($attribute) ? get_dia_attribute_value($attribute) : 0;

    $attribute = get_first_subnode_by_nodename_attribute(1,$object_node, "dia:attribute", "name", "arrow_on_start");
    $bond_id_arrow_on_start{$id} = defined($attribute) ? get_dia_attribute_value("dia:boolean",$attribute) : 0;

    $attribute = get_first_subnode_by_nodename_attribute(1,$object_node, "dia:attribute", "name", "start_arrow");
    $bond_id_start_arrow{$id} = defined($attribute) ? get_dia_attribute_value($attribute) : 0;

    check_arrow_values($bond_id_start_arrow{$id});
    check_arrow_values($bond_id_end_arrow{$id});
    $attribute = get_first_subnode_by_nodename_attribute(1,$object_node, "dia:attribute", "name", "effort_causality");
    change_causality($id_index, $attribute, $change_effort_causality);
    $bond_id_effort_causality{$id} = defined($attribute) ? get_dia_attribute_value("dia:enum",$attribute)-1 : 1;

    $attribute = get_first_subnode_by_nodename_attribute(1,$object_node, "dia:attribute", "name", "flow_causality");
    change_causality($id_index, $attribute, $change_flow_causality);
    $bond_id_flow_causality{$id} = defined($attribute) ? get_dia_attribute_value("dia:enum",$attribute)-1 : 1;
}

sub change_causality() {
    my ($id_index, $attribute_node, $causality_change_string)=@_;
    my ($mtt_id, $arrow_oriented_causality);

    foreach my $id_causality (split(/;/,$causality_change_string)) {
	($mtt_id, $arrow_oriented_causality) = split(/:/,$id_causality);
	if ($mtt_id eq "all" || $id_index == $mtt_id) {
	    set_dia_attribute_value("dia:enum",$attribute_node,$arrow_oriented_causality + 1);
	}
    }
}

sub get_bond_data {
    my ( $objects_node )= @_;
    my ($id_index, $obj, $id, $connections, $connection, $to, $handle,
	$connections_att);

    print_debug("READING BONDS FROM $dia_file...\n");
    print_debug("READING BONDS FROM $dia_input_file...\n");
    $id_index = 0;
    for my $i (0..$objects_node->getLength-1) {
      $obj = $objects_node->item($i);
      next if ($obj->getAttributeNode("type")->getValue ne "Standard - Line");
      next if ($obj->getAttributeNode("type")->getValue ne "BondGraph - MTT bond");
      
      $id = $obj->getAttributeNode("id")->getValue;
      
      print_debug("Bond " . $id . ":\n");
      $mtt_bond_id_index{$id} = ++$id_index;
      
      get_arrow_info($obj,$id);
      print_debug("Start arrow:" . $bond_id_start_arrow{$id} . "\n");
      print_debug("End arrow:" . $bond_id_end_arrow{$id} . "\n");
      get_arrow_info($obj,$id,$id_index);
      print_debug("Flow causality ($id):" . $bond_id_flow_causality{$id} . "\n");
      print_debug("Effort causality ($id):" . $bond_id_effort_causality{$id} . "\n");
      print_debug("Arrow on start ($id):" . $bond_id_arrow_on_start{$id} . "\n");

      # get connection info
      $connections_att = $obj->getElementsByTagName('dia:connections');
      die "A bond without connections exists!\n"
	  unless $connections_att->getLength > 0;

      $connections = $connections_att->item(0)->getElementsByTagName('dia:connection');
609
610
611
612
613
614
615
616

617
618
619
620
621
622












623
624

605
606
607
608
609
610
611

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633







-
+






+
+
+
+
+
+
+
+
+
+
+
+


+
}

sub usage {
    return 
	"\n" .
	"Usage: dia2abg.pl --diagram_name <diagram_name> [options]\n" .
	"Options:\n" .
	"\t--dia_file <dia_file>\n" .
	"\t--dia_input_file <dia_input_file>\n" .
	"\t--label_file <label_file>\n" .
	"\t--component_list_file\n" .
	"\t--create_component_list\n" .
	"\t--create_abg\n" .
	"\t--debug\n" .
	"\t--abg_file <abg_file>\n" .
	"\t--change_flow_causality <bond causality spec>\n" .
	"\t--change_effort_causality <bond causality spec>\n" .
	"\n" .
	"\t\tBond causality spec:\n" .
	"\t\t 'bond:causality;bond:causality;...'\n" .
	"\t\tbond:\n" .
	"\t\t [mtt_bond_id|all]\n" .
	"\t\tcausality:\n" .
	"\t\t [-1|0|1]\n" .
	"\n" .
	"\t\tCausality is arrow-oriented-causality.\n" .
	"\t\tAny causality changes are made BEFORE further processing.\n" .
	"\n"
}


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