// // //Query ::= [Modifer]Claus([Conjuction][Modifer]Clause)* CQuery* CQueryParser::Query() { std::vector<CBooleanClause*> clauses; clauses.reserve(CBooleanQuery::GetMaxClauseCount()); CQuery *q = NULL,*pfirstQuery = NULL; ModiferType mods; ConjuctionType conj; mods = Modfier(); q = Claus(); AddClause(clauses,mods,CONJ_NONE,q); if(mods == MOD_NONE) pfirstQuery = q; bool bexitloop = false; while(true) { TokenType type = m_currentToken.GetTokenType(); switch(type ) { case kID: case kPlus: case kMinus: case kQuote: case kLParen: case kAnd: case kOr: case kNot: case kExclam: { conj = Conjuction(); mods = Modfier(); q = Claus(); if(q== NULL) bexitloop = true; else AddClause(clauses,mods,conj,q); } break; default: { bexitloop = true; } break; } if(bexitloop) break; } size_t clause_size = clauses.size(); if(clause_size == 1 && pfirstQuery ) return pfirstQuery; else if(clause_size > 1) { CBooleanQuery *pQuery = new CBooleanQuery; for(size_t k = 0; k < clause_size;++k) { pQuery->Add(clauses[k]); } return pQuery; } return NULL; }
void AND( ADNFClause dst, ADNFClause src) { ADNFClause newChainEnd; //the CURRENT tail of the new chain of and clauses ADNFClause oldChainI; //old chain iterator // ADNFClause oldCur; ADNFClause srcI; //source iterator ADNFClause t; //temp! oldChainI=dst->next; //to read the original and clauses from the original linked list newChainEnd=dst; //the head newChainEnd->next=0;//empty list for now while (oldChainI) { for (srcI= src->next; srcI; srcI= srcI->next) //traverse the other operand to and all the and clauses! { t=AddClause( newChainEnd, srcI->set | oldChainI->set); //if it's not added, t=0! //Orring two sets means their union, therefore ANDing the and clauses newChainEnd= (t)? (Last(t)): (newChainEnd); //if the new clause is not added, it has not deleted anything either } t= oldChainI->next; free(oldChainI); oldChainI= t; } }
void AddDontCare(ADNFClause dst, ADNFClause src) { int dontCaresSize= DNFSize(src); ASet * dontCares= malloc(dontCaresSize); AAndClause *curDontCare; dontCaresSize=0; for (src=src->next; src; src= src->next) { curDontCare= AddClause(dst, src->set); //add the and clause and keep a record of it // if (curDontCare && curDontCare->set== src->set) //if it is really added and not yet proven useful //if the set is different than what was originally added, means it has simplified. // { dontCares[dontCaresSize]= src->set; dontCaresSize++; // } } QuickSort(dontCares, dontCaresSize); //Delete the ones who didn't make a difference for (dst= dst->next; dst; dst=dst->next) if (BinSearch(dst->set, dontCares, dontCaresSize)) dst = RemoveClause(dst); //go back to prev }
bool CSentence::BuildInitialClauses() { CClauseCollection::Clear(); IsValid(); m_bFirstInPairFound = false; int iFirstWord, iLastWord; iFirstWord = 0; int iStartSearch; int iPunctSignsCount = 0; for(int WordNo = 0 ; WordNo < m_Words.size(); WordNo = iStartSearch) { int iNextPunctCount = 0; if ( ((iLastWord = IsClauseBorder(WordNo, iStartSearch,iNextPunctCount, iFirstWord)) != -1) && (iLastWord >= iFirstWord) ) { if( m_pSyntaxOptions->m_KillHomonymsMode != DontKillHomonyms ) SolveAmbiguityUsingRuleForTwoPredicates(iFirstWord,iLastWord); //creating clause CClause clause(this, iFirstWord,iLastWord); //assigning clause type InitClauseType(clause); int debug = clause.m_vectorTypes.size(); clause.m_iPunctSignsCount = iPunctSignsCount; iPunctSignsCount = iNextPunctCount; InitConjunctions(&clause); AddClause(clause); iFirstWord = iLastWord + 1; } } return true; }
void NOT( ADNFClause dst) { ADNFClause oldChainHead; ADNFClause newORClause; //each previously AND clause becomes an OR clause int i; oldChainHead->next= dst->next; oldChainHead->prev= 0; dst->next=0; InitAndClauseLinkedList(&newORClause); for (oldChainHead= oldChainHead->next; oldChainHead; oldChainHead= oldChainHead->next) { for (i=0; i<ASET_SIZE; i++) if (SET_MEMBER(oldChainHead->set, i)) AddClause(newORClause, SET_ADDE(0, SET_INDEX_NEGATE(i))); AND(dst, newORClause); ClearDNFClause(newORClause); } }
int main() { int i; ADNFClause eq, lt, t1, t2; AAndClause *clauseI; ASet andClause; InitAndClauseLinkedList(<); InitAndClauseLinkedList(&eq); InitAndClauseLinkedList(&t1); InitAndClauseLinkedList(&t2); //initialize eq: andClause=0; //0 is ALWAYS TRUE AddClause(eq, andClause); andClause=-1; //-1 is ALWAYS FALSE AddClause(lt, andClause); for (i=0; i<3; i++) { ClearDNFClause(t1); SET_CLEAR(andClause); SET_ADD(andClause,i); //A_i SET_ADD(andClause,i+4);//B_i AddClause(t1, andClause);//A_i . B_i SET_CLEAR(andClause); SET_ADD(andClause, SET_INDEX_NEGATE(i ));//not(A_i) SET_ADD(andClause, SET_INDEX_NEGATE(i+4));//not(B_i) AddClause(t1, andClause);//not(A_i) . not(B_i) AND(eq, t1); // eq_i = (a_i.b_i + ~a_i.~b_i).eq_{i-1} AND(lt, t1); SET_CLEAR(andClause); SET_ADD(andClause, SET_INDEX_NEGATE(i)); //~a_i SET_ADD(andClause, i+4); //b_i AddClause(lt, andClause); //lt_i = (a_i.b_i + ~a_i.~b_i).lt_{i-1} + ~a_i.b_i printf("%dbLt:\n", i+1); PrintDNFClause(lt); // printf("%dbEq:\n", i+1); // PrintDNFClause(eq); } return 0; //Adding BCD DON'T CARE!! ClearDNFClause(t1); SET_CLEAR(andClause); SET_ADD(andClause, 3); //a_3 SET_ADD(andClause, SET_INDEX_NEGATE(2)); //~a_2 SET_ADD(andClause, 1); //a_1 AddClause(t1, andClause); //a= 1010 or 1011 SET_CLEAR(andClause); SET_ADD(andClause, 3); //a_3 SET_ADD(andClause, 2); //a_2 AddClause(t1, andClause); //a>= 1100 SET_CLEAR(andClause); SET_ADD(andClause, 7); //b_3 SET_ADD(andClause, SET_INDEX_NEGATE(6)); //~b_2 SET_ADD(andClause, 5); //b_1 AddClause(t1, andClause); //b= 1010 or 1011 SET_CLEAR(andClause); SET_ADD(andClause, 7); //b_3 SET_ADD(andClause, 6); //b_2 AddClause(t1, andClause); //b>= 1100 printf("Don't care= "); PrintDNFClause(t1); AddDontCare(eq, t1); printf("BCDEq:\n"); PrintDNFClause(eq); AddDontCare(lt, t1); printf("BDCLt:\n"); PrintDNFClause(lt); return 0; }
int TkGetFileFilters( Tcl_Interp *interp, /* Interpreter to use for error reporting. */ FileFilterList *flistPtr, /* Stores the list of file filters. */ Tcl_Obj *types, /* Value of the -filetypes option. */ int isWindows) /* True if we are running on Windows. */ { int listObjc; Tcl_Obj ** listObjv = NULL; int i; if (types == NULL) { return TCL_OK; } if (Tcl_ListObjGetElements(interp, types, &listObjc, &listObjv) != TCL_OK) { return TCL_ERROR; } if (listObjc == 0) { return TCL_OK; } /* * Free the filter information that have been allocated the previous time; * the -filefilters option may have been used more than once in the * command line. */ TkFreeFileFilters(flistPtr); for (i = 0; i<listObjc; i++) { /* * Each file type should have two or three elements: the first one is * the name of the type and the second is the filter of the type. The * third is the Mac OSType ID, but we don't care about them here. */ int count; FileFilter *filterPtr; Tcl_Obj **typeInfo; if (Tcl_ListObjGetElements(interp, listObjv[i], &count, &typeInfo) != TCL_OK) { return TCL_ERROR; } if (count != 2 && count != 3) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( "bad file type \"%s\", should be " "\"typeName {extension ?extensions ...?} " "?{macType ?macTypes ...?}?\"", Tcl_GetString(listObjv[i]))); Tcl_SetErrorCode(interp, "TK", "VALUE", "FILE_TYPE", NULL); return TCL_ERROR; } filterPtr = GetFilter(flistPtr, Tcl_GetString(typeInfo[0])); if (AddClause(interp, filterPtr, typeInfo[1], (count==2 ? NULL : typeInfo[2]), isWindows) != TCL_OK) { return TCL_ERROR; } } return TCL_OK; }
void OR ( ADNFClause dst, ADNFClause src) { for (src=src->next; src; src=src->next) AddClause(dst, src->set); }
int AddClause(ADNFClause dnf, ASet clause) //returns address if really added { int i; AAndClause *cur= dnf; ASet allAtomsIntersection; if (SET_ISFALSE(clause)) //Not adding ( a AND NOT(a) ) return 0; while (cur->next) { cur=cur->next; switch (Contains( &(cur->set), &clause) )// The one in the chain is preferred if they are equal { case 1: //The one in the chain contains the one to be added ( Is more specific) // printf("head %d head->prev->next %d\n", head, head->prev->next); cur= RemoveClause(cur); break; case -1: //The one to be added contains (is more specific than) the one already in the chain #ifdef LOG_LINKEDLIST printf("Clause %d>%d already there in wire %d at stack entry %d\n", clause, cur->set, w, cur->inQueue); #endif break; case 0: //Nothing to see here allAtomsIntersection= SET_INTERSECTION( SET_ALL_ATOMS(cur->set), SET_ALL_ATOMS(clause) ); for (i=0; i<ASET_SIZE_HALF; i++) { if (SET_MEMBER(allAtomsIntersection, i) ) //looking for a and ~a { if ( LOGIC_NORMALIZE(SET_MEMBER(cur->set, i)) != LOGIC_NORMALIZE(SET_MEMBER(clause, i)) ) //one of them has a and the other ~a { ASet f1, f2; if ( SET_MEMBER(clause, i)) { f1=clause; f2=cur->set; } else { f2= clause; f1= cur->set; } // f1 has a and f2 has ~a SET_REMOVE(f2, SET_INDEX_NEGATE(i)); SET_REMOVE(f1, i); //Now neither has either!! if (f1==f2) { RemoveClause(cur); clause= f1; cur= dnf; break; //break out of the for loop. } else { int fa= //1 if it is of form fa + ff'~a, -1 if f~a + ff'a, 0 if neither SET_CONTAINS(f2,f1); if (fa==1) //fa+ ff'~a = fa + ff'a. f1=f, f2=ff' { //if fa is in the chain, we need not touch it! Just simplify ff'~a to ff' if ( SET_MEMBER(cur->set, i)) clause= f2; else // if ff'~a is in the chain, we have to delete it and insert ff' in again. Then fa will be added back. (Is it necessary?) { RemoveClause(cur); AddClause(dnf, f2); } cur= dnf;// Restart the outer while loop break; //from the for loop } else if (fa==-1)//ff'a + f~a= ff' + f~a. f1=ff', f2=f { //if f~a is in the chain, we need not touch it. Just simplify ff'a to ff' if (SET_MEMBER(cur->set, SET_INDEX_NEGATE(i)) ) clause= f1; else // if ff'a is in the chain, we delete it and insert ff' in again. Then f~a will be added again. { RemoveClause(cur); AddClause(dnf, f1); } cur= dnf; break; } }//end if }//end if }//end if }//end for }//end switch } return AddClauseNoQuestionsAsked(cur, clause); }