rtstodot.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Yosuke Matsusaka and Geoffrey Biggs
00009     Intelligent Systems Research Institute,
00010     National Institute of Advanced Industrial Science and Technology (AIST),
00011     Japan
00012     All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015 
00016 Implementation of the RT System visualisation command.
00017 
00018 '''
00019 
00020 
00021 import optparse
00022 import os.path
00023 import rtsprofile.rts_profile
00024 import sys
00025 import traceback
00026 
00027 import rtshell
00028 
00029 
00030 def port_name(s):
00031     parts = s.split('.')
00032     if len(parts) == 1:
00033         return s
00034     else:
00035         return parts[-1]
00036 
00037 
00038 def escape(s):
00039     return s.replace('.', '_dot_').replace('/', '_slash_')
00040 
00041 
00042 def get_ports(rtsp):
00043     in_ports = []
00044     out_ports = []
00045     for dp in rtsp.data_port_connectors:
00046         in_ports.append((dp.source_data_port.instance_name,
00047             port_name(dp.source_data_port.port_name)))
00048         out_ports.append((dp.target_data_port.instance_name,
00049             port_name(dp.target_data_port.port_name)))
00050     for sp in rtsp.service_port_connectors:
00051         out_ports.append((dp.source_data_port.instance_name,
00052             port_name(dp.source_data_port.port_name)))
00053         in_ports.append((dp.target_data_port.instance_name,
00054             port_name(dp.target_data_port.port_name)))
00055     return in_ports, out_ports
00056 
00057 
00058 def make_comp_str(c, in_ports, out_ports):
00059     in_ports_str = ''
00060     out_ports_str = ''
00061     for dp in c.data_ports:
00062         if (c.instance_name, dp.name) in in_ports:
00063             in_ports_str += '<{0}>{0}|'.format(port_name(dp.name))
00064         if (c.instance_name, dp.name) in out_ports:
00065             out_ports_str += '<{0}>{0}|'.format(port_name(dp.name))
00066     for sp in c.service_ports:
00067         if (c.instance_name, sp.name) in in_ports:
00068             in_ports_str += '<{0}>{0}|'.format(port_name(sp.name))
00069         if (c.instance_name, sp.name) in out_ports:
00070             out_ports_str += '<{0}>{0}|'.format(port_name(sp.name))
00071     label_str = '{{{{{0}}}|{1}|{{{2}}}}}'.format(in_ports_str[:-1], c.instance_name,
00072             out_ports_str[:-1])
00073     return '  {0} [label="{1}"];'.format(escape(c.instance_name), label_str)
00074 
00075 
00076 def make_conn_str(s_port, d_port):
00077     return '  {0}:{1} -> {2}:{3}'.format(
00078             escape(s_port.instance_name), port_name(s_port.port_name),
00079             escape(d_port.instance_name), port_name(d_port.port_name))
00080 
00081 
00082 def visualise(profile=None, xml=True, tree=None):
00083     # Load the profile
00084     if profile:
00085         # Read from a file
00086         with open(profile) as f:
00087             if xml:
00088                 rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=f)
00089             else:
00090                 rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=f)
00091     else:
00092         # Read from standard input
00093         lines = sys.stdin.read()
00094         if xml:
00095             rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=lines)
00096         else:
00097             rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=lines)
00098 
00099     result = ['digraph rtsprofile {', '  rankdir=LR;', '  node [shape=Mrecord];']
00100     in_ports, out_ports = get_ports(rtsp)
00101     for comp in rtsp.components:
00102         result.append(make_comp_str(comp, in_ports, out_ports))
00103     for conn in rtsp.data_port_connectors:
00104         result.append(make_conn_str(conn.source_data_port,
00105             conn.target_data_port) + ';')
00106     for conn in rtsp.service_port_connectors:
00107         result.append(make_conn_str(conn.source_service_port,
00108             conn.target_service_port) + ' [arrowhead="odot"];')
00109     result.append('}')
00110     return result
00111 
00112 
00113 def main(argv=None, tree=None):
00114     usage = '''Usage: %prog [options] [RTSProfile file]
00115 Visualise RT Systems using dot files.'''
00116     version = rtshell.RTSH_VERSION
00117     parser = optparse.OptionParser(usage=usage, version=version)
00118     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00119             default=False,
00120             help='Output verbose information. [Default: %default]')
00121     parser.add_option('-x', '--xml', dest='xml', action='store_true',
00122             default=True, help='Use XML input format. [Default: True]')
00123     parser.add_option('-y', '--yaml', dest='xml', action='store_false',
00124             help='Use YAML input format. [Default: False]')
00125 
00126     if argv:
00127         sys.argv = [sys.argv[0]] + argv
00128     try:
00129         options, args = parser.parse_args()
00130     except optparse.OptionError, e:
00131         print >>sys.stderr, 'OptionError:', e
00132         return 1
00133 
00134     if not args:
00135         profile = None
00136     elif len(args) == 1:
00137         profile = args[0]
00138     else:
00139         print >>sys.stderr, usage
00140         return 1
00141 
00142     try:
00143         for l in visualise(profile=profile, xml=options.xml, tree=tree):
00144             print l
00145     except Exception, e:
00146         if options.verbose:
00147             traceback.print_exc()
00148         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00149         return 1
00150     return 0
00151 
00152 
00153 # vim: tw=79
00154 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12