Managing Object Relationships
In PLEXOS, objects can have relationships with each other. These relationships are managed through memberships in PlexosDB.
Creating Relationships (Memberships)
Section titled “Creating Relationships (Memberships)”Create relationships between objects:
from plexosdb import PlexosDBfrom plexosdb.enums import ClassEnum, CollectionEnum
# Initialize databasedb = PlexosDB()db.create_schema(version=10)
# Create parent and child objectsdb.add_object(ClassEnum.Region, "Region1")db.add_object(ClassEnum.Node, "Node1")db.add_object(ClassEnum.Node, "Node2")
# Create a membership between Region and Nodedb.add_membership( parent_class_enum=ClassEnum.Node, child_class_enum=ClassEnum.Region, parent_object_name="Node1", child_object_name="Region1", collection_enum=CollectionEnum.Region)
# Add another node to the regiondb.add_membership( parent_class_enum=ClassEnum.Node, child_class_enum=ClassEnum.Region, parent_object_name="Node2", child_object_name="Region1", collection_enum=CollectionEnum.Region)Checking Memberships
Section titled “Checking Memberships”Check if a membership exists:
# Check if a membership existsmembership_exists = db.check_membership_exists( "Region1", "Node1", parent_class=ClassEnum.Region, child_class=ClassEnum.Node, collection=CollectionEnum.ReferenceNode)
if membership_exists: print("Membership exists")else: print("Membership does not exist")Getting Membership Information
Section titled “Getting Membership Information”Retrieve membership information:
# Get membership IDmembership_id = db.get_membership_id( "Region1", "Node1", CollectionEnum.ReferenceNode)print(f"Membership ID: {membership_id}")
# Get all memberships for an objectmemberships = db.get_object_memberships( "Node1", class_enum=ClassEnum.Node)
for membership in memberships: print(f"Parent: {membership['parent']} ({membership['parent_class_name']})") print(f"Child: {membership['child']} ({membership['child_class_name']})") print(f"Collection: {membership['collection_name']}") print("---")Listing Child Objects
Section titled “Listing Child Objects”List all child objects for a parent:
# List all nodes in a regionchild_objects = db.list_child_objects( "Region1", parent_class=ClassEnum.Region, child_class=ClassEnum.Node, collection=CollectionEnum.ReferenceNode)
print("Nodes in Region1:")for child in child_objects: print(f"- {child['name']}")Listing Parent Objects
Section titled “Listing Parent Objects”List all parent objects for a child:
# List all regions a node belongs toparent_objects = db.list_parent_objects( "Node1", child_class=ClassEnum.Node, parent_class=ClassEnum.Region, collection=CollectionEnum.ReferenceNode)
print("Regions containing Node1:")for parent in parent_objects: print(f"- {parent['name']}")Bulk Adding Memberships
Section titled “Bulk Adding Memberships”For efficiency when adding many memberships:
# Create membership recordsmembership_records = [ {"parent": "Region1", "child": "Node1"}, {"parent": "Region1", "child": "Node2"}, {"parent": "Region1", "child": "Node3"}, # Add more relationships as needed]
# Add objects if they do not exist yettry: db.add_object(ClassEnum.Region, "Region1")except: pass
for record in membership_records: try: db.add_object(ClassEnum.Node, record["child"]) except: pass
db_records = []parent_class_id = db.get_class_id(ClassEnum.Region)child_class_id = db.get_class_id(ClassEnum.Node)collection_id = db.get_collection_id( CollectionEnum.ReferenceNode, ClassEnum.Region, ClassEnum.Node
# Bulk add membershipsdb.add_memberships_from_records( membership_records, parent_class=ClassEnum.Node, child_class=ClassEnum.Region, collection=CollectionEnum.Region, create_missing_objects=True # This will create Node3 if it doesn't exist)
for record in membership_records: parent_object_id = db.get_object_id(ClassEnum.Region, record["parent"]) child_object_id = db.get_object_id(ClassEnum.Node, record["child"])
db_records.append({ "parent_class_id": parent_class_id, "parent_object_id": parent_object_id, "collection_id": collection_id, "child_class_id": child_class_id, "child_object_id": child_object_id, })
# Bulk add membershipsdb.add_memberships_from_records(db_records)