Developer guide


[PDF]Developer guide - Rackcdn.com0eca4fe331aaaa0387ab-39017777f15f755539d3047328d4a990.r16.cf3.rackcdn.co...

1 downloads 203 Views 1MB Size

Writing rules for the Rule Based Checker May 2013 Ying Tao TASS ©

(c)Copyright 2011 by TASS All rights reserved. MADYMO(r) has been developed at TASS BV. This document contains proprietary and confidential information of TASS. The contents of this document may not be disclosed to third parties, copied or duplicated in any form, in whole or in part, without prior written permission of TASS. The terms and conditions governing the license of MADYMO(r) software consist solely of those set forth in the written contracts between TASS or TASS authorized third parties and its customers. The software may only be used or copied in accordance with the terms of these contracts. Version table Version Date

Author

Description

1.0

12/01/2010

J.van der Waal Initial version

1.1

06/29/2011

Ying Tao

Added installation/usage Eclipse IDE

1.2

15/05/2013

Ying Tao

Updated refresh function for reloading rule in XMADgic

Table of contents 1Writing rules for the Rule Based Checker.....................................................................................4 2Rule Based Checker integration within XMADgic.......................................................................6 3Important RBC modules................................................................................................................9 3.1The rule based checker module..............................................................................................9 3.2Madymo XML module.........................................................................................................11 3.3Reporter module...................................................................................................................12 4Creating new rules.......................................................................................................................13 4.1The first rule example..........................................................................................................13 4.2The second rule example......................................................................................................16 4.3The third rule example.........................................................................................................17 4.4The fourth example..............................................................................................................19 4.5Remarks on writing rules.....................................................................................................21 5Using the Eclipse IDE..................................................................................................................23

1

Writing rules for the Rule Based Checker

Introduction This document describes how to write rules for the rule based checker(RBC). We assume that the reader has knowledge of Madymo and has experience in Python programming. The RBC is a system to check Madymo models against certain criteria. Two examples to illustrate its usage. Assume you have a company guideline that states: “all NAME attributes of a GROUP_MB should end with _gmb”. The RBC is able to check the model and report any nonconformities to the user. More advanced, the RBC can be used to predict model stability. A rule could be created to advice the user about the correct time-step value. The RBC is build up from different modules, which are illustrated in the next figure. The main part is the Rule Based Checker module. This module contains all the core functionality like loading a Madymo deck (or model), search file systems for rules, managing rules for execution and generating a report. A rule has access to the Madymo model to check its criteria. A rule reports to the Reporter module. Rules are searched for at RBC startup. The RBC comes with rules written by Tass. Users can write their own rules. This document explains how to write your own rules. The Rule Based Checker module is depending on the Reporter module and the Madymo XML module. The Reporter module is for managing reported issues and creating HTML formatted output. The Madymo XML module is the module that presents the loaded Madymo deck (or model) in a XML tree which can be browsed by a rule. The RBC system is mainly written in Python. The system can operate stand alone, or integrated within an application. For the TASS environment tools, we integrated the RBC module into the XMADgic application.

Illustration 1: XMADgic and RBC components

2

Rule Based Checker integration within XMADgic

The XMADgic application uses the RBC python module directly. Therefor the RBC module should not be changed, otherwise the XMADgic rule check functionality may not work anymore. The installation of XMADgic (as part of the Madymo environment tools) comes with it's own Python 2.6 distribution. Therefore it is not needed to install Python yourself. If you have your own version installed, make sure that the PYTHONHOME is not set. Otherwise both Python installations may interfere. If the installation is correct, the menu option “Validation->Select User Rules” as shown in illustration 1 is enabled. Within the console the first line presents which Python installation is used. This installation can be overridden by setting the environment variable PYTHONHOME. The second line of the console shows where TASS rules are located on the file system. This directory contains the rules provided by Tass.

Illustration 2: XMADgic installed correctly

Illustration 2 shows what happens if the Python installation is not correct. The console issues a warning and within the validation menu the 'Select User Rules...' is grayed out.

