Main Content

Create Architectural Views Programmatically

You can create an architecture view programmatically. This topic presents two examples of creating architecture views programmatically and shows you how to use queries to find elements in a System Composer™ model.

A query is a specification that describes certain constraints or criteria to be satisfied by model elements. Use queries to search elements with constraint criteria and to filter views.

Architecture Views in System Composer with Keyless Entry System

This example shows how to use a keyless entry system to programmatically create architecture views using API.

1. Import the package with the queries.

import systemcomposer.query.*;

2. Open the Simulink® project file for the Keyless Entry System.

scKeylessEntrySystem

3. Load the example model into System Composer™.

zcModel = systemcomposer.loadModel('KeylessEntryArchitecture');

Example 1: Hardware Component Review Status View

Create a filtered view that selects all of the hardware components in the architecture model and groups them using the ReviewStatus property.

1. Construct the query to select all of the hardware components.

hwCompQuery = HasStereotype(IsStereotypeDerivedFrom('AutoProfile.HardwareComponent'))
hwCompQuery = 
  HasStereotype with properties:

    AllowedParentConstraints: {1x3 cell}
               SubConstraint: [1x1 systemcomposer.query.IsStereotypeDerivedFrom]
              SkipValidation: 0

2. Use the query to create a view.

zcModel.createView('Hardware Component Review Status',...
 'Select',hwCompQuery,... % Query to use for the selection
 'GroupBy',{'AutoProfile.BaseComponent.ReviewStatus'},... % Stereotype property to qualify by
 'IncludeReferenceModels',true,... % Include components in referenced models
 'Color','purple');

3. Open the Architecture Views Gallery.

zcModel.openViews

Example 2: FOB Locator System Supplier View

This example shows how to create a freeform view that manually pulls the components from the FOB Locator System and then groups them using existing and new view components for the suppliers. In this example, you will use element groups, groupings of components in a view, to programmatically populate a view.

1. Create a view architecture.

fobSupplierView = zcModel.createView('FOB Locator System Supplier Breakdown',...
    'Color','lightblue');

2. Add a subgroup called 'Supplier D'. Add the FOB Locator Module to the view element subgroup.

supplierD = fobSupplierView.Root.createSubGroup('Supplier D');
supplierD.addElement('KeylessEntryArchitecture/FOB Locator System/FOB Locator Module');

3. Create a new subgroup for 'Supplier A'.

supplierA = fobSupplierView.Root.createSubGroup('Supplier A');

4. Add each of the FOB Receivers to view element subgroup.

FOBLocatorSystem = zcModel.lookup('Path','KeylessEntryArchitecture/FOB Locator System');

% Find all the components which contain the name "Receiver"
receiverCompPaths = zcModel.find(...
    contains(systemcomposer.query.Property('Name'),'Receiver'),...
    FOBLocatorSystem.Architecture);

supplierA.addElement(receiverCompPaths)

Find Elements in a Model Using Queries

This example shows how to find components in a System Composer model using queries.

Open the model.

import systemcomposer.query.*;

scKeylessEntrySystem
zcModel = systemcomposer.loadModel('KeylessEntryArchitecture');

Find all the software components in the system.

con1 = HasStereotype(Property("Name") == "SoftwareComponent");
[compPaths, compObjs] = zcModel.find(con1)
compPaths = 5x1 cell
    {'KeylessEntryArchitecture/Engine Control System/Keyless Start Controller'}
    {'KeylessEntryArchitecture/Lighting System/Lighting Controller'           }
    {'KeylessEntryArchitecture/Sound System/Sound Controller'                 }
    {'KeylessEntryArchitecture/FOB Locator System/FOB Locator Module'         }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Door Lock Controller' }

compObjs=1×5 object
  1x5 Component array with properties:

    IsAdapterComponent
    Architecture
    ReferenceName
    Name
    Parent
    Ports
    OwnedPorts
    OwnedArchitecture
    Position
    Model
    SimulinkHandle
    SimulinkModelHandle
    UUID
    ExternalUID

