clasp_app.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2012, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #ifndef CLASP_CLASP_APP_H_INCLUDED
00021 #define CLASP_CLASP_APP_H_INCLUDED
00022 
00023 #ifdef _MSC_VER
00024 #pragma warning (disable : 4200) // nonstandard extension used : zero-sized array
00025 #pragma once
00026 #endif
00027 #include <program_opts/typed_value.h>
00028 #include <program_opts/application.h>
00029 #include <clasp/util/timer.h>
00030 #include <clasp/cli/clasp_options.h>
00031 #include <clasp/cli/clasp_output.h>
00032 #include <string>
00033 #include <vector>
00034 #include <iosfwd>
00035 #include <memory>
00036 namespace Clasp { namespace Cli {
00038 // clasp exit codes
00040 enum ExitCode {
00041         E_UNKNOWN   = 0,  
00042         E_INTERRUPT = 1,  
00043         E_SAT       = 10, 
00044         E_EXHAUST   = 20, 
00045         E_MEMORY    = 33, 
00046         E_ERROR     = 65, 
00047         E_NO_RUN    = 128 
00048 };
00050 // clasp app helpers
00052 class WriteLemmas {
00053 public:
00054         WriteLemmas(std::ostream& os);
00055         ~WriteLemmas();
00056         void attach(SharedContext& ctx);
00057         void detach();
00058         void flush(Constraint_t::Set types, uint32 maxLbd);
00059         bool unary(Literal, Literal) const;
00060         bool binary(Literal, Literal, Literal) const;
00061 private:
00062         WriteLemmas& operator=(const WriteLemmas&);
00063         SharedContext* ctx_;
00064         std::ostream&  os_;
00065         mutable uint32 outShort_;
00066 };
00067 class WriteCnf {
00068 public:
00069         WriteCnf(std::ostream& os) : os_(os) {}
00070         void writeHeader(uint32 numVars, uint32 numCons);
00071         void write(Var maxVar, const ShortImplicationsGraph& g);
00072         void write(ClauseHead* h);
00073         void write(Literal unit);
00074         void close();
00075         bool unary(Literal, Literal) const;
00076         bool binary(Literal, Literal, Literal) const;
00077 private:
00078         WriteCnf& operator=(const WriteCnf&);
00079         std::ostream& os_;
00080         LitVec        lits_;
00081 };
00083 // clasp specific application options
00085 struct ClaspAppOptions {
00086         ClaspAppOptions();
00087         typedef std::vector<std::string>  StringSeq;
00088         static bool mappedOpts(ClaspAppOptions*, const std::string&, const std::string&);
00089         void initOptions(ProgramOptions::OptionContext& root);
00090         bool validateOptions(const ProgramOptions::ParsedOptions& parsed);
00091         StringSeq   input;     // list of input files - only first used!
00092         std::string lemmaOut;  // optional file name for writing learnt lemmas
00093         std::string lemmaIn;   // optional file name for reading learnt lemmas
00094         std::string hccOut;    // optional file name for writing scc programs
00095         std::string outAtom;   // optional format string for atoms
00096         uint32      outf;      // output format
00097         char        ifs;       // output field separator
00098         bool        hideAux;   // output aux atoms?
00099         uint8       quiet[3];  // configure printing of models, optimization values, and call steps
00100         bool        onlyPre;   // run preprocessor and exit
00101         bool        printPort; // print portfolio and exit
00102         uint8       outLbd;    // optional lbd limit for lemma out
00103         uint8       inLbd;     // optional lbd for lemma in
00104         enum OutputFormat { out_def = 0, out_comp = 1, out_json = 2, out_none = 3 };
00105 };
00107 // clasp application base
00109 // Base class for applications using the clasp library.
00110 class ClaspAppBase : public ProgramOptions::Application, public Clasp::EventHandler {
00111 public:
00112         typedef ClaspFacade::Summary  RunSummary;
00113         typedef ProgramOptions::PosOption PosOption;
00114 protected:
00115         using ProgramOptions::Application::run;
00116         ClaspAppBase();
00117         ~ClaspAppBase();
00118         // -------------------------------------------------------------------------------------------
00119         // Functions to be implemented by subclasses
00120         virtual ProblemType   getProblemType()             = 0;
00121         virtual void          run(ClaspFacade& clasp)      = 0;
00122         virtual Output*       createOutput(ProblemType f);
00123         virtual void          storeCommandArgs(const ProgramOptions::ParsedValues& values);
00124         // -------------------------------------------------------------------------------------------
00125         // Helper functions that subclasses should call during run
00126         bool handlePostGroundOptions(ProgramBuilder& prg);
00127         bool handlePreSolveOptions(ClaspFacade& clasp);
00128         // -------------------------------------------------------------------------------------------
00129         // Application functions
00130         virtual const int*  getSignals()    const;
00131         virtual HelpOpt     getHelpOption() const { return HelpOpt("Print {1=basic|2=more|3=full} help and exit", 3); }
00132         virtual PosOption   getPositional() const { return parsePositional; }
00133         virtual void        initOptions(ProgramOptions::OptionContext& root);
00134         virtual void        validateOptions(const ProgramOptions::OptionContext& root, const ProgramOptions::ParsedOptions& parsed, const ProgramOptions::ParsedValues& values);
00135         virtual void        setup();
00136         virtual void        run();
00137         virtual void        shutdown();
00138         virtual bool        onSignal(int);
00139         virtual void        printHelp(const ProgramOptions::OptionContext& root);
00140         virtual void        printVersion();
00141         static  bool        parsePositional(const std::string& s, std::string& out);
00142         // -------------------------------------------------------------------------------------------
00143         // Event handler
00144         virtual void onEvent(const Event& ev);
00145         virtual bool onModel(const Solver& s, const Model& m);
00146         // -------------------------------------------------------------------------------------------
00147         // Status information & output
00148         int  exitCode(const RunSummary& sol)    const;
00149         void printTemplate()                    const;
00150         void printDefaultConfigs()              const;
00151         void printLibClaspVersion()             const;
00152         std::istream&   getStream();
00153         // -------------------------------------------------------------------------------------------  
00154         // Functions called in handlePreSolveOptions()
00155         void readLemmas(SharedContext& ctx);
00156         void writeNonHcfs(const SharedDependencyGraph& graph) const;
00157         typedef SingleOwnerPtr<Output>              OutPtr;
00158         typedef SingleOwnerPtr<ClaspFacade>         ClaspPtr;
00159         ClaspCliConfig  claspConfig_;
00160         ClaspAppOptions claspAppOpts_;
00161         ClaspPtr        clasp_;
00162         OutPtr          out_;
00163 };
00165 // clasp application
00167 // Standalone clasp application.
00168 class ClaspApp : public ClaspAppBase {
00169 public:
00170         ClaspApp();
00171         const char* getName()       const { return "clasp"; }
00172         const char* getVersion()    const { return CLASP_VERSION; }
00173         const char* getUsage()      const { return "[number] [options] [file]"; }
00174 protected:
00175         virtual ProblemType getProblemType();
00176         virtual void        run(ClaspFacade& clasp);
00177         virtual void        printHelp(const ProgramOptions::OptionContext& root);
00178 private:
00179         ClaspApp(const ClaspApp&);
00180         ClaspApp& operator=(const ClaspApp&);
00181 };
00182 }}
00183 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:38