Illustration 3: XMADgic Python not found When everything is working, it is possible to open the 'Select User Rules' dialog. The dialog supports adding search paths for rules, selecting rules for execution, and loading and saving to a file. Using the save and load option, the user is able to create different sets of rules checking different aspects of a model. Illustration 3 shows the rule selection dialog. There are two tabs, Predefined Rules and Custom Rules. Predefined Rules are the rules provided by TASS. Custom Rules are the rules found in the additional search directories that can be added using the Settings... button. The left side shows the rules. Rules can be added to the 'Selected Rules' by double clicking or by using the green arrow. When clicked, detailed information about a rule is shown at the bottom of the dialog. Using the save and load buttons, a selection can be stored and retrieved.

Illustration 4: Select User Rules dialog

3

Important RBC modules

To write new rules for the RBC, some module knowledge is needed. The following modules will be explained (as far it is relevant for writing rules). •

The Rule Based Checker module



XML tree module



Reporter module

Detailed information about these and other modules can be found in the Rule Based Checker API documentation which is part of the XMADgic help system.

.3.1 The rule based checker module The rule based checker is the main module. To develop new rules, you need to instantiate the rule_based_checker class which is part of this module. The module is responsible for: •

maintaining a list of directories to search for rules



scanning the list of directories for Python files containing rules



load a Madymo model to check



selecting/configuring rules for checking



execute rules



create a report of the results

The following code is a simple setup. It follows the enumeration from above. 1

#simple setup of the rule based checker for rule development

2

import pyrbc

3

rbc=pyrbc.rule_based_checker('C:\Program Files\MADYMO\MADYMO Workspace\7.3\share')

4

path_id = rbc.add_search_path('C:\path\to\rule\example\files', 'custom')

5

print '**search paths**\n', rbc.search_paths('custom')

6

rbc.update()

7

print '**rules**\n', rbc.available_rules('custom')

8

rbc.select(0)

9

rbc.select(1,{'ElementName':'FE_MODEL'})

10

print '**selected**\n',rbc.selected()

11

rbc.load_madymo_file('C:\samples\XMADgic\0_General\a_frontalel.xml')

12

report = rbc.run_check()

13

print '**report**',report

14

rbc.html_report('C:\temp\report.html')

Line 2: the pyrbc package is imported. The package contains all the Python files and a dynamic library. It is located within the site-packages directory of the Python installation. The import may fail if this pyrbc directory cannot be found or that some of the files on which the dynamic library depends can not found. If the import fails, this may have different reasons: •

the operating system could not find the dynamic library or one of it's dependencies.



The interpreter is unable to find the pyrbc files, normally located within the site-packages directory.

Line 3: creates an instantiation of the main class rule_based_checker. The path provided is the dtd_search_path. It is the path to share directory of the Workspace installation, so that the DTD for the MADYMO XML file can be found. Line 4: The RBC uses a list of directories to search for rules. The next line adds a single directory to the list. All rules found will be tagged with the group name 'custom'. Group names are used for organizing rules from different directories. The first parameter is the path itself, the second parameter is the group name. The group name is used to tag rules found. Groups are used for filters and for identification. The path_id is a handle that needs to be kept for operations like remove_search_path. Line 5: lists the current search paths, in this example only one. The custom parameter filters the paths for the group 'custom'. If no parameter is provided, all paths are returned. Line 6: when the search path list is changed using add_search_path or remove_search_path, this may effect the list of available rules. Hence if a directory is added to the list, the rules in that directory will be added to the ones already found. When the list is changed, it really becomes effective after calling update. It is important to call update when the list is changed, otherwise the changes will have no effect. Line 7: after update, the found rules are available. This line print out the found rules. Every rule is assigned a unique identifier (id). This is called the rule identifier or rule id for short. The rule id is used in the process of selecting rules. Line 8: A rule will only be executed if it is selected. This line selects the rule having rule_id zero. Line 9: This selects rule with rule id 1. This is the rule 'count' from example_02.py. This rule has one parameter 'ElementName'. To specify the parameter value, the second parameter is a dictionary with the parameter names and values. Line 10: print out the selected rules. Every selected rule has again its own identifier: the selection id for short. This identifier is used for other operations like unselect a rule. Line 11: the Madymo deck is loaded Line 12: at last, we are really checking now! The run_check really executes the selected rules on the loaded Madymo model. The function returns a list of reported issues. Line 13: the report can be obtained in HTML by calling this function. It will be written to the indicated location.

