如何在Ecore模型中批量生成指定结构的Plane实例
Batch-Generating Plane Instances with Incrementing MotorType Values
Got it, let's work through how to batch-generate those Plane instances exactly as you need—keeping all the original subclass properties intact, only incrementing the MotorType value, and naming them sequentially like plane2, plane3, etc.
Core Approach
The key here is to:
- Grab your existing
plane1instance as a template - For each new plane, duplicate all its properties (MotorType's type, OtherClass1, OtherClass2)
- Only modify the MotorType's numerical value to increment by 1 each iteration
- Assign a sequential name and add the new Plane to your Simulation
Example Implementation (Python)
First, let's assume your class structure looks something like this (adjust to match your actual code):
class MotorType: def __init__(self, type_name, value): self.type_name = type_name # e.g., "TypeB" self.value = value # e.g., 10 class OtherClass1: def __init__(self, attr1, attr2): self.attr1 = attr1 self.attr2 = attr2 class OtherClass2: def __init__(self, attr_x): self.attr_x = attr_x class Plane: def __init__(self, name, motor_type, other1, other2): self.name = name self.motor_type = motor_type self.other_class1 = other1 self.other_class2 = other2 class Simulation: def __init__(self): self.planes = {} # Store planes by name for easy access
Here's how to generate the new planes:
# Your existing setup sim = Simulation() original_motor = MotorType("TypeB", 10) original_other1 = OtherClass1("foo", "bar") original_other2 = OtherClass2(42) sim.planes["plane1"] = Plane("plane1", original_motor, original_other1, original_other2) # ---------------------- # Batch generation code # ---------------------- import copy # Define how many new planes you want to create number_of_new_planes = 5 # Get your template plane template_plane = sim.planes["plane1"] for iteration in range(1, number_of_new_planes + 1): # 1. Create a new MotorType with incremented value new_motor_value = template_plane.motor_type.value + iteration new_motor = MotorType(template_plane.motor_type.type_name, new_motor_value) # 2. Copy OtherClass1/OtherClass2 instances (use deepcopy for independent objects) # If these classes have simple immutable properties, you could skip deepcopy and reuse the original new_other1 = copy.deepcopy(template_plane.other_class1) new_other2 = copy.deepcopy(template_plane.other_class2) # 3. Create the new plane with a sequential name new_plane_name = f"plane{iteration + 1}" new_plane = Plane(new_plane_name, new_motor, new_other1, new_other2) # 4. Add the new plane to your Simulation sim.planes[new_plane_name] = new_plane # Verify the results for plane_name, plane in sim.planes.items(): print(f"{plane_name}: MotorType={plane.motor_type.type_name}, Value={plane.motor_type.value}")
Key Notes
- Deep vs. Shallow Copy: Use
copy.deepcopy()forOtherClass1andOtherClass2if you want these to be independent instances (so changes to a new plane'sOtherClass1don't affect the originalplane1). If they're simple immutable objects (like strings/numbers), you can safely reuse the original instances instead. - Naming Flexibility: The f-string
f"plane{iteration + 1}"ensures sequential naming, but you can adjust this to match any naming convention you need. - Language Adaptability: If you're using a different language (like Java/C#), the logic stays the same—clone the template object, modify the MotorType value, assign a new name, and add to your Simulation collection. For example, in Java you'd use copy constructors or implement the
Cloneableinterface.
内容的提问来源于stack exchange,提问作者Thibault Santonja
相关产品推荐
相关产品推荐