% Include reference models in the search
softwareComps = zcModel.find(con1, 'IncludeReferenceModels', true)
softwareComps = 9x1 cell
    {'KeylessEntryArchitecture/Engine Control System/Keyless Start Controller'                                }
    {'KeylessEntryArchitecture/Lighting System/Lighting Controller'                                           }
    {'KeylessEntryArchitecture/Sound System/Sound Controller'                                                 }
    {'KeylessEntryArchitecture/FOB Locator System/FOB Locator Module'                                         }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Door Lock Controller'                                 }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Pass Door Lock Sensor/Detect Door Lock Status'   }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Driver Door Lock Sensor/Detect Door Lock Status' }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Pass Door Lock Sensor/Detect Door Lock Status'  }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Driver Door Lock Sensor/Detect Door Lock Status'}

Find all the base components in the system.

con2 = HasStereotype(IsStereotypeDerivedFrom("AutoProfile.BaseComponent"));
baseComps = zcModel.find(con2)
baseComps = 18x1 cell
    {'KeylessEntryArchitecture/Engine Control System/Keyless Start Controller'          }
    {'KeylessEntryArchitecture/Lighting System/Lighting Controller'                     }
    {'KeylessEntryArchitecture/Sound System/Sound Controller'                           }
    {'KeylessEntryArchitecture/FOB Locator System/FOB Locator Module'                   }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Door Lock Controller'           }
    {'KeylessEntryArchitecture/Sound System/Dashboard Speaker'                          }
    {'KeylessEntryArchitecture/Engine Control System/Start//Stop Button'                }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Driver Door Lock Sensor'  }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Pass Door Lock Sensor'    }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Driver Door Lock Sensor'   }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Pass Door Lock Sensor'     }
    {'KeylessEntryArchitecture/FOB Locator System/Center Receiver'                      }
    {'KeylessEntryArchitecture/FOB Locator System/Front Receiver'                       }
    {'KeylessEntryArchitecture/FOB Locator System/Rear Receiver'                        }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Driver Door Lock Actuator'}
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Front Pass Door Lock Actuator'  }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Driver Door Lock Actuator' }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Rear Pass Door Lock Actuator'   }

Find all components using the interface KeyFOBPosition.

con3 = HasPort(HasInterface(Property("Name") == "KeyFOBPosition"));
con3_a = HasPort(Property("InterfaceName") == "KeyFOBPosition");
keyFOBPosComps = zcModel.find(con3)
keyFOBPosComps = 10x1 cell
    {'KeylessEntryArchitecture/Door Lock//Unlock System'                      }
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Door Lock Controller' }
    {'KeylessEntryArchitecture/Engine Control System'                         }
    {'KeylessEntryArchitecture/Engine Control System/Keyless Start Controller'}
    {'KeylessEntryArchitecture/FOB Locator System'                            }
    {'KeylessEntryArchitecture/FOB Locator System/FOB Locator Module'         }
    {'KeylessEntryArchitecture/Lighting System'                               }
    {'KeylessEntryArchitecture/Lighting System/Lighting Controller'           }
    {'KeylessEntryArchitecture/Sound System'                                  }
    {'KeylessEntryArchitecture/Sound System/Sound Controller'                 }

Find all components whose WCET is less than or equal to 5ms.

con4 = PropertyValue("AutoProfile.SoftwareComponent.WCET") <= 5;
zcModel.find(con4)
ans = 1x1 cell array
    {'KeylessEntryArchitecture/Sound System/Sound Controller'}

% You can specify units and it will do the conversions for you
con5 = PropertyValue("AutoProfile.SoftwareComponent.WCET") <= Value(5, 'ms');
query1Comps = zcModel.find(con5)
query1Comps = 3x1 cell
    {'KeylessEntryArchitecture/Lighting System/Lighting Controller'  }
    {'KeylessEntryArchitecture/Sound System/Sound Controller'        }
    {'KeylessEntryArchitecture/FOB Locator System/FOB Locator Module'}

Find all components whose WCET is greater than 1 ms OR has a cost greater than 10 USD.

con6 = PropertyValue("AutoProfile.SoftwareComponent.WCET") > Value(1, 'ms') | PropertyValue("AutoProfile.Base.Cost") > Value(10, 'USD');
query2Comps = zcModel.find(con6)
query2Comps = 2x1 cell
    {'KeylessEntryArchitecture/Door Lock//Unlock System/Door Lock Controller' }
    {'KeylessEntryArchitecture/Engine Control System/Keyless Start Controller'}

Close the model.

zcModel.close;

See Also

| | | | | | | |

Related Topics