.3.2 Madymo XML module A rule needs access to the Madymo model to check its criteria. The model is presented to a rule by means of an XML tree (class MadymoXmlTree). This tree can be traversed and queried. In this section the most common calls are explained. Some of the code of example_03.py is used. 1

#some code to demonstrate XML tree access

2

root = tree.root()

3

print '-- name of root element: ', root.name()

4

print '-- attributes of root element:', str(root.attributes()))

5

children = root.children()

6

for child print print print

in children: 'name:', child.name(), 'attributes:', str(child.attributes()) 'numerical path:', child.reference().numerical_reference() 'named path:', child.reference().named_reference()

Line 2: the root XmlElement is retrieved from the tree. A rule will get the tree as a parameter when the rule is executed. The XmlElement the API through which the name and attributes can be retrieved from an element. Line 3: to get the element name, just call this function on the XmlElement instance. The XmlElement represents the element as described in the Madymo Reference manual. Line 4: attributes are retrieved in a dictionary. The key is the name of the attribute, the value is the attribute value. Both are strings. Line 5: the children of an XmlElement can be retrieved using this function. The result is a list of XmlElement objects. Line 6: print out of the name, the attributes, the numerical reference and the named reference. The tree itself is of type MadymoXmlTree. An instance of this tree is created by the RBC. This instance is passed to every rule that is executed. Except for getting the root as demonstrated in the code above, there are two more interesting functions. 1. elements_by_name(parent, element_name, max depth=-1, bUseWildCard=False) 2. resolve_reference(element, attribute_name) Elements by name is a function to get child elements with a certain name. The parent is the starting point in the tree. For example tree.root() can be given to search the whole tree. Next parameter is the name of the element(s) to match. The name can be a full name like 'BODY.RIGID' or only a class name (including the dot) like 'BODY.' When only the class name is provided, all types of that class are matched (BODY.DEFORMABLE, BODY.FLEXIBLE_BEAM and BODY.RIGID). For more information on element classes and types see the Madymo reference guide paragraph 3.3 (version 7.0) The max_depth parameter limits the recursion depth. Setting it -1 means no limit. A value of 1 means only the direct children. bUseWildCard parameter must be set to True if only the class name is provided for the name

parameter. The resolve_reference function resolves the path to the referred element. E.g. ’INITIAL.JOINT POS’ element has an attribute called ’JOINT’ which refers to the joint it applies to. The resolver can be used to find the joint element. The function returns an XmlElement object. More information on references can be found in paragraph 3.5 of the version 7 Madymo manual.

.3.3 Reporter module The Reporter is responsible for managing the reported issues. The reporter also creates HTML formatted reports. If the XML tree fails to meet the criteria of a rule, the rule will report an issue via the Reporter. The module has only one function: reporter.report(finding, xml_element=None, severity=REMARK)

The finding is a string to describe what the rule discovered. Only report when there is something wrong, on other cases do not report. The xml_element parameter can be supplied if the violating element is known. Supplying the element, XMADgic will show in the console a link to the element. XMADgic will jump to the location within the tree if clicked on this link. The default severity level is REMARK. This will appear in the XMADgic console. Two other levels are supported: WARNING and ERROR. It is up to the rule write to choose the correct severity level.

4

Creating new rules

Included in the distribution are examples that show how to write rules. In general, a rule is written to focus on a single criterion. They check for a particular aspect of a model: 1. The NAME attribute of the element GROUP_MB should end with _gmb 2. The integrator within the CONTROL_ANALYSIS.TIME should be always EULER 3. A rule may advise on a CONTROL_ANALYSIS.TIME time step for stability reasons. Sometimes it is possible to create a more generic rule. For example the rule, “The NAME attribute of the element GROUP_MB should end with _gmb” can be rephrased to “The NAME attribute of a element X should end with Y”. The latter type is an example of a parameterized rule or so called template-rule. The next examples are taken from the Workspace distribution. Please have a look at these example files, they are well documented.

.4.1 The first rule example This is only the code from example_01.py 1 2 3 4 5 6 7 8 9

def very_simple_rule(tree,reporter): """This simple rule does not do much""" reporter.report("Reported finding of example 1") reporter.report("Reported warning by example 1", severity=reporter.WARNING) reporter.report("Reported error by example 1", severity=reporter.ERROR) __rules__ = [very_simple_rule]

