📄 rules.c
字号:
/*--------------------------------------------------------------------*/void rs_ruleexg (RULESET *set, int ruleid1, int ruleid2){ /* --- exchange two rules */ RULE **p1, **p2, *rule; /* exchange buffer */ assert(set && (ruleid1 >= 0) && (ruleid1 < set->rulecnt) && (ruleid2 >= 0) && (ruleid2 < set->rulecnt)); p1 = set->rules +ruleid1; rule = *p1; p2 = set->rules +ruleid2; /* get pointers to attributes, */ *p1 = *p2; (*p1)->id = ruleid1; /* exchange them, and */ *p2 = rule; rule->id = ruleid2; /* set new identifiers */} /* rs_ruleexg() *//*--------------------------------------------------------------------*/void rs_rulemove (RULESET *set, int off, int cnt, int pos){ /* --- move some rules */ int n; /* temporary buffer */ RULE **p; /* to traverse rules */ assert(set); /* check for a valid attribute set */ n = set->rulecnt; /* check and adapt insert position */ if (pos > n) pos = n; /* and number of attributes */ if (cnt > n -off) cnt = n -off; assert((cnt >= 0) && (off >= 0) && (pos >= 0) && ((pos <= off) || (pos >= off +cnt))); v_move(set->rules, off, cnt, pos, (int)sizeof(RULE*)); if (pos <= off) { /* move rules in vector */ cnt += off; off = pos; pos = cnt; } p = set->rules +off; /* set new rule identifiers */ while (off < pos) (*p++)->id = off++;} /* rs_rulemove() *//*--------------------------------------------------------------------*/#if 0int rs_rulecut (RULESET *dst, RULESET *src, int mode, ...){ /* --- cut/copy rules */ return 0; /* not implemented yet */} /* rs_rulecut() */#endif/*--------------------------------------------------------------------*/void rs_sort (RULESET *set, RULE_CMPFN cmpfn, void *data){ /* --- sort a ruleset */ int i; /* loop variable */ RULE **p; /* to traverse rules */ assert(set && cmpfn); /* check arguments */ v_sort(set->rules, set->rulecnt, (VCMPFN*)cmpfn, data); p = set->rules +set->rulecnt; /* sort rules and set new identifiers */ for (i = set->rulecnt; --i >= 0; ) (*--p)->id = i;} /* rs_sort() *//*--------------------------------------------------------------------*/int rs_exec (RULESET *set){ /* --- find first applicable rule */ int i; /* loop variable */ assert(set); /* check the function argument */ for (i = 0; i < set->rulecnt; i++) if (r_check(set->rules[i]) < 0) return i; return -1; /* traverse and check the rules */} /* rs_exec() *//*--------------------------------------------------------------------*/#ifdef RS_DESCstatic void _condout (DESC *desc, COND *cond, int op){ /* --- print a condition */ ATT *att; /* attribute of the condition */ int i, bit; /* loop variable, bit index */ int *val; /* to traverse attribute values */ int len; /* length of attribute/value name */ char buf[8 *AS_MAXLEN +64]; /* output buffer */ char *s = buf; /* to traverse the output buffer */ const char *t; /* to traverse the operator sign */ if (op > 0) { *s++ = ' '; *s++ = '&'; *s++ = ' '; } else if (op == 0) { *s++ = ' '; *s++ = '<'; *s++ = '-'; *s++ = ' '; } /* store logical operator (if any) */ att = as_att(desc->attset, cond->att); s += sc_format(s, att_name(att), 0); *s++ = ' '; /* get and format attribute name */ for (t = _opsigns[cond->op]; *t; ) *s++ = *t++; /* store the condition operator */ *s++ = ' '; /* and a separating blank */ if (att_type(att) != AT_SYM) /* if the attribute is numeric, */ s += sprintf(s, "%g", cond->val.f); /* store its value */ else if (cond->op >= R_IN) { /* if subset test on symbolic att., */ *s++ = '{'; *s = '\0'; } /* start an attribute value set */ else { /* if simple symbolic comparison */ s += sc_format(s, att_valname(att, cond->val.i), 0); } /* get and format attribute value */ if ((desc->pos > 4) /* if the line would get too long, */ && (desc->pos +(s -buf) >= desc->max)) { /* start a new line */ fputs((op > 0) ? "\n " : "\n ", desc->file); desc->pos = 1; } fputs(buf, desc->file); /* print output buffer contents */ desc->pos += (int)(s -buf); /* compute new position */ if (cond->op < R_IN) return; /* if this is no subset test, abort */ s = NULL; buf[0] = ' '; /* use string as a first value flag */ if (cond->op == R_IN) { /* if vector representation */ val = cond->val.p; /* traverse value vector */ for (i = *val++; --i >= 0; val++) { if (s) { /* if this is not the first value, */ fputc(',', desc->file); desc->pos++;} /* print a separator */ len = sc_format(s = buf+1, att_valname(att, *val), 0); if ((desc->pos > 4) /* if the line would get too long, */ && (desc->pos +len +3 > desc->max)) { /* start a new line */ fputs("\n ", desc->file); desc->pos = 4; } fputs(buf, desc->file); /* print attribute name */ desc->pos += len +1; /* and compute new position */ } } else { /* if bitflag representation */ for (bit = 1, i = 0; bit; i++, bit <<= 1) { if (!(cond->val.i & bit)) /* traverse bit flags */ continue; /* and skip cleared bits */ if (s) { /* if this is not the first value, */ fputc(',', desc->file); desc->pos++;} /* print a separator */ len = sc_format(s = buf+1, att_valname(att, i), 0); if ((desc->pos > 4) /* if the line would get too long, */ && (desc->pos +len +3 > desc->max)) { /* start a new line */ fputs("\n ", desc->file); desc->pos = 4; } fputs(buf, desc->file); /* print attribute name */ desc->pos += len +1; /* and compute new position */ } } fputs(" }", desc->file); /* terminate value set */ desc->pos += 2; /* and compute new position */} /* _condout() *//*--------------------------------------------------------------------*/int rs_desc (RULESET *set, FILE *file, int mode, int maxlen){ /* --- describe a rule set */ int i, k; /* loop variables */ DESC desc; /* description info */ RULE **rule; /* to traverse the rule vector */ COND *cond; /* to traverse a condition vector */ int op; /* logical operator */ char buf[64]; /* output buffer */ char *s; /* to traverse the output buffer */ assert(set && file); /* check arguments */ /* --- print header (as a comment) --- */ if (mode & RS_TITLE) { /* if the title flag is set */ i = k = (maxlen > 0) ? maxlen -2 : 70; fputs("/*", file); while (--i >= 0) fputc('-', file); fprintf(file, "\n rules\n"); while (--k >= 0) fputc('-', file); fputs("*/\n", file); } /* print title header */ desc.file = file; /* note the output file */ desc.attset = set->attset; /* and the attribute set */ desc.max = (maxlen <= 0) ? INT_MAX : maxlen; /* --- print a list of rules --- */ rule = set->rules; /* traverse the rules */ for (i = set->rulecnt; --i >= 0; rule++) { if ((*rule)->head.att < 0) {/* if there is no rule head, */ fputc('#', file); desc.pos = 1; } /* print a special mark */ else { /* if there is a rule head */ _condout(&desc, &(*rule)->head,-1); if (mode & RS_CONDLN) { fputs("\n ", file); desc.pos = 1; } } /* print head condition */ op = 0; /* set implication first */ cond = (*rule)->conds; /* traverse conditions */ for (k = (*rule)->condcnt; --k > 0; cond++) { _condout(&desc, cond, op); op = 1; /* print a condition, switch to and */ if (mode & RS_CONDLN) { fputs("\n ", file); desc.pos = 2; } } /* in line mode start a new line */ if (k >= 0) /* print last condition (if any, */ _condout(&desc, cond, op);/* there could be no cond. at all) */ if (mode & (RS_SUPP|RS_CONF)) { s = buf; /* if to print support or confidence */ *s++ = ' '; *s++ = '['; /* start additional information */ if (mode & RS_SUPP) /* if to print the support */ s += sprintf(s, "%g", (*rule)->supp); if (mode & RS_CONF) { /* if to print the confidence */ if (mode & RS_SUPP) *s++ = '/'; k = (*rule)->head.att; /* check the head attribute */ if ((k >= 0) && (att_type(as_att(set->attset, k)) != AT_SYM)) s += sprintf(s, "~%g", (*rule)->conf); else /* numeric: print rmse as confidence */ s += sprintf(s, "%.1f%%", (*rule)->conf *100); } /* symbolic: print conf. in percent */ *s++ = ']'; *s = '\0'; /* terminate additional information */ k = (int)(s -buf); /* and compute its length */ if (desc.pos+k > desc.max)/* if the line would get too long, */ fputs("\n ", file); /* start a new line and indent */ fputs(buf, file); /* print support and confidence */ } fputs(";\n", file); /* terminate the output line */ desc.pos = 0; /* and reset the position */ } /* --- print additional information (as a comment) --- */ if (mode & RS_INFO) { /* if the add. info. flag is set */ i = k = (maxlen > 0) ? maxlen -2 : 70; fputs("\n/*", file); while (--i >= 0) fputc('-', file); i = as_attcnt(set->attset); fprintf(file, "\n number of attributes: %d", i); fprintf(file, "\n number of rules : %d\n", set->rulecnt); while (--k >= 0) fputc('-', file); fputs("*/\n", file); } /* print add. rule set information */ return ferror(file) ? -1 : 0; /* check for a write error */} /* rs_desc() */#endif /* #ifdef RS_DESC *//*--------------------------------------------------------------------*/#ifdef RS_PARSERULESET* rs_parse (ATTSET *attset, SCAN *scan){ /* --- parse a rule set */ RULESET *set; /* created rule set */ RULE *rule; /* to read/traverse the rules */ int err = 0; /* error flag */ assert(attset && scan); /* check the function arguments */ set = rs_create(attset, r_delete); if (!set) return NULL; /* create a rule set */ while (sc_token(scan) == T_ID) { /* rule read loop */ rule = r_parse(attset, scan); /* parse a rule and */ if (rule) rs_ruleadd(set, rule); /* add it to the rule set */ else { err = 1; /* try to recover after an error */ if (sc_recover(scan, ';', 0, 0, 0) == T_EOF) break; } } if (err) { rs_delete(set, 0); return NULL; } return set; /* return the created rule set */} /* rs_parse() */#endif /* #ifdef RS_PARSE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -