OSDN Git Service

22d21b386d5cd6a4437d3060073dc0a1ff94eff1
[tjqt4port/tj2qt4.git] / taskjuggler / ExpressionTree.cpp
1 /*
2  * ExpressionTree.h - TaskJuggler
3  *
4  * Copyright (c) 2001, 2002 by Chris Schlaeger <cs@suse.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * $Id$
11  */
12
13 #include "ExpressionTree.h"
14 #include "CoreAttributes.h"
15 #include "Project.h"
16 #include "Task.h"
17 #include "Utility.h"
18
19 // Dummy marco to mark all keywords of taskjuggler syntax
20 #define KW(a) a
21
22 QDict<int> ExpressionTree::funcArgc;
23
24 long
25 Operation::evalAsInt(ExpressionTree* et)
26 {
27         switch (opt)
28         {
29         case Const:
30                 return value;
31         case Variable:
32         case Id:
33                 return et->resolve(name);
34         case Function:
35                 return evalFunction(et);
36         case Date:
37                 return value;
38         case Not:
39                 return !ops.at(0)->evalAsInt(et);
40         case And:
41                 return ops.at(0)->evalAsInt(et) && ops.at(1)->evalAsInt(et);
42         case Or:
43                 return ops.at(0)->evalAsInt(et) || ops.at(1)->evalAsInt(et);
44         default:
45                 qFatal("Operation::evalAsInt: "
46                            "Unknown opType %d (name: %s)", opt, name.ascii());
47                 return 0;
48         }
49 }
50
51 time_t
52 Operation::evalAsTime(ExpressionTree* et)
53 {
54         switch(opt)
55         {
56         case Const:
57         case Date:
58                 return value;
59         case Variable:
60         case Id:
61                 return et->resolve(name);
62         case Function:
63                 return evalFunction(et);
64         default:
65                 qFatal("Operation::evalAsTime: "
66                            "Unknown opType %d (name: %s)", opt, name.ascii());
67                 return 0;
68         }
69 }
70
71 QString
72 Operation::evalAsString(ExpressionTree* et)
73 {
74         switch(opt)
75         {
76         case Const:
77                 return QString("%1").arg(value);
78         case Function:
79                 return evalFunctionAsString(et);
80         case Date:
81                 return time2date(value);
82         case Id:
83                 return name;
84         default:
85                 qFatal("Operation::evalAsString: "
86                            "Unknown opType %d (name: %s)", opt, name.ascii());
87                 return QString::null;
88         }
89 }
90
91 long
92 Operation::evalFunction(ExpressionTree* et)
93 {
94         if (name == "istask")
95         {
96                 return strcmp(et->getCoreAttributes()->getType(), "Task") == 0
97                         && et->getCoreAttributes()->getId() ==
98                         ops.at(0)->evalAsString(et);
99         }
100         else if (name == "issubtaskof")
101         {
102                 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
103                         return 0;
104                 Task* p;
105                 if ((p = et->getCoreAttributes()->getProject()->getTask
106                          (ops.at(0)->evalAsString(et))) == 0)
107                         return 0;
108                 return p->isSubTask((Task*) et->getCoreAttributes());
109         }
110         else if (name == "containstask")
111         {
112                 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
113                         return 0;
114                 Task* st;
115                 if ((st = et->getCoreAttributes()->getProject()->getTask
116                          (ops.at(0)->evalAsString(et))) == 0)
117                         return 0;
118                 return ((Task*) et->getCoreAttributes())->isSubTask(st);
119         }
120         else if (name == "ismilestone")
121         {
122                 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
123                         return 0;
124                 return ((Task*) et->getCoreAttributes())->isMilestone();
125         }
126         else if (name == "isresource")
127         {
128                 return strcmp(et->getCoreAttributes()->getType(), "Resource") == 0
129                         && et->getCoreAttributes()->getId() ==
130                         ops.at(0)->evalAsString(et);
131         }
132         else if (name == "isaccount")
133         {
134                 return strcmp(et->getCoreAttributes()->getType(), "Account") == 0
135                         && et->getCoreAttributes()->getId() ==
136                         ops.at(0)->evalAsString(et);
137         }
138         else if (name == "isplanallocated")
139         {
140                 if (strcmp(et->getCoreAttributes()->getType(), "Resource") != 0)
141                         qFatal("Operation::evalFunction: isplanallocated called for "
142                                    "non-resource");
143                 return ((Resource*) et->getCoreAttributes())->isPlanAllocated
144                         (Interval(ops.at(1)->evalAsTime(et), ops.at(2)->evalAsTime(et)), 
145                          ops.at(0)->evalAsString(et));
146         }
147         else if (name == "isactualallocated")
148         {
149                 if (strcmp(et->getCoreAttributes()->getType(), "Resource") != 0)
150                         qFatal("Operation::evalFunction: isactualallocated called for "
151                                    "non-resource");
152                 return ((Resource*) et->getCoreAttributes())->isActualAllocated
153                         (Interval(ops.at(1)->evalAsTime(et), ops.at(2)->evalAsTime(et)), 
154                          ops.at(0)->evalAsString(et));
155         }
156         else
157                 qFatal("Unknown function %s", name.data());     
158
159         return 0;
160 }
161
162 QString
163 Operation::evalFunctionAsString(ExpressionTree* )
164 {
165         // There are no functions yet that return a string.
166         return QString::null;
167 }
168
169 ExpressionTree::ExpressionTree(Operation* op) : expression(op)
170 {
171         symbolTable.setAutoDelete(TRUE);
172         if (funcArgc.isEmpty())
173         {
174                 funcArgc.insert(KW("istask"), new int(1));
175                 funcArgc.insert(KW("issubtaskof"), new int(1));
176                 funcArgc.insert(KW("containstask"), new int(1));
177                 funcArgc.insert(KW("ismilestone"), new int(0));
178                 funcArgc.insert(KW("isresource"), new int(1));
179                 funcArgc.insert(KW("isaccount"), new int(1));
180                 funcArgc.insert(KW("isplanallocated"), new int(3));
181                 funcArgc.insert(KW("isactualallocated"), new int(3));
182         }
183 }
184
185 long
186 ExpressionTree::resolve(const QString& symbol)
187 {
188         return symbolTable[symbol] != 0 ? *(symbolTable[symbol]) : 0;
189 }
190
191 bool
192 ExpressionTree::isFunction(const QString& name)
193 {
194         return funcArgc[name];
195 }
196
197 int
198 ExpressionTree::arguments(const QString& name)
199 {
200         return *funcArgc[name];
201 }
202