kplato

kptresource.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 Thomas Zander zander@kde.org
3 Copyright (C) 2004, 2005 Dag Andersen <danders@get2net.dk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KPTRESOURCE_H
22#define KPTRESOURCE_H
23
24#include "kptduration.h"
25#include "kptdatetime.h"
26
27#include <tqdom.h>
28#include <tqintdict.h>
29#include <tqstring.h>
30#include <tqptrlist.h>
31
32#include <kdebug.h>
33
34class TQTime;
35
36namespace KPlato
37{
38
39class Risk;
40class Effort;
41class Appointment;
42class Task;
43class Node;
44class Project;
45class Resource;
46class ResourceRequest;
47class ResourceGroupRequest;
48class Calendar;
49class ResourceRequestCollection;
50class EffortCostMap;
51class Schedule;
53class Schedule;
54
59
60/* IDEA; lets create a resourceGroup that has the intelligence to import PIM schedules
61 * from the kroupware project and use the schedules to use the factory pattern to build
62 * Resources (probably a derived class) which returns values on getFirstAvailableTime
63 * and friends based on the schedules we got from the PIM projects.
64 * (Thomas Zander mrt-2003 by suggestion of Shaheed)
65 */
66class ResourceGroup {
67 public:
68 ResourceGroup(Project *project);
69 ~ResourceGroup();
70
71 enum Type { Type_Work, Type_Material };
72
73 TQString id() const { return m_id; }
74 bool setId(TQString id);
75 void generateId();
76
77 Project *project() { return m_project; }
78
79 void setName(TQString n) {m_name=n;}
80 const TQString &name() const {return m_name;}
81 void setType(Type type) { m_type = type; }
82 //void setType(const TQString &type);
83 Type type() const { return m_type; }
84
92 void addResource(Resource*, Risk*);
93 void insertResource( unsigned int index, Resource *resource );
94 void removeResource( Resource *resource );
95 Resource *takeResource( Resource *resource );
96 void removeResource(int);
97
98 Resource* getResource(int);
99 Risk* getRisk(int);
100
107 TQPtrList<Resource> availableResources(const DateTime start, const Duration duration, int num);
113 void addRequiredResource(ResourceGroup*);
119 ResourceGroup* getRequiredResource(int);
125 void removeRequiredResource(int);
126 int numResources() const { return m_resources.count(); }
127 TQPtrList<Resource> &resources() { return m_resources; }
128
129 bool load(TQDomElement &element);
130 void save(TQDomElement &element) const;
131
132 void initiateCalculation(Schedule &sch);
133
134 void addNode(const Node *node) { m_nodes.append(node); }
135 void clearNodes() { m_nodes.clear(); }
136
137 Calendar *defaultCalendar() { return m_defaultCalendar; }
138
139 int units();
140
141 void registerRequest(ResourceGroupRequest *request)
142 { m_requests.append(request); }
143 void unregisterRequest(ResourceGroupRequest *request)
144 { m_requests.removeRef(request); }
145 const TQPtrList<ResourceGroupRequest> &requests() const
146 { return m_requests; }
147
148 ResourceGroup *findId() const { return findId(m_id); }
149 ResourceGroup *findId(const TQString &id) const;
150 bool removeId() { return removeId(m_id); }
151 bool removeId(const TQString &id);
152 void insertId(const TQString &id);
153
154 Appointment appointmentIntervals() const;
155
156#ifndef NDEBUG
157 void printDebug(TQString ident);
158#endif
159
160 private:
161 Project *m_project;
162 TQString m_id; // unique id
163 TQString m_name;
164 TQPtrList<Resource> m_resources;
165 TQPtrList<Risk> m_risks;
166 TQPtrList<ResourceGroup> m_requires;
167
168 TQPtrList<Node> m_nodes; //The nodes that want resources from us
169
170 Calendar *m_defaultCalendar;
171 Type m_type;
172
173 TQPtrList<ResourceGroupRequest> m_requests;
174
175};
176
186
187class Resource {
188public:
189
190 Resource(Project *project);
191 Resource(Resource *resource);
192 virtual ~Resource();
193
194 TQString id() const { return m_id; }
195 bool setId(TQString id);
196 void generateId();
197
198 enum Type { Type_Work, Type_Material };
199 void setType(Type type) { m_type = type; }
200 void setType(const TQString &type);
201 Type type() const { return m_type; }
202 TQString typeToString() const;
203
204 void setName(TQString n) {m_name=n;}
205 const TQString &name() const {return m_name;}
206
207 void setInitials(TQString initials) {m_initials=initials;}
208 const TQString &initials() const {return m_initials;}
209
210 void setEmail(TQString email) {m_email=email;}
211 const TQString &email() const {return m_email;}
212
213 void copy(Resource *resource);
214
216 void setAvailableFrom(const TQDateTime &af) {m_availableFrom=af;}
218 const DateTime &availableFrom() const {return m_availableFrom;}
220 void setAvailableUntil(const TQDateTime au) {m_availableUntil=au;}
222 const DateTime &availableUntil() const {return m_availableUntil;}
223
224 void addWorkingHour(TQTime from, TQTime until);
225 TQPtrList<TQTime> workingHours() { return m_workingHours; }
226
227 DateTime getFirstAvailableTime(DateTime after = DateTime());
228 DateTime getBestAvailableTime(Duration duration);
229 DateTime getBestAvailableTime(const DateTime after, const Duration duration);
230
231 bool load(TQDomElement &element);
232 void save(TQDomElement &element) const;
233
235 TQPtrList<Appointment> appointments();
236
237 Appointment *findAppointment(Node *node);
239 virtual bool addAppointment(Appointment *appointment);
241 virtual bool addAppointment(Appointment *appointment, Schedule &main);
243 virtual void addAppointment(Schedule *node, DateTime &start, DateTime &end, double load=100);
244
245 void initiateCalculation(Schedule &sch);
246 bool isAvailable(Task *task);
247 void makeAppointment(Schedule *schedule);
248
249 bool isOverbooked() const;
250 bool isOverbooked(const TQDate &date) const;
251 bool isOverbooked(const DateTime &start, const DateTime &end) const;
252
253 double normalRate() const { return cost.normalRate; }
254 void setNormalRate(double rate) { cost.normalRate = rate; }
255 double overtimeRate() const { return cost.overtimeRate; }
256 void setOvertimeRate(double rate) { cost.overtimeRate = rate; }
257 double fixedCost() const { return cost.fixed; }
258 void setFixedCost(double value) { cost.fixed = value; }
259
263 int units() const { return m_units; }
267 void setUnits(int units) { m_units = units; }
268
269 Project *project() const { return m_project; }
270
275 Calendar *calendar(bool local=false) const;
276 Calendar *calendar(const TQString id) const;
277 void setCalendar(Calendar *calendar) { m_calendar = calendar; }
278
282 void registerRequest(const ResourceRequest *request)
283 { m_requests.append(request); }
284 void unregisterRequest(const ResourceRequest *request)
285 { m_requests.removeRef(request); }
286 const TQPtrList<ResourceRequest> &requests() const
287 { return m_requests; }
288
289 Duration effort(const DateTime &start, const Duration &duration, bool backward, bool *ok=0) const;
290
295 DateTime availableAfter(const DateTime &time, const DateTime limit=DateTime(), bool checkAppointments=false) const;
300 DateTime availableBefore(const DateTime &time, const DateTime limit=DateTime(), bool checkAppointments=false) const;
301
302 Resource *findId() const { return findId(m_id); }
303 Resource *findId(const TQString &id) const;
304 bool removeId() { return removeId(m_id); }
305 bool removeId(const TQString &id);
306 void insertId(const TQString &id);
307
308 Calendar *findCalendar(const TQString &id) const;
309
310 Appointment appointmentIntervals() const;
311 Duration plannedEffort(const TQDate &date) const;
312
313 void setCurrentSchedule(Schedule *schedule) { m_currentSchedule = schedule; }
314 void setCurrentSchedule(long id) { m_currentSchedule = findSchedule(id); }
315 Schedule *currentSchedule() const { return m_currentSchedule; }
316
317 TQIntDict<Schedule> &schedules() { return m_schedules; }
318 Schedule *findSchedule(long id) { return m_schedules[id]; }
320 void removeSchedule(Schedule *schedule);
322 void takeSchedule(const Schedule *schedule);
323 void addSchedule(Schedule *schedule);
324 ResourceSchedule *createSchedule(TQString name, int type, long id);
325 ResourceSchedule *createSchedule(Schedule *parent);
326
327protected:
328 void makeAppointment(Schedule *node, const DateTime &from, const DateTime &end);
329
330private:
331 Project *m_project;
332 TQIntDict<Schedule> m_schedules;
333 TQString m_id; // unique id
334 TQString m_name;
335 TQString m_initials;
336 TQString m_email;
337 DateTime m_availableFrom;
338 DateTime m_availableUntil;
339 TQPtrList<TQTime> m_workingHours;
340
341 int m_units; // avalable units in percent
342
343 Type m_type;
344
345 struct Cost {
346 double normalRate;
347 double overtimeRate;
348 double fixed;
349 } cost;
350
351 Calendar *m_calendar;
352 TQPtrList<ResourceRequest> m_requests;
353
354 Schedule *m_currentSchedule;
355
356public:
357#ifndef NDEBUG
358 void printDebug(TQString ident);
359#endif
360};
361
362
368class Risk {
369 public:
370
371 enum RiskType {
372 NONE=0,
373 LOW=1,
374 HIGH=2
375 };
376
377 Risk(Node *n, Resource *r, RiskType rt=NONE);
378 ~Risk();
379
380 RiskType riskType() { return m_riskType; }
381
382 Node *node() { return m_node; }
383 Resource *resource() { return m_resource; }
384
385 private:
386 Node *m_node;
387 Resource *m_resource;
388 RiskType m_riskType;
389};
390
391class ResourceRequest {
392 public:
393 ResourceRequest(Resource *resource=0, int units = 1);
394
395 ~ResourceRequest();
396
397 ResourceGroupRequest *parent() const { return m_parent; }
398 void setParent(ResourceGroupRequest *parent) { m_parent = parent; }
399
400 Resource *resource() const { return m_resource; }
401 void setResource(Resource* resource) { m_resource = resource; }
402
403 bool load(TQDomElement &element, Project &project);
404 void save(TQDomElement &element) const;
405
409 int units() const;
410
414 int workUnits() const;
415
416 void registerRequest() { if (m_resource) m_resource->registerRequest(this); }
417 void unregisterRequest() { if (m_resource) m_resource->unregisterRequest(this); }
418
419 void makeAppointment(Schedule *schedule) {
420 if (m_resource)
421 m_resource->makeAppointment(schedule);
422 }
423 Task *task() const;
424
425 private:
426 Resource *m_resource;
427 int m_units;
428 ResourceGroupRequest *m_parent;
429
430#ifndef NDEBUG
431public:
432 void printDebug(TQString ident);
433#endif
434};
435
436class ResourceGroupRequest {
437 public:
438 ResourceGroupRequest(ResourceGroup *group=0, int units=0);
439 ~ResourceGroupRequest();
440
441 void setParent(ResourceRequestCollection *parent) { m_parent = parent;}
442 ResourceRequestCollection *parent() const { return m_parent; }
443
444 ResourceGroup *group() const { return m_group; }
445 TQPtrList<ResourceRequest> &resourceRequests() { return m_resourceRequests; }
446 void addResourceRequest(ResourceRequest *request);
447 void removeResourceRequest(ResourceRequest *request) { m_resourceRequests.removeRef(request); }
448 ResourceRequest *takeResourceRequest(ResourceRequest *request);
449 ResourceRequest *find(Resource *resource) const;
450
451 bool load(TQDomElement &element, Project &project);
452 void save(TQDomElement &element) const;
453
457 int units() const;
458
462 int workUnits() const;
463
464 Duration effort(const DateTime &time, const Duration &duration, bool backward, bool *ok=0) const;
465
466 int numDays(const DateTime &time, bool backward) const;
467
472 Duration duration(const DateTime &start, const Duration &effort, bool backward=false);
473
474 DateTime availableAfter(const DateTime &time);
475 DateTime availableBefore(const DateTime &time);
476
481 void makeAppointments(Schedule *schedule);
482
486 void reserve(const DateTime &start, const Duration &duration);
487
488 bool isEmpty() const;
489
490 Task *task() const;
491
492 private:
493 ResourceGroup *m_group;
494 int m_units;
495 ResourceRequestCollection *m_parent;
496
497 TQPtrList<ResourceRequest> m_resourceRequests;
498 DateTime m_start;
499 Duration m_duration;
500
501#ifndef NDEBUG
502public:
503 void printDebug(TQString ident);
504#endif
505};
506
507class ResourceRequestCollection {
508public:
509 ResourceRequestCollection(Task &task);
510 ~ResourceRequestCollection();
511
512 const TQPtrList<ResourceGroupRequest> &requests() const { return m_requests; }
513 void addRequest(ResourceGroupRequest *request) {
514 m_requests.append(request);
515 request->setParent(this);
516 }
517 void removeRequest(ResourceGroupRequest *request) { m_requests.removeRef(request); }
518 void takeRequest(ResourceGroupRequest *request) { m_requests.take(m_requests.findRef(request)); }
519 ResourceGroupRequest *find(ResourceGroup *resource) const;
520 ResourceRequest *find(Resource *resource) const;
521 bool isEmpty() const;
522
523 //bool load(TQDomElement &element, Project &project);
524 void save(TQDomElement &element) const;
525
526 void clear() { m_requests.clear(); }
527
531 int units() const;
532
536 int workUnits() const;
537
542 Duration duration(const DateTime &time, const Duration &effort, bool backward=false);
543
544 DateTime availableAfter(const DateTime &time);
545 DateTime availableBefore(const DateTime &time);
546
551 void makeAppointments(Schedule *schedule);
555 void reserve(const DateTime &start, const Duration &duration);
556
557 Task &task() const { return m_task; }
558
559protected:
560 struct Interval {
561 DateTime start;
562 DateTime end;
563 Duration effort;
564 };
565
566
567private:
568 Task &m_task;
569 TQPtrList<ResourceGroupRequest> m_requests;
570
571#ifndef NDEBUG
572public:
573 void printDebug(TQString ident);
574#endif
575};
576
577} //KPlato namespace
578
579#endif
A resource (Resource) can be scheduled to be used at any time, this is represented internally with Ap...
Calendar defines the working and nonworking days and hours.
DateTime is a TQDateTime which knows about Duration.
Definition kptdatetime.h:33
The duration class can be used to store a timespan in a convenient format.
Definition kptduration.h:34
Any Node will store how much time it takes to complete the node (typically a Task) in the traditional...
Definition kptnode.h:556
This class represents any node in the project, a node can be a project or a subproject or any task.
Definition kptnode.h:57
Project is the main node in a project, it contains child nodes and possibly sub-projects.
Definition kptproject.h:52
void addRequiredResource(ResourceGroup *)
Manage the dependent resources.
ResourceGroup * getRequiredResource(int)
Manage the dependent resources.
void removeRequiredResource(int)
Manage the dependent resources.
void addResource(Resource *, Risk *)
Manage the resources in this list.
TQPtrList< Resource > availableResources(const DateTime start, const Duration duration, int num)
Get the "num" resources which is available in the time frame defined by "start" and "duration".
ResourceSchedule holds scheduling information for a resource.
Any resource that is used by a task.
void setAvailableFrom(const TQDateTime &af)
Set the time from when the resource is available to this project.
int units() const
Return available units in percent.
void takeSchedule(const Schedule *schedule)
Take, don't delete.
TQPtrList< Appointment > appointments()
Return the list of appointments for current schedule.
Calendar * calendar(bool local=false) const
Get the calendar for this resource.
void removeSchedule(Schedule *schedule)
Take, and delete.
void setUnits(int units)
Set available units in percent.
DateTime availableBefore(const DateTime &time, const DateTime limit=DateTime(), bool checkAppointments=false) const
Find the first available time before time, within limit.
DateTime availableAfter(const DateTime &time, const DateTime limit=DateTime(), bool checkAppointments=false) const
Find the first available time after time, within limit.
const DateTime & availableFrom() const
Return the time when the resource is available to this project.
virtual bool addAppointment(Appointment *appointment)
Adds appointment to current schedule.
void setAvailableUntil(const TQDateTime au)
Set the time when the resource is no longer available to this project.
const DateTime & availableUntil() const
Return the time when the resource is no longer available to this project.
void registerRequest(const ResourceRequest *request)
Used to clean up requests when the resource is deleted.
Risk is associated with a resource/task pairing to indicate the planner's confidence in the estimated...
The Schedule class holds data calculated during project calculation and scheduling,...
Definition kptschedule.h:55
A task in the scheduling software is represented by this class.
Definition kpttask.h:40