dlvhex  2.5.0
include/dlvhex2/Atoms.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 
00034 #ifndef ATOMS_HPP_INCLUDED__14102010
00035 #define ATOMS_HPP_INCLUDED__14102010
00036 
00037 #include "dlvhex2/PlatformDefinitions.h"
00038 #include "dlvhex2/fwd.h"
00039 #include "dlvhex2/Logger.h"
00040 #include "dlvhex2/ID.h"
00041 #include "dlvhex2/PredicateMask.h"
00042 #include "dlvhex2/ExtSourceProperties.h"
00043 
00044 #include <boost/shared_ptr.hpp>
00045 #include <boost/weak_ptr.hpp>
00046 
00047 #include <vector>
00048 #include <list>
00049 
00050 DLVHEX_NAMESPACE_BEGIN
00051 
00055 struct DLVHEX_EXPORT Atom
00056 {
00058     IDKind kind;
00059 
00064     Tuple tuple;
00065 
00066     // used for indices
00067     ID front() const { return tuple.front(); }
00068 
00069     protected:
00073         Atom(IDKind kind):
00074         kind(kind), tuple()
00075             { assert(ID(kind,0).isAtom()); }
00080         Atom(IDKind kind, const Tuple& tuple):
00081         kind(kind), tuple(tuple)
00082             { assert(ID(kind,0).isAtom()); }
00083 };
00084 
00085 // regarding strong negation:
00086 // during the parse process we do the following:
00087 // we convert strong negation -<foo> into <foo'> (careful with variables in <foo>!)
00088 // we add constraint :- <foo>, <foo'>.
00089 // we somehow mark the <foo'> as strongly negated helper s.t. output can correctly print results
00090 //
00091 // for the first implementation, we leave out strong negation alltogether (not parseable)
00095 struct DLVHEX_EXPORT OrdinaryAtom:
00096 public Atom,
00097 private ostream_printable<OrdinaryAtom>
00098 {
00111     std::string text;
00112 
00120     bool unifiesWith(const OrdinaryAtom& a) const;
00129     bool unifiesWith(const OrdinaryAtom& a, RegistryPtr reg) const;
00139     bool existsHomomorphism(RegistryPtr reg, const OrdinaryAtom& a) const;
00140 
00145     OrdinaryAtom(IDKind kind):
00146     Atom(kind), text()
00147         { assert(ID(kind,0).isOrdinaryAtom()); }
00153     OrdinaryAtom(IDKind kind, const std::string& text):
00154     Atom(kind), text(text)
00155         { assert(ID(kind,0).isOrdinaryAtom()); assert(!text.empty()); }
00162     OrdinaryAtom(IDKind kind, const std::string& text, const Tuple& tuple):
00163     Atom(kind, tuple), text(text) {
00164         assert(ID(kind,0).isOrdinaryAtom());
00165         assert(!text.empty());
00166     }
00172     std::ostream& print(std::ostream& o) const
00173         { return o << "OrdinaryAtom(" << std::hex << kind << std::dec << ",'" << text << "'," << printvector(tuple) << ")"; }
00174 
00175 };
00176 
00182 struct DLVHEX_EXPORT BuiltinAtom:
00183 public Atom,
00184 private ostream_printable<BuiltinAtom>
00185 {
00190     BuiltinAtom(IDKind kind):
00191     Atom(kind)
00192         { assert(ID(kind,0).isBuiltinAtom()); }
00198     BuiltinAtom(IDKind kind, const Tuple& tuple):
00199     Atom(kind, tuple)
00200         { assert(ID(kind,0).isBuiltinAtom()); }
00201     std::ostream& print(std::ostream& o) const
00202         { return o << "BuiltinAtom(" << printvector(tuple) << ")"; }
00203 };
00204 
00210 struct DLVHEX_EXPORT AggregateAtom:
00211 public Atom,
00212 private ostream_printable<AggregateAtom>
00213 {
00218     Tuple variables;
00223     Tuple literals;
00224 
00231     std::vector<Tuple> mvariables;
00232 
00239     std::vector<Tuple> mliterals;
00240 
00245     AggregateAtom(IDKind kind):
00246     Atom(kind, Tuple(5, ID_FAIL)),
00247         variables(), literals()
00248         { assert(ID(kind,0).isAggregateAtom()); }
00262     AggregateAtom(IDKind kind,
00263         const Tuple& tuple, const Tuple& variables, const Tuple& literals):
00264     Atom(kind, tuple), variables(variables), literals(literals) {
00265         assert(ID(kind,0).isAggregateAtom()); assert(tuple.size() == 5);
00266         assert(!variables.empty()); assert(!literals.empty());
00267     }
00281     AggregateAtom(IDKind kind,
00282         const Tuple& tuple, const std::vector<Tuple>& mvariables, const std::vector<Tuple>& mliterals):
00283     Atom(kind, tuple), mvariables(mvariables), mliterals(mliterals) {
00284         assert(ID(kind,0).isAggregateAtom()); assert(tuple.size() == 5);
00285         assert(!mvariables.empty()); assert(!mliterals.empty());
00286         assert(mvariables.size() == mliterals.size());
00287         for (int i = 0; i < mvariables.size(); ++i) { assert(!mvariables[i].empty() && !mliterals[i].empty() ); }
00288     }
00289     std::ostream& print(std::ostream& o) const
00290     {
00291         return o << "AggregateAtom(" << printvector(tuple) << " with vars " <<
00292             printvector(variables) << " and literals " << printvector(literals) << ")";
00293     }
00294 };
00295 
00301 struct DLVHEX_EXPORT ExternalAtom:
00302 public Atom,
00303 private ostream_printable<ExternalAtom>
00304 {
00306     ID predicate;
00307 
00309     Tuple inputs;
00310 
00311     // Atom::tuple is used for output terms
00312 
00321     mutable PluginAtom* pluginAtom;
00322 
00324     ID auxInputPredicate;
00325     typedef std::vector<std::list<unsigned> > AuxInputMapping;
00340     AuxInputMapping auxInputMapping;
00341 
00342     // auxiliary replacement predicate name is stored in pluginAtom!
00343 
00344     WARNING("inputMask seems to be duplicated in parts of ExternalAtomMask")
00357         mutable boost::shared_ptr<PredicateMask> inputMask;
00359     mutable boost::shared_ptr<PredicateMask> auxInputMask;
00360 
00367     mutable ExtSourceProperties prop;
00368 
00369     public:
00377         ExternalAtom(IDKind kind, ID predicate, const Tuple& inputs, const Tuple& outputs):
00378         Atom(kind, outputs),
00379             predicate(predicate),
00380             inputs(inputs),
00381             pluginAtom(),
00382             auxInputPredicate(ID_FAIL),
00383             inputMask(new PredicateMask),
00384             auxInputMask(new PredicateMask)
00385             { assert(ID(kind,0).isExternalAtom()); assert(predicate.isConstantTerm()); prop.ea = this; }
00390         ExternalAtom(IDKind kind):
00391         Atom(kind),
00392             predicate(ID_FAIL),
00393             inputs(),
00394             pluginAtom(),
00395             auxInputPredicate(ID_FAIL),
00396             inputMask(new PredicateMask),
00397             auxInputMask(new PredicateMask)
00398             { assert(ID(kind,0).isExternalAtom()); prop.ea = this; }
00399         ~ExternalAtom();
00400 
00405         ExternalAtom(const ExternalAtom& ea) : Atom(ea) {
00406             predicate = ea.predicate;
00407             inputs = ea.inputs;
00408             pluginAtom = ea.pluginAtom;
00409             auxInputPredicate = ea.auxInputPredicate;
00410             auxInputMapping = ea.auxInputMapping;
00411             inputMask = ea.inputMask;
00412             auxInputMask = ea.auxInputMask;
00413             prop = ea.prop;
00414             prop.ea = this;      // use the containing external atom
00415         }
00416 
00421         void operator=(const ExternalAtom& ea) {
00422             Atom::operator=(ea);
00423             predicate = ea.predicate;
00424             inputs = ea.inputs;
00425             pluginAtom = ea.pluginAtom;
00426             auxInputPredicate = ea.auxInputPredicate;
00427             auxInputMapping = ea.auxInputMapping;
00428             inputMask = ea.inputMask;
00429             auxInputMask = ea.auxInputMask;
00430             prop = ea.prop;
00431             prop.ea = this;      // use the containing external atom
00432         }
00433 
00434         const ExtSourceProperties& getExtSourceProperties() const;
00435 
00436         std::ostream& print(std::ostream& o) const;
00437 
00445         void updatePredicateInputMask() const;
00448         InterpretationConstPtr getPredicateInputMask() const
00449             { return inputMask->mask(); }
00452         InterpretationConstPtr getAuxInputMask() const
00453             { return auxInputMask->mask(); }
00454 };
00455 
00457 struct DLVHEX_EXPORT ModuleAtom:
00458 public Atom,
00459 private ostream_printable<ModuleAtom>
00460 {
00461 
00463     ID predicate;
00465     Tuple inputs;
00467     ID outputAtom;
00469     std::string actualModuleName;
00470     // Atom::tuple is used for output terms
00471 
00472     public:
00481         ModuleAtom(IDKind kind, ID predicate, const Tuple& inputs, ID outputAtom, std::string actualModuleName):
00482         Atom(kind),
00483             predicate(predicate),
00484             inputs(inputs),
00485             outputAtom(outputAtom),
00486             actualModuleName(actualModuleName)
00487             { }
00488 
00493         ModuleAtom(IDKind kind):
00494         Atom(kind),
00495             predicate(ID_FAIL),
00496             inputs(),
00497             outputAtom(ID_FAIL),
00498             actualModuleName("")
00499             { }
00500 
00501         std::ostream& print(std::ostream& o) const;
00502 
00503 };
00504 
00505 // to prefixed atom (predicate name of the atom)
00506 const std::string MODULEPREFIXSEPARATOR="__";
00507 const std::string MODULEINSTSEPARATOR="___";
00508 
00509 DLVHEX_NAMESPACE_END
00510 #endif                           // ATOMS_HPP_INCLUDED__14102010
00511 
00512 // vim:expandtab:ts=4:sw=4:
00513 // mode: C++
00514 // End: