/*===========================================================================*/ static int parseSingleTransition(xmlDocPtr doc, xmlNodePtr cur, ghmm_xmlfile* f, int modelNo) { #define CUR_PROC "parseTransition" int retval=-1; int source, target, error; int in_state, out_state; double p; char * s; xmlNodePtr elem; assert((f->modelType & GHMM_kTransitionClasses) == 0); source = getIntAttribute(cur, "source", &error); target = getIntAttribute(cur, "target", &error); elem = cur->children; while (elem!=NULL) { if ((!xmlStrcmp(elem->name, BAD_CAST "probability"))) { s = (char *)xmlNodeGetContent(elem); p = atof(s); m_free(s) break; } elem = elem->next; }
/*===========================================================================*/ static int parseBackground(xmlDocPtr doc, xmlNodePtr cur, ghmm_xmlfile* f, int modelNo) { #define CUR_PROC "parseBackground" int error, order; int bgNr, rev; double *b = NULL; char *s = NULL; assert(f->modelType & GHMM_kDiscreteHMM); bgNr = f->model.d[modelNo]->bp->n++; /* get order */ order = getIntAttribute(cur, "order", &error); if (error) order=0; else if (order && !(f->modelType & GHMM_kHigherOrderEmissions)) { GHMM_LOG(LERROR, "background distribution has order > 0, but model is not higher order"); goto STOP; } f->model.d[modelNo]->bp->order[bgNr] = order; /* get name */ s = (char *)getXMLCharAttribute(cur, "key", &error); f->model.d[modelNo]->bp->name[bgNr] = s; rev = getIntAttribute(cur, "rev", &error); if (error) rev = 0; /* get distribution */ s = (char *)xmlNodeGetContent(cur); ARRAY_MALLOC(b, pow(f->model.d[modelNo]->bp->m, order+1)); if (-1 != parseCSVList(s, pow(f->model.d[modelNo]->bp->m, order+1), b, rev)) f->model.d[modelNo]->bp->b[bgNr] = b; else { GHMM_LOG(LERROR, "Can not parse background CSV list."); goto STOP; } free(s); return 0; STOP: m_free(b); free(s); return -1; #undef CUR_PROC }
void IntegerAttribute::initialize(QXmlStreamReader& xml) { m_value = getIntAttribute(xml, "IntegerAttribute", "value"); QString name = getStringAttribute(xml, "IntegerAttribute", "name"); if (name == "cut_line") { m_name = IntegerAttribute::cut_line; } else if (name == "design_origin_x") { m_name = IntegerAttribute::design_origin_x; } else if (name == "design_origin_y") { m_name = IntegerAttribute::design_origin_y; } else if (name == "num_local_fiducials") { m_name = IntegerAttribute::num_local_fiducials; } else if (name == "pilot_hole") { m_name = IntegerAttribute::pilot_hole; } else if (name == "testpoint_count") { m_name = IntegerAttribute::testpoint_count; } else { throw new InvalidAttributeError("IntegerAttribute", "name"); } }
void Color::initialize(QXmlStreamReader& xml) { m_r = getIntAttribute(xml, "Color", "r"); m_g = getIntAttribute(xml, "Color", "g"); m_b = getIntAttribute(xml, "Color", "b"); if (m_r < 0 || m_r > 255) { throw new InvalidAttributeError("Color", "r"); } if (m_g < 0 || m_g > 255) { throw new InvalidAttributeError("Color", "g"); } if (m_b < 0 || m_b > 255) { throw new InvalidAttributeError("Color", "b"); } }
QString DkFileNameConverter::resolveIdx(const QString& tag) const { QString result = ""; // append zeros int numZeros = getIntAttribute(tag); int startIdx = getIntAttribute(tag, 2); int fIdx = startIdx+mCIdx; if (numZeros > 0) { // if fIdx <= 0, log10 must not be evaluated int cNumZeros = fIdx > 0 ? numZeros - qFloor(std::log10(fIdx)) : numZeros; // zero padding for (int idx = 0; idx < cNumZeros; idx++) { result += "0"; } } result += QString::number(fIdx); return result; }
/*===========================================================================*/ static ghmm_alphabet * parseAlphabet(xmlDocPtr doc, xmlNodePtr cur, ghmm_xmlfile* f) { #define CUR_PROC "parseAlphabet" char * str; int M, code, error; xmlNodePtr symbol; ghmm_alphabet * alfa; ARRAY_CALLOC(alfa, 1); symbol = cur->children; M=0; while (symbol!=NULL) { if ((!xmlStrcmp(symbol->name, BAD_CAST "symbol"))) { code = getIntAttribute(symbol, "code", &error); if (error || code!=M) { str = ighmm_mprintf(NULL, 0, "non consecutive code %d == %d", code, M); GHMM_LOG(LERROR, str); m_free(str); goto STOP; } else M++; } symbol=symbol->next; } alfa->size = M; /*printf("Parsing alphabet with %d symbols\n", alfa->size);*/ ARRAY_MALLOC(alfa->symbols, M); symbol = cur->children; M=0; while (symbol!=NULL) { if ((!xmlStrcmp(symbol->name, BAD_CAST "symbol"))) { alfa->symbols[M++] = (char *)xmlNodeGetContent(symbol); /*printf("%d. symbol: %s\n", M, alfa->symbols[M-1]);*/ } symbol=symbol->next; } return alfa; STOP: m_free(alfa->symbols); m_free(alfa) return NULL; #undef CUR_PROC }
QString DkFileNameConverter::resolveFilename(const QString& tag) const { QString result = mFileName; // remove extension (Qt's QFileInfo.baseName() does a bad job if you have filenames with dots) result = result.replace("." + QFileInfo(mFileName).suffix(), ""); int attr = getIntAttribute(tag); if (attr == 1) result = result.toLower(); else if (attr == 2) result = result.toUpper(); return result; }
/*===========================================================================*/ static int parseState(xmlDocPtr doc, xmlNodePtr cur, ghmm_xmlfile* f, int * inDegree, int * outDegree, int modelNo) { #define CUR_PROC "parseState" int i, error, order=0, state=-1442, fixed=-985, tied=-9354, M, aprox, label; int curX=0, curY=0; double pi, prior; double *emissions = NULL; char *desc = NULL; char *s = NULL, *estr; int rev, stateFixed=1; ghmm_cstate *newcstate; ghmm_c_emission *emission; xmlNodePtr elem, child, multichild; state = getIntAttribute(cur, "id", &error); pi = getDoubleAttribute(cur, "initial", &error); if (error) { estr = ighmm_mprintf(NULL, 0, "can't read required intial probability for" "state %d", state); GHMM_LOG(LERROR, estr); goto STOP; } else desc = xmlGetProp(cur, BAD_CAST "desc"); elem = cur->children; while (elem!=NULL) { /* ======== silent state ============================================== */ if ((!xmlStrcmp(elem->name, BAD_CAST "silent"))) { switch (f->modelType & PTR_TYPE_MASK) { case (GHMM_kDiscreteHMM): f->model.d[modelNo]->silent[state] = 1; break; case (GHMM_kDiscreteHMM+GHMM_kTransitionClasses): f->model.ds[modelNo]->silent[state] = 1; break; case (GHMM_kDiscreteHMM+GHMM_kPairHMM): case (GHMM_kDiscreteHMM+GHMM_kPairHMM+GHMM_kTransitionClasses): f->model.dp[modelNo]->silent[state] = 1; break; default: GHMM_LOG(LERROR, "invalid modelType"); goto STOP; } } /* ======== discrete state (possible higher order) ==================== */ if ((!xmlStrcmp(elem->name, BAD_CAST "discrete"))) { assert((f->modelType & GHMM_kDiscreteHMM) && ((f->modelType & GHMM_kPairHMM) == 0)); /* fixed is a propety of the distribution and optional */ fixed = getIntAttribute(elem, "fixed", &error); if (error) fixed = 0; /* order is optional for discrete */ if (f->modelType & GHMM_kHigherOrderEmissions) { order = getIntAttribute(elem, "order", &error); if (error) order = 0; } rev = getIntAttribute(cur, "rev", &error); if (error) rev = 0; /* parsing emission probabilities */ s = (char *)xmlNodeGetContent(elem); switch (f->modelType & PTR_TYPE_MASK) { case (GHMM_kDiscreteHMM): f->model.d[modelNo]->s[state].desc = desc; f->model.d[modelNo]->s[state].pi = pi; f->model.d[modelNo]->s[state].fix = fixed; if (f->modelType & GHMM_kHigherOrderEmissions) { f->model.d[modelNo]->order[state] = order; if (f->model.d[modelNo]->maxorder < order) { f->model.d[modelNo]->maxorder = order; estr = ighmm_mprintf(NULL, 0, "Updated maxorder to %d\n", f->model.d[modelNo]->maxorder); GHMM_LOG(LDEBUG, estr); m_free(estr); } } ARRAY_MALLOC(emissions, pow(f->model.d[modelNo]->M, order+1)); parseCSVList(s, pow(f->model.d[modelNo]->M, order+1), emissions, rev); free(f->model.d[modelNo]->s[state].b); f->model.d[modelNo]->s[state].b = emissions; break; case (GHMM_kDiscreteHMM+GHMM_kTransitionClasses): f->model.ds[modelNo]->s[state].desc = desc; f->model.ds[modelNo]->s[state].pi = pi; f->model.ds[modelNo]->s[state].fix = fixed; if (f->modelType & GHMM_kHigherOrderEmissions) f->model.ds[modelNo]->order[state] = order; ARRAY_MALLOC(emissions, pow(f->model.ds[modelNo]->M, order+1)); parseCSVList(s, pow(f->model.ds[modelNo]->M, order+1), emissions, rev); f->model.ds[modelNo]->s[state].b = emissions; break; default: GHMM_LOG(LERROR, "invalid modelType"); goto STOP; } m_free(s); } /* ======== continuous state ========================================== */ if ((!xmlStrcmp(elem->name, BAD_CAST "mixture"))) { assert(f->modelType & GHMM_kContinuousHMM); M = 0; child = elem->children; while (child != NULL) { if ((!xmlStrcmp(child->name, BAD_CAST "normal")) || (!xmlStrcmp(child->name, BAD_CAST "normalLeftTail")) || (!xmlStrcmp(child->name, BAD_CAST "normalRightTail")) || (!xmlStrcmp(child->name, BAD_CAST "multinormal")) || (!xmlStrcmp(child->name, BAD_CAST "uniform"))){ M ++; } child = child->next; } ghmm_cstate_alloc(f->model.c[modelNo]->s + state, M, inDegree[state], outDegree[state], f->model.c[modelNo]->cos); newcstate = f->model.c[modelNo]->s + state; newcstate->desc = desc; newcstate->M = M; newcstate->pi = pi; if( f->model.c[modelNo]->M < M) f->model.c[modelNo]->M = M; child = elem->children; i = 0; while (child != NULL) { emission = newcstate->e+i; /* common attributes */ if ((!xmlStrcmp(child->name, BAD_CAST "normal")) || (!xmlStrcmp(child->name, BAD_CAST "normalLeftTail")) || (!xmlStrcmp(child->name, BAD_CAST "normalRightTail")) || (!xmlStrcmp(child->name, BAD_CAST "multinormal")) || (!xmlStrcmp(child->name, BAD_CAST "uniform"))){ fixed = getIntAttribute(child, "fixed", &error); if (error) fixed = 0; stateFixed = fixed && stateFixed; /* allocate emission */ emission->fixed = fixed; prior = getDoubleAttribute(child, "prior", &error); if (error) prior = 1.0; newcstate->c[i] = prior; } /* child is not a density, continue with the next child */ else { child = child->next; continue; } /* density type dependent attributes */ if ((!xmlStrcmp(child->name, BAD_CAST "normal"))) { emission->mean.val = getDoubleAttribute(child, "mean", &error); emission->variance.val = getDoubleAttribute(child, "variance", &error); /* should the normal distribution be approximated? */ aprox = getIntAttribute(child, "approx", &error); if (error) aprox = 0; emission->type = aprox ? normal_approx : normal; emission->dimension = 1; if (f->model.c[modelNo]->dim > 1) { GHMM_LOG(LERROR, "All emissions must have same dimension."); goto STOP; } } if ((!xmlStrcmp(child->name, BAD_CAST "normalLeftTail"))) { emission->mean.val = getDoubleAttribute(child, "mean", &error); emission->variance.val = getDoubleAttribute(child, "variance", &error); emission->min = getDoubleAttribute(child, "max", &error); emission->type = normal_left; emission->dimension = 1; if (f->model.c[modelNo]->dim > 1) { GHMM_LOG(LERROR, "All emissions must have same dimension."); goto STOP; } } if ((!xmlStrcmp(child->name, BAD_CAST "normalRightTail"))) { emission->mean.val = getDoubleAttribute(child, "mean", &error); emission->variance.val = getDoubleAttribute(child, "variance", &error); emission->max = getDoubleAttribute(child, "min", &error); emission->type = normal_right; emission->dimension = 1; if (f->model.c[modelNo]->dim > 1) { GHMM_LOG(LERROR, "All emissions must have same dimension."); goto STOP; } } if ((!xmlStrcmp(child->name, BAD_CAST "uniform"))) { emission->max = getDoubleAttribute(child, "max", &error); emission->min = getDoubleAttribute(child, "min", &error); emission->type = uniform; emission->dimension = 1; if (f->model.c[modelNo]->dim > 1) { GHMM_LOG(LERROR, "All emissions must have same dimension."); goto STOP; } } if ((!xmlStrcmp(child->name, BAD_CAST "multinormal"))) { emission->type = multinormal; emission->dimension = getIntAttribute(child, "dimension", &error); /* check that all emissions in all states have same dimension or set when first emission is read*/ if (f->model.c[modelNo]->dim <= 1) f->model.c[modelNo]->dim = emission->dimension; else if (f->model.c[modelNo]->dim != emission->dimension) { GHMM_LOG(LERROR, "All emissions must have same dimension."); goto STOP; } if (0 != ghmm_c_emission_alloc(emission, emission->dimension)) { GHMM_LOG(LERROR, "Can not allocate multinormal emission."); goto STOP; } multichild = child->children; while (multichild != NULL) { if ((!xmlStrcmp(multichild->name, BAD_CAST "mean"))) { s = (char *)xmlNodeGetContent(multichild); if (-1 == parseCSVList(s, emission->dimension, emission->mean.vec, 0)) { GHMM_LOG(LERROR, "Can not parse mean CSV list."); goto STOP; } } if ((!xmlStrcmp(multichild->name, BAD_CAST "variance"))) { s = (char *)xmlNodeGetContent(multichild); if (-1 == parseCSVList(s, emission->dimension * emission->dimension, emission->variance.mat, 0)) { GHMM_LOG(LERROR, "Can not parse variance CSV list."); goto STOP; } if (0 != ighmm_invert_det(emission->sigmainv, &emission->det, emission->dimension, emission->variance.mat)) { GHMM_LOG(LERROR, "Can not calculate inverse of covariance matrix."); goto STOP; } if (0 != ighmm_cholesky_decomposition(emission->sigmacd, emission->dimension, emission->variance.mat)) { GHMM_LOG(LERROR, "Can not calculate cholesky decomposition of covariance matrix."); goto STOP; } } multichild = multichild->next; } } i++; child = child->next; } newcstate->fix = stateFixed; } /* ======== pair hmm state ============================================ */ if ((!xmlStrcmp(elem->name, BAD_CAST "pair"))) { } /* -------- background name ------------------------------------------ */ if ((!xmlStrcmp(elem->name, BAD_CAST "backgroundKey"))) { assert(f->modelType & GHMM_kBackgroundDistributions); s = (char *)xmlNodeGetContent(elem); for (i=0; i<f->model.d[modelNo]->bp->n; i++) { if (0 == strcmp(s, f->model.d[modelNo]->bp->name[i])) { if (order != f->model.d[modelNo]->bp->order[i]) { estr = ighmm_mprintf(NULL, 0, "order of background %s and state %d" " does not match", f->model.d[modelNo]->bp->name[i], state); GHMM_LOG(LERROR, estr); m_free(estr); goto STOP; } else { f->model.d[modelNo]->background_id[state] = i; break; } } } if (i == f->model.d[modelNo]->bp->n) { estr = ighmm_mprintf(NULL, 0, "can't find background with name %s in" " state %d", s, state); GHMM_LOG(LERROR, estr); m_free(estr); goto STOP; } m_free(s); } /* -------- tied to --------------------------------------------------- */ if ((!xmlStrcmp(elem->name, BAD_CAST "class"))) { assert(f->modelType & GHMM_kLabeledStates); s = (char *)xmlNodeGetContent(elem); label = atoi(s); m_free(s); if ((f->modelType & PTR_TYPE_MASK) == GHMM_kDiscreteHMM) { if (f->model.d[modelNo]->label_alphabet->size > label) f->model.d[modelNo]->label[state] = label; else GHMM_LOG(LWARN, "Invalid label"); } } /* -------- tied to --------------------------------------------------- */ if ((!xmlStrcmp(elem->name, BAD_CAST "tiedTo"))) { assert(f->modelType & GHMM_kTiedEmissions); s = (char *)xmlNodeGetContent(elem); tied = atoi(s); if (state>=tied) { f->model.d[modelNo]->tied_to[state] = tied; if (f->model.d[modelNo]->tied_to[tied] != tied) { estr = ighmm_mprintf(NULL, 0, "state %d not tied to tie group leader", state); GHMM_LOG(LERROR, estr); m_free(estr); goto STOP; } } else { estr = ighmm_mprintf(NULL, 0, "state %d tiedTo (%d) is invalid", state, tied); GHMM_LOG(LERROR, estr); m_free(estr); goto STOP; } m_free(s); } /* -------- position for graphical editing ---------------------------- */ if ((!xmlStrcmp(elem->name, BAD_CAST "position"))) { curX = getIntAttribute(elem, "x", &error); if (error) GHMM_LOG(LWARN, "failed to read x position"); curY = getIntAttribute(elem, "y", &error); if (error) GHMM_LOG(LWARN, "failed to read y position"); switch (f->modelType & PTR_TYPE_MASK) { case GHMM_kDiscreteHMM: f->model.d[modelNo]->s[state].xPosition = curX; f->model.d[modelNo]->s[state].yPosition = curY; break; case GHMM_kDiscreteHMM+GHMM_kTransitionClasses: f->model.ds[modelNo]->s[state].xPosition = curX; f->model.ds[modelNo]->s[state].yPosition = curY; break; case GHMM_kDiscreteHMM+GHMM_kPairHMM: case GHMM_kDiscreteHMM+GHMM_kPairHMM+GHMM_kTransitionClasses: f->model.dp[modelNo]->s[state].xPosition = curX; f->model.dp[modelNo]->s[state].yPosition = curY; break; case GHMM_kContinuousHMM: case GHMM_kContinuousHMM+GHMM_kTransitionClasses: case (GHMM_kContinuousHMM+GHMM_kMultivariate): case (GHMM_kContinuousHMM+GHMM_kMultivariate+GHMM_kTransitionClasses): f->model.c[modelNo]->s[state].xPosition = curX; f->model.c[modelNo]->s[state].yPosition = curY; break; default: GHMM_LOG(LERROR, "invalid modelType"); goto STOP; } } elem = elem->next; } return 0; STOP: m_free(s); m_free(desc); m_free(emissions) return -1; #undef CUR_PROC }
bool cAlarmServer::loadAlarmData(TiXmlElement* element, cAlarmData* data) { UtlString codeStr; UtlString idStr; UtlString compStr(""); UtlString sevStr("minor"); UtlString resStr(""); UtlString groupName("disabled"); bool actEmail = true; bool actSms = true; bool actLog = true; bool actTrap = true; int filtMax = INT_MAX; int filtMin = 0; if (element && data) { idStr = element->Attribute("id"); if (idStr.isNull()) { OsSysLog::add(FAC_ALARM, PRI_ERR,"code=%s: alarm ID is required", codeStr.data()); return false; } TiXmlElement* codeElement = element->FirstChildElement("code"); if ( !codeElement ) { OsSysLog::add(FAC_ALARM, PRI_ERR, "id=%s: alarm code is required", element->Attribute("id")); return false; } textContentShallow(codeStr, codeElement); codeElement = element->FirstChildElement("severity"); if ( codeElement ) { textContentShallow(sevStr, codeElement); } else { OsSysLog::add(FAC_ALARM, PRI_WARNING, "id=%s: no severity; assuming %s", idStr.data(), sevStr.data()); } codeElement = element->FirstChildElement("component"); if ( codeElement ) { textContentShallow(compStr, codeElement); } else { OsSysLog::add(FAC_ALARM, PRI_WARNING,"id=%s: no component; set to null", idStr.data()); } codeElement = element->FirstChildElement("action"); actLog = getBoolAttribute(codeElement, "log", true); if ( codeElement ) { // Get the alarm group name used for both regular emails and SMS emails. groupName = codeElement->Attribute("email"); if (strcmp(groupName.data(), "disabled") == 0) { // All regular/SMS notifications for this alarm type must be disabled. actEmail = false; actSms = false; actTrap = false; } } codeElement = element->FirstChildElement("filter"); filtMax = getIntAttribute(codeElement, "max_reports", INT_MAX); filtMin = getIntAttribute(codeElement, "min_threshold"); } else { return false; } data->setAlarmId(idStr); data->setCode(codeStr); data->setSeverity(sevToSyslogPri(sevStr)); data->setComponent(compStr); data->setShortTitle(idStr); // default fallback title is internal id data->setDescription(idStr); // default fallback description is internal id data->setResolution(resStr); data->actions[cAlarmData::eActionLog] = actLog; data->actions[cAlarmData::eActionEmail] = actEmail; data->actions[cAlarmData::eActionSms] = actSms; data->actions[cAlarmData::eActionTrap] = actTrap; data->group_name = groupName; data->max_report = filtMax; data->min_threshold = filtMin; data->resetCount(); return true; }