Hi,
I’m trying to create a propertyValueGroup and PropertyValue from dataframe, as shown in the code below
pc_pkg = se.get_physical_architecture().get_physical_component_pkg()
data = {'Name':['ComponentA', 'Propulsion', 'ComponentC', 'ComponentD'],
'PVG':['PVG1', 'PVG2', 'PVG3', 'PVG4']
, 'values':[['test1', 'test2'],'test3',[ 'test4', 'test5], 'test6']}
df = pd.DataFrame(data)
df.any()
model.start_transaction()
try:
for row in range(0,5):
component=df['Name']
if component:
element=pc_pkg.get_owned_physical_components()
for e in element:
element_name=e.get_name()
print(element_name)
if any(element_name in df['Name'].values):
pvg_=df['PVG']
pvg=PropertyValueGroup()
pvg.set_name(df['PVG'])
#add the new PVG
e.get_owned_property_value_groups().add(pvg)
e.get_applied_property_value_groups().add(pvg)
pv=df['values']
pv=PropertyValue()
pv.set_name(df['values'])
pvg.get_owned_property_values().add(pv)
pvg.get_applied_property_values().add(pv)
else:
pass
else:
pass
except:
# if something went wrong we rollback the transaction
model.rollback_transaction()
raise
# start a transaction to modify the Capella model
model.start_transaction()
So if a physical component is in dataframe df[‘Name’] , the pvg corresponding to the same index of the column df[‘PVG’] is created, and then create the pv taken value of df[‘values’].
Thanks in advance
If the script freezes. There is a bug when using numpy (a pandas dependency). You can find more details here.
Your script should probably look like this:
# include needed for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
from simplified_api.capella import *
import pandas as pd
# change this path to execute the script on your model (here is the IFE sample).
# comment it if you want to use the "Run configuration" instead
aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
'''
#Here is the "Run Configuration" part to uncomment if you want to use this functionality :
#check parameter numbers
if len(argv) != 1:
# use IFE default values
aird_path = "/In-Flight Entertainment System/In-Flight Entertainment System.aird"
else:
# Load the Capella model from the first argument of the script
aird_path = argv[0]
'''
model = CapellaModel()
model.open(aird_path)
# gets the SystemEngineering and print its name
se = model.get_system_engineering()
pc_pkg = se.get_physical_architecture().get_physical_component_pkg()
data = {'Name':['ComponentA', 'Propulsion', 'ComponentC', 'ComponentD'],
'PVG':['PVG1', 'PVG2', 'PVG3', 'PVG4']
, 'values':[['test1', 'test2'],'test3',[ 'test4', 'test5'], 'test6']}
df = pd.DataFrame(data)
df = df.set_index("Name", drop = False)
model.start_transaction()
try:
element=pc_pkg.get_owned_physical_components()
for e in element:
element_name=e.get_name()
print(element_name)
if element_name in df['Name'].values:
row = df.loc[element_name, : ]
pvg=PropertyValueGroup()
pvg_name = row['PVG']
print(' - ' + pvg_name)
pvg.set_name(pvg_name)
#add the new PVG
e.get_owned_property_value_groups().add(pvg)
e.get_applied_property_value_groups().add(pvg)
for pv_name in row['values']:
print(' - ' + pv_name)
pv=PropertyValue()
pv.set_name(pv_name)
pv.set_value(pv_name)
pvg.get_owned_property_values().add(pv)
pvg.get_applied_property_values().add(pv)
except:
# if something went wrong we rollback the transaction
model.rollback_transaction()
raise
else:
# if everything is ok we commit the transaction
model.commit_transaction()
# save the Capella model
model.save()