2 * ExpressionTree.h - TaskJuggler
4 * Copyright (c) 2001, 2002 by Chris Schlaeger <cs@suse.de>
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.
13 #include "ExpressionTree.h"
14 #include "CoreAttributes.h"
19 // Dummy marco to mark all keywords of taskjuggler syntax
22 QDict<int> ExpressionTree::funcArgc;
25 Operation::evalAsInt(ExpressionTree* et)
33 return et->resolve(name);
35 return evalFunction(et);
39 return !ops.at(0)->evalAsInt(et);
41 return ops.at(0)->evalAsInt(et) && ops.at(1)->evalAsInt(et);
43 return ops.at(0)->evalAsInt(et) || ops.at(1)->evalAsInt(et);
45 qFatal("Operation::evalAsInt: "
46 "Unknown opType %d (name: %s)", opt, name.ascii());
52 Operation::evalAsTime(ExpressionTree* et)
61 return et->resolve(name);
63 return evalFunction(et);
65 qFatal("Operation::evalAsTime: "
66 "Unknown opType %d (name: %s)", opt, name.ascii());
72 Operation::evalAsString(ExpressionTree* et)
77 return QString("%1").arg(value);
79 return evalFunctionAsString(et);
81 return time2date(value);
85 qFatal("Operation::evalAsString: "
86 "Unknown opType %d (name: %s)", opt, name.ascii());
92 Operation::evalFunction(ExpressionTree* et)
96 return strcmp(et->getCoreAttributes()->getType(), "Task") == 0
97 && et->getCoreAttributes()->getId() ==
98 ops.at(0)->evalAsString(et);
100 else if (name == "issubtaskof")
102 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
105 if ((p = et->getCoreAttributes()->getProject()->getTask
106 (ops.at(0)->evalAsString(et))) == 0)
108 return p->isSubTask((Task*) et->getCoreAttributes());
110 else if (name == "containstask")
112 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
115 if ((st = et->getCoreAttributes()->getProject()->getTask
116 (ops.at(0)->evalAsString(et))) == 0)
118 return ((Task*) et->getCoreAttributes())->isSubTask(st);
120 else if (name == "ismilestone")
122 if (strcmp(et->getCoreAttributes()->getType(), "Task") != 0)
124 return ((Task*) et->getCoreAttributes())->isMilestone();
126 else if (name == "isresource")
128 return strcmp(et->getCoreAttributes()->getType(), "Resource") == 0
129 && et->getCoreAttributes()->getId() ==
130 ops.at(0)->evalAsString(et);
132 else if (name == "isaccount")
134 return strcmp(et->getCoreAttributes()->getType(), "Account") == 0
135 && et->getCoreAttributes()->getId() ==
136 ops.at(0)->evalAsString(et);
138 else if (name == "isplanallocated")
140 if (strcmp(et->getCoreAttributes()->getType(), "Resource") != 0)
141 qFatal("Operation::evalFunction: isplanallocated called for "
143 return ((Resource*) et->getCoreAttributes())->isPlanAllocated
144 (Interval(ops.at(1)->evalAsTime(et), ops.at(2)->evalAsTime(et)),
145 ops.at(0)->evalAsString(et));
147 else if (name == "isactualallocated")
149 if (strcmp(et->getCoreAttributes()->getType(), "Resource") != 0)
150 qFatal("Operation::evalFunction: isactualallocated called for "
152 return ((Resource*) et->getCoreAttributes())->isActualAllocated
153 (Interval(ops.at(1)->evalAsTime(et), ops.at(2)->evalAsTime(et)),
154 ops.at(0)->evalAsString(et));
157 qFatal("Unknown function %s", name.data());
163 Operation::evalFunctionAsString(ExpressionTree* )
165 // There are no functions yet that return a string.
166 return QString::null;
169 ExpressionTree::ExpressionTree(Operation* op) : expression(op)
171 symbolTable.setAutoDelete(TRUE);
172 if (funcArgc.isEmpty())
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));
186 ExpressionTree::resolve(const QString& symbol)
188 return symbolTable[symbol] != 0 ? *(symbolTable[symbol]) : 0;
192 ExpressionTree::isFunction(const QString& name)
194 return funcArgc[name];
198 ExpressionTree::arguments(const QString& name)
200 return *funcArgc[name];