Main Content

Use Callbacks to Customize Requirement Import Behavior

You can use callbacks to execute code when you import requirements from third-party tools or when you update requirements. The PreImportFcn callback executes before you import requirements and the PostImportFcn callback executes after you import the requirements. You can use these callbacks to customize import behavior.

Assign Code to Callbacks

You can assign code to the PreImportFcn and PostImportFcn callbacks by using the Requirements Editor or at the MATLAB® command line.

Assign Code to Callbacks by Using the Requirements Editor

To use the Requirements Editor to assign code to the PreImportFcn and PostImportFcn callbacks:

  1. In the Requirements Editor, click Import.

  2. In the Importing Requirements dialog, set Document type to the third-party tool that you are importing requirements from.

  3. Next to Document location, click Browse and select the requirement file, document, module, or other requirement container.

  4. Apply your desired import settings. For more information, see Import Requirements from Third-Party Applications.

  5. In the Importing Requirements dialog box, expand the Callbacks section.

  6. Select the PreImportFcn or PostImportFcn tab.

    Importing Requirements dialog box with the PreImportFcn and PostImportFcn tabs in view.

  7. Enter your code in the box. You can enter code or the name of a MATLAB script that contains your code.

  8. Click Import.

After you import the requirements, you can view and edit the callbacks in the Requirements Editor. Select the import node and, in the right pane, under Callbacks, select the PreImportFcn or PostImportFcn tab.

Assign Code to Callbacks Programmatically

To assign code to callbacks programmatically:

  1. In MATLAB, select the Home tab, then click New Script.

  2. In the script, enter the code that you want the callback to execute.

  3. Select the Editor tab, then click Save. Enter a name for the script, then click Save.

  4. At the MATLAB command line, use slreq.import to import your requirements. Use the preImportFcn and postImportFcn arguments to assign your scripts to the callbacks.

After you import the requirements, you can get the code registered to the callbacks by using getPreImportFcn and getPostImportFcn. You can change the code assigned to the callbacks by using setPreImportFcn and setPostImportFcn.

Customize Requirement Import Behavior

You can use the code assigned to callbacks to execute commands before and after you import requirements. For example, you can use the PreImportFcn callback to customize your import options or use the PostImportFcn callback to specify property values of imported requirements.

Customize the Pre-Import Behavior

You can use the PreImportFcn callback to specify how Requirements Toolbox™ imports requirements from different third-party tools. Use slreq.getCurrentImportOptions in the callback to return one of these objects, depending on the third-party tool:

You can modify this object in the callback code to change how Requirements Toolbox imports the requirements.

For example, you can specify the mapping file to use when you import ReqIF™ files. Modify this example code to specify the full file path of your mapping file and assign the code to the PreImportFcn callback:

importOptions = slreq.getCurrentImportOptions;
importOptions.MappingFile = "C:\Users\jdoe\Documents\myMappingFile.xml";

Customize the Post-Import Behavior

You can use the PostImportFcn callback to execute code that modifies requirements after the import completes.

For example, to specify property values of the imported requirements in the PostImportFcn callback, use slreq.getCurrentObject to get a handle to the Import node, then use slreq.Reference methods to get handles to the imported requirements. Then, use dot notation to set the property values. For more information, see Property Access Syntax. This example code shows how to get handles to the child referenced requirements under the current import node.

topRef = slreq.getCurrentObject;
refs = find(topRef);
You can also use the IndexEnabled and IndexNumber properties to customize requirement index numbering. For more information, see Customize Requirement Index Numbering. For an example, see Import Requirements from a Microsoft Excel Document.

To move a referenced requirement, use setParent, moveUp, or moveDown in the PostImportFcn callback.

See Also

| |

Related Examples

More About