/**** , [ Worker.cc ], Copyright (c) 2010 Universite de Caen Basse Normandie- Jeremie Vautard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *************************************************************************/ #include "Worker.hh" inline vector getTheValues(MySpace* sol,int vmin,int vmax){ vector zevalues; // cout< (sol->nbVars())) cout<<"getTheValues mal appele"<type_of_v[i]) { case VTYPE_INT : zevalues.push_back( (static_cast(sol->v[i]))->val() ); break; case VTYPE_BOOL : zevalues.push_back( (static_cast(sol->v[i]))->val() ); break; default : cout<<"4Unknown variable type"< 0); } void QWorker::wake() { // cout<<"Awaking "< QWorker::workPosition() { // access.acquire(); // vector ret = currentWork.root(); // access.release(); return currentWork.root(); } void QWorker::run() { while(true) { access.acquire(); stopandforget=0; finished=false; todo.clear(); access.release(); // cout<<"QWorker "<getWork(this); // cout<<"QWorker "<returnWork(this,ret,todo,cur.root()); } else { // cout<<"QWorker "<::iterator i(todo.begin());i != todo.end();i++) { (*i).clean(); } todo.clear(); } } } } Strategy QWorker::solve() { return rsolve(currentWork.getScope(),currentWork.getRemaining()); } Strategy QWorker::rsolve(int scope,/*vector assignments,*/Engine* L) { access.acquire(); bool forget = (stopandforget==2); access.release(); if (forget) { delete L; return Strategy::Dummy(); } MySpace* sol = static_cast(L->next()); Strategy ret=Strategy::Dummy(); bool LwasEmpty = true; while ((sol != NULL) ) { LwasEmpty=false; vector assignments = getTheValues(sol,0,sol->nbVars()-1); Strategy result; if (scope == (problem->spaces() - 1) ) { // last scope reached. Verify the goal... MySpace* g = problem->getGoal(); for (int i=0;inbVars();i++) { switch (g->type_of_v[i]) { case VTYPE_INT : rel(*g,*(static_cast(g->v[i])) == assignments[i]); break; case VTYPE_BOOL : rel(*g,*(static_cast(g->v[i])) == assignments[i]); break; default : cout<<"Unknown variable type"< solutions(g); MySpace* goalsol = solutions.next(); if (goalsol == NULL) { delete g; delete L; result = Strategy::SFalse(); } else { int vmin = ( (scope==0)? 0 : (problem->nbVarInScope(scope-1)) ); int vmax = (problem->nbVarInScope(scope))-1; vector zevalues=getTheValues(sol,vmin,vmax); result=Strategy(problem->quantification(scope),vmin,vmax,scope,zevalues); result.attach(Strategy::STrue()); delete g; // delete sol; delete goalsol; } } else { // This is not the last scope... MySpace* espace = problem->getSpace(scope+1); for (int i=0;itype_of_v[i]) { case VTYPE_INT : rel(*espace,*(static_cast(espace->v[i])) == assignments[i]); break; case VTYPE_BOOL : rel(*espace,*(static_cast(espace->v[i])) == assignments[i]); break; default : cout<<"Unknown variable type"<(espace,/*sizeof(MySpace),*/o); delete espace; access.acquire(); forget=(stopandforget == 2); bool stop=(stopandforget==1); access.release(); if (forget) { delete sol; delete solutions; return Strategy::Dummy(); } if (stop) { vector root1; if (scope!=0) {root1= getTheValues(sol,0,problem->nbVarInScope(scope-1)-1);} QWork current(scope,root1,L); vector root2 = getTheValues(sol,0,problem->nbVarInScope(scope)-1); QWork onemore(scope+1,root2,solutions); todo.push_back(current); todo.push_back(onemore); int vmin = ( (scope==0)? 0 : (problem->nbVarInScope(scope-1)) ); int vmax = (problem->nbVarInScope(scope))-1; vector zevalues=getTheValues(sol,vmin,vmax); Strategy toAttach(problem->quantification(scope),vmin,vmax,scope,zevalues); toAttach.attach(Strategy::Stodo()); ret.attach(toAttach); ret.attach(Strategy::Stodo()); return ret; } result=rsolve(scope+1,solutions); } int vmin = ( (scope == 0) ? 0 : (problem->nbVarInScope(scope-1)) ); int vmax = (problem->nbVarInScope(scope))-1; vector zevalues=getTheValues(sol,vmin,vmax); delete sol; access.acquire(); forget=(stopandforget == 2); access.release(); if (forget) { delete L; return Strategy::Dummy(); } if (problem->quantification(scope)) { // current scope is universal if (result.isFalse()) // one branch fails { delete L; return Strategy::SFalse(); } else { Strategy toAttach(true,vmin,vmax,scope,zevalues); toAttach.attach(result); ret.attach(toAttach); } } else { //current scope is existential if (!result.isFalse()) { // result not the truivilally false strategy... if (result.isComplete()) { // ...and has no todo nodes. So... ret = Strategy(problem->quantification(scope),vmin,vmax,scope,zevalues); ret.attach(result); delete L; return ret; // we return it immediately } else { // If result is not false, but still has todo nodes... Strategy toAttach(problem->quantification(scope),vmin,vmax,scope,zevalues); toAttach.attach(result); // the node corresponding to the current scope iis added... ret.attach(toAttach); // and the corresponding strategy is attached to a Dummy node. Next loop in the outer while will need it... } } } sol = static_cast(L->next()); } delete L; if (problem->quantification(scope)) //universal scope return (LwasEmpty ? Strategy::STrue() : ret); else { if (ret.isComplete()) return Strategy::SFalse(); else return ret; } }