dlvhex  2.5.0
testsuite/TestComponentGraph.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 // HAVE_CONFIG_H
00034 
00035 #include <boost/cstdint.hpp>
00036 #include "dlvhex2/ComponentGraph.h"
00037 #include "dlvhex2/DependencyGraph.h"
00038 #include "dlvhex2/HexParser.h"
00039 #include "dlvhex2/InputProvider.h"
00040 #include "dlvhex2/ProgramCtx.h"
00041 #include "dlvhex2/PluginInterface.h"
00042 
00043 #define BOOST_TEST_MODULE "TestComponentGraph"
00044 #include <boost/test/unit_test.hpp>
00045 
00046 #include "fixturesExt1.h"
00047 #include "fixturesMCS.h"
00048 #include "graphviz.h"
00049 
00050 #include <iostream>
00051 #include <fstream>
00052 #include <cstdlib>
00053 
00054 #define LOG_REGISTRY_PROGRAM(ctx) \
00055   LOG(INFO,*ctx.registry()); \
00056     RawPrinter printer(std::cerr, ctx.registry()); \
00057     std::cerr << "edb = " << *ctx.edb << std::endl; \
00058     LOG(INFO,"idb"); \
00059     printer.printmany(ctx.idb,"\n"); \
00060     std::cerr << std::endl; \
00061     LOG(INFO,"idb end");
00062 
00063 LOG_INIT(Logger::ERROR | Logger::WARNING)
00064 
00065 DLVHEX_NAMESPACE_USE
00066 
00067 BOOST_AUTO_TEST_CASE(testNonext) 
00068 {
00069   ProgramCtx ctx;
00070   ctx.setupRegistry(RegistryPtr(new Registry));
00071 
00072   std::stringstream ss;
00073   ss <<
00074     "a v f(X)." << std::endl <<
00075     "b :- f(a), not f(b)." << std::endl <<
00076     ":- f(b), not f(a)." << std::endl;
00077   InputProviderPtr ip(new InputProvider);
00078   ip->addStreamInput(ss, "testinput");
00079   ModuleHexParser parser;
00080   BOOST_REQUIRE_NO_THROW(parser.parse(ip, ctx));
00081 
00082     //LOG_REGISTRY_PROGRAM(ctx);
00083 
00084   DependencyGraph depgraph(ctx, ctx.registry());
00085     std::vector<ID> auxRules;
00086     depgraph.createDependencies(ctx.idb, auxRules);
00087 
00088     ComponentGraph compgraph(depgraph, ctx, ctx.registry());
00089 
00090   // TODO test dependencies (will do manually with graphviz at the moment)
00091 
00092   const char* fnamev = "testComponentGraphNonextVerbose.dot";
00093   LOG(INFO,"dumping verbose graph to " << fnamev);
00094   std::ofstream filev(fnamev);
00095   compgraph.writeGraphViz(filev, true);
00096   makeGraphVizPdf(fnamev);
00097 
00098   const char* fnamet = "testComponentGraphNonextTerse.dot";
00099   LOG(INFO,"dumping terse graph to " << fnamet);
00100   std::ofstream filet(fnamet);
00101   compgraph.writeGraphViz(filet, false);
00102   makeGraphVizPdf(fnamet);
00103 }
00104 
00105 BOOST_FIXTURE_TEST_CASE(testExt1, ProgramExt1ProgramCtxDependencyGraphFixture) 
00106 {
00107   LOG(INFO,"createing compgraph");
00108   ComponentGraph compgraph(depgraph, ctx, ctx.registry());
00109 
00110   // TODO test scc infos (will do manually with graphviz at the moment)
00111 
00112   const char* fnamev = "testComponentGraphExt1Verbose.dot";
00113   LOG(INFO,"dumping verbose graph to " << fnamev);
00114   std::ofstream filev(fnamev);
00115   compgraph.writeGraphViz(filev, true);
00116   makeGraphVizPdf(fnamev);
00117 
00118   const char* fnamet = "testComponentGraphExt1Terse.dot";
00119   LOG(INFO,"dumping terse graph to " << fnamet);
00120   std::ofstream filet(fnamet);
00121   compgraph.writeGraphViz(filet, false);
00122   makeGraphVizPdf(fnamet);
00123 
00124     #warning TODO create new testcases for EvalGraphBuilder instead of component collapsing tests
00125     #if 0
00126     // test collapsing (poor (wo)man's way)
00127     // [we trust on the order of components to stay the same!]
00128     {
00129         LOG(INFO,"components are ordered as follows:" << printrange(
00130                     boost::make_iterator_range(compgraph.getComponents())));
00131         typedef ComponentGraph::Component Component;
00132         ComponentGraph::ComponentIterator it, itend, itc0, itc1, itc2, itc3, itc4, itc5, itc6;
00133         boost::tie(it, itend) = compgraph.getComponents();
00134         itc0 = it; it++;
00135         itc1 = it; it++;
00136         itc2 = it; it++;
00137         itc3 = it; it++;
00138         itc4 = it; it++;
00139         itc5 = it; it++;
00140         itc6 = it; it++;
00141         assert(it == itend);
00142 
00143         std::set<Component> coll0;
00144         coll0.insert(*itc0);
00145         coll0.insert(*itc1);
00146         coll0.insert(*itc4);
00147 
00148         std::set<Component> coll1;
00149         coll1.insert(*itc2);
00150         coll1.insert(*itc5);
00151 
00152         std::set<Component> coll2;
00153         coll2.insert(*itc3);
00154         coll2.insert(*itc6);
00155 
00156         Component comp0 = compgraph.collapseComponents(coll0);
00157         LOG(INFO,"collapsing 0 yielded component " << comp0);
00158         Component comp1 = compgraph.collapseComponents(coll1);
00159         LOG(INFO,"collapsing 1 yielded component " << comp1);
00160         Component comp2 = compgraph.collapseComponents(coll2);
00161         LOG(INFO,"collapsing 2 yielded component " << comp2);
00162     }
00163     #endif
00164 
00165     // print final result
00166     {
00167         const char* fnamev = "testComponentGraphExt1CollapsedVerbose.dot";
00168         LOG(INFO,"dumping verbose graph to " << fnamev);
00169         std::ofstream filev(fnamev);
00170         compgraph.writeGraphViz(filev, true);
00171         makeGraphVizPdf(fnamev);
00172 
00173         const char* fnamet = "testComponentGraphExt1CollapsedTerse.dot";
00174         LOG(INFO,"dumping terse graph to " << fnamet);
00175         std::ofstream filet(fnamet);
00176         compgraph.writeGraphViz(filet, false);
00177         makeGraphVizPdf(fnamet);
00178     }
00179 }
00180 
00181 // example using MCS-IE encoding from KR 2010 for calculation of equilibria in medical example
00182 BOOST_FIXTURE_TEST_CASE(testMCSMedEQ,ProgramMCSMedEQProgramCtxDependencyGraphFixture) 
00183 {
00184     ComponentGraph compgraph(depgraph, ctx, ctx.registry());
00185 
00186   // TODO test scc infos (will do manually with graphviz at the moment)
00187 
00188   const char* fnamev = "testComponentGraphMCSMedEqVerbose.dot";
00189   LOG(INFO,"dumping verbose graph to " << fnamev);
00190   std::ofstream filev(fnamev);
00191   compgraph.writeGraphViz(filev, true);
00192   makeGraphVizPdf(fnamev);
00193 
00194   const char* fnamet = "testComponentGraphMCSMedEqTerse.dot";
00195   LOG(INFO,"dumping terse graph to " << fnamet);
00196   std::ofstream filet(fnamet);
00197   compgraph.writeGraphViz(filet, false);
00198   makeGraphVizPdf(fnamet);
00199 }
00200 
00201 // example using MCS-IE encoding from KR 2010 for calculation of diagnoses in medical example
00202 BOOST_FIXTURE_TEST_CASE(testMCSMedD,ProgramMCSMedEQProgramCtxDependencyGraphFixture) 
00203 {
00204     //LOG_REGISTRY_PROGRAM(ctx);
00205 
00206     ComponentGraph compgraph(depgraph, ctx, ctx.registry());
00207 
00208   // TODO test scc infos (will do manually with graphviz at the moment)
00209 
00210   const char* fnamev = "testComponentGraphMCSMedDVerbose.dot";
00211   LOG(INFO,"dumping verbose graph to " << fnamev);
00212   std::ofstream filev(fnamev);
00213   compgraph.writeGraphViz(filev, true);
00214   makeGraphVizPdf(fnamev);
00215 
00216   const char* fnamet = "testComponentGraphMCSMedDTerse.dot";
00217   LOG(INFO,"dumping terse graph to " << fnamet);
00218   std::ofstream filet(fnamet);
00219   compgraph.writeGraphViz(filet, false);
00220   makeGraphVizPdf(fnamet);
00221 }
00222 
00223 // TODO test SCCs containing extatoms