| 1 | /* Program P6B4: dumps the field contents of all active records |
|---|
| 2 | of a given data base |
|---|
| 3 | use b7_() |
|---|
| 4 | */ |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | #include <ctype.h> |
|---|
| 8 | #include <limits.h> |
|---|
| 9 | #include <math.h> |
|---|
| 10 | |
|---|
| 11 | #include "cisis.h" /* CISIS Interface header file */ |
|---|
| 12 | #include "cirun.h" /* CISIS Interface runtime declarations */ |
|---|
| 13 | #include "cib70.h" |
|---|
| 14 | |
|---|
| 15 | #define MAXTEMS 40000 |
|---|
| 16 | #define TAG20 2 |
|---|
| 17 | |
|---|
| 18 | #define MAXRELAT 101 |
|---|
| 19 | |
|---|
| 20 | float QVW[MAXTEMS]; |
|---|
| 21 | |
|---|
| 22 | float TABSIM[MAXRELAT]; |
|---|
| 23 | LONGX TABSIMMFN[MAXRELAT]; |
|---|
| 24 | |
|---|
| 25 | float wrkarea; |
|---|
| 26 | int idx1,idx2,idx3,idx4,point1; |
|---|
| 27 | |
|---|
| 28 | int *DVN; |
|---|
| 29 | unsigned dvnsize; |
|---|
| 30 | float *DVW; |
|---|
| 31 | unsigned dvwsize; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | void main(int argc, char *argv[]) |
|---|
| 36 | { |
|---|
| 37 | RECSTRU *recp; /* mandatory for defines REC,MFR,DIR,FIELDP */ |
|---|
| 38 | LONGX irec; |
|---|
| 39 | |
|---|
| 40 | char *dbnp; |
|---|
| 41 | |
|---|
| 42 | LONGX mfn,mfnq; |
|---|
| 43 | int diridx,dirloop,diridx2,dirloop2; |
|---|
| 44 | |
|---|
| 45 | char *p; |
|---|
| 46 | |
|---|
| 47 | /* bool */ |
|---|
| 48 | /* areas para b7_() */ |
|---|
| 49 | LONGX boolridx; |
|---|
| 50 | LONGX boolhits=0,boolmfn,boolh; |
|---|
| 51 | LONGX booltell=1L; /* 0L = no msg */ |
|---|
| 52 | LONGX booltlir=0L; /* b40 invx */ |
|---|
| 53 | b7_CIB7 *cib7p=NULL; |
|---|
| 54 | char *booldbnp = NULL; /* b4x query dbn */ |
|---|
| 55 | LONGX crec=0L; /* b5_exp usa crec condicionado a booldbnp - ie, nao vai usar */ |
|---|
| 56 | char booltmpnam[CIMPL+1]={'\0'}; /* b72 MFQTHMEM tmp file */ |
|---|
| 57 | char boolqryp[30*(6+1)+1]; /* 30 chaves de 6 chars+operadorOR + null final */ |
|---|
| 58 | char *buffup=NULL; /* area de trabalho = parametro do b7_() */ |
|---|
| 59 | LONGX buffuplen=0; |
|---|
| 60 | /* end areas para b7_() */ |
|---|
| 61 | char *bp; |
|---|
| 62 | void *ciapip = NULL; |
|---|
| 63 | /* end bool */ |
|---|
| 64 | |
|---|
| 65 | char areaval[100+1]; |
|---|
| 66 | int i,g1,g2; |
|---|
| 67 | |
|---|
| 68 | float W, DW; |
|---|
| 69 | float somap,somaq2,somad2,denom,sim; |
|---|
| 70 | |
|---|
| 71 | LONGX parmfrom=1,parmto=99999999L,parmloop=1; |
|---|
| 72 | LONGX parmdfrom=1,parmdto=99999999L; |
|---|
| 73 | int parmtrace=1; |
|---|
| 74 | |
|---|
| 75 | if (argc < 2) |
|---|
| 76 | { |
|---|
| 77 | printf("P6B4 <dbname> <parmfrom> <parmto> <parmdfrom> <parmdto> \n"); |
|---|
| 78 | exit(1); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | if (argc >= 2) dbnp=argv[1]; |
|---|
| 82 | if (argc >= 3) if (sscanf(argv[2],"%"_LD_,&parmfrom) != 1) fatal(argv[2]); |
|---|
| 83 | if (argc >= 4) if (sscanf(argv[3],"%"_LD_,&parmto) != 1) fatal(argv[3]); |
|---|
| 84 | if (argc >= 5) if (sscanf(argv[4],"%"_LD_,&parmdfrom) != 1) fatal(argv[4]); |
|---|
| 85 | if (argc >= 6) if (sscanf(argv[5],"%"_LD_,&parmdto) != 1) fatal(argv[5]); |
|---|
| 86 | |
|---|
| 87 | p=ALLOC((ALLOPARM)(dvnsize=sizeof(int)*MAXTEMS)); |
|---|
| 88 | if (p == NULL) fatal("ALLOC/DVN"); |
|---|
| 89 | DVN=(int *)p; |
|---|
| 90 | p=ALLOC((ALLOPARM)(dvwsize=sizeof(float)*MAXTEMS)); |
|---|
| 91 | if (p == NULL) fatal("ALLOC/DVW"); |
|---|
| 92 | DVW=(float *)p; |
|---|
| 93 | |
|---|
| 94 | /* init b7_() */ |
|---|
| 95 | #if CIAPI |
|---|
| 96 | ciapip = cisisApplication( MAXNREC,MAXNTRM );/* mandatory for CIAPI definitions */ |
|---|
| 97 | #endif /* CIAPI */ |
|---|
| 98 | cib7p=b7_cib7(cib7p,ciapip); |
|---|
| 99 | for (boolridx=nrecs, mfn=1; ; mfn++) { /* get actual maxmfn */ |
|---|
| 100 | RECORD(boolridx,dbnp,mfn); |
|---|
| 101 | if (RECrc == RCEOF) fatal(dbnp); |
|---|
| 102 | if (RECrc == RCNORMAL) break; |
|---|
| 103 | } |
|---|
| 104 | VRECdbxp(boolridx)=NULL; /* nao usa base de log */ |
|---|
| 105 | VMFRmfn(boolridx)=0L; |
|---|
| 106 | |
|---|
| 107 | /* allocate buffup para b7_() */ |
|---|
| 108 | if (!buffup) { |
|---|
| 109 | if (boolqryp) buffuplen= |
|---|
| 110 | strlen(dbnp) + /* field MFQTDBN0=11 (may be longer than CIMPL+1) */ |
|---|
| 111 | sizeof(boolqryp) + /* the expression */ |
|---|
| 112 | 2*sizeof(boolqryp) + /* the terms + pseudo operators _1termX+11X */ |
|---|
| 113 | BUFSIZ+strlen(dbnp) + /* field MFQSWDBN=20 + a diferent db */ |
|---|
| 114 | 24*(strlen(B7DELADD)+3*B7UPLEN) + /* 2 duzias de control fields */ /* DxxAxx|xxx| */ |
|---|
| 115 | sizeof(boolqryp)+4 + /* tmpfile .%03x */ |
|---|
| 116 | BUFSIZ; /* por si las moscas */ |
|---|
| 117 | buffup=loadfile(NULL,'@',"",NULL,buffuplen,'\0'); |
|---|
| 118 | if (buffup == NULL) fatal("ALLOC/buffup"); |
|---|
| 119 | } |
|---|
| 120 | /* end allocate buffup para b7_() */ |
|---|
| 121 | /* end init b7_() */ |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | irec=nrecs; |
|---|
| 126 | for (mfn=parmfrom; mfn<=parmto; mfn+=parmloop) |
|---|
| 127 | { |
|---|
| 128 | RECORD(irec,dbnp,mfn); |
|---|
| 129 | if (RECrc == RCEOF) break; |
|---|
| 130 | if (RECrc != RCNORMAL) continue; |
|---|
| 131 | memset(QVW,0,dvnsize); |
|---|
| 132 | mfnq=mfn; |
|---|
| 133 | /* init boolqryp */ |
|---|
| 134 | bp=boolqryp; *bp='\0'; |
|---|
| 135 | |
|---|
| 136 | for (diridx=0, dirloop=MFRnvf; dirloop--; diridx++) |
|---|
| 137 | { |
|---|
| 138 | if (DIRtag(diridx) != TAG20) continue; |
|---|
| 139 | p=FIELDP(diridx); |
|---|
| 140 | subfield((UBYTE *)p, DIRlen(diridx), 'f', (UBYTE *)areaval); |
|---|
| 141 | W=atof(areaval); |
|---|
| 142 | subfield((UBYTE *)p, DIRlen(diridx), 'd', (UBYTE *)areaval); |
|---|
| 143 | g1=atoi(areaval); |
|---|
| 144 | QVW[g1]=W; |
|---|
| 145 | /* monta boolqryp */ |
|---|
| 146 | if (boolqryp[0]) *bp++ = '+'; /* OR */ |
|---|
| 147 | memcpy(bp,areaval,DIRlen(diridx)); bp+=DIRlen(diridx); *bp='\0'; |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /* recupera boolqryp */ |
|---|
| 152 | if (boolqryp) { /* mxbool/boolh */ |
|---|
| 153 | p=b7_exp(cib7p,boolridx,dbnp,boolqryp,buffup,booldbnp,crec,&errno); |
|---|
| 154 | if (p || errno) printf("Expression syntax error %d: '%s'\n",errno,p); |
|---|
| 155 | else { |
|---|
| 156 | /* set hit file name to "null=don't write" or as provided by "b70x=" */ |
|---|
| 157 | if (!booldbnp || booltmpnam[0]) { |
|---|
| 158 | char *hitfnamp="null"; |
|---|
| 159 | if (booltmpnam[0]) hitfnamp=booltmpnam; |
|---|
| 160 | sprintf(buffup,"D%dA%d|%s|",MFQTHMEM,MFQTHMEM,hitfnamp); |
|---|
| 161 | if (fldupdat(boolridx,buffup) != NULL) fatal("mxtb/boolridx/fldupdat"); |
|---|
| 162 | if (b70trace) prtfields(vrecp[boolridx],VMFRmfn(boolridx)); |
|---|
| 163 | } |
|---|
| 164 | /* b7_run faz "trirecp=vrep[tlirec]" */ |
|---|
| 165 | if (!booltlir) record(booltlir=nrecs,"null",1L); |
|---|
| 166 | |
|---|
| 167 | p=b7_run(cib7p,boolridx,dbnp,buffup,buffuplen,booltell,isisuctab,"",booltlir,crec,&errno); |
|---|
| 168 | if (p || errno) printf("Execution error %d: '%s'\n",errno,p); |
|---|
| 169 | else { |
|---|
| 170 | boolhits=b7_hit(cib7p,boolridx,0L,b7setno); |
|---|
| 171 | if (booltell) printf("Hits=%"_LD_"\n",boolhits); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | if (p || errno) fatal("Expression execution error"); |
|---|
| 175 | } /* end mxbool/boolh */ |
|---|
| 176 | /* end recupera boolqryp */ |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | boolh=1; |
|---|
| 180 | while((boolmfn=b7_hit(cib7p,boolridx,boolh,b7setno))>0L) { |
|---|
| 181 | boolh++; |
|---|
| 182 | if (parmtrace) printf("+++ boolmfn=%"_LD_"\n",boolmfn); |
|---|
| 183 | if (boolmfn <= 0L) break; |
|---|
| 184 | if (boolmfn < parmdfrom) continue; |
|---|
| 185 | if (boolmfn > parmdto) break; |
|---|
| 186 | |
|---|
| 187 | RECORD(irec,dbnp,boolmfn); |
|---|
| 188 | memset(DVW,0,dvwsize); |
|---|
| 189 | for (diridx2=0, dirloop2=MFRnvf; dirloop2--; diridx2++) { |
|---|
| 190 | if (DIRtag(diridx2) != TAG20) continue; |
|---|
| 191 | p=FIELDP(diridx2); |
|---|
| 192 | subfield((UBYTE *)p, DIRlen(diridx2), 'd', (UBYTE *)areaval); |
|---|
| 193 | g2=atoi(areaval); |
|---|
| 194 | subfield((UBYTE *)p, DIRlen(diridx2), 'f', (UBYTE *)areaval); |
|---|
| 195 | DW=atof(areaval); |
|---|
| 196 | // printf("2. %6"_LD_"|%6d|%2.9f \n",mfn2,g2,DW); |
|---|
| 197 | DVW[g2]=DW; |
|---|
| 198 | } |
|---|
| 199 | // Calcula a similaridade |
|---|
| 200 | for (somap=somaq2=somad2=0, i=0; i<MAXTEMS; i++) { |
|---|
| 201 | W=QVW[i]; DW= *(DVW+i); |
|---|
| 202 | somap += W*DW; |
|---|
| 203 | somaq2 += W*W; |
|---|
| 204 | somad2 += DW*DW; |
|---|
| 205 | } |
|---|
| 206 | denom=sqrt(somaq2*somad2); |
|---|
| 207 | if (denom > 0) sim=somap/denom; |
|---|
| 208 | else sim=0; |
|---|
| 209 | |
|---|
| 210 | //Coloca na fila de maiores; |
|---|
| 211 | |
|---|
| 212 | if (sim == 0) continue; |
|---|
| 213 | |
|---|
| 214 | for (idx1=1 ; idx1 < MAXRELAT; ++idx1) { |
|---|
| 215 | if (sim > TABSIM[idx1]) break; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | idx1++; |
|---|
| 219 | for (idx2=MAXRELAT-1; idx2 >= idx1; --idx2) { |
|---|
| 220 | idx4=idx2-1; |
|---|
| 221 | TABSIM[idx2]=TABSIM[idx4]; |
|---|
| 222 | TABSIMMFN[idx2]=TABSIMMFN[idx4]; |
|---|
| 223 | } |
|---|
| 224 | idx3=idx1-1; |
|---|
| 225 | TABSIM[idx3]=sim; |
|---|
| 226 | TABSIMMFN[idx3]=boolmfn; |
|---|
| 227 | //printf(". %"_LD_"|%"_LD_" \n",mfn2,TABSIMMFN[idx3]); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | for (i=1; i<MAXRELAT; i++) |
|---|
| 232 | { |
|---|
| 233 | if (TABSIM[i] == 0) break; |
|---|
| 234 | printf("%"_LD_"|%"_LD_"|%9.6f|",i,mfnq,TABSIM[i]); |
|---|
| 235 | printf("%"_LD_" ",TABSIMMFN[i]); |
|---|
| 236 | printf(" \n"); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | exit(0); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | /* end of main */ |
|---|