Index: mttroot/mtt/bin/trans/dia2abg.pl ================================================================== --- mttroot/mtt/bin/trans/dia2abg.pl +++ mttroot/mtt/bin/trans/dia2abg.pl @@ -98,11 +98,13 @@ my $label_file = ''; my $component_list_file = ''; my $debug = 0; my $create_component_list = 0; my $create_abg = 0; +my $create_ibg = 1; my $abg_file = ''; +my $ibg_file = ''; my $change_flow_causality = ''; my $change_effort_causality = ''; GetOptions ('diagram_name=s' => \$diagram_name, 'dia_input_file=s' => \$dia_input_file, 'dia_output_file=s' => \$dia_output_file, @@ -110,10 +112,11 @@ 'component_list_file=s' => \$component_list_file, 'debug' => \$debug, 'create_component_list' => \$create_component_list, 'create_abg' => \$create_abg, 'abg_file=s' => \$abg_file, + 'ibg_file=s' => \$ibg_file, 'change_flow_causality=s' => \$change_flow_causality, 'change_effort_causality=s' => \$change_effort_causality, ); die usage() if $diagram_name eq ''; @@ -121,10 +124,11 @@ # Use defaults if necessary: $dia_input_file = $diagram_name . "_abg.dia" if ($dia_input_file eq ''); $dia_output_file = $diagram_name . "_cbg.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 ''); +$ibg_file = $diagram_name . "_ibg.m" if ($ibg_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); @@ -151,10 +155,19 @@ output_abg(); output_bond_causality(); parse_aliases(); print OUT "endfunction\n"; } + +if ($create_ibg) { + open (OUT,">$ibg_file") || + die "Cannot open $ibg_file for writing.\n"; + # don't bother about creating cmp.m simultaneously + # don't need getting label data + output_ibg(); + +} 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; @@ -262,10 +275,36 @@ # Subsystems and Ports EOF } + +sub output_ibg_header { + my ($date); + $date = `date`; chomp($date); + + print OUT <<"EOF"; +## -*-octave-*- put Emacs into Octave mode + +function [${diagram_name}] = ${diagram_name}_ibg + + ## Intermediate bond graph representation of $diagram_name + ## Generated by MTT on $date + + ## head refers to the harpoon end of a bond + ## tail refers to the other end of a bond + ## causality.? is the end at which ? is imposed + + +EOF +} + +sub output_ibg_footer { + print OUT <<"EOF"; +endfunction +EOF +} sub output_component { my ($NM,$type,$cr,$arg,$rep,$stat,$connections,$subsys_or_port) = @_; $_=$NM; remove_brackets(); $NM = $_; @@ -367,10 +406,80 @@ $i++; } } print OUT "\n"; } + +sub output_ibg { + my (%components,$key,$component,$type,$name, + %reverse_mtt_bond_id_index,$mtt_bond_id,$dia_bond_id, + @bonds, + $bond_id,$start,$end,$id, + $head,$head_component,$head_type,$head_name, + $tail,$tail_component,$tail_type,$tail_name, + $anon_id); + + # copy component_id_tag and assign names to anonymous components + %components = %component_id_tag; + + $anon_id = 0; + while (($id, $component) = each(%components)) { + $component =~ s/\#//g; + ($type, $name) = split (/:/, $component); + if (! $name) { + $component = "${component}:mtt${type}_${anon_id}"; + } + $anon_id++; + $components{$id} = $component; + } + + output_ibg_header(); + + %reverse_mtt_bond_id_index = reverse (%mtt_bond_id_index); + +# while (($mtt_bond_id, $dia_bond_id) = each(%reverse_mtt_bond_id_index)) { + @bonds = (sort keys (%reverse_mtt_bond_id_index)); + foreach $mtt_bond_id (@bonds) { + + $dia_bond_id = $reverse_mtt_bond_id_index{$mtt_bond_id}; + + $start = $bond_id_start_id{$dia_bond_id}; + $end = $bond_id_end_id {$dia_bond_id}; + + if ($bond_id_arrow_on_start{$dia_bond_id}) { + $head = $end; + $tail = $start; + } else { + $head = $start; + $tail = $end; + } + + $head_component = $components{$head}; + $tail_component = $components{$tail}; + + print OUT + " ## bond $mtt_bond_id \n" . + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "head.component = \"${head_component}\";\n" . + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "tail.component = \"${tail_component}\";\n"; + + print OUT + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "head.ports = \"FIXME!\";\n" . + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "tail.ports = \"FIXME!\";\n"; + + print OUT + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "causality.effort = \"FIXME!\";\n" . + " ${diagram_name}.bonds.bond${mtt_bond_id_index{$dia_bond_id}}." . + "causality.flow = \"FIXME!\";\n\n"; + } + + output_ibg_footer(); +} sub output_bond_causality { my ($mtt_bond_id,$dia_bond_id,$mtt_flow_causality,$mtt_effort_causality, %reverse_mtt_bond_id_index);