dlvhex  2.5.0
include/dlvhex2/ComfortPluginInterface.h
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005-2007 Roman Schindlauer
00003  * Copyright (C) 2006-2015 Thomas Krennwallner
00004  * Copyright (C) 2009-2016 Peter Schüller
00005  * Copyright (C) 2011-2016 Christoph Redl
00006  * Copyright (C) 2015-2016 Tobias Kaminski
00007  * Copyright (C) 2015-2016 Antonius Weinzierl
00008  *
00009  * This file is part of dlvhex.
00010  *
00011  * dlvhex is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU Lesser General Public License as
00013  * published by the Free Software Foundation; either version 2.1 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * dlvhex is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with dlvhex; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00063 #ifndef COMFORT_PLUGIN_INTERFACE_HPP_INCLUDED_19012011
00064 #define COMFORT_PLUGIN_INTERFACE_HPP_INCLUDED_19012011
00065 
00066 #include "dlvhex2/PlatformDefinitions.h"
00067 #include "dlvhex2/PluginInterface.h"
00068 #include <boost/foreach.hpp>
00069 
00070 #include <cctype>
00071 
00072 DLVHEX_NAMESPACE_BEGIN
00073 
00082 struct DLVHEX_EXPORT ComfortTerm:
00083 public ostream_printable<ComfortTerm>
00084 {
00085     enum Type
00086     {
00088         STR,
00090         INT
00091     };
00092 
00098     Type type;
00099 
00105     std::string strval;
00106 
00112     int intval;
00113 
00118     bool isConstant() const
00119         { return (type == STR) && (!isupper(strval[0])); }
00120 
00125     bool isVariable() const
00126         { return (type == STR) && (isupper(strval[0])); }
00127 
00132     bool isInteger() const
00133         { return type == INT; }
00138     bool isAnon() const
00139         { return type == STR && strval == "_"; }
00140 
00146     static ComfortTerm createVariable(const std::string& s)
00147         { assert(!s.empty() && isupper(s[0])); return ComfortTerm(STR, s, 0); }
00148 
00154     static ComfortTerm createConstant(const std::string& s)
00155         { assert(!s.empty() && !isupper(s[0])); return ComfortTerm(STR, s, 0); }
00156 
00162     static ComfortTerm createInteger(int i)
00163         { return ComfortTerm(INT, "", i); }
00164 
00170     inline bool operator==(const ComfortTerm& other) const
00171     {
00172         return
00173             (type == other.type) &&
00174             (type == STR || intval == other.intval) &&
00175             (type == INT || strval == other.strval);
00176     }
00177 
00183     inline bool operator!=(const ComfortTerm& other) const
00184         { return !operator==(other); }
00185 
00193     inline bool operator<(const ComfortTerm& other) const
00194     {
00195         return
00196             (type < other.type) ||
00197             (type == STR && other.type == STR && strval < other.strval) ||
00198             (type == INT && other.type == INT && intval < other.intval);
00199     }
00200 
00208     std::ostream& print(std::ostream& o) const;
00209 
00210     protected:
00219         ComfortTerm(Type type, const std::string& strval, int intval):
00220         type(type), strval(strval), intval(intval) {}
00221 
00222     public:
00224         ComfortTerm() : type(STR), strval("") {}
00225 
00228         ComfortTerm(int intval) : type(INT), intval(intval) {
00229         }
00230 
00234         ComfortTerm(std::string strval, bool addQuotes = false) : type(STR) {
00235             if (addQuotes && (strval.length() == 0 || strval[0] != '\"' || strval[strval.length() - 1] != '\"')) this->strval = std::string("\"") + strval + std::string("\"");
00236             else this->strval = strval;
00237         }
00238 
00241         std::string getUnquotedString() const
00242         {
00243             if (strval.length() > 1 && strval[0] == '\"' && strval[strval.length() - 1] == '\"')
00244                 return strval.substr(1, strval.length() - 2);
00245             else
00246                 return strval;
00247         }
00248 
00251         std::string getString() const
00252         {
00253             return strval;
00254         }
00255 
00258         std::string getVariable() const
00259         {
00260             return strval;
00261         }
00262 };
00263 
00267 typedef std::vector<ComfortTerm> ComfortTuple;
00268 
00280 struct DLVHEX_EXPORT ComfortAtom:
00281 public ostream_printable<ComfortAtom>
00282 {
00288     ComfortTuple tuple;
00289 
00294     inline const std::string& toString() const
00295         { if( strval.empty() ) calculateStrVal(); return strval; }
00296 
00304     bool operator<(const ComfortAtom& other) const
00305         { return tuple < other.tuple; }
00306 
00314     std::ostream& print(std::ostream& o) const;
00315 
00320     inline const std::string& getPredicate() const
00321     {
00322         assert(!tuple.empty() && !tuple.front().isInteger());
00323         return tuple.front().strval;
00324     }
00325 
00330     inline const ComfortTuple getArguments() const
00331     {
00332         ComfortTuple ct = tuple;
00333         assert(ct.size() > 0);
00334         ct.erase(ct.begin());
00335         return ct;
00336     }
00337 
00343     inline const ComfortTerm getArgument(int index) const
00344     {
00345         assert(index >= 0 && index < (int)tuple.size());
00346         return tuple[index];
00347     }
00348 
00351     inline unsigned getArity() const
00352     {
00353         return tuple.size() - 1;
00354     }
00355 
00358     inline unsigned isStrongNegated() const
00359     {
00360         assert(!tuple.empty() && !tuple.front().isInteger());
00361         assert(!tuple[0].strval.length() == 0);
00362         return tuple[0].strval[0] == '-';
00363     }
00364 
00368     inline void setArgument(int index, ComfortTerm arg) {
00369         assert(index >= 0 && index < (int)tuple.size());
00370         tuple[index] = arg;
00371     }
00372 
00375     inline void setArguments(ComfortTuple args) {
00376         assert(tuple.size() > 0);
00377         ComfortTerm pred = tuple[0];
00378         tuple.clear();
00379         tuple.push_back(pred);
00380         BOOST_FOREACH (ComfortTerm arg, args) {
00381             tuple.push_back(arg);
00382         }
00383     }
00384 
00386     inline ComfortAtom(){}
00387 
00392     inline ComfortAtom(ComfortTerm pred, ComfortTuple args, bool stronglyNegated = false) {
00393         tuple.push_back(pred);
00394         BOOST_FOREACH (ComfortTerm arg, args) {
00395             tuple.push_back(arg);
00396         }
00397     }
00398 
00399     // TODO implement setArgument, setArguments, setPredicate, getArguments, getArgument, getArity, isStrongNegated
00400 
00401     // TODO it might be useful to also implement setArgument, setArguments, setPredicate, getArguments, getArgument, getArity
00402 
00408     bool unifiesWith(const ComfortAtom& other) const;
00409 
00410     protected:
00412         mutable std::string strval;
00414         void calculateStrVal() const;
00415 };
00416 
00417 // you can stream out ComfortAtom objects, e.g., for debugging
00418 struct ComfortLiteral:
00419 public ostream_printable<ComfortLiteral>
00420 {
00421     public:
00424         inline const std::string& toString() const
00425             { if( strval.empty() ) calculateStrVal(); return strval; }
00426 
00427     protected:
00429         mutable std::string strval;
00431         void calculateStrVal() const;
00432 };
00433 
00434 // this mimicks the old AtomSet
00435 // you can stream out ComfortInterpretation objects, e.g., for debugging
00436 
00444 struct ComfortInterpretation;
00445 struct DLVHEX_EXPORT ComfortInterpretation:
00446 public std::set<ComfortAtom>,
00447 public ostream_printable<ComfortInterpretation>
00448 {
00453     void insert(const ComfortAtom& a);
00454 
00459     void insert(const ComfortInterpretation& i);
00460 
00465     void remove(const std::set<std::string>& predicates);
00466 
00472     void keep(const std::set<std::string>& predicates);
00473 
00480     void matchPredicate(
00481         const std::string& predicate,
00482         ComfortInterpretation& destination) const;
00483 
00490     void matchAtom(
00491         const ComfortAtom& atom,
00492         ComfortInterpretation& destination) const;
00493 
00499     ComfortInterpretation difference(const ComfortInterpretation& subtractThis) const;
00500 
00508     std::ostream& print(std::ostream& o) const;
00509 
00515     bool operator==(const ComfortInterpretation& c2) const;
00516 };
00517 
00524 class DLVHEX_EXPORT ComfortPluginAtom:
00525 public PluginAtom
00526 {
00527     public:
00544         struct ComfortQuery
00545         {
00547             ComfortInterpretation interpretation;
00549             ComfortTuple input;
00551             ComfortTuple pattern;
00552         };
00553 
00561         typedef std::set<ComfortTuple>
00562             ComfortAnswer;
00563 
00573         ComfortPluginAtom(const std::string& predicate, bool monotonic=false):
00574         PluginAtom(predicate, monotonic) {}
00575 
00579         virtual ~ComfortPluginAtom() {}
00580 
00594         virtual void retrieve(const ComfortQuery& q, ComfortAnswer& a) = 0;
00595 
00596     protected:
00607         virtual void retrieve(const Query& q, Answer& a);
00608 };
00609 
00610 DLVHEX_NAMESPACE_END
00611 #endif                           // COMFORT_PLUGIN_INTERFACE_HPP_INCLUDED_19012011
00612 
00613 
00614 // vim:expandtab:ts=4:sw=4:
00615 // mode: C++
00616 // End: