1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
* $Id: $
* $Version: $
*
* Copyright (c) Tanel Tammet 2004,2005,2006,2007,2008,2009,2010
*
* Contact: tanel.tammet@gmail.com
*
* This file is part of WhiteDB
*
* WhiteDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WhiteDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WhiteDB. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @file build.h
* Term and clause building functions.
*
*/
/* ====== Includes =============== */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "rincludes.h"
/* ====== Private headers and defs ======== */
#define PRINT_LIMITS
/* ======= Private protos ================ */
/* ====== Functions ============== */
/* ** subst and calc term
uses global flags set before:
g->build_subst // subst var values into vars
g->build_calc // do fun and pred calculations
g->build_dcopy // copy nonimmediate data (vs return ptr)
g->build_buffer; // build everything into tmp local buffer area (vs main area)
*/
gptr wr_build_calc_cl(glb* g, gptr xptr) {
void* db;
int ruleflag;
gptr yptr;
gint xlen;
gint xatomnr;
gint xmeta, xatom, yatom;
int i;
gint tmp;
int ilimit;
//printf("wr_build_calc_cl called\n");
db=g->db;
if (g->build_rename) (g->build_rename_vc)=0;
ruleflag=wg_rec_is_rule_clause(db,xptr);
if (!ruleflag) {
// in some cases, no change, no copy: normally copy
//yptr=xptr;
tmp=wr_build_calc_term(g,encode_datarec_offset(pto(db,xptr)));
if (tmp==WG_ILLEGAL) return NULL; // could be memory err
yptr=rotp(g,tmp);
} else {
xlen=get_record_len(xptr);
// allocate space
if ((g->build_buffer)!=NULL) {
yptr=wr_alloc_from_cvec(g,g->build_buffer,(RECORD_HEADER_GINTS+xlen));
} else {
yptr=wg_create_raw_record(db,xlen);
}
if (yptr==NULL) return NULL;
// copy rec header and clause header
ilimit=RECORD_HEADER_GINTS+(g->unify_firstuseterm);
for(i=0;i<ilimit;i++) {
yptr[i]=xptr[i];
}
//wr_print_varbank(g,g->varbanks);
// loop over clause elems
xatomnr=wg_count_clause_atoms(db,xptr);
for(i=0;i<xatomnr;i++) {
xmeta=wg_get_rule_clause_atom_meta(db,xptr,i);
xatom=wg_get_rule_clause_atom(db,xptr,i);
wr_set_rule_clause_atom_meta(g,yptr,i,xmeta);
yatom=wr_build_calc_term(g,xatom);
if (yatom==WG_ILLEGAL) return NULL; // could be memory err
wr_set_rule_clause_atom(g,yptr,i,yatom);
}
//wr_print_varbank(g,g->varbanks);
}
++(g->stat_built_cl);
return yptr;
}
/* ** subst and calc term
uses global flags set before:
g->build_subst // subst var values into vars
g->build_calc // do fun and pred calculations
g->build_dcopy // copy nonimmediate data (vs return ptr)
g->build_buffer; // build everything into tmp local buffer area (vs main area)
*/
gint wr_build_calc_term(glb* g, gint x) {
void* db;
gptr xptr,yptr;
gint xlen,uselen;
gint tmp; // used by VARVAL_F
gint vnr;
gint newvar;
int i;
gint res;
int ilimit;
int substflag;
//printf("wr_build_calc_term called with x %d type %d\n",x,wg_get_encoded_type(g->db,x));
if (isvar(x) && (g->build_subst || g->build_rename)) x=VARVAL_F(x,(g->varbanks));
if (!isdatarec(x)) {
// now we have a simple value
if (!isvar(x) || !(g->build_rename)) return x;
vnr=decode_var(x);
if (vnr<FIRST_UNREAL_VAR_NR) {
//normal variable, has to be renamed
newvar=(g->build_rename_vc)+FIRST_UNREAL_VAR_NR;
if ((g->build_rename_vc)>=NROF_VARSINBANK) {
++(g->stat_internlimit_discarded_cl);
(g->alloc_err)=3;
#ifdef PRINT_LIMITS
printf("limiterr in wr_build_calc_term for renamed var nrs\n");
#endif
return WG_ILLEGAL;
}
++(g->build_rename_vc);
SETVAR(x,encode_var(newvar),(g->varbanks),(g->varstack),(g->tmp_unify_vc));
return encode_var(((g->build_rename_banknr)*NROF_VARSINBANK)+(newvar-FIRST_UNREAL_VAR_NR));
} else {
return encode_var(((g->build_rename_banknr)*NROF_VARSINBANK)+(vnr-FIRST_UNREAL_VAR_NR));
}
}
// now we have a datarec
if (0) {
} else {
db=g->db;
xptr=decode_record(db,x);
xlen=get_record_len(xptr);
//printf("wr_build_calc_term xptr %d xlen %d\n",(gint)xptr,xlen);
// allocate space
if ((g->build_buffer)!=NULL) {
yptr=wr_alloc_from_cvec(g,g->build_buffer,(RECORD_HEADER_GINTS+xlen));
//yptr=malloc(64);
} else {
yptr=wg_create_raw_record(db,xlen);
}
if (yptr==NULL) return WG_ILLEGAL;
// copy rec header and term header
ilimit=RECORD_HEADER_GINTS+(g->unify_firstuseterm);
for(i=0;i<ilimit;i++) yptr[i]=xptr[i];
//printf("wr_build_calc_term i %d \n",i);
// loop over term elems, i already correct
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<xlen)
uselen=((g->unify_maxuseterms)+(g->unify_firstuseterm)+RECORD_HEADER_GINTS);
else
uselen=xlen+RECORD_HEADER_GINTS;
} else {
uselen=xlen+RECORD_HEADER_GINTS;
}
substflag=(g->build_subst || g->build_rename);
for(;i<uselen;i++) {
//printf("wr_build_calc_term loop i %d xptr[i] %d\n",i,xptr[i]);
if (!substflag && !isdatarec(xptr[i])) yptr[i]=xptr[i];
else {tmp=wr_build_calc_term(g,xptr[i]);
if (tmp==WG_ILLEGAL) return WG_ILLEGAL;
//printf("wr_build_calc_term loop tmp %d \n",(gint)tmp);
yptr[i]=tmp;
}
}
// copy term footer (in addition to rec/term header), i already correct
if (g->unify_maxuseterms) {
ilimit=RECORD_HEADER_GINTS+xlen;
for(;i<ilimit;i++) {
yptr[i]=xptr[i];
}
}
if ((g->use_comp_funs) && wr_computable_termptr(g,yptr)) {
res=wr_compute_from_termptr(g,yptr);
if (res==WG_ILLEGAL) return WG_ILLEGAL;
} else {
res=encode_record(db,yptr);
}
return res;
}
}
int wr_computable_termptr(glb* g, gptr tptr) {
gint fun;
gint nr;
//printf("wr_computable_termptr called with rec\n");
//wg_print_record(g->db,tptr);
//printf("\n");
fun=tptr[RECORD_HEADER_GINTS+(g->unify_funpos)];
//printf("cp1 fun %d type %d :\n",fun,wg_get_encoded_type(g->db,fun));
//wg_debug_print_value(g->db,fun);
//printf("\n");
if (isanonconst(fun)) {
nr=decode_anonconst(fun);
//printf("nr %d\n",nr);
if (nr<(dbmemsegh(g->db)->anonconst.anonconst_nr) && nr>=0) return 1;
}
return 0;
}
gint wr_compute_from_termptr(glb* g, gptr tptr) {
gint fun;
gint res;
//printf("wr_compute_from_termptr called\n");
fun=tptr[RECORD_HEADER_GINTS+(g->unify_funpos)];
// assume fun is anonconst!!
switch (fun) {
case ACONST_PLUS:
res=wr_compute_fun_plus(g,tptr);
break;
case ACONST_EQUAL:
res=wr_compute_fun_equal(g,tptr);
break;
default:
res=encode_record(g->db,tptr);
}
return res;
}
gint wr_compute_fun_plus(glb* g, gptr tptr) {
void* db=g->db;
gint len;
gint a,b;
gint atype, btype;
gint ri;
double ad,bd,rd;
//printf("wr_compute_fun_plus called\n");
len=get_record_len(tptr);
if (len<(g->unify_firstuseterm)+3) return encode_record(db,tptr);
a=tptr[RECORD_HEADER_GINTS+(g->unify_funarg1pos)];
atype=wg_get_encoded_type(db,a);
if (atype!=WG_INTTYPE && atype!=WG_DOUBLETYPE) return encode_record(db,tptr);
b=tptr[RECORD_HEADER_GINTS+(g->unify_funarg2pos)];
btype=wg_get_encoded_type(db,b);
if (btype!=WG_INTTYPE && btype!=WG_DOUBLETYPE) return encode_record(db,tptr);
if (atype==WG_INTTYPE && btype==WG_INTTYPE) {
// integer res case
ri=wg_decode_int(db,a)+wg_decode_int(db,b);
return wg_encode_int(db,ri);
} else {
// double res case
if (atype==WG_INTTYPE) ad=(double)(wg_decode_int(db,a));
else ad=wg_decode_double(db,a);
if (btype==WG_INTTYPE) bd=(double)(wg_decode_int(db,b));
else bd=wg_decode_double(db,b);
rd=ad+bd;
return wg_encode_double(db,rd);
}
}
gint wr_compute_fun_equal(glb* g, gptr tptr) {
void* db=g->db;
int len;
gint a,b;
gint atype, btype;
len=get_record_len(tptr);
if (len<(g->unify_firstuseterm)+3) return encode_record(db,tptr);
a=tptr[RECORD_HEADER_GINTS+(g->unify_funarg1pos)];
atype=wg_get_encoded_type(db,a);
b=tptr[RECORD_HEADER_GINTS+(g->unify_funarg2pos)];
btype=wg_get_encoded_type(db,b);
if (wr_equal_term(g,a,b,1)) return ACONST_TRUE;
atype=wg_get_encoded_type(db,a);
if (atype==WG_VARTYPE) return encode_record(db,tptr);
btype=wg_get_encoded_type(db,b);
if (btype==WG_VARTYPE) return encode_record(db,tptr);
// here we have not equal a and b with non-var types
if (atype==WG_RECORDTYPE || atype==WG_URITYPE || atype==WG_ANONCONSTTYPE ||
btype==WG_RECORDTYPE || btype==WG_URITYPE || btype==WG_ANONCONSTTYPE) {
return encode_record(db,tptr);
} else {
return ACONST_FALSE;
}
}
#ifdef __cplusplus
}
#endif
|