dlvhex
2.5.0
|
00001 /* dlvhex -- Answer-Set Programming with external interfaces. 00002 * Copyright (C) 2005, 2006, 2007 Roman Schindlauer 00003 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Thomas Krennwallner 00004 * Copyright (C) 2009, 2010 Peter Schüller 00005 * 00006 * This file is part of dlvhex. 00007 * 00008 * dlvhex is free software; you can redistribute it and/or modify it 00009 * under the terms of the GNU Lesser General Public License as 00010 * published by the Free Software Foundation; either version 2.1 of 00011 * the License, or (at your option) any later version. 00012 * 00013 * dlvhex is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with dlvhex; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00021 * 02110-1301 USA. 00022 */ 00023 00031 #ifdef HAVE_CONFIG_H 00032 #include "config.h" 00033 #endif // HAVE_CONFIG_H 00034 00035 #include "dummytypes.h" 00036 00037 using namespace dlvhex; 00038 00039 TestModelGeneratorFactory::ModelGenerator::ModelGenerator( 00040 InterpretationConstPtr input, 00041 TestModelGeneratorFactory& factory): 00042 ModelGeneratorBase<TestInterpretation>(input), 00043 factory(factory), 00044 models(), 00045 mit(models.begin()) 00046 { 00047 LOG_VSCOPE(INFO,"ModelGenerator()",this,true); 00048 const std::string& rules = factory.ctx.rules; 00049 LOG(INFO,"rules '" << rules << "'"); 00050 if( input ) 00051 LOG(INFO,"input '" << *input << "'"); 00052 00053 // hardcode models of given programs with given inputs 00054 if( rules == "plan(a) v plan(b)." ) 00055 { 00056 assert(!input); 00057 TestAtomSet ma; 00058 ma.insert("plan(a)"); 00059 TestAtomSet mb; 00060 mb.insert("plan(b)"); 00061 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00062 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mb))); 00063 mit = models.begin(); 00064 } 00065 else if( rules == "need(p,C) :- &cost[plan](C). :- need(_,money)." ) 00066 { 00067 assert(input); 00068 const TestAtomSet& inp = input->getAtoms(); 00069 assert(inp.size() == 1); 00070 if( inp.count("plan(a)") == 1 ) 00071 { 00072 // no models (constraint violated) 00073 } 00074 else if( inp.count("plan(b)") == 1 ) 00075 { 00076 TestAtomSet a; 00077 a.insert("need(p,time)"); 00078 models.push_back(TestInterpretation::Ptr(new TestInterpretation(a))); 00079 mit = models.begin(); 00080 } 00081 else 00082 { 00083 assert(false); 00084 } 00085 } 00086 else if( rules == "use(X) v use(Y) :- plan(P), choose(P,X,Y). choose(a,c,d). choose(b,e,f)." ) 00087 { 00088 assert(input); 00089 const TestAtomSet& inp = input->getAtoms(); 00090 assert(inp.size() == 1); 00091 if( inp.count("plan(a)") == 1 ) 00092 { 00093 TestAtomSet ma; 00094 ma.insert("use(c)"); 00095 TestAtomSet mb; 00096 mb.insert("use(d)"); 00097 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00098 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mb))); 00099 mit = models.begin(); 00100 } 00101 else if( inp.count("plan(b)") == 1 ) 00102 { 00103 TestAtomSet ma; 00104 ma.insert("use(e)"); 00105 TestAtomSet mb; 00106 mb.insert("use(f)"); 00107 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00108 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mb))); 00109 mit = models.begin(); 00110 } 00111 else 00112 { 00113 assert(false); 00114 } 00115 } 00116 else if( rules == "need(u,C) :- &cost[use](C). :- need(_,money)." ) 00117 { 00118 assert(input); 00119 const TestAtomSet& inp = input->getAtoms(); 00120 assert(inp.size() == 2); 00121 if( inp.count("need(p,time)") == 1 && inp.count("use(e)") ) 00122 { 00123 TestAtomSet ma; 00124 ma.insert("need(u,time)"); 00125 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00126 mit = models.begin(); 00127 } 00128 else if( inp.count("need(p,time)") == 1 && inp.count("use(f)") ) 00129 { 00130 // no models (constraint violated) 00131 } 00132 else 00133 { 00134 assert(false); 00135 } 00136 } 00137 else if( rules == 00138 "plan(a) v plan(b)." 00139 "use(X) v use(Y) :- plan(P), choose(P,X,Y)." 00140 "choose(a,c,d). choose(b,e,f)." ) 00141 { 00142 assert(!input); 00143 TestAtomSet mac; 00144 mac.insert("plan(a)"); 00145 mac.insert("use(c)"); 00146 TestAtomSet mad; 00147 mad.insert("plan(a)"); 00148 mad.insert("use(d)"); 00149 TestAtomSet mbe; 00150 mbe.insert("plan(b)"); 00151 mbe.insert("use(e)"); 00152 TestAtomSet mbf; 00153 mbf.insert("plan(b)"); 00154 mbf.insert("use(f)"); 00155 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mac))); 00156 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mad))); 00157 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mbe))); 00158 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mbf))); 00159 mit = models.begin(); 00160 } 00161 else if( rules == 00162 "need(p,C) :- &cost[plan](C)." 00163 "need(u,C) :- &cost[use](C)." ) 00164 { 00165 assert(input); 00166 const TestAtomSet& inp = input->getAtoms(); 00167 assert(inp.size() == 2); 00168 TestAtomSet m; 00169 if( inp.count("plan(a)") == 1 ) 00170 m.insert("need(p,money)"); 00171 else 00172 m.insert("need(p,time)"); 00173 if( inp.count("use(f)") == 1 ) 00174 m.insert("need(u,money)"); 00175 else 00176 m.insert("need(u,time)"); 00177 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00178 mit = models.begin(); 00179 } 00180 else if( rules == ":- need(X,money)." ) 00181 { 00182 assert(input); 00183 const TestAtomSet& inp = input->getAtoms(); 00184 assert(inp.size() == 2); 00185 if( inp.count("need(p,money)") == 1 || inp.count("need(u,money)") == 1 ) 00186 { 00187 // no models (constraint violated) 00188 } 00189 else 00190 { 00191 // empty model (consistent) 00192 TestAtomSet m; 00193 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00194 mit = models.begin(); 00195 } 00196 } 00197 else if( rules.size() == 6 && rules.substr(1,3) == " v " && rules[5] == '.' ) 00198 { 00199 // generic "a v b." for one-character a's or b's 00200 assert(!input); 00201 TestAtomSet ma; 00202 ma.insert(rules.substr(0,1)); 00203 TestAtomSet mb; 00204 mb.insert(rules.substr(4,1)); 00205 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00206 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mb))); 00207 mit = models.begin(); 00208 } 00209 else if( rules == "f :- b." ) 00210 { 00211 assert(input); 00212 const TestAtomSet& inp = input->getAtoms(); 00213 if( inp.count("b") == 1 ) 00214 { 00215 TestAtomSet m; 00216 m.insert("f"); 00217 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00218 mit = models.begin(); 00219 } 00220 else 00221 { 00222 TestAtomSet m; 00223 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00224 mit = models.begin(); 00225 } 00226 } 00227 else if( rules == "j :- d. :- f, c." ) 00228 { 00229 assert(input); 00230 const TestAtomSet& inp = input->getAtoms(); 00231 if( inp.count("f") == 1 && inp.count("c") == 1 ) 00232 { 00233 // no model 00234 } 00235 else 00236 { 00237 if( inp.count("d") == 1 ) 00238 { 00239 TestAtomSet m; 00240 m.insert("j"); 00241 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00242 mit = models.begin(); 00243 } 00244 else 00245 { 00246 TestAtomSet m; 00247 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00248 mit = models.begin(); 00249 } 00250 } 00251 } 00252 else if( rules == "g v h :- f." ) 00253 { 00254 assert(input); 00255 const TestAtomSet& inp = input->getAtoms(); 00256 if( inp.count("f") == 1 ) 00257 { 00258 TestAtomSet ma; 00259 ma.insert("g"); 00260 models.push_back(TestInterpretation::Ptr(new TestInterpretation(ma))); 00261 TestAtomSet mb; 00262 mb.insert("h"); 00263 models.push_back(TestInterpretation::Ptr(new TestInterpretation(mb))); 00264 mit = models.begin(); 00265 } 00266 else 00267 { 00268 TestAtomSet m; 00269 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00270 mit = models.begin(); 00271 } 00272 } 00273 else if( rules == "i :- h. :- g." ) 00274 { 00275 assert(input); 00276 const TestAtomSet& inp = input->getAtoms(); 00277 if( inp.count("g") == 1 ) 00278 { 00279 // no model 00280 } 00281 else 00282 { 00283 if( inp.count("h") == 1 ) 00284 { 00285 TestAtomSet m; 00286 m.insert("i"); 00287 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00288 mit = models.begin(); 00289 } 00290 else 00291 { 00292 TestAtomSet m; 00293 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00294 mit = models.begin(); 00295 } 00296 } 00297 } 00298 else if( rules == "k :- j, i." ) 00299 { 00300 assert(input); 00301 const TestAtomSet& inp = input->getAtoms(); 00302 if( inp.count("j") == 1 && inp.count("i") == 1 ) 00303 { 00304 TestAtomSet m; 00305 m.insert("k"); 00306 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00307 mit = models.begin(); 00308 } 00309 else 00310 { 00311 TestAtomSet m; 00312 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00313 mit = models.begin(); 00314 } 00315 } 00316 else if( rules == "o :- m, k." ) 00317 { 00318 assert(input); 00319 const TestAtomSet& inp = input->getAtoms(); 00320 if( inp.count("m") == 1 && inp.count("k") == 1 ) 00321 { 00322 TestAtomSet m; 00323 m.insert("o"); 00324 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00325 mit = models.begin(); 00326 } 00327 else 00328 { 00329 TestAtomSet m; 00330 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00331 mit = models.begin(); 00332 } 00333 } 00334 else if( rules == "l :- not k." ) 00335 { 00336 assert(input); 00337 const TestAtomSet& inp = input->getAtoms(); 00338 if( inp.count("k") == 1 ) 00339 { 00340 TestAtomSet m; 00341 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00342 mit = models.begin(); 00343 } 00344 else 00345 { 00346 TestAtomSet m; 00347 m.insert("l"); 00348 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00349 mit = models.begin(); 00350 } 00351 } 00352 else if( rules == ":- k, l. :- o, not k." ) 00353 { 00354 assert(input); 00355 const TestAtomSet& inp = input->getAtoms(); 00356 if( (inp.count("k") == 1 && inp.count("l") == 1) || 00357 (inp.count("o") == 1 && inp.count("k") == 0) ) 00358 { 00359 // no model 00360 } 00361 else 00362 { 00363 TestAtomSet m; 00364 models.push_back(TestInterpretation::Ptr(new TestInterpretation(m))); 00365 mit = models.begin(); 00366 } 00367 } 00368 else 00369 { 00370 std::cerr << "TODO hardcode rules '" << rules << "'" << std::endl; 00371 throw std::runtime_error("not implemented!"); 00372 } 00373 00374 LOG_INDENT(INFO); 00375 BOOST_FOREACH(TestInterpretation::Ptr intp, models) 00376 LOG(INFO,"model " << *intp); 00377 }