Line 1: a rule is captured in a Python function taking at least two parameters. The first parameter is the tree. This is the MadymoXmlTree as described in the previous chapter, it gives the rule access to the Madymo model. The second parameter, is the reporter. This is the reporter as described in the previous chapter, the Reporter module.

Illustration 5: Rule selection dialog shows doc-string and function-name Line 2: the doc-string is mandatory and will be presented to the XMADgic user in the Rule selection dialog to describe the rule. See illustration 4. The 'Available rules' shows a list of rules ordered by the module name. Below in the dialog the description and the function-name are shown. Line 4: This is the first reported issue for this rule. The message will be shown in the console of XMADgic. It will have the default severity 'remark'. Line 5: idem, but now for severity level 'warning' Line 6: idem, but now for severity level 'error' Line 8: this line is very important. A Python function is only recognized being a rule function if the function name is within the list __rules__. This mechanism makes it possible to have normal Python functions that are not rules but can be used by functions that are. Next image shows the output in XMADgic. The severity level is printed first in bold.

Illustration 6: Output of example_01.py in XMADgic console

.4.2 The second rule example This section is about example_02 The code including some additional comments can be found in the examples. 1 2 3 4 5 6 7 8

def count(tree, reporter, ElementName): """Counts the occurrences of a certain element within a MADYMO deck""" result = tree.elements_by_name(tree.root(), ElementName) count = len(result) reporter.report('Counted elements: ' + str(count)) __rules__=[count]

This example counts the number of a certain element within a model (not really a check). This introduces the need for having a parameterized rule. Line 1: The signature of a rule function contains always the tree and the reporter. Additional parameters like 'ElementName' in this example can be used to parameterize the rule. This kind of rule is called a template-rule. The 'ElementName' is user defined. When a template-rule is selected within XMADgic, the user is asked to specify the value.

Illustration 7: The ElementName needs to be specified by the user Illustration 6 shows that the extra parameter within the rule function is presented in the 'Selected

Rules' list of the Rule Selection dialog. The XMADgic user needs to specify the 'ElementName'. Line 2: the doc-string which is presented in the Rule Selection dialog Line 4: the Madymo XML tree object contains a few utility functions. The elements_by_name is a function that searches for children having a certain element name. The results are returned as a list of XmlElement objects. Line 6: reports the count to the XMADgic console. Line 8: identifies the count function being a rule.

.4.3 The third rule example The following code is taken from example_03.py from the example directory. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

def xml_tree_access(tree, report): """Sample accessing the XML tree using the MadymoXmlTree API""" # Get the root element of the tree # root is of type XmlElement. XmlElements have functions like name(), # children(), attributes() etc. root = tree.root() # report the name of the root element report.report("-- Name of root element: " + root.name()) # Get attributes of this element. The attributes are presented # as a dictionary. report.report("-- attributes of root element: " + str(root.attributes())) # The reference of the root element. Both numerical and named path # can be obtained. report.report( "-- numerical path of root element: " + root.reference().numerical_reference()) report.report("-- named path of root element: " + root.reference().named_reference()) # All Child XmlElement objects can be requested via the next function. children = root.children() # The next code shows the children of root. The name and the attributes of # each child is reported. for child in children: report.report("name: " + child.name() + ", attributes: " + str(child.attributes())) # Via this list the RBC knows that 'xml_tree_access' is a rule function __rules__ = [xml_tree_access]

This example is about accessing the Madymo model via the XML tree. When a rule is executed, the Madymo model is already loaded into memory. It can be accessed by the 'tree' parameter which is an instance of MadymoXmlTree. Line 7: this will get the root of the tree. Returned is an instance of an XmlElement. Line 9: reports the name of the root element Line 12: reports the dictionary of attributes of the root element Line 15: reports the numerical_reference

Line 16: reports the named_reference Line 20: the children are returned in a list of XmlElement objects Line 24: in this loop the children are reported with their names and attributes. The next image shows the first few lines of the XMADgic output console.

Illustration 8: First few lines of output XMADgic console for example_03.py

.4.4 The fourth example Next the source code of example_04.py is presented. This example contains two rules. The example code expects that the Madymo deck a_frontalel.xml is loaded. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 45 46 47 48 49 50 51 52 53 54

# # # #

In this example some utility functions are explained. E.g. it shows the usage of the 'elements_by_name' functions. For the rules in this file to work, it is important that a_frontalel.xml (from the XMADgic examples) is used as MADYMO deck.

def xml_tree_utility(tree, report): """shows utility functions of the 'tree' object""" # retrieve from the XML tree XmlElements which are named "FE_MODEL" root = tree.root() fe_model_elements = tree.elements_by_name(root, "FE_MODEL") report.report("Found: "+ str(len(fe_model_elements))+ " occurence(s) of FE_MODEL") for elem in fe_model_elements: report.report("--Found: " + elem.name() + ", path: " + \ elem.reference().named_reference() + \ ", attributes: " + str(elem.attributes())) # Next example rule shows the usage of the reference resolver. The reference # resolve may help by finding a referred element. In this example an FE_MODEL is # searched for which is referred by a GROUP_FE. def xml_tree_resolver(tree, report): """Search a referred element""" # Get the tree route root = tree.root() # Search for the SURFACE.PLANE with name 'toe_board' located in the SYSTEM.MODEL # with name 'Vehicle' surface_plane_toe_board = None for surface in tree.elements_by_name(root, "SURFACE.PLANE"): if surface.NAME == 'toe_board': surface_plane_toe_board = surface break if surface_plane_toe_board: report.report("SURFACE.PLANE with name toe_board found, name:" + \ surface_plane_toe_board.name() + ", path:"+ \ surface_plane_toe_board.reference().named_reference()) report.report("Dictionary for SURFACE.PLANE" + \ str(surface_plane_toe_board.attributes())) #Use the reference resolver to find the BODY resolved = tree.resolve_reference(surface_plane_toe_board, "BODY") report.report("resolved to: "+resolved.name() + ", path: " + \ resolved.reference().named_reference() + ", attributes: " + \ str(resolved.attributes())) else: report.report("toe_board not found, example ended") # Via this list the RBC knows that 'xml_tree_utility' and 'xml_tree_resolver' are rule functions __rules__ = [xml_tree_utility, xml_tree_resolver]

Line 6: The first rule presents the first of two utility functions within the MadymoXmlTree. Line 11: It searches for elements having a certain name. In this case the tree is searched for elements having the name 'FE_MODEL'. The first parameter is the start position within the tree. For full documentation please consult the API documentation (part of the XMADgic help system). Line 22: This rule features the reference resolver. The reference resolver searches elements which are referred to by attributes. The version 7.0 documentation of the Maydmo Reference Manual paragraph 3.5 is explaining about references. In short Elements may have attributes which are pointing to other elements. In this example a SURFACE.PLANE which has the name 'toe_board' is searched for. This element has an attribute called 'BODY' that refers to another XmlElement. The reference resolver tries to find the referred BODY element. Line 44: the actual call for the resolver. Next the output of the XMADgic console is presented.

Illustration 9: Example_04 output within the XMADgic console From the rule function xml_tree_resolver the console shows: The example shows that SURFACE.PLANE has a BODY attribute. The BODY attribute has the value TOE_BOARD. The resolver resolves the BODY attribute to a BODY.RIGID. The path of the BODY.RIGID is /Vehicle/TOE_BOARD. The last 5 lines of the console output are submitted by the xml_tree_utility: It searches the whole tree for FE_MODEL elements. It shows that the model contains 4 FE_MODEL's. The elements found are also reported.

.4.5 Remarks on writing rules Write new rules in a separate environment like Eclipse. XMADgic is not suited for this. If any loaded existing rule is modified, users need to press the Refresh button in XMADgic's Select User Rules dialog to let the change take effect. Some tips on writing new rules: •

Group related rules into a single module.



If possible write a more generic rule (template-rule).



Make sure that the function-name is unique among all other rules (e.g. prefix your own rules with your company name)



For every rule provide a clear doc-string



Only report when the user needs to now about something. If a rule doesn't find anything, please do not report.



Do not change the core files (all files within the pyrbc package). XMADgic is depending on them for correct functioning of the RBC.



Use the write HTML report of the RBC. This report is easy to read.

