File mtt/bin/trans/m/write_abg.m artifact 1dcbed067b part of check-in a8cce33cfa


function write_abg(system_name,bonds,connections,n_vector_bonds);

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.11  2004/09/12 22:27:27  geraint
## Appended 't' to fopen mode string to open in text mode.
##
## Revision 1.10  2004/08/09 14:47:28  gawthrop
## Changed arg to args to avoid strange octave bug
##
## Revision 1.9  2001/04/15 21:15:41  geraint
## Added interface definition rep: _ICD.(txt|c|cc|m).
##
## Revision 1.8  1999/10/18 22:41:41  peterg
## Corrected vector junction expansion
##
## Revision 1.7  1999/10/18 05:16:51  peterg
## Now vectorises 0 and 1 junctions !!
##
## Revision 1.6  1998/09/02 11:35:20  peterg
## Removed port.index field
##
## Revision 1.5  1998/09/02 10:30:30  peterg
## Now writes out list of ports ans list of subsystems.
## These ordereded lists determine the order of processing of ports ans
## subsystems.
##
## Revision 1.4  1998/08/26 12:45:38  peterg
## Just prefix ports (comps can't start with numeral)
## Prefix with mttp
##
## Revision 1.3  1998/08/26 12:31:07  peterg
## numerical names prefixed by mtt
##
## Revision 1.2  1998/08/26 11:59:20  peterg
## Don't use strrep to remove[]
##
## Revision 1.1  1998/08/25 06:22:02  peterg
## Initial revision
##
###############################################################


  fid=fopen([system_name,"_abg.m"], "wt");
  [N,M]=size(connections);
  Sformat = "  %s.subsystems.%s.%s = \"%s\";\n";
  PSformat = "  %s.ports.%s.%s = \"%s\";\n";
  Iformat = "  %s.subsystems.%s.%s = %i;\n";
  PIformat = "  %s.ports.%s.%s = %i;\n";
  Cformat = "  %s.subsystems.%s.connections = [";
  PCformat = "  %s.ports.%s.connections = [";
  Bformat = "  %s.bonds = [\n";

  fprintf(fid,"function [%s] =  %s_abg\n", system_name, system_name);
  fprintf(fid,"# This function is the acausal bond graph representation of %s\n",system_name);
  fprintf(fid,"# Generated by MTT on %s",ctime(time));
  fprintf(fid,"# The file is in Octave format\n");

  fprintf(fid,"\n# Subsystems and Ports\n");
  i_port=0; SubsystemList = ""; PortList ="";
  for i=1:N
    eval(["[comp_type, name, cr, args, repetitions] = ", system_name, "_cmp(i);"]);
    c = nozeros(connections(i,:));# Connections to this component
    m = length(c);		# Number of connections

    ## Vectorise junctions?
    n_bonds = n_vector_bonds(i);
    if strcmp(comp_type,"0")||strcmp(comp_type,"1")
            n_vector = m/n_bonds;
	    m_vector = n_bonds;
    else
      n_vector = 1;
      m_vector = m;
    endif
      
    if index(name,"[")==0	# Not a port
      for i_vector = 1:n_vector;

	if i_vector>1		# Extras
	  new_name = sprintf("%sv%i", name,i_vector);
	else
	  new_name = name;
	endif
	
	SubsystemList = [SubsystemList; new_name];
	fprintf(fid,"\n# Component %s\n", new_name);
	fprintf(fid,Sformat,system_name,new_name,"type",comp_type);
	fprintf(fid,Sformat,system_name,new_name,"cr",cr);
	fprintf(fid,Sformat,system_name,new_name,"arg",args);
	fprintf(fid,Iformat,system_name,new_name,"repetitions",repetitions);
	fprintf(fid,Iformat,system_name,new_name,"status",-1);

	##Connections
	fprintf(fid,Cformat,system_name,new_name);
	## Each vector junction has n*m bonds
	## n - dimension of vector
	## m - number of bonds
	
	## The first m bonds (in c) correspond to the first vector element
	if i_vector==1		
	  for j=1:m_vector
      	    fprintf(fid,"%i ", c(j) );
	  endfor
	  ## The next block of n-1 bonds are the first bonds on vector elements 2,3 ...
	  ## followed by a  block of n-1 bonds being the second bonds on vector elements 2,3 ...
	  ## etc
	else	
	  k = m_vector+i_vector-1; # index of first bond at level i_vector
	  for j=1:m_vector
      	    fprintf(fid,"%i ", c(k) );
	    k = k + n_vector-1;
	  endfor
	endif
	
	fprintf(fid,"];\n");
      endfor
    else			# Its a port
      name=name(2:length(name)-1); # Strip []
      ch=name(1);		# First char of name
      if (ch>="0")&&(ch<="9")	# Its a numeral
      	name=["mttp",name];	# prefix by mttp
      endif;

      for i_port=1:m
	if m>1			# Index the port name
	  name_i = sprintf("%s_%i",name,i_port);
	else
	  name_i = name;	# Leave it alone
	endif;

	PortList = [PortList; name_i]; # Update port list
	
	fprintf(fid,"\n# Port %s\n", name_i); 
	fprintf(fid,PSformat,system_name,name_i,"type",comp_type);
	fprintf(fid,PSformat,system_name,name_i,"cr",cr);
	fprintf(fid,PSformat,system_name,name_i,"arg",args);
	fprintf(fid,PIformat,system_name,name_i,"repetitions",repetitions);
	fprintf(fid,PIformat,system_name,name_i,"status",-1);
	
	fprintf(fid,PCformat,system_name,name_i);
      	fprintf(fid,"%i ", c(i_port));
	fprintf(fid,"];\n");

      endfor;
    endif;
  endfor;

  [N,M]=size(PortList);
  if N>0			# Put name in list
    fprintf(fid,"\n# Ordered list of Port names\n");
    for i=1:N
      fprintf(fid,"  %s.portlist(%i,:) = \"%s\";\n", system_name, i, PortList(i,:));
    endfor;
  endif;

  [N,M]=size(SubsystemList);
  if N>0			# Put name in list
    fprintf(fid,"\n# Ordered list of subsystem names\n");
    for i=1:N
      fprintf(fid,"  %s.subsystemlist(%i,:) = \"%s\";\n", system_name, i, SubsystemList(i,:));
    endfor;
  endif;


  fprintf(fid,"\n# Bonds \n");
  [N,M]=size(bonds);		# Bonds
  fprintf(fid,Bformat,system_name);
  for i=1:N
    fprintf(fid,"      ");
    for j=1:M
      fprintf(fid,"%i ", bonds(i,j));
    endfor;
    fprintf(fid,"\n");
  endfor;
  fprintf(fid,"      ];\n");
    
  fprintf(fid,"\n# Aliases \n");
  fprintf(fid,"# A double underscore __ represents a comma \n");
  eval(["alias = ", system_name, "_alias;"]);
  if isstruct(alias)
    for [val,key] = alias
      fprintf(fid,"%s.alias.%s = \"%s\";\n", system_name,key,val);
    endfor
  endif

  fprintf(fid,"\n# Interface Definition\n");
  eval(["icd = ",system_name, "_icd;"]);
  if isstruct(icd)
    for [val,key] = icd
      fprintf(fid,"%s.icd.%s = \"%s\";\n",system_name,key,val);
    endfor
  endif
	

  fclose(fid);
  



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