dlvhex  2.5.0
testsuite/TestMLPSolver.cpp
Go to the documentation of this file.
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
00034 
00035 //#define NDEBUG
00036 #include <boost/cstdint.hpp>
00037 #include "dlvhex2/HexParser.h"
00038 #include "dlvhex2/ProgramCtx.h"
00039 #include "dlvhex2/Printer.h"
00040 #include "dlvhex2/Registry.h"
00041 #include "dlvhex2/MLPSyntaxChecker.h"
00042 #include "dlvhex2/Interpretation.h"
00043 #include "dlvhex2/MLPSolver.h"
00044 
00045 #define BOOST_TEST_MODULE "TestMLPSolver"
00046 #include <boost/test/unit_test.hpp>
00047 
00048 #include <iostream>
00049 #include <fstream>
00050 
00051 #ifdef NDEBUG
00052     #define LOG_REGISTRY_PROGRAM(ctx) {}
00053 #else
00054     #define LOG_REGISTRY_PROGRAM(ctx) \
00055       LOG(INFO, *ctx.registry()); \
00056         RawPrinter printer(std::cerr, ctx.registry()); \
00057         std::cerr << "first edb = " << *ctx.edbList.front() << std::endl; \
00058         LOG(DBG, "first idb"); \
00059         printer.printmany(ctx.idbList.front(),"\n"); \
00060         std::cerr << std::endl; \
00061         LOG(DBG, "idb end");
00062 #endif
00063 
00064 LOG_INIT(Logger::ERROR | Logger::WARNING)
00065 
00066 DLVHEX_NAMESPACE_USE
00067 
00068 // 1
00069 template<typename SolverSoftwareConfiguration>
00070 void testInconsistentProgram()
00071 {
00072 
00073 #ifdef NDEBUG
00074   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00075 #endif
00076 
00077   LOG(DBG, " ");
00078   LOG(DBG, "Test Inconsistent Program begin");
00079 
00080   ProgramCtx ctx;
00081   ctx.setupRegistry(RegistryPtr(new Registry));
00082   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00083 
00084   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00085   assert(TOP_SRCDIR != 0);
00086 
00087   std::string filename(TOP_SRCDIR);
00088   filename += "/examples/module-Inconsistent.mlp";
00089   std::ifstream ifs;
00090   std::ostringstream buf;
00091 
00092   ifs.open(filename.c_str());
00093   BOOST_REQUIRE(ifs.is_open());
00094   buf << ifs.rdbuf();
00095   ifs.close();
00096 
00097   std::stringstream ss;
00098   ss << buf.str();
00099 
00100   InputProviderPtr ip(new InputProvider);
00101   ip->addStreamInput(ss, "testinput");
00102   ModuleHexParser parser;
00103   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00104   // after parser, print ctx
00105   LOG_REGISTRY_PROGRAM(ctx);
00106 
00107   // syntax verifying:
00108   MLPSyntaxChecker sC(ctx);
00109   BOOST_REQUIRE( sC.verifySyntax() == true );
00110 
00111   MLPSolver m(ctx);
00112   BOOST_REQUIRE ( m.solve() == true );
00113   BOOST_REQUIRE ( m.ctrAS == 0 );
00114   LOG(DBG, "Test Inconsistent Program finish"); 
00115 }
00116 
00117 // 2
00118 template<typename SolverSoftwareConfiguration>
00119 void testNoticStratifiedProgram()
00120 {
00121 
00122 #ifdef NDEBUG
00123   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00124 #endif
00125 
00126   LOG(DBG, " ");
00127   LOG(DBG, "Test Not ic Stratified Program begin");
00128 
00129   ProgramCtx ctx;
00130   ctx.setupRegistry(RegistryPtr(new Registry));
00131   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00132 
00133   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00134   assert(TOP_SRCDIR != 0);
00135 
00136   std::string filename(TOP_SRCDIR);
00137   filename += "/examples/module-Not-ic-Stratified.mlp";
00138   std::ifstream ifs;
00139   std::ostringstream buf;
00140 
00141   ifs.open(filename.c_str());
00142   BOOST_REQUIRE(ifs.is_open());
00143   buf << ifs.rdbuf();
00144   ifs.close();
00145 
00146   std::stringstream ss;
00147   ss << buf.str();
00148 
00149   InputProviderPtr ip(new InputProvider);
00150   ip->addStreamInput(ss, "testinput");
00151   ModuleHexParser parser;
00152   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00153   // after parser, print ctx
00154   LOG_REGISTRY_PROGRAM(ctx);
00155 
00156   // syntax verifying:
00157   MLPSyntaxChecker sC(ctx);
00158   BOOST_REQUIRE( sC.verifySyntax() == true );
00159 
00160   MLPSolver m(ctx);
00161   BOOST_REQUIRE_THROW( m.solve() , FatalError);
00162   BOOST_REQUIRE ( m.ctrAS == 0 );
00163   LOG(DBG, "Test Not ic Stratified Program finish");
00164 }
00165 
00166 
00167 // 3
00168 template<typename SolverSoftwareConfiguration>
00169 void testOneMainModules()
00170 {
00171 
00172 #ifdef NDEBUG
00173   Logger::Instance().setPrintLevels(Logger::INFO | Logger::ERROR | Logger::WARNING);
00174 #endif
00175 
00176   LOG(DBG, " ");
00177   LOG(DBG, "Test One Main Modules begin");
00178 
00179   ProgramCtx ctx;
00180   ctx.setupRegistry(RegistryPtr(new Registry));
00181   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00182 
00183   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00184   assert(TOP_SRCDIR != 0);
00185 
00186   std::string filename1(TOP_SRCDIR);
00187   std::string filename2(TOP_SRCDIR);
00188   std::string filename3(TOP_SRCDIR);
00189   filename1 += "/examples/module1.mlp";
00190   filename2 += "/examples/module2.mlp";
00191   filename3 += "/examples/module3.mlp";
00192   std::ifstream ifs;
00193   std::ostringstream buf;
00194 
00195   ifs.open(filename1.c_str());
00196   BOOST_REQUIRE(ifs.is_open());
00197   buf << ifs.rdbuf();
00198   ifs.close();
00199 
00200   ifs.open(filename2.c_str());
00201   BOOST_REQUIRE(ifs.is_open());
00202   buf << ifs.rdbuf();
00203   ifs.close();
00204 
00205   ifs.open(filename3.c_str());
00206   BOOST_REQUIRE(ifs.is_open());
00207   buf << ifs.rdbuf();
00208   ifs.close();
00209 
00210   std::stringstream ss;
00211   ss << buf.str();
00212 
00213   InputProviderPtr ip(new InputProvider);
00214   ip->addStreamInput(ss, "testinput");
00215   ModuleHexParser parser;
00216   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00217   // after parser, print ctx
00218   LOG_REGISTRY_PROGRAM(ctx);
00219 
00220   // syntax verifying:
00221   MLPSyntaxChecker sC(ctx);
00222   BOOST_REQUIRE( sC.verifySyntax() == true );
00223 
00224   MLPSolver m(ctx);
00225   m.setNASReturned(0); 
00226   BOOST_REQUIRE ( m.solve() == true );
00227   BOOST_REQUIRE( m.ctrAS == 2 );
00228   LOG(DBG, "Test One Main Modules finish");
00229 }
00230 
00231 // 4
00232 template<typename SolverSoftwareConfiguration>
00233 void testTwoMainModules()
00234 {
00235 
00236 #ifdef NDEBUG
00237   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00238 #endif
00239 
00240   LOG(DBG, " ");
00241   LOG(DBG, "Test Two Main Modules begin");
00242   ProgramCtx ctx;
00243   ctx.setupRegistry(RegistryPtr(new Registry));
00244   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00245 
00246   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00247   assert(TOP_SRCDIR != 0);
00248 
00249   std::string filename1(TOP_SRCDIR);
00250   std::string filename2(TOP_SRCDIR);
00251   std::string filename3(TOP_SRCDIR);
00252 
00253   filename1 += "/examples/module1-MainModules.mlp";
00254   filename2 += "/examples/module2.mlp";
00255   filename3 += "/examples/module3.mlp";
00256   std::ifstream ifs;
00257   std::ostringstream buf;
00258 
00259   ifs.open(filename1.c_str());
00260   BOOST_REQUIRE(ifs.is_open());
00261   buf << ifs.rdbuf();
00262   ifs.close();
00263 
00264   ifs.open(filename2.c_str());
00265   BOOST_REQUIRE(ifs.is_open());
00266   buf << ifs.rdbuf();
00267   ifs.close();
00268 
00269   ifs.open(filename3.c_str());
00270   BOOST_REQUIRE(ifs.is_open());
00271   buf << ifs.rdbuf();
00272   ifs.close();
00273 
00274   std::stringstream ss;
00275   ss << buf.str();
00276 
00277   InputProviderPtr ip(new InputProvider);
00278   ip->addStreamInput(ss, "testinput");
00279   ModuleHexParser parser;
00280   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00281   // after parser, print ctx
00282   LOG_REGISTRY_PROGRAM(ctx);
00283 
00284   // syntax verifying:
00285   MLPSyntaxChecker sC(ctx);
00286   BOOST_REQUIRE( sC.verifySyntax() == true );
00287 
00288   MLPSolver m(ctx);
00289   BOOST_REQUIRE ( m.solve() == true );
00290   BOOST_REQUIRE( m.ctrAS == 4 );
00291   LOG(DBG, "Test Two Main Modules finish");
00292 }
00293 
00294 
00295 // 5
00296 template<typename SolverSoftwareConfiguration>
00297 void testTwoModuleCalls1()
00298 {
00299 
00300 #ifdef NDEBUG
00301   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00302 #endif
00303 
00304   LOG(DBG, " ");
00305   LOG(DBG, "Test Two Module Calls 1 begin");
00306   ProgramCtx ctx;
00307   ctx.setupRegistry(RegistryPtr(new Registry));
00308   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00309 
00310   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00311   assert(TOP_SRCDIR != 0);
00312 
00313   std::string filename1(TOP_SRCDIR);
00314   std::string filename2(TOP_SRCDIR);
00315   std::string filename3(TOP_SRCDIR);
00316 
00317   filename1 += "/examples/module1-Two.mlp";
00318   filename2 += "/examples/module2.mlp";
00319   filename3 += "/examples/module3.mlp";
00320   std::ifstream ifs;
00321   std::ostringstream buf;
00322 
00323   ifs.open(filename1.c_str());
00324   BOOST_REQUIRE(ifs.is_open());
00325   buf << ifs.rdbuf();
00326   ifs.close();
00327 
00328   ifs.open(filename2.c_str());
00329   BOOST_REQUIRE(ifs.is_open());
00330   buf << ifs.rdbuf();
00331   ifs.close();
00332 
00333   ifs.open(filename3.c_str());
00334   BOOST_REQUIRE(ifs.is_open());
00335   buf << ifs.rdbuf();
00336   ifs.close();
00337 
00338   std::stringstream ss;
00339   ss << buf.str();
00340 
00341   InputProviderPtr ip(new InputProvider);
00342   ip->addStreamInput(ss, "testinput");
00343   ModuleHexParser parser;
00344   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00345   // after parser, print ctx
00346   LOG_REGISTRY_PROGRAM(ctx);
00347 
00348   // syntax verifying:
00349   MLPSyntaxChecker sC(ctx);
00350   BOOST_REQUIRE( sC.verifySyntax() == true );
00351 
00352   MLPSolver m(ctx);
00353   BOOST_REQUIRE ( m.solve() == true );
00354   BOOST_REQUIRE( m.ctrAS == 2 );
00355   LOG(DBG, "Test Two Module Calls 1 finish");
00356 }
00357 
00358 
00359 // 6
00360 template<typename SolverSoftwareConfiguration>
00361 void testTwoModuleCalls2()
00362 {
00363 
00364 #ifdef NDEBUG
00365   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00366 #endif
00367 
00368   LOG(DBG, " ");
00369   LOG(DBG, "Test Two Module Calls 2 begin");
00370   ProgramCtx ctx;
00371   ctx.setupRegistry(RegistryPtr(new Registry));
00372   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00373 
00374   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00375   assert(TOP_SRCDIR != 0);
00376 
00377   std::string filename1(TOP_SRCDIR);
00378   std::string filename2(TOP_SRCDIR);
00379   std::string filename3(TOP_SRCDIR);
00380 
00381   filename1 += "/examples/module1.mlp";
00382   filename2 += "/examples/module2-Two.mlp";
00383   filename3 += "/examples/module3.mlp";
00384 
00385   std::ifstream ifs;
00386   std::ostringstream buf;
00387 
00388   ifs.open(filename1.c_str());
00389   BOOST_REQUIRE(ifs.is_open());
00390   buf << ifs.rdbuf();
00391   ifs.close();
00392 
00393   ifs.open(filename2.c_str());
00394   BOOST_REQUIRE(ifs.is_open());
00395   buf << ifs.rdbuf();
00396   ifs.close();
00397 
00398   ifs.open(filename3.c_str());
00399   BOOST_REQUIRE(ifs.is_open());
00400   buf << ifs.rdbuf();
00401   ifs.close();
00402 
00403   std::stringstream ss;
00404   ss << buf.str();
00405 
00406   InputProviderPtr ip(new InputProvider);
00407   ip->addStreamInput(ss, "testinput");
00408   ModuleHexParser parser;
00409   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00410   // after parser, print ctx
00411   LOG_REGISTRY_PROGRAM(ctx);
00412 
00413   // syntax verifying:
00414   MLPSyntaxChecker sC(ctx);
00415   BOOST_REQUIRE( sC.verifySyntax() == true );
00416 
00417   MLPSolver m(ctx);
00418   BOOST_REQUIRE ( m.solve() == true );
00419   BOOST_REQUIRE( m.ctrAS == 2 );
00420   LOG(DBG, "Test Two Module Calls 2 finish");
00421 }
00422 
00423 
00424 // 7
00425 template<typename SolverSoftwareConfiguration>
00426 void testReachabilityNonGroundProgram()
00427 {
00428 
00429 #ifdef NDEBUG
00430   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00431 #endif
00432 
00433   LOG(DBG, " ");
00434   LOG(DBG, "Test Reachability Non Ground Program begin");
00435 
00436   ProgramCtx ctx;
00437   ctx.setupRegistry(RegistryPtr(new Registry));
00438   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00439 
00440   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00441   assert(TOP_SRCDIR != 0);
00442 
00443   std::string filename(TOP_SRCDIR);
00444 
00445   filename += "/examples/module-Reachability.mlp";
00446   std::ifstream ifs;
00447   std::ostringstream buf;
00448 
00449   ifs.open(filename.c_str());
00450   BOOST_REQUIRE(ifs.is_open());
00451   buf << ifs.rdbuf();
00452   ifs.close();
00453 
00454   std::stringstream ss;
00455   ss << buf.str();
00456 
00457   InputProviderPtr ip(new InputProvider);
00458   ip->addStreamInput(ss, "testinput");
00459   ModuleHexParser parser;
00460   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00461   // after parser, print ctx
00462   LOG_REGISTRY_PROGRAM(ctx);
00463 
00464   // syntax verifying:
00465   MLPSyntaxChecker sC(ctx);
00466   BOOST_REQUIRE( sC.verifySyntax() == true );
00467 
00468   MLPSolver m(ctx);
00469   BOOST_REQUIRE ( m.solve() == true );
00470   BOOST_REQUIRE ( m.ctrAS == 1 );
00471   LOG(DBG, "Test Reachability Non Ground Program finish");
00472 }
00473 
00474 
00475 // 8
00476 template<typename SolverSoftwareConfiguration>
00477 void testCardinalityProgram()
00478 {
00479 
00480 #ifdef NDEBUG
00481   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00482 #endif
00483 
00484   LOG(DBG, " ");
00485   LOG(DBG, "Test Cardinality Program begin");
00486 
00487   ProgramCtx ctx;
00488   ctx.setupRegistry(RegistryPtr(new Registry));
00489   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00490 
00491   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00492   assert(TOP_SRCDIR != 0);
00493 
00494   std::string filename(TOP_SRCDIR);
00495   filename += "/examples/module-Cardinality.mlp";
00496   std::ifstream ifs;
00497   std::ostringstream buf;
00498 
00499   ifs.open(filename.c_str());
00500   BOOST_REQUIRE(ifs.is_open());
00501   buf << ifs.rdbuf();
00502   ifs.close();
00503 
00504   std::stringstream ss;
00505   ss << buf.str();
00506 
00507   InputProviderPtr ip(new InputProvider);
00508   ip->addStreamInput(ss, "testinput");
00509   ModuleHexParser parser;
00510   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00511   // after parser, print ctx
00512   LOG_REGISTRY_PROGRAM(ctx);
00513 
00514   // syntax verifying:
00515   MLPSyntaxChecker sC(ctx);
00516   BOOST_REQUIRE( sC.verifySyntax() == true );
00517 
00518   MLPSolver m(ctx);
00519   BOOST_REQUIRE ( m.solve() == true );
00520   BOOST_REQUIRE ( m.ctrAS == 16 );
00521   LOG(DBG, "Test Cardinality Program finish");
00522 }
00523 
00524 
00525 // 9
00526 template<typename SolverSoftwareConfiguration>
00527 void testABBAProgram()
00528 {
00529 
00530 #ifdef NDEBUG
00531   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00532 #endif
00533 
00534   LOG(DBG, " ");
00535   LOG(DBG, "Test ABBA Program begin");
00536 
00537   ProgramCtx ctx;
00538   ctx.setupRegistry(RegistryPtr(new Registry));
00539   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00540 
00541   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00542   assert(TOP_SRCDIR != 0);
00543 
00544   std::string filename(TOP_SRCDIR);
00545 
00546   filename += "/examples/module-ABBA.mlp";
00547   std::ifstream ifs;
00548   std::ostringstream buf;
00549 
00550   ifs.open(filename.c_str());
00551   BOOST_REQUIRE(ifs.is_open());
00552   buf << ifs.rdbuf();
00553   ifs.close();
00554 
00555   std::stringstream ss;
00556   ss << buf.str();
00557 
00558   InputProviderPtr ip(new InputProvider);
00559   ip->addStreamInput(ss, "testinput");
00560   ModuleHexParser parser;
00561   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00562   // after parser, print ctx
00563   LOG_REGISTRY_PROGRAM(ctx);
00564 
00565   // syntax verifying:
00566   MLPSyntaxChecker sC(ctx);
00567   BOOST_REQUIRE( sC.verifySyntax() == true );
00568 
00569   MLPSolver m(ctx);
00570   BOOST_REQUIRE ( m.solve() == true );
00571   BOOST_REQUIRE ( m.ctrAS == 2 );
00572   LOG(DBG, "Test ABBA Program finish");
00573 }
00574 
00575 
00576 // 10
00577 template<typename SolverSoftwareConfiguration>
00578 void testDisjunctionProgram()
00579 {
00580 
00581 #ifdef NDEBUG
00582   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00583 #endif
00584 
00585   LOG(DBG, " ");
00586   LOG(DBG, "Test Disjunction Program begin");
00587 
00588   ProgramCtx ctx;
00589   ctx.setupRegistry(RegistryPtr(new Registry));
00590   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00591 
00592   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00593   assert(TOP_SRCDIR != 0);
00594 
00595   std::string filename(TOP_SRCDIR);
00596   filename += "/examples/module-Disjunction.mlp";
00597   std::ifstream ifs;
00598   std::ostringstream buf;
00599 
00600   ifs.open(filename.c_str());
00601   BOOST_REQUIRE(ifs.is_open());
00602   buf << ifs.rdbuf();
00603   ifs.close();
00604 
00605   std::stringstream ss;
00606   ss << buf.str();
00607 
00608   InputProviderPtr ip(new InputProvider);
00609   ip->addStreamInput(ss, "testinput");
00610   ModuleHexParser parser;
00611   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00612   // after parser, print ctx
00613   LOG_REGISTRY_PROGRAM(ctx);
00614 
00615   // syntax verifying:
00616   MLPSyntaxChecker sC(ctx);
00617   BOOST_REQUIRE( sC.verifySyntax() == true );
00618 
00619   MLPSolver m(ctx);
00620   BOOST_REQUIRE ( m.solve() == true );
00621   BOOST_REQUIRE ( m.ctrAS == 2 );
00622   LOG(DBG, "Test Disjunction Program finish");
00623 }
00624 
00625 
00626 // 11
00627 template<typename SolverSoftwareConfiguration>
00628 void testNegationProgram()
00629 {
00630 
00631 #ifdef NDEBUG
00632   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00633 #endif
00634 
00635   LOG(DBG, " ");
00636   LOG(DBG, "Test Negation Program begin");
00637 
00638   ProgramCtx ctx;
00639   ctx.setupRegistry(RegistryPtr(new Registry));
00640   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00641 
00642   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00643   assert(TOP_SRCDIR != 0);
00644 
00645   std::string filename(TOP_SRCDIR);
00646   filename += "/examples/module-Negation.mlp";
00647   std::ifstream ifs;
00648   std::ostringstream buf;
00649 
00650   ifs.open(filename.c_str());
00651   BOOST_REQUIRE(ifs.is_open());
00652   buf << ifs.rdbuf();
00653   ifs.close();
00654 
00655   std::stringstream ss;
00656   ss << buf.str();
00657 
00658   InputProviderPtr ip(new InputProvider);
00659   ip->addStreamInput(ss, "testinput");
00660   ModuleHexParser parser;
00661   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00662   // after parser, print ctx
00663   LOG_REGISTRY_PROGRAM(ctx);
00664 
00665   // syntax verifying:
00666   MLPSyntaxChecker sC(ctx);
00667   BOOST_REQUIRE( sC.verifySyntax() == true );
00668 
00669   MLPSolver m(ctx);
00670   BOOST_REQUIRE ( m.solve() == true );
00671   BOOST_REQUIRE ( m.ctrAS == 0 );
00672   LOG(DBG, "Test Negation Program finish");
00673 }
00674 
00675 
00676 //12
00677 template<typename SolverSoftwareConfiguration>
00678 void testIndirectionProgram()
00679 {
00680 
00681 #ifdef NDEBUG
00682   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00683 #endif
00684 
00685   LOG(DBG, " ");
00686   LOG(DBG, "Test Indirection Program begin");
00687 
00688   ProgramCtx ctx;
00689   ctx.setupRegistry(RegistryPtr(new Registry));
00690   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00691 
00692   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00693   assert(TOP_SRCDIR != 0);
00694 
00695   std::string filename(TOP_SRCDIR);
00696   filename += "/examples/module-Indirection.mlp";
00697   std::ifstream ifs;
00698   std::ostringstream buf;
00699 
00700   ifs.open(filename.c_str());
00701   BOOST_REQUIRE(ifs.is_open());
00702   buf << ifs.rdbuf();
00703   ifs.close();
00704 
00705   std::stringstream ss;
00706   ss << buf.str();
00707 
00708   InputProviderPtr ip(new InputProvider);
00709   ip->addStreamInput(ss, "testinput");
00710   ModuleHexParser parser;
00711   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00712   // after parser, print ctx
00713   LOG_REGISTRY_PROGRAM(ctx);
00714 
00715   // syntax verifying:
00716   MLPSyntaxChecker sC(ctx);
00717   BOOST_REQUIRE( sC.verifySyntax() == true );
00718   MLPSolver m(ctx);
00719   BOOST_REQUIRE ( m.solve() == true );
00720   BOOST_REQUIRE ( m.ctrAS == 1 );
00721   LOG(DBG, "Test Indirection Program finish");
00722 }
00723 
00724 
00725 //13
00726 template<typename SolverSoftwareConfiguration>
00727 void testAFinProgram()
00728 {
00729 
00730 #ifdef NDEBUG
00731   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00732 #endif
00733 
00734   LOG(DBG, " ");
00735   LOG(DBG, "Test AFin Program begin");
00736 
00737   ProgramCtx ctx;
00738   ctx.setupRegistry(RegistryPtr(new Registry));
00739   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00740 
00741   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00742   assert(TOP_SRCDIR != 0);
00743 
00744   std::string filename(TOP_SRCDIR);
00745   filename += "/examples/module-AFin.mlp";
00746   std::ifstream ifs;
00747   std::ostringstream buf;
00748 
00749   ifs.open(filename.c_str());
00750   BOOST_REQUIRE(ifs.is_open());
00751   buf << ifs.rdbuf();
00752   ifs.close();
00753 
00754   std::stringstream ss;
00755   ss << buf.str();
00756 
00757   InputProviderPtr ip(new InputProvider);
00758   ip->addStreamInput(ss, "testinput");
00759   ModuleHexParser parser;
00760   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00761   // after parser, print ctx
00762   LOG_REGISTRY_PROGRAM(ctx);
00763 
00764   // syntax verifying:
00765   MLPSyntaxChecker sC(ctx);
00766   BOOST_REQUIRE( sC.verifySyntax() == true );
00767   MLPSolver m(ctx);
00768   BOOST_REQUIRE ( m.solve() == true );
00769   BOOST_REQUIRE ( m.ctrAS == 1 );
00770   LOG(DBG, "Test AFin Program finish");
00771 }
00772 
00773 
00774 //14
00775 template<typename SolverSoftwareConfiguration>
00776 void testCsProgram()
00777 {
00778 
00779 #ifdef NDEBUG
00780   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00781 #endif
00782 
00783   LOG(DBG, " ");
00784   LOG(DBG, "Test C more than one begin");
00785 
00786   ProgramCtx ctx;
00787   ctx.setupRegistry(RegistryPtr(new Registry));
00788   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00789 
00790   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00791   assert(TOP_SRCDIR != 0);
00792 
00793   std::string filename(TOP_SRCDIR);
00794   filename += "/examples/module-Cs.mlp";
00795   std::ifstream ifs;
00796   std::ostringstream buf;
00797 
00798   ifs.open(filename.c_str());
00799   BOOST_REQUIRE(ifs.is_open());
00800   buf << ifs.rdbuf();
00801   ifs.close();
00802 
00803   std::stringstream ss;
00804   ss << buf.str();
00805 
00806   InputProviderPtr ip(new InputProvider);
00807   ip->addStreamInput(ss, "testinput");
00808   ModuleHexParser parser;
00809   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00810   // after parser, print ctx
00811   LOG_REGISTRY_PROGRAM(ctx);
00812 
00813   // syntax verifying:
00814   MLPSyntaxChecker sC(ctx);
00815   BOOST_REQUIRE( sC.verifySyntax() == true );
00816   MLPSolver m(ctx);
00817   BOOST_REQUIRE ( m.solve() == true );
00818   BOOST_REQUIRE ( m.ctrAS == 1 );
00819   LOG(DBG, "Test Cs Program finish");
00820 }
00821 
00822 
00823 //15
00824 template<typename SolverSoftwareConfiguration>
00825 void testIStratifiedProgram()
00826 {
00827 
00828 #ifdef NDEBUG
00829   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00830 #endif
00831 
00832   LOG(DBG, " ");
00833   LOG(DBG, "Test i Stratified begin");
00834 
00835   ProgramCtx ctx;
00836   ctx.setupRegistry(RegistryPtr(new Registry));
00837   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00838 
00839   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00840   assert(TOP_SRCDIR != 0);
00841 
00842   std::string filename(TOP_SRCDIR);
00843   filename += "/examples/module-i-Stratified.mlp";
00844   std::ifstream ifs;
00845   std::ostringstream buf;
00846 
00847   ifs.open(filename.c_str());
00848   BOOST_REQUIRE(ifs.is_open());
00849   buf << ifs.rdbuf();
00850   ifs.close();
00851 
00852   std::stringstream ss;
00853   ss << buf.str();
00854 
00855   InputProviderPtr ip(new InputProvider);
00856   ip->addStreamInput(ss, "testinput");
00857   ModuleHexParser parser;
00858   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00859   // after parser, print ctx
00860   LOG_REGISTRY_PROGRAM(ctx);
00861 
00862   // syntax verifying:
00863   MLPSyntaxChecker sC(ctx);
00864   BOOST_REQUIRE( sC.verifySyntax() == true );
00865   MLPSolver m(ctx);
00866   BOOST_REQUIRE ( m.solve() == true );
00867   BOOST_REQUIRE ( m.ctrAS == 1 );
00868   LOG(DBG, "Test i stratified Program finish");
00869 }
00870 
00871 
00872 //16
00873 template<typename SolverSoftwareConfiguration>
00874 void testIStratified2Program()
00875 {
00876 
00877 #ifdef NDEBUG
00878   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00879 #endif
00880 
00881   LOG(DBG, " ");
00882   LOG(DBG, "Test i stratified 2 begin");
00883 
00884   ProgramCtx ctx;
00885   ctx.setupRegistry(RegistryPtr(new Registry));
00886   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00887 
00888   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00889   assert(TOP_SRCDIR != 0);
00890 
00891   std::string filename(TOP_SRCDIR);
00892   filename += "/examples/module-i-Stratified-2.mlp";
00893   std::ifstream ifs;
00894   std::ostringstream buf;
00895 
00896   ifs.open(filename.c_str());
00897   BOOST_REQUIRE(ifs.is_open());
00898   buf << ifs.rdbuf();
00899   ifs.close();
00900 
00901   std::stringstream ss;
00902   ss << buf.str();
00903 
00904   InputProviderPtr ip(new InputProvider);
00905   ip->addStreamInput(ss, "testinput");
00906   ModuleHexParser parser;
00907   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00908   // after parser, print ctx
00909   LOG_REGISTRY_PROGRAM(ctx);
00910 
00911   // syntax verifying:
00912   MLPSyntaxChecker sC(ctx);
00913   BOOST_REQUIRE( sC.verifySyntax() == true );
00914   MLPSolver m(ctx);
00915   BOOST_REQUIRE ( m.solve() == true );
00916   BOOST_REQUIRE ( m.ctrAS == 1 );
00917   LOG(DBG, "Test i stratified 2 Program finish");
00918 }
00919 
00920 
00921 // 17
00922 template<typename SolverSoftwareConfiguration>
00923 void testHanoiProgram()
00924 {
00925 
00926 #ifdef NDEBUG
00927   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00928 #endif
00929 
00930   LOG(DBG, " ");
00931   LOG(DBG, "Test Hanoi Program begin");
00932 
00933   ProgramCtx ctx;
00934   ctx.setupRegistry(RegistryPtr(new Registry));
00935   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00936 
00937   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00938   assert(TOP_SRCDIR != 0);
00939 
00940   std::string filename(TOP_SRCDIR);
00941   filename += "/examples/module-Hanoi.mlp";
00942   std::ifstream ifs;
00943   std::ostringstream buf;
00944 
00945   ifs.open(filename.c_str());
00946   BOOST_REQUIRE(ifs.is_open());
00947   buf << ifs.rdbuf();
00948   ifs.close();
00949 
00950   std::stringstream ss;
00951   ss << buf.str();
00952 
00953   InputProviderPtr ip(new InputProvider);
00954   ip->addStreamInput(ss, "testinput");
00955   ModuleHexParser parser;
00956   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00957   // after parser, print ctx
00958   //...LOG_REGISTRY_PROGRAM(ctx);
00959 
00960   // syntax verifying:
00961   MLPSyntaxChecker sC(ctx);
00962   BOOST_REQUIRE( sC.verifySyntax() == true );
00963 
00964   MLPSolver m(ctx);
00965   BOOST_REQUIRE ( m.solve() == true );
00966   //rmv. std::cerr << "ctrAS: " << m.ctrAS << std::endl;
00967   BOOST_REQUIRE ( m.ctrAS == 1 );
00968   LOG(DBG, "Test Hanoi Program finish");
00969 }
00970 
00971 
00972 //18
00973 template<typename SolverSoftwareConfiguration>
00974 void testComplexProgram()
00975 {
00976 
00977 #ifdef NDEBUG
00978   Logger::Instance().setPrintLevels(Logger::ERROR | Logger::WARNING);
00979 #endif
00980 
00981   LOG(DBG, " ");
00982   LOG(DBG, "Test Complex Program begin");
00983 
00984   ProgramCtx ctx;
00985   ctx.setupRegistry(RegistryPtr(new Registry));
00986   ctx.setASPSoftware(ASPSolverManager::SoftwareConfigurationPtr(new SolverSoftwareConfiguration));
00987 
00988   char *TOP_SRCDIR = getenv("TOP_SRCDIR");
00989   assert(TOP_SRCDIR != 0);
00990 
00991   std::string filename(TOP_SRCDIR);
00992   filename += "/examples/module-Complex.mlp";
00993   std::ifstream ifs;
00994   std::ostringstream buf;
00995 
00996   ifs.open(filename.c_str());
00997   BOOST_REQUIRE(ifs.is_open());
00998   buf << ifs.rdbuf();
00999   ifs.close();
01000 
01001   std::stringstream ss;
01002   ss << buf.str();
01003 
01004   InputProviderPtr ip(new InputProvider);
01005   ip->addStreamInput(ss, "testinput");
01006   ModuleHexParser parser;
01007   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
01008   // after parser, print ctx
01009   //...LOG_REGISTRY_PROGRAM(ctx);
01010 
01011   // syntax verifying:
01012   MLPSyntaxChecker sC(ctx);
01013   BOOST_REQUIRE( sC.verifySyntax() == true );
01014 
01015   MLPSolver m(ctx);
01016   BOOST_REQUIRE ( m.solve() == true );
01017   //std::cerr << "ctrAS: " << m.ctrAS << std::endl;
01018   BOOST_REQUIRE ( m.ctrAS == 12 );
01019   LOG(DBG, "Test Complex Program finish");
01020 }
01021 
01022 template<typename SolverSoftwareConfiguration>
01023 void testAll()
01024 {
01025   testInconsistentProgram<SolverSoftwareConfiguration>();
01026   testNoticStratifiedProgram<SolverSoftwareConfiguration>();
01027   testOneMainModules<SolverSoftwareConfiguration>();
01028   testTwoMainModules<SolverSoftwareConfiguration>();
01029   testTwoModuleCalls1<SolverSoftwareConfiguration>();
01030   testTwoModuleCalls2<SolverSoftwareConfiguration>();
01031   testReachabilityNonGroundProgram<SolverSoftwareConfiguration>();
01032   testCardinalityProgram<SolverSoftwareConfiguration>();
01033   testABBAProgram<SolverSoftwareConfiguration>();
01034   testDisjunctionProgram<SolverSoftwareConfiguration>();
01035 
01036   testNegationProgram<SolverSoftwareConfiguration>();
01037   testIndirectionProgram<SolverSoftwareConfiguration>();
01038   testAFinProgram<SolverSoftwareConfiguration>();
01039   testCsProgram<SolverSoftwareConfiguration>();
01040   testIStratifiedProgram<SolverSoftwareConfiguration>();
01041   testIStratified2Program<SolverSoftwareConfiguration>();
01042   testHanoiProgram<SolverSoftwareConfiguration>();
01043   testComplexProgram<SolverSoftwareConfiguration>();
01044 }
01045 
01046 
01047 
01048 #ifdef HAVE_DLV
01049 BOOST_AUTO_TEST_CASE(testMLPSolverDLV) 
01050 {
01051   testAll<ASPSolver::DLVSoftware::Configuration>();
01052 }
01053 #endif
01054  
01055 #ifdef HAVE_DLVDB
01056 BOOST_AUTO_TEST_CASE(testMLPSolverDLVDB) 
01057 {
01058   testAll<ASPSolver::DLVDBSoftware::Configuration>();
01059 }
01060 #endif
01061  
01062 #ifdef HAVE_LIBDLV
01063 BOOST_AUTO_TEST_CASE(testMLPSolverDLVLib) 
01064 {
01065   testAll<ASPSolver::DLVLibSoftware::Configuration>();
01066 }
01067 #endif
01068  
01069 #ifdef HAVE_LIBCLINGO
01070 BOOST_AUTO_TEST_CASE(testMLPSolverClingo) 
01071 {
01072   testAll<ASPSolver::ClingoSoftware::Configuration>();
01073   //testInconsistentProgram<ASPSolver::ClingoSoftware::Configuration>();
01074   //testNoticStratifiedProgram<ASPSolver::ClingoSoftware::Configuration>();
01075   //testOneMainModules<ASPSolver::ClingoSoftware::Configuration>();
01076 }
01077 #endif
01078 
01079