Links within the XMADgic console. For a small set of Madymo elements XMADgic supports click-able links. At the time of writing the following element classes(names) are supported: BELT_SEGMENT, BODY, CHAR_LOAD, CONSTRAINT, CONTACT, CONTACT_FORCE, CONTACT_METHOD, CRDSYS, FE_MODEL, FUNCTION, GROUP_FE, GROUP_MB, INITFEMODEL, INITREFSPACE, JOINT, LOAD, MADYMO, MATERIAL, OUTPUT_BELT, OUTPUT_BODY, OUTPUT_BODY_REL, OUTPUT_BODY_STATE, OUTPUT_JOINT_CONSTRAINT, OUTPUT_JOINT_DOF, PART, POINT_OBJECT, PROPERTY, STRAP, SURFACE, SWITCH, SYSTEM, TABLE

From these classes the subtypes are also supported. If a reported issue contains such a link, the editor will jump to the indicated location when clicked. A small demonstration for click-able links (assuming that the a_frontalel.xml is used as Madymo model): 1 def clickable_link(tree, reporter): """Demonstration click-able link""" 2 #Shows the clickable link option within XMADgic 3 #Requires the a_frontalel.xml to be loaded as Madymo model 4 root = tree.root() 5 6 7 systems = root.children('SYSTEM.MODEL') 8 for system in systems: 9 reporter.report("System name: "+ system.NAME, system, reporter.WARNING) 10 11 __rules__ = [clickable_link]

In this case the report function takes an extra parameter, an instance of XmlElement. If supplied, the report function will create a link to the indicated element. Of course, only supply this element when it is meaningful. The next image shows the XMADgic application after running the code above.

The results of the rule are visible in the console. The text of the console is blue underlined, indicating that it can be clicked. If clicked on the Belt, a jump will be made within the editor (see the blue ellipsis) .

5

Using the Eclipse IDE

An Integrated Development Environment (IDE) can be of help when creating or modifying rules. Eclipse is such an IDE that can be downloaded for free from http://www.eclipse.org/downloads. There are many flavors, 'Eclipse IDE for C/C++ Developers is a good choice'. Workspace 7.3 (or later) needs to be installed. This version and future versions comes with a complete Python run-time, together with the pyrbc (the rule based checker site package). The default installation directory for Workspace tools is assumed within the examples given. If you install Workspace at another location, you have to adapt the examples accordingly. Follow the steps below to use Eclipse IDE for RBC rule editing: 1. Download and install Eclipse 2. Install Python 2.6 3. Extend Eclipse with support for Python 4. Add the Python interpreter, which is installed due to step 2 5. Create a test project and execute test code (optional) 6. Set up RBC environment and test RBC Step 1 Download the Eclipse IDE for C/C++ Developers from http://www.eclipse.org/downloads and install it. Step 2 Download the installer for Python 2.6 from http://www.python.org/download/releases/2.6/. Step 3 Install PyDev for Eclipse to make it support Python programming. Please refer to http://pydev.org/manual_101_install.html for the installation process. Step 4 In Eclipse go to the menu 'window > preferences' and you will get the dialog shown in Illustration 10 below. Please make sure the Python interpreter's path is set to the location where you installed your Python2.6 executable. And check that in the System libs list (as shown in Illustration 10) /lib/site-packages directory under Python installation directory is included.

Illustration 10: Setting Python interpreter

Step 5 This step is only to test everything installed till now works correctly. You can skip it if you want. To make sure PyDev works correctly, create a test project and execute the code. Go to menu 'File > New > PyDev Project' and the dialog in Illustration 11 below will be shown. Type in the project name as 'py_test'. Make sure the interpreter is the one you just installed. Check 'Don't configure PYTHONPATH' as shown and click 'Finish' button. Then a new python project 'py_test' is created.

Illustration 11: Create PyDev test project Now an icon for 'py_test' project will be shown in the 'PyDev Package Explorer' on the left side of Eclipse. Right click it and choose 'New > Source Folder'. In the dialog popped up type in the folder name 'src' and click 'Finish'. Then Right click 'src' and choose 'New > File' and type 'py_test.py' in the popped up dialog and click 'Finish'. Now we made a test python file under the project 'py_test'. In the newly created 'py_test.py' file type in the following test line and select 'Run > Run As > 1 Python Run'. 1

