Generate functions for all API usecases for better static code features. See #2693.

This commit is contained in:
Dion Moult
2024-05-06 14:35:39 +10:00
parent 10f894e2ea
commit d11ec67129
330 changed files with 13283 additions and 13751 deletions
@@ -22,68 +22,71 @@ import ifcopenshell.util.date
import ifcopenshell.util.element
def calculate_task_duration(file, task=None) -> None:
"""Calculates the task duration based on resource usage
If a task has labour or equipment resources assigned to it, its duration
may be parametrically derived from the scheduled work of the resource.
For example, a labour resource with scheduled work of 10 working days
and a resource utilisation of 200% (i.e. two labour teams) will imply
that the task duration is 5 working days.
If this data is not available, such as if the task has no resources,
then nothing happens.
:param task: The IfcTask to calculate the duration for.
:type task: ifcopenshell.entity_instance
:return: None
:rtype: None
Example:
.. code:: python
# Add our own crew
crew = ifcopenshell.api.run("resource.add_resource", model, ifc_class="IfcCrewResource")
# Add some labour to our crew.
labour = ifcopenshell.api.run("resource.add_resource", model,
parent_resource=crew, ifc_class="IfcLaborResource")
# Labour resource is quantified in terms of time.
quantity = ifcopenshell.api.run("resource.add_resource_quantity", model,
resource=labour, ifc_class="IfcQuantityTime")
# Store the unit time used in hours
ifcopenshell.api.run("resource.edit_resource_quantity", model,
physical_quantity=quantity, attributes={"TimeValue": 8.0})
# Let's imagine we've used the resource for 10 days with a
# utilisation of 200%.
time = ifcopenshell.api.run("resource.add_resource_time", model, resource=labour)
ifcopenshell.api.run("resource.edit_resource_time", model,
resource_time=time, attributes={"ScheduleWork": "PT80H", "ScheduleUsage": 2})
# Let's imagine we are creating a construction schedule. All tasks
# need to be part of a work schedule.
schedule = ifcopenshell.api.run("sequence.add_work_schedule", model, name="Construction Schedule A")
# Let's create a construction task. Note that the predefined type is
# important to distinguish types of tasks.
task = ifcopenshell.api.run("sequence.add_task", model,
work_schedule=schedule, name="Foundations", identification="A")
# Assign our resource to the task.
ifcopenshell.api.run("sequence.assign_process", model, relating_process=task, related_object=labour)
# Now we can calculate the task duration based on the resource. This
# will set task.TaskTime.ScheduleDuration to be P5D.
ifcopenshell.api.run("sequence.calculate_task_duration", model, task=task)
"""
usecase = Usecase()
usecase.file = file
usecase.settings = {"task": task}
return usecase.execute()
class Usecase:
def __init__(self, file, task=None):
"""Calculates the task duration based on resource usage
If a task has labour or equipment resources assigned to it, its duration
may be parametrically derived from the scheduled work of the resource.
For example, a labour resource with scheduled work of 10 working days
and a resource utilisation of 200% (i.e. two labour teams) will imply
that the task duration is 5 working days.
If this data is not available, such as if the task has no resources,
then nothing happens.
:param task: The IfcTask to calculate the duration for.
:type task: ifcopenshell.entity_instance
:return: None
:rtype: None
Example:
.. code:: python
# Add our own crew
crew = ifcopenshell.api.run("resource.add_resource", model, ifc_class="IfcCrewResource")
# Add some labour to our crew.
labour = ifcopenshell.api.run("resource.add_resource", model,
parent_resource=crew, ifc_class="IfcLaborResource")
# Labour resource is quantified in terms of time.
quantity = ifcopenshell.api.run("resource.add_resource_quantity", model,
resource=labour, ifc_class="IfcQuantityTime")
# Store the unit time used in hours
ifcopenshell.api.run("resource.edit_resource_quantity", model,
physical_quantity=quantity, attributes={"TimeValue": 8.0})
# Let's imagine we've used the resource for 10 days with a
# utilisation of 200%.
time = ifcopenshell.api.run("resource.add_resource_time", model, resource=labour)
ifcopenshell.api.run("resource.edit_resource_time", model,
resource_time=time, attributes={"ScheduleWork": "PT80H", "ScheduleUsage": 2})
# Let's imagine we are creating a construction schedule. All tasks
# need to be part of a work schedule.
schedule = ifcopenshell.api.run("sequence.add_work_schedule", model, name="Construction Schedule A")
# Let's create a construction task. Note that the predefined type is
# important to distinguish types of tasks.
task = ifcopenshell.api.run("sequence.add_task", model,
work_schedule=schedule, name="Foundations", identification="A")
# Assign our resource to the task.
ifcopenshell.api.run("sequence.assign_process", model, relating_process=task, related_object=labour)
# Now we can calculate the task duration based on the resource. This
# will set task.TaskTime.ScheduleDuration to be P5D.
ifcopenshell.api.run("sequence.calculate_task_duration", model, task=task)
"""
self.file = file
self.settings = {"task": task}
def execute(self):
self.seconds_per_workday = self.calculate_seconds_per_workday()
duration = self.calculate_max_resource_usage_duration()
@@ -93,9 +96,7 @@ class Usecase:
def calculate_seconds_per_workday(self):
def get_work_schedule(task):
for rel in task.HasAssignments or []:
if rel.is_a("IfcRelAssignsToControl") and rel.RelatingControl.is_a(
"IfcWorkSchedule"
):
if rel.is_a("IfcRelAssignsToControl") and rel.RelatingControl.is_a("IfcWorkSchedule"):
return rel.RelatingControl
for rel in task.Nests or []:
return get_work_schedule(rel.RelatingObject)
@@ -111,9 +112,7 @@ class Usecase:
or "WorkDayDuration" not in psets["Pset_WorkControlCommon"]
):
return default_seconds_per_workday
work_day_duration = ifcopenshell.util.date.ifc2datetime(
psets["Pset_WorkControlCommon"]["WorkDayDuration"]
)
work_day_duration = ifcopenshell.util.date.ifc2datetime(psets["Pset_WorkControlCommon"]["WorkDayDuration"])
return work_day_duration.seconds
def calculate_max_resource_usage_duration(self):
@@ -133,23 +132,15 @@ class Usecase:
if not resource.Usage or not resource.Usage.ScheduleWork:
return
schedule_usage = resource.Usage.ScheduleUsage or 1
schedule_duration = ifcopenshell.util.date.ifc2datetime(
resource.Usage.ScheduleWork
)
schedule_duration = ifcopenshell.util.date.ifc2datetime(resource.Usage.ScheduleWork)
if is_hourly_work(resource.Usage.ScheduleWork):
schedule_seconds = (
schedule_duration.days * 24 * 60 * 60
) + schedule_duration.seconds
schedule_seconds = (schedule_duration.days * 24 * 60 * 60) + schedule_duration.seconds
else:
partial_days = schedule_duration.seconds / (24 * 60 * 60)
schedule_seconds = (
schedule_duration.days + partial_days
) * self.seconds_per_workday
schedule_seconds = (schedule_duration.days + partial_days) * self.seconds_per_workday
return math.ceil((schedule_seconds / self.seconds_per_workday) / schedule_usage)
def set_task_duration(self, duration):
if not self.settings["task"].TaskTime:
ifcopenshell.api.run(
"sequence.add_task_time", self.file, task=self.settings["task"]
)
ifcopenshell.api.run("sequence.add_task_time", self.file, task=self.settings["task"])
self.settings["task"].TaskTime.ScheduleDuration = f"P{duration}D"