Skip to main content

ifc_schedule/task/
schedule_time_control.rs

1//! IFC2X3 `IfcScheduleTimeControl`, the task time IFC2X3 assigns through
2//! `IfcRelAssignsTasks.TimeForTask` (#235).
3//!
4//! # From IFC2X3 TC1 EXPRESS
5//!
6//! IFC2X3 declares no `IfcTask.TaskTime` and no `IfcTaskTime`. A task's
7//! times are an `IfcScheduleTimeControl`, an `IfcControl` (whose IFC2X3
8//! declaration adds no attribute to `IfcObject`), reached through the
9//! `IfcRelAssignsTasks` that assigns the task to its work control:
10//!
11//! ```text
12//! IfcRelAssignsTasks  (IfcRelAssignsToControl)
13//! 0 GlobalId  1 OwnerHistory  2 Name  3 Description
14//! 4 RelatedObjects  5 RelatedObjectsType  6 RelatingControl
15//! 7 TimeForTask : OPTIONAL IfcScheduleTimeControl
16//!   WR1 exactly one related object, WR2 an IfcTask,
17//!   WR3 RelatingControl an IfcWorkControl
18//!
19//! IfcScheduleTimeControl  (IfcControl)
20//! 0 GlobalId  1 OwnerHistory  2 Name  3 Description  4 ObjectType
21//! 5 ActualStart  6 EarlyStart  7 LateStart  8 ScheduleStart
22//! 9 ActualFinish  10 EarlyFinish  11 LateFinish  12 ScheduleFinish
23//! 13 ScheduleDuration  14 ActualDuration  15 RemainingTime
24//! 16 FreeFloat  17 TotalFloat  18 IsCritical  19 StatusTime
25//! 20 StartFloat  21 FinishFloat  22 Completion
26//! ```
27//!
28//! The dates are `IfcDateTimeSelect` records (`IfcCalendarDate`,
29//! `IfcLocalTime`, `IfcDateAndTime`) and the durations `IfcTimeMeasure`
30//! numbers in the project's time unit, returned as authored. Every
31//! attribute is read by name in the bound release. IFC4 and IFC4X3 declare
32//! neither entity, so there a task has no schedule time control.
33
34use ifc_model::{Entity, EntityId, Model, Value};
35
36use super::definition::{Task, TaskTimeAnomaly};
37use crate::release::ReadRelease;
38use crate::schedule::AuthoredDateTime;
39use crate::SchemaVersion;
40
41const ASSIGNS_TASKS: &str = "IFCRELASSIGNSTASKS";
42const TIME_CONTROL: &str = "IFCSCHEDULETIMECONTROL";
43
44/// A borrowed view of an IFC2X3 `IfcScheduleTimeControl`, as one
45/// `IfcRelAssignsTasks` assigns it to a task (#235).
46#[derive(Debug, Clone, Copy)]
47pub struct ScheduleTimeControl<'m> {
48    id: EntityId,
49    entity: &'m Entity,
50    assignment: EntityId,
51    work_control: Option<EntityId>,
52    release: ReadRelease,
53}
54
55impl<'m> ScheduleTimeControl<'m> {
56    /// The `IfcScheduleTimeControl` entity id.
57    #[must_use]
58    pub fn id(&self) -> EntityId {
59        self.id
60    }
61
62    /// The `IfcRelAssignsTasks` whose `TimeForTask` this is.
63    #[must_use]
64    pub fn assignment(&self) -> EntityId {
65        self.assignment
66    }
67
68    /// That assignment's `RelatingControl`, the work plan or schedule the
69    /// task is timed in.
70    #[must_use]
71    pub fn work_control(&self) -> Option<EntityId> {
72        self.work_control
73    }
74
75    /// The release this record is read against (IFC2X3).
76    #[must_use]
77    pub fn release(&self) -> SchemaVersion {
78        self.release.version()
79    }
80
81    fn value(&self, attribute: &'static str) -> Option<&'m Value> {
82        self.release.value(TIME_CONTROL, self.entity, attribute)
83    }
84
85    fn date_time(&self, attribute: &'static str) -> Option<AuthoredDateTime<'m>> {
86        match self.value(attribute)? {
87            Value::Ref(record) => Some(AuthoredDateTime::Record(*record)),
88            value => value.unwrap_typed().as_text().map(AuthoredDateTime::Text),
89        }
90    }
91
92    fn measure(&self, attribute: &'static str) -> Option<f64> {
93        self.value(attribute)?.unwrap_typed().as_f64()
94    }
95
96    /// The `GlobalId` string.
97    #[must_use]
98    pub fn global_id(&self) -> Option<&'m str> {
99        self.release.text(TIME_CONTROL, self.entity, "GlobalId")
100    }
101
102    /// The name.
103    #[must_use]
104    pub fn name(&self) -> Option<&'m str> {
105        self.release.text(TIME_CONTROL, self.entity, "Name")
106    }
107
108    /// `ActualStart`, an `IfcDateTimeSelect` record.
109    #[must_use]
110    pub fn actual_start(&self) -> Option<AuthoredDateTime<'m>> {
111        self.date_time("ActualStart")
112    }
113
114    /// `EarlyStart`, an `IfcDateTimeSelect` record.
115    #[must_use]
116    pub fn early_start(&self) -> Option<AuthoredDateTime<'m>> {
117        self.date_time("EarlyStart")
118    }
119
120    /// `LateStart`, an `IfcDateTimeSelect` record.
121    #[must_use]
122    pub fn late_start(&self) -> Option<AuthoredDateTime<'m>> {
123        self.date_time("LateStart")
124    }
125
126    /// `ScheduleStart`, an `IfcDateTimeSelect` record.
127    #[must_use]
128    pub fn schedule_start(&self) -> Option<AuthoredDateTime<'m>> {
129        self.date_time("ScheduleStart")
130    }
131
132    /// `ActualFinish`, an `IfcDateTimeSelect` record.
133    #[must_use]
134    pub fn actual_finish(&self) -> Option<AuthoredDateTime<'m>> {
135        self.date_time("ActualFinish")
136    }
137
138    /// `EarlyFinish`, an `IfcDateTimeSelect` record.
139    #[must_use]
140    pub fn early_finish(&self) -> Option<AuthoredDateTime<'m>> {
141        self.date_time("EarlyFinish")
142    }
143
144    /// `LateFinish`, an `IfcDateTimeSelect` record.
145    #[must_use]
146    pub fn late_finish(&self) -> Option<AuthoredDateTime<'m>> {
147        self.date_time("LateFinish")
148    }
149
150    /// `ScheduleFinish`, an `IfcDateTimeSelect` record.
151    #[must_use]
152    pub fn schedule_finish(&self) -> Option<AuthoredDateTime<'m>> {
153        self.date_time("ScheduleFinish")
154    }
155
156    /// `StatusTime`, an `IfcDateTimeSelect` record.
157    #[must_use]
158    pub fn status_time(&self) -> Option<AuthoredDateTime<'m>> {
159        self.date_time("StatusTime")
160    }
161
162    /// `ScheduleDuration`, an `IfcTimeMeasure` in the project's time unit.
163    #[must_use]
164    pub fn schedule_duration(&self) -> Option<f64> {
165        self.measure("ScheduleDuration")
166    }
167
168    /// `ActualDuration`, an `IfcTimeMeasure`.
169    #[must_use]
170    pub fn actual_duration(&self) -> Option<f64> {
171        self.measure("ActualDuration")
172    }
173
174    /// `RemainingTime`, an `IfcTimeMeasure`.
175    #[must_use]
176    pub fn remaining_time(&self) -> Option<f64> {
177        self.measure("RemainingTime")
178    }
179
180    /// `FreeFloat`, an `IfcTimeMeasure`.
181    #[must_use]
182    pub fn free_float(&self) -> Option<f64> {
183        self.measure("FreeFloat")
184    }
185
186    /// `TotalFloat`, an `IfcTimeMeasure`.
187    #[must_use]
188    pub fn total_float(&self) -> Option<f64> {
189        self.measure("TotalFloat")
190    }
191
192    /// `StartFloat`, an `IfcTimeMeasure`.
193    #[must_use]
194    pub fn start_float(&self) -> Option<f64> {
195        self.measure("StartFloat")
196    }
197
198    /// `FinishFloat`, an `IfcTimeMeasure`.
199    #[must_use]
200    pub fn finish_float(&self) -> Option<f64> {
201        self.measure("FinishFloat")
202    }
203
204    /// `IsCritical`, as authored.
205    #[must_use]
206    pub fn is_critical(&self) -> Option<bool> {
207        self.value("IsCritical")?.as_bool()
208    }
209
210    /// `Completion`, an `IfcPositiveRatioMeasure`, as authored.
211    #[must_use]
212    pub fn completion(&self) -> Option<f64> {
213        self.measure("Completion")
214    }
215}
216
217impl<'m> Task<'m> {
218    /// The IFC2X3 `IfcScheduleTimeControl`s assigned to this task through
219    /// `IfcRelAssignsTasks.TimeForTask`, in file order (#235).
220    ///
221    /// A task timed in several work controls has one per assignment. Empty
222    /// in IFC4 and IFC4X3, which declare neither entity and time tasks
223    /// with [`Self::time`]. A `TimeForTask` that references anything but an
224    /// `IfcScheduleTimeControl` is reported as
225    /// [`TaskTimeAnomaly::NotAScheduleTimeControl`], never read.
226    #[must_use]
227    pub fn schedule_time_controls(
228        &self,
229        model: &'m Model,
230    ) -> (Vec<ScheduleTimeControl<'m>>, Vec<TaskTimeAnomaly>) {
231        let release = self.read_release();
232        let mut found = Vec::new();
233        let mut anomalies = Vec::new();
234        if !release.declares(ASSIGNS_TASKS) {
235            return (found, anomalies);
236        }
237        for assignment in release.instances_of(model, ASSIGNS_TASKS) {
238            let Some(relation) = model.get(assignment) else {
239                continue;
240            };
241            let mut related = false;
242            if let Some(objects) = release.value(ASSIGNS_TASKS, relation, "RelatedObjects") {
243                objects.for_each_ref(&mut |id| related |= id == self.id());
244            }
245            if !related {
246                continue;
247            }
248            let Some(target) = release.reference(ASSIGNS_TASKS, relation, "TimeForTask") else {
249                continue;
250            };
251            let Some(entity) = model.get(target) else {
252                continue;
253            };
254            if !release.is_a(&entity.type_name, TIME_CONTROL) {
255                anomalies.push(TaskTimeAnomaly::NotAScheduleTimeControl {
256                    task: self.id(),
257                    assignment,
258                    target,
259                    found: entity.type_name.to_string(),
260                });
261                continue;
262            }
263            found.push(ScheduleTimeControl {
264                id: target,
265                entity,
266                assignment,
267                work_control: release.reference(ASSIGNS_TASKS, relation, "RelatingControl"),
268                release,
269            });
270        }
271        (found, anomalies)
272    }
273}