print 'test for PyDev'

If PyDev and Python are installed and configured correctly, in the Console of Eclipse 'test for PyDev' should be printed as shown in Illustration 12.

Illustration 12: Test project

Step 6 To setup RBC environment we first need to copy several files from the workspace installation. Here we assume that your workspace installation is at location 'C:\Program Files\MADYMO\MADYMO_Workspace\' and we refer to it later as WORKSPACE_DIR. Also we assume that your Python is installed at location 'C:\Python26\' and we refer to it later as PYTHON_DIR. We assume here that you have workspace version 7.3 and machine architecture am64t-win. First we copy the files 'boost_system-vc80-mt-1_42.dll', 'MSVCR80.dll', 'qt-mt336.dll' from directory WORKSPACE_DIR\7.3\em64t-win\bin as well as file '_pyrbcxmltree.pyd' from directory WORKSPACE_DIR\7.3\em64t-win\python26\Lib\site-packages\pyrbc to the directory PYTHON_DIR\DLLs. Then we copy the 'pyrbc' directory under WORKSPACE_DIR\7.3\em64t-

win\python26\Lib\site-packages to PYTHON_DIR\Lib\site-packages. Create a new project for testing and use the code below to test whether RBC works correctly: 1 2

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58

# # # # # # #

Rules can be developed outside XMADgic using e.g. Eclipse Some lines of code are needed to setup the Rule Based Checker This is an example showing how to do it Assumes that Madymo Workspace (version 7.3) is installed installed at it's default locations C:\Program Files\MADYMO\MADYMO Workspace\7.3

# import the module provided by TASS import pyrbc # modules provided by Python distribution import os from pprint import pprint # Instantiate a rule based checker. rbc=pyrbc.rule_based_checker('C:\\Program Files\\MADYMO\\MADYMO Workspace\\7.3\\share') # Add search path to search for rules. path_id = rbc.add_search_path('C:\\Program Files\\MADYMO\MADYMO_Workspace\\' '7.3\\samples\\Rule_Based_Checker', 'custom') # Show the path just added print '\n**search paths**' pprint( rbc.search_paths('custom') ) # Request the RBC to search for rules in the added search path(s). rbc.update() # Show the rules found print '\n**rules**' pprint( rbc.available_rules('custom') ) # Select one or more rule(s) to be executed rbc.select(1, {'ElementName':'FE_MODEL'}) # Show the rule(s) selected print '\n**selected**' pprint( rbc.selected() ) # Load Madymo deck to be checked rbc.load_madymo_file('C:\\Program Files\\MADYMO\\MADYMO_Workspace\\7.3\\' 'samples\\XMADgic\\0_General\\a_frontalel.xml')

59 60 61 62 63 64 65 66 67

# Execute the rule(s) and create an report object report = rbc.run_check() print '\n**report**' pprint( report ) desktop_path = os.path.join(os.path.expanduser("~"), "Desktop") rbc.html_report( desktop_path + '\\report.html')

In the code above it first import module pyrbc in line 12 to load all the RBC related classes and functions. Then in line 22 an RBC instance is initialized with the share directory specified. And in line 27 custom rule searching paths are specified. Update() function is then called by the RBC instance to search for the rules in the paths just specified. Till this part of the code the RBC should have several available custom rules. So in line 46 we select the available rule with ID 1 and give the parameter 'ElementName' the value 'FE_MODEL'. And in line 55 we load a MADYMO deck for checking. In line 61 RBC runs the checks and in the last line it writes report lines to the HTML file specified with the path. Run the file containing the code above to test. You may encounter the errors shown below in Illustration 13 . If that's the case, try to add PYTHON_DIR\DLLs to your PATH environment variable. Under windows you can either open a command window and type in 'set PATH=%PATH %;c:\Python26\DLLs' (here PYTHON_DIR is c:\Python26) or edit the 'Environment Variables' via right-click on 'My Computer' and select the 'Properties' option.

Illustration 13: Possible importing error for _pyrbcxmltree.pyd If everything runs correctly, you should see the following results in Illustration 14 in the Eclipse console. Also a 'report.html' file can be found on your desktop with the contents as in Illustration 15.

Illustration 14: Eclipse output for testing RBC example

Illustration 15: Output report HTML file for testing RBC example