From e2b5ecf0a1874aa0ce34a99a72c9b2eefd08b2f1 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Mon, 20 Sep 2021 13:26:01 +1000 Subject: [PATCH] Resource work durations can now be calculated either daily or hourly --- .../api/resource/calculate_resource_work.py | 19 ++--- .../test/api/resource/__init__.py | 0 .../resource/test_calculate_resource_work.py | 70 +++++++++++++++++++ 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/ifcopenshell-python/test/api/resource/__init__.py create mode 100644 src/ifcopenshell-python/test/api/resource/test_calculate_resource_work.py diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py index aa68a8fe54..92a0ae82a8 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/calculate_resource_work.py @@ -23,14 +23,22 @@ class Usecase: total_produced = self.get_total_produced() if not unit_consumed or not unit_produced or not total_produced: return - seconds_worked = total_produced / unit_produced * unit_consumed - self.set_schedule_work(seconds_worked) + if not self.settings["resource"].Usage: + ifcopenshell.api.run("resource.add_resource_time", self.file, resource=self.settings["resource"]) + if "T" in self.productivity.get("BaseQuantityConsumed", None): + seconds = (unit_consumed.days * 24 * 60 * 60) + unit_consumed.seconds + amount_worked = total_produced / unit_produced * seconds + self.settings["resource"].Usage.ScheduleWork = f"PT{amount_worked / 60 / 60}H" + else: + days = unit_consumed.days + (unit_consumed.seconds / (24 * 60 * 60)) + amount_worked = total_produced / unit_produced * days + self.settings["resource"].Usage.ScheduleWork = f"P{amount_worked}D" def get_unit_consumed(self): duration = self.productivity.get("BaseQuantityConsumed", None) if not duration: return - return ifcopenshell.util.date.ifc2datetime(duration).seconds + return ifcopenshell.util.date.ifc2datetime(duration) def get_total_produced(self): total = 0 @@ -49,8 +57,3 @@ class Usecase: except: pass return total - - def set_schedule_work(self, seconds): - if not self.settings["resource"].Usage: - ifcopenshell.api.run("resource.add_resource_time", self.file, resource=self.settings["resource"]) - self.settings["resource"].Usage.ScheduleWork = f"PT{seconds / 60 / 60}H" diff --git a/src/ifcopenshell-python/test/api/resource/__init__.py b/src/ifcopenshell-python/test/api/resource/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/ifcopenshell-python/test/api/resource/test_calculate_resource_work.py b/src/ifcopenshell-python/test/api/resource/test_calculate_resource_work.py new file mode 100644 index 0000000000..931539e11c --- /dev/null +++ b/src/ifcopenshell-python/test/api/resource/test_calculate_resource_work.py @@ -0,0 +1,70 @@ +import test.bootstrap +import ifcopenshell.api + + +class TestCalculateResourceWork(test.bootstrap.IFC4): + def test_calculating_resource_work_based_on_a_daily_productivity_rate(self): + ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + + resource = ifcopenshell.api.run("resource.add_resource", self.file, ifc_class="IfcLaborResource") + pset = ifcopenshell.api.run("pset.add_pset", self.file, product=resource, name="EPset_Productivity") + ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={ + "BaseQuantityConsumed": "P0.5D", + "BaseQuantityProducedName": "GrossVolume", + "BaseQuantityProducedValue": 5, + }) + + slab = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=slab, name="Qto_SlabBaseQuantities") + ifcopenshell.api.run("pset.edit_qto", self.file, qto=qto, properties={"GrossVolume": 20}) + + task = ifcopenshell.api.run("sequence.add_task", self.file) + ifcopenshell.api.run("sequence.assign_product", self.file, relating_product=slab, related_object=task) + ifcopenshell.api.run("sequence.assign_process", self.file, relating_process=task, related_object=resource) + ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource) + assert resource.Usage.ScheduleWork == "P2.0D" + + + def test_calculating_resource_work_based_on_an_hourly_productivity_rate(self): + ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + + resource = ifcopenshell.api.run("resource.add_resource", self.file, ifc_class="IfcLaborResource") + pset = ifcopenshell.api.run("pset.add_pset", self.file, product=resource, name="EPset_Productivity") + ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={ + "BaseQuantityConsumed": "PT1H", + "BaseQuantityProducedName": "GrossVolume", + "BaseQuantityProducedValue": 5, + }) + + slab = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=slab, name="Qto_SlabBaseQuantities") + ifcopenshell.api.run("pset.edit_qto", self.file, qto=qto, properties={"GrossVolume": 20}) + + task = ifcopenshell.api.run("sequence.add_task", self.file) + ifcopenshell.api.run("sequence.assign_product", self.file, relating_product=slab, related_object=task) + ifcopenshell.api.run("sequence.assign_process", self.file, relating_process=task, related_object=resource) + ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource) + assert resource.Usage.ScheduleWork == "PT4.0H" + + def test_no_calculation_if_no_productivity_data_available(self): + ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") + + resource = ifcopenshell.api.run("resource.add_resource", self.file, ifc_class="IfcLaborResource") + + slab = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcSlab") + qto = ifcopenshell.api.run("pset.add_qto", self.file, product=slab, name="Qto_SlabBaseQuantities") + ifcopenshell.api.run("pset.edit_qto", self.file, qto=qto, properties={"GrossVolume": 20}) + + task = ifcopenshell.api.run("sequence.add_task", self.file) + ifcopenshell.api.run("sequence.assign_product", self.file, relating_product=slab, related_object=task) + ifcopenshell.api.run("sequence.assign_process", self.file, relating_process=task, related_object=resource) + ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource) + + assert resource.Usage is None + + pset = ifcopenshell.api.run("pset.add_pset", self.file, product=resource, name="EPset_Productivity") + ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"BaseQuantityProducedName": "foo"}) + + ifcopenshell.api.run("resource.calculate_resource_work", self.file, resource=resource) + + assert resource.Usage is None