00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "projectiontreelabel.h"
00036 #include "varname.h"
00037 #include "fsamap.h"
00038 #include "pathsteptagexpression.h"
00039
00040 ProjectionTreeLabel::ProjectionTreeLabel(ProjectionTreeNode * _n,
00041 unsigned _position, unsigned _id,
00042 ProjectionTreeLabel *
00043 _same_level_successor,
00044 vector <
00045 ProjectionTreeLabel *
00046 >_child_successors,
00047 vector <
00048 ProjectionTreeLabel *
00049 >_self_successors):parent(NULL),
00050 predecessor(NULL),
00051 n(_n),
00052 position(_position),
00053 id(_id),
00054 same_level_successor(_same_level_successor),
00055 child_successors(_child_successors),
00056 self_successors(_self_successors) {
00057 }
00058
00059 ProjectionTreeLabel::~ProjectionTreeLabel() {
00060 }
00061
00062 void ProjectionTreeLabel::
00063 updateParentPredecessorInformation(ProjectionTreeLabel * _parent,
00064 ProjectionTreeLabel * _predecessor) {
00065 parent = _parent;
00066 predecessor = _predecessor;
00067
00068 if (same_level_successor) {
00069 same_level_successor->updateParentPredecessorInformation(_parent, this);
00070 } else {
00071 for (unsigned i = 0; i < child_successors.size(); i++) {
00072 child_successors[i]->updateParentPredecessorInformation(this, this);
00073 }
00074 }
00075 }
00076
00077 void ProjectionTreeLabel::print(OutputStream & dos) {
00078
00079 dos << "[" << id << "] ";
00080
00081
00082 if (n->isRoot()) {
00083 dos << "path=<ROOT>";
00084 } else if (n->getPath()) {
00085 dos << "path=" << *n->getPath();
00086 dos << ", step=[" << position << "]";
00087 } else {
00088 dos << "path=%";
00089 }
00090
00091
00092 dos << "; sl_succ=";
00093 if (same_level_successor) {
00094 dos << same_level_successor->getId();
00095 } else {
00096 dos << "%";
00097 }
00098
00099
00100 dos << ", c_succ=[";
00101 for (unsigned i = 0; i < child_successors.size(); i++) {
00102 if (i)
00103 dos << ",";
00104 dos << child_successors[i]->getId();
00105 }
00106 dos << "]";
00107
00108
00109 dos << ", self_succ=[";
00110 for (unsigned i = 0; i < self_successors.size(); i++) {
00111 if (i)
00112 dos << ",";
00113 dos << self_successors[i]->getId();
00114 }
00115 dos << "]";
00116
00117
00118 dos << " (";
00119 int var_id = getProjectionTreeNode()->getVar();
00120
00121 if (var_id >= 0) {
00122 dos << "$" << VarName::getInstance()->getVarname(var_id);
00123 } else {
00124 dos << "%";
00125 }
00126 dos << ")";
00127
00128 if (parent != NULL) {
00129 dos << " p=[" << parent->getId() << "]";
00130 }
00131
00132 dos << NEWLINE;
00133 }
00134
00135 ProjectionTreeLabel *ProjectionTreeLabel::getLeftmostSLPredecessor() {
00136 if (predecessor == parent) {
00137 return this;
00138 } else {
00139 return predecessor->getLeftmostSLPredecessor();
00140 }
00141 }
00142
00143 unsigned ProjectionTreeLabel::getDependingVar() {
00144 int var = getProjectionTreeNode()->getVar();
00145
00146 return var == -1 ? parent->getDependingVar() : (unsigned) var;
00147 }
00148
00149 PathExpression *ProjectionTreeLabel::getPath() {
00150 return n->getPath();
00151 }
00152
00153 PathStepExpression *ProjectionTreeLabel::getPathStep() {
00154 return n->getPath()? n->getPath()->getPathStepAt(position) : NULL;
00155 }
00156
00157 TAG ProjectionTreeLabel::getTag() {
00158 if (n->isRoot())
00159 return TAGID_ROOT;
00160
00161
00162
00163 if (getPathStep()->isTagNodeTest()) {
00164 PathStepTagExpression *ptt = (PathStepTagExpression *) getPathStep();
00165
00166 return ptt->getNodeTest();
00167 } else {
00168 throw RuntimeException("ProjectionTreeLabel: Internal Cast Error",
00169 eid_runtime_ptlabel);
00170 return TAGID_ROOT;
00171 }
00172 }
00173
00174 AXIS_TYPE ProjectionTreeLabel::getAxisFollowingFSA() {
00175 return getAxisFollowingFSA(FSAMap::getInstance()->
00176 getFSA(getDependingVar()));
00177 }
00178
00179 AXIS_TYPE ProjectionTreeLabel::getAxisFollowingFSA(unsigned fsa) {
00180 return predecessor->getProjectionTreeNode()->getVar() == (int) fsa ?
00181 getPathStep()->getAxisType() : predecessor->getAxisFollowingFSA(fsa);
00182 }
00183
00184 ProjectionTreeLabel *ProjectionTreeLabel::getFSALabel() {
00185 int var_fsa = FSAMap::getInstance()->getFSA(getDependingVar());
00186
00187 return locateVarAncestorLabel(var_fsa);
00188 }
00189
00190 bool ProjectionTreeLabel::isChildOrDescendantLabel() {
00191 if (!n->getPath())
00192 return false;
00193 return getPathStep()->getAxisType() == at_child ||
00194 getPathStep()->getAxisType() == at_descendant;
00195 }
00196
00197 bool ProjectionTreeLabel::isDescendantLabel() {
00198 return n->getPath() && !n->getPath()->isEmptyPath()?
00199 getPathStep()->getAxisType() == at_descendant : false;
00200 }
00201
00202 bool ProjectionTreeLabel::isChildLabel() {
00203 return n->getPath() && !n->getPath()->isEmptyPath()?
00204 getPathStep()->getAxisType() == at_child : false;
00205 }
00206
00207 bool ProjectionTreeLabel::isDosLabel() {
00208 return n->getPath() && !n->getPath()->isEmptyPath()?
00209 n->getPath() && getPathStep()->getAxisType() == at_dos : false;
00210 }
00211
00212 bool ProjectionTreeLabel::isDosNodeLabel() {
00213 return n->getPath() && !n->getPath()->isEmptyPath()?
00214 getPathStep()->isNodeNodeTest() &&
00215 getPathStep()->getAxisType() == at_dos : false;
00216 }
00217
00218 bool ProjectionTreeLabel::isDosOrDescendantLabel() {
00219 return n->getPath() && !n->getPath()->isEmptyPath()?
00220 getPathStep()->getAxisType() == at_dos
00221 || getPathStep()->getAxisType() == at_descendant : false;
00222 }
00223
00224 bool ProjectionTreeLabel::isNodeLabel() {
00225 return n->getPath() && !n->getPath()->isEmptyPath()?
00226 getPathStep()->isNodeNodeTest() : false;
00227 }
00228
00229 bool ProjectionTreeLabel::isStarLabel() {
00230 return n->getPath() && !n->getPath()->isEmptyPath()?
00231 getPathStep()->isStarNodeTest() : false;
00232 }
00233
00234 bool ProjectionTreeLabel::hasSameLevelOrChildSuccessors() {
00235 return same_level_successor || child_successors.size();
00236 }
00237
00238 bool ProjectionTreeLabel::atEndOfPath() {
00239 return n->getPath()? n->getPath()->getPathSize() == position + 1 : true;
00240 }
00241
00242 ProjectionTreeLabel *ProjectionTreeLabel::locateVarAncestorLabel(unsigned var) {
00243 if (getProjectionTreeNode()->getVar() == (int) var) {
00244 return this;
00245 } else {
00246 for (unsigned i = 0; i < self_successors.size(); i++) {
00247 if (self_successors[i]->getProjectionTreeNode()->getVar() ==
00248 (int) var) {
00249 return this;
00250 }
00251 }
00252
00253
00254 return parent->locateVarAncestorLabel(var);
00255 }
00256 }
00257
00258 bool ProjectionTreeLabel::matchesTag(TAG t) {
00259 if (!n->getPath())
00260 return false;
00261
00262 return getPathStep()->isMatchingTag(t);
00263 }
00264
00265 bool ProjectionTreeLabel::matchesText() {
00266 return n->getPath()? getPathStep()->isTextNodeTest()
00267 || getPathStep()->isNodeNodeTest() : false;
00268 }
00269
00270 bool ProjectionTreeLabel::descendantAxisBetw(unsigned basing_id,
00271 bool consider_final) {
00272
00273
00274 if (id == basing_id) {
00275 if (consider_final) {
00276 return getPathStep()->getAxisType() == at_descendant ||
00277 getPathStep()->getAxisType() == at_dos;
00278 } else {
00279 return false;
00280 }
00281 } else if (getPathStep()->getAxisType() == at_descendant ||
00282 getPathStep()->getAxisType() == at_dos) {
00283 return true;
00284 } else {
00285 return getPredecessor()->descendantAxisBetw(basing_id, consider_final);
00286 }
00287 }