6 Copyright (C) 2009-2010 8 RT-Synthesis Research Group 9 Intelligent Systems Research Institute, 10 National Institute of Advanced Industrial Science and Technology (AIST), 13 Licensed under the Eclipse Public License -v 1.0 (EPL) 14 http://www.opensource.org/licenses/eclipse-1.0.txt 18 Object representing an RT system. The object constructs itself using an XML 19 parser and an XML specification meeting the RtsProfile schema. 23 __version__ =
'$Revision: $' 27 from datetime
import datetime, MINYEAR
29 import xml.dom.minidom
32 from rtsprofile
import RTS_NS, RTS_NS_S, RTS_EXT_NS, RTS_EXT_NS_S, \
37 InvalidRtsProfileNodeError, RtsProfileError
39 Deactivation, Resetting, Initialize, \
43 indent_string, parse_properties_xml, \
44 properties_to_xml, validate_attribute
51 def __init__(self, xml_spec=None, yaml_spec=None):
54 Pass in an RTSProfile specification either in a string or a file 55 object, and the RtsProfile will be loaded from that. 57 If no specification is provided, the RtsProfile will be constructed 58 from the default values. Use the properties to set the values you need. 60 @param xml_spec A string or open file object containing the XML 61 specification of the RTS Profile. If present, the other arguments must 64 @param yaml_spec A string or open file object containing the YAML 65 specification of the RTS Profile. If present, the other arguments must 83 result =
'ID: {0}\nAbstract: {1}\nCreation date: {2}\n\ 84 Update date: {3}\nVersion: {4}\n'.format(self.
id, self.
abstract,
88 result +=
'Components:\n' 96 result +=
'Data port connectors:\n' 100 result +=
'Service port connectors:\n' 104 result +=
'Startup: {0}\n'.format(self.
startup)
106 result +=
'Shutdown: {0}\n'.format(self.
shutdown)
108 result +=
'Activation: {0}\n'.format(self.
activation)
110 result +=
'Deactivation: {0}\n'.format(self.
deactivation)
112 result +=
'Resetting: {0}\n'.format(self.
resetting)
114 result +=
'Initializing: {0}\n'.format(self.
initializing)
116 result +=
'Finalizing: {0}\n'.format(self.
finalizing)
118 result +=
'Comment: {0}\n'.format(self.
comment)
120 result +=
'Version up logs:\n' 124 result +=
'Properties:\n' 126 result +=
' {0}: {1}\n'.format(p, self.
properties[p])
134 '''ID used to distinguish the RT system. 136 Typically in the format '[vendor name].[system name].[version]'. 143 print 'setting id to ', id
145 expected_type=[str, unicode], required=
True)
150 '''Description of this RT system. 160 expected_type=[str, unicode], required=
True)
165 '''The date this RT system was first created. 167 Usually set automatically by the tool that created the system. 172 @creation_date.setter
175 expected_type=[str, unicode], required=
True)
180 '''The date this RT system was most recently updated. 182 Usually set automatically by the tool that created the system. 190 expected_type=[str, unicode], required=
True)
195 '''Version of the RTSProfile specification this is in.''' 201 expected_type=[str, unicode], required=
True)
206 '''Information about the components that make up the RT system. 208 May be an empty list if there are no components. Members are of type 217 expected_type=list, required=
True)
222 '''Information about the component groups in the RT system. 224 May be an empty list if there are no groups. Members are of type @ref 233 expected_type=list, required=
True)
238 '''Connections between data ports in the RT system. 240 Members are of type @ref DataPortConnector. 245 @data_port_connectors.setter
248 'rts_profile.DataPortConnectors',
249 expected_type=list, required=
True)
254 '''Connections between service ports in the RT system. 256 Members are of type @ref ServicePortConnector. 261 @service_port_connectors.setter
264 'rts_profile.ServicePortConnectors',
265 expected_type=list, required=
True)
270 '''Ordering and conditions for when the RT system is started.''' 276 expected_type=StartUp, required=
False)
281 '''Ordering and conditions for when the RT system is shut down.''' 287 expected_type=ShutDown, required=
False)
292 '''Ordering and conditions for when the RT system is activated.''' 298 expected_type=Activation, required=
False)
303 '''Ordering and conditions for when the RT system is deactivated.''' 309 expected_type=Deactivation, required=
False)
314 '''Ordering and conditions for when the RT system is reset.''' 320 expected_type=Resetting, required=
False)
325 '''Ordering and conditions for when the RT system is initialised.''' 331 expected_type=Initialize, required=
False)
336 '''Ordering and conditions for when the RT system is finalised.''' 342 expected_type=Finalize, required=
False)
347 '''Comment about the system. 349 A brief comment about the system. May or may not be displayed in other 352 Part of the extended profile. 360 expected_type=[str, unicode], required=
False)
365 '''Log entries for new versions. 367 When an update to a system is made, the log entry describing changes is 368 stored in this value. 370 Part of the extended profile. 375 @version_up_log.setter
378 expected_type=list, required=
False)
383 '''Miscellaneous properties. 385 Stores key/value pair properties. 387 Part of the extended profile. 395 expected_type=dict, required=
False)
402 '''Finds a component using a TargetComponent or one of its subclasses. 404 @param A @ref TargetComponent object or subclass of @ref 406 @return A Component object matching the target. 407 @raises MissingComponentError 411 if comp.id == target.component_id
and \
412 comp.instance_name == target.instance_name:
414 raise MissingComponentError
417 '''Finds all data connections in which one or more components are not 420 If all the components involved in a connection are required, that 421 connection is also required. If one or more are not required, that 422 connection is optional. 429 if not source_comp.is_required
or not target_comp.is_required:
434 '''Finds all service connections in which one or more components are 437 If all the components involved in a connection are required, that 438 connection is also required. If one or more are not required, that 439 connection is optional. 446 if not source_comp.is_required
or not target_comp.is_required:
451 '''Finds all data connections in which all components are required. 453 If all the components involved in a connection are required, that 454 connection is also required. If one or more are not required, that 455 connection is optional. 462 if source_comp.is_required
and target_comp.is_required:
467 '''Finds all service connections in which all components are required. 469 If all the components involved in a connection are required, that 470 connection is also required. If one or more are not required, that 471 connection is optional. 478 if source_comp.is_required
and target_comp.is_required:
486 '''Parse a string or file containing an XML specification.''' 487 if type(xml_spec) == str
or type(xml_spec) == unicode:
488 dom = xml.dom.minidom.parseString(xml_spec)
490 dom = xml.dom.minidom.parse(xml_spec)
495 '''Save this RtsProfile into an XML-formatted string.''' 497 return xml_obj.toprettyxml(indent=
' ')
503 '''Parse a string or file containing a YAML specification.''' 507 '''Save this RtsProfile into a YAML-formatted string.''' 515 root = dom.documentElement
517 self.
id = root.getAttributeNS(RTS_NS,
'id')
518 self.
abstract = root.getAttributeNS(RTS_NS,
'abstract')
521 self.
version = root.getAttributeNS(RTS_NS,
'version')
522 self.
comment = root.getAttributeNS(RTS_EXT_NS,
'comment')
524 for c
in root.getElementsByTagNameNS(RTS_NS,
'Components'):
525 self._components.append(
Component().parse_xml_node(c))
526 for c
in root.getElementsByTagNameNS(RTS_NS,
'Groups'):
528 for c
in root.getElementsByTagNameNS(RTS_NS,
'DataPortConnectors'):
530 for c
in root.getElementsByTagNameNS(RTS_NS,
'ServicePortConnectors'):
533 c = root.getElementsByTagNameNS(RTS_NS,
'StartUp')
536 raise InvalidRtsProfileNodeError(
'StartUp')
538 c = root.getElementsByTagNameNS(RTS_NS,
'ShutDown')
541 raise InvalidRtsProfileNodeError(
'ShutDown')
543 c = root.getElementsByTagNameNS(RTS_NS,
'Activation')
546 raise InvalidRtsProfileNodeError(
'Activation')
548 c = root.getElementsByTagNameNS(RTS_NS,
'Deactivation')
551 raise InvalidRtsProfileNodeError(
'Deactivation')
553 c = root.getElementsByTagNameNS(RTS_NS,
'Resetting')
556 raise InvalidRtsProfileNodeError(
'Resetting')
557 self.
_resetting = Resetting().parse_xml_node(c[0])
558 c = root.getElementsByTagNameNS(RTS_NS,
'Initializing')
561 raise InvalidRtsProfileNodeError(
'Initializing')
563 c = root.getElementsByTagNameNS(RTS_NS,
'Finalizing')
566 raise InvalidRtsProfileNodeError(
'Finalizing')
567 self.
_finalizing = Finalizing().parse_xml_node(c[0])
569 for c
in root.getElementsByTagNameNS(RTS_EXT_NS,
'VersionUpLog'):
570 if c.nodeType == c.TEXT_NODE:
571 self._version_up_log.append(c.data)
573 print >>sys.stderr,
'Warning: bad VersionUpLog node type.' 575 local_name=
'Properties'):
581 if not 'rtsProfile' in spec:
582 raise RtsProfileError(
'Missing root node.')
583 root = spec[
'rtsProfile']
586 if 'abstract' in root:
588 self.
creation_date =
'{year:04}-{month:02}-{day:02}T{hour:02}:\ 589 {minute:02}:{second:02}'.format(**root[
'creationDate'])
590 self.
update_date =
'{year:04}-{month:02}-{day:02}T{hour:02}:\ 591 {minute:02}:{second:02}'.format(**root[
'updateDate'])
593 if RTS_EXT_NS_YAML +
'comment' in root:
594 self.
comment = root[RTS_EXT_NS_YAML +
'comment']
596 if 'components' in root:
597 for c
in root[
'components']:
598 self._components.append(
Component().parse_yaml(c))
600 for c
in root[
'groups']:
602 if 'dataPortConnectors' in root:
603 for c
in root[
'dataPortConnectors']:
605 if 'servicePortConnectors' in root:
606 for c
in root[
'servicePortConnectors']:
609 if 'startUp' in root:
611 if 'shutDown' in root:
613 if 'activation' in root:
615 if 'deactivation' in root:
616 self.
_deactivation = Deactivation().parse_yaml(root[
'deactivation'])
617 if 'resetting' in root:
618 self.
_resetting = Resetting().parse_yaml(root[
'resetting'])
619 if 'initializing' in root:
620 self.
_initializing = Initializing().parse_yaml(root[
'initializing'])
621 if 'finalizing' in root:
622 self.
_finalizing = Finalizing().parse_yaml(root[
'finalizing'])
624 if RTS_EXT_NS_YAML +
'versionUpLogs' in root:
625 for c
in root[RTS_EXT_NS_YAML +
'versionUpLogs']:
626 self._version_up_log.append(c)
627 if RTS_EXT_NS_YAML +
'properties' in root:
628 for p
in root[RTS_EXT_NS_YAML +
'properties']:
666 prof = {
'id': self.
id,
672 prof[RTS_EXT_NS_YAML +
'comment'] = self.
comment 676 components.append(c.to_dict())
678 prof[
'components'] = components
681 groups.append(g.to_dict())
683 prof[
'groups'] = groups
686 d_connectors.append(c.to_dict())
688 prof[
'dataPortConnectors'] = d_connectors
691 s_connectors.append(c.to_dict())
693 prof[
'servicePortConnectors'] = s_connectors
696 prof[
'startUp'] = self.startup.to_dict()
698 prof[
'shutDown'] = self.shutdown.to_dict()
700 prof[
'activation'] = self.activation.to_dict()
702 prof[
'deactivation'] = self.deactivation.to_dict()
704 prof[
'resetting'] = self.resetting.to_dict()
706 prof[
'initializing'] = self.initializing.to_dict()
708 prof[
'finalizing'] = self.finalizing.to_dict()
714 prof[RTS_EXT_NS_YAML +
'versionUpLogs'] = log
722 prof[RTS_EXT_NS_YAML +
'properties'] = props
724 return {
'rtsProfile': prof}
727 impl = xml.dom.minidom.getDOMImplementation()
728 doc = impl.createDocument(RTS_NS, RTS_NS_S +
'RtsProfile',
None)
729 doc.documentElement.setAttribute(
'xmlns:rts', RTS_NS)
730 doc.documentElement.setAttribute(
'xmlns:rtsExt', RTS_EXT_NS)
731 doc.documentElement.setAttribute(
'xmlns:xsi',
732 'http://www.w3.org/2001/XMLSchema-instance')
734 doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S +
'id', self.
id)
735 doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S +
'abstract',
737 doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S +
'creationDate',
739 doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S +
'updateDate',
741 doc.documentElement.setAttributeNS(RTS_NS, RTS_NS_S +
'version',
744 doc.documentElement.setAttributeNS(RTS_EXT_NS,
745 RTS_EXT_NS_S +
'comment',
748 new_comp_element = doc.createElementNS(RTS_NS,
749 RTS_NS_S +
'Components')
750 c.save_xml(doc, new_comp_element)
751 doc.documentElement.appendChild(new_comp_element)
753 new_group_element = doc.createElementNS(RTS_NS,
755 g.save_xml(doc, new_group_element)
756 doc.documentElement.appendChild(new_group_element)
758 new_conn_element = doc.createElementNS(RTS_NS,
759 RTS_NS_S +
'DataPortConnectors')
760 dc.save_xml(doc, new_conn_element)
761 doc.documentElement.appendChild(new_conn_element)
763 new_conn_element = doc.createElementNS(RTS_NS,
764 RTS_NS_S +
'ServicePortConnectors')
765 sc.save_xml(doc, new_conn_element)
766 doc.documentElement.appendChild(new_conn_element)
768 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'StartUp')
769 self.startup.save_xml(doc, new_cond)
770 doc.documentElement.appendChild(new_cond)
772 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'ShutDown')
773 self.shutdown.save_xml(doc, new_cond)
774 doc.documentElement.appendChild(new_cond)
776 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'Activation')
777 self.activation.save_xml(doc, new_cond)
778 doc.documentElement.appendChild(new_cond)
780 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'Deactivation')
781 self.deactivation.save_xml(doc, new_cond)
782 doc.documentElement.appendChild(new_cond)
784 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'Resetting')
785 self.resetting.save_xml(doc, new_cond)
786 doc.documentElement.appendChild(new_cond)
788 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'Initializing')
789 self.initializing.save_xml(doc, new_cond)
790 doc.documentElement.appendChild(new_cond)
792 new_cond = doc.createElementNS(RTS_NS, RTS_NS_S +
'Finalizing')
793 self.finalizing.save_xml(doc, new_cond)
794 doc.documentElement.appendChild(new_cond)
796 new_vl_element = doc.createElementNS(RTS_EXT_NS,
797 RTS_EXT_NS_S +
'VersionUpLog')
798 new_text_node = doc.createTextNode(vl)
799 new_vl_element.appendChild(new_text_node)
800 doc.documentElement.appendChild(new_vl_element)
802 new_prop_element = doc.createElementNS(RTS_EXT_NS,
803 RTS_EXT_NS_S +
'Properties')
805 doc.documentElement.appendChild(new_prop_element)
809 return yaml.safe_dump(self.
_to_dict())
def _parse_yaml(self, spec)
def find_comp_by_target(self, target)
API functions.
def service_port_connectors(self)
def _parse_xml(self, dom)
Internal functions.
def date_to_dict(date)
Public API functions.
def required_service_connections(self)
DataPortConnector object.
def properties_to_xml(element, name, value=None)
def parse_properties_xml(node)
def __init__(self, xml_spec=None, yaml_spec=None)
def optional_service_connections(self)
def parse_from_xml(self, xml_spec)
XML.
def validate_attribute(attr, name, expected_type=None, required=False)
def parse_from_yaml(self, yaml_spec)
YAML.
def get_direct_child_elements_xml(node, prefix=None, local_name=None)
ServicePortConnector object.
def required_data_connections(self)
def data_port_connectors(self)
def indent_string(string, num_spaces=2)
def optional_data_connections(self)