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 #ifndef DUMMYTYPES_HPP_INCLUDED__24092010 00032 #define DUMMYTYPES_HPP_INCLUDED__24092010 00033 00034 #include "dlvhex2/Logger.h" 00035 #include "dlvhex2/EvalGraph.h" 00036 #include "dlvhex2/ModelGraph.h" 00037 #include "dlvhex2/ModelGenerator.h" 00038 #include <boost/test/unit_test.hpp> 00039 00040 // for testing we use stupid types 00041 struct TestProgramCtx 00042 { 00043 typedef std::string Rule; 00044 typedef std::string Constraint; 00045 00046 Rule rules; 00047 00048 TestProgramCtx(const Rule& rules): rules(rules) 00049 { 00050 //std::cerr << this << " TestProgramCtx()" << std::endl; 00051 } 00052 00053 ~TestProgramCtx() 00054 { 00055 //std::cerr << this << " ~TestProgramCtx()" << std::endl; 00056 } 00057 }; 00058 00059 // for testing we use stupid types 00060 typedef std::set<std::string> TestAtomSet; 00061 00062 class TestInterpretation: 00063 public dlvhex::InterpretationBase 00064 { 00065 public: 00066 typedef boost::shared_ptr<TestInterpretation> Ptr; 00067 typedef boost::shared_ptr<const TestInterpretation> ConstPtr; 00068 00069 public: 00070 // create empty 00071 TestInterpretation(): atoms() {} 00072 // create from atom set 00073 TestInterpretation(const TestAtomSet& as): atoms(as) {} 00074 // destruct 00075 ~TestInterpretation() {} 00076 00077 void add(const TestAtomSet& as) 00078 { 00079 atoms.insert(as.begin(), as.end()); 00080 } 00081 00082 void add(const TestInterpretation& i) 00083 { 00084 add(i.getAtoms()); 00085 } 00086 00087 // output 00088 std::ostream& print(std::ostream& o) const 00089 { 00090 TestAtomSet::const_iterator it = atoms.begin(); 00091 o << "{"; 00092 if( !atoms.empty() ) 00093 { 00094 o << *it; 00095 ++it; 00096 for(;it != atoms.end(); ++it) 00097 o << "," << *it; 00098 } 00099 o << "}"; 00100 return o; 00101 } 00102 00103 inline const TestAtomSet& getAtoms() const 00104 { return atoms; }; 00105 00106 private: 00107 TestAtomSet atoms; 00108 }; // class Interpretation 00109 00110 // syntactic operator<< sugar for printing interpretations 00111 inline std::ostream& operator<<(std::ostream& o, const TestInterpretation& i) 00112 { 00113 return i.print(o); 00114 } 00115 00116 class TestModelGeneratorFactory: 00117 public dlvhex::ModelGeneratorFactoryBase<TestInterpretation> 00118 { 00119 // 00120 // types 00121 // 00122 public: 00123 typedef dlvhex::ModelGeneratorFactoryBase<TestInterpretation> 00124 Base; 00125 00126 class ModelGenerator: 00127 public dlvhex::ModelGeneratorBase<TestInterpretation> 00128 { 00129 public: 00130 typedef std::list<TestInterpretation::Ptr> 00131 TestModelList; 00132 00133 TestModelGeneratorFactory& factory; 00134 00135 // list of models 00136 TestModelList models; 00137 // next output model 00138 TestModelList::iterator mit; 00139 00140 public: 00141 ModelGenerator( 00142 InterpretationConstPtr input, 00143 TestModelGeneratorFactory& factory); 00144 00145 virtual ~ModelGenerator() 00146 { 00147 LOG_VSCOPE(INFO,"~ModelGenerator()",this,true); 00148 } 00149 00150 virtual InterpretationPtr generateNextModel() 00151 { 00152 const std::string& rules = factory.ctx.rules; 00153 LOG_VSCOPE(INFO,"generateNextModel()",this,true); 00154 factory.generateNextModelCount++; 00155 LOG(INFO,"returning next model for rules '" << rules << "':"); 00156 if( mit == models.end() ) 00157 { 00158 LOG(INFO,"null"); 00159 return InterpretationPtr(); 00160 } 00161 else 00162 { 00163 InterpretationPtr ret = *mit; 00164 mit++; 00165 LOG(INFO,*ret); 00166 return ret; 00167 } 00168 } 00169 00170 // debug output 00171 virtual std::ostream& print(std::ostream& o) const 00172 { 00173 const std::string& rules = factory.ctx.rules; 00174 return o << "TestMGF::ModelGenerator with rules '" << rules << "'"; 00175 } 00176 }; 00177 00178 // 00179 // storage 00180 // 00181 public: 00182 const TestProgramCtx& ctx; 00183 unsigned generateNextModelCount; 00184 00185 // 00186 // members 00187 // 00188 public: 00189 TestModelGeneratorFactory(const TestProgramCtx& ctx): 00190 ctx(ctx), 00191 generateNextModelCount(0) 00192 { 00193 LOG_VSCOPE(INFO,"TestModelGeneratorFactory()",this,true); 00194 LOG(INFO,"rules='" << ctx.rules << "'"); 00195 } 00196 00197 virtual ~TestModelGeneratorFactory() 00198 { 00199 LOG_VSCOPE(INFO,"~TestModelGeneratorFactory()",this,true); 00200 LOG(INFO,"generateNextModelCount=" << generateNextModelCount); 00201 } 00202 00203 virtual ModelGeneratorPtr createModelGenerator( 00204 TestInterpretation::ConstPtr input) 00205 //InterpretationConstPtr input) 00206 { 00207 LOG_VSCOPE(INFO,"createModelGenerator()",this,true); 00208 LOG(INFO,"input=" << printptr(input)); 00209 return ModelGeneratorPtr(new ModelGenerator(input, *this)); 00210 } 00211 00212 // debug output 00213 virtual std::ostream& print(std::ostream& o) const 00214 { 00215 return o << "TestModelGeneratorFactory with rules '" << ctx.rules << "'"; 00216 } 00217 }; 00218 00219 // TestEvalGraph 00220 struct TestEvalUnitPropertyBase: 00221 public dlvhex::EvalUnitProjectionProperties, 00222 public dlvhex::EvalUnitModelGeneratorFactoryProperties<TestInterpretation> 00223 { 00224 TestProgramCtx ctx; 00225 00226 TestEvalUnitPropertyBase(): 00227 dlvhex::EvalUnitProjectionProperties(), 00228 dlvhex::EvalUnitModelGeneratorFactoryProperties<TestInterpretation>(), 00229 ctx("unset") 00230 { } 00231 TestEvalUnitPropertyBase(const std::string& rules): 00232 dlvhex::EvalUnitProjectionProperties(), 00233 dlvhex::EvalUnitModelGeneratorFactoryProperties<TestInterpretation>(), 00234 ctx(rules) 00235 { } 00236 }; 00237 00238 typedef dlvhex::EvalGraph<TestEvalUnitPropertyBase> 00239 TestEvalGraph; 00240 typedef TestEvalGraph::EvalUnit EvalUnit; 00241 typedef TestEvalGraph::EvalUnitDep EvalUnitDep; 00242 00243 // TestModelGraph 00244 struct TestModelPropertyBase 00245 { 00246 // interpretation of the model 00247 TestInterpretation interpretation; 00248 00249 TestModelPropertyBase() {} 00250 TestModelPropertyBase(const TestInterpretation& interpretation): 00251 interpretation(interpretation) {} 00252 }; 00253 00254 typedef dlvhex::ModelGraph<TestEvalGraph, TestModelPropertyBase> 00255 TestModelGraph; 00256 typedef TestModelGraph::Model Model; 00257 typedef TestModelGraph::ModelPropertyBundle ModelProp; 00258 typedef TestModelGraph::ModelDep ModelDep; 00259 typedef TestModelGraph::ModelDepPropertyBundle ModelDepProp; 00260 00261 template<typename EvalGraphT> 00262 class CounterVerification 00263 { 00264 protected: 00265 EvalGraphT& eg; 00266 00267 typedef std::map<TestEvalGraph::EvalUnit, unsigned> IterCountMap; 00268 std::vector<IterCountMap> counters; 00269 00270 public: 00271 CounterVerification(EvalGraphT& eg, unsigned iterations): 00272 eg(eg), 00273 counters(iterations + 1, IterCountMap()) 00274 { 00275 recordCounters(0); 00276 } 00277 00278 void recordCounters(unsigned iteration) 00279 { 00280 assert(iteration <= counters.size()); 00281 00282 LOG_SCOPE(INFO,"CounterVerification", false); 00283 00284 LOG(INFO,"recording iteration " << iteration); 00285 typename EvalGraphT::EvalUnitIterator unit, unitsbegin, unitsend; 00286 boost::tie(unitsbegin, unitsend) = eg.getEvalUnits(); 00287 for(unit = unitsbegin; unit != unitsend; ++unit) 00288 { 00289 boost::shared_ptr<TestModelGeneratorFactory> mgf = 00290 boost::dynamic_pointer_cast<TestModelGeneratorFactory>( 00291 eg.propsOf(*unit).mgf); 00292 if( !mgf ) 00293 { 00294 LOG(INFO,"could not downcast mgf of unit " << *unit << "!"); 00295 } 00296 else 00297 { 00298 counters[iteration][*unit] = mgf->generateNextModelCount; 00299 LOG(INFO,"initial counter of mgf of unit " << *unit << " = " << counters[iteration][*unit]); 00300 } 00301 } 00302 } 00303 00304 void printCounters() 00305 { 00306 typename EvalGraphT::EvalUnitIterator unit, unitsbegin, unitsend; 00307 boost::tie(unitsbegin, unitsend) = eg.getEvalUnits(); 00308 for(unsigned counteridx = 0; counteridx != counters.size(); ++counteridx) 00309 { 00310 LOG(INFO,"model generation counter for iteration " << counteridx << ":"); 00311 LOG_INDENT(INFO); 00312 for(unit = unitsbegin; unit != unitsend; ++unit) 00313 { 00314 LOG(INFO,"u" << *unit << " -> " << counters[counteridx][*unit]); 00315 } 00316 } 00317 } 00318 00319 void verifyEqual(unsigned iterationa, unsigned iterationb) 00320 { 00321 assert(iterationa < iterationb <= counters.size()); 00322 typename EvalGraphT::EvalUnitIterator unit, unitsbegin, unitsend; 00323 boost::tie(unitsbegin, unitsend) = eg.getEvalUnits(); 00324 for(unit = unitsbegin; unit != unitsend; ++unit) 00325 { 00326 BOOST_CHECK((counters[iterationa][*unit] - counters[iterationb][*unit]) == 0); 00327 } 00328 } 00329 }; 00330 00331 #endif // DUMMYTYPES_HPP_INCLUDED__24092010