Skip to content

Commit

Permalink
add support to load multiple slices of aspif
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaminsk committed Dec 10, 2024
1 parent 8c178c0 commit 3733f33
Show file tree
Hide file tree
Showing 12 changed files with 3,050 additions and 2,818 deletions.
2,877 changes: 1,471 additions & 1,406 deletions app/pyclingo/_clingo.c

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions libclingo/clingo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,20 @@ CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control);
//! - ::clingo_error_runtime if parsing or checking fails
CLINGO_VISIBILITY_DEFAULT bool clingo_control_load(clingo_control_t *control, char const *file);

//! Load files in aspif format.
//!
//! This function should be called on an empty control object.
//!
//! If more than one file is given, they are merged into one file. Only the first one should have a preamble.
//!
//! @param[in] control the target
//! @param[in] files the array of files to load
//! @param[in] size the size of the array
//! @return whether the call was successful; might set one of the following error codes:
//! - ::clingo_error_bad_alloc
//! - ::clingo_error_runtime if parsing or checking fails
CLINGO_VISIBILITY_DEFAULT bool clingo_control_load_aspif(clingo_control_t *ctl, char const **files, size_t size);

//! Extend the logic program with the given non-ground logic program in string form.
//!
//! This function puts the given program into a block of form: <tt>\#program name(parameters).</tt>
Expand Down
1 change: 1 addition & 0 deletions libclingo/clingo/clingocontrol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public:
void ground(Control::GroundVec const &vec, Context *ctx) override;
void add(std::string const &name, Gringo::StringVec const &params, std::string const &part) override;
void load(std::string const &filename) override;
void load_aspif(Potassco::Span<char const *> files) override;
bool blocked() override;
std::string str();
void assignExternal(Potassco::Atom_t ext, Potassco::Value_t) override;
Expand Down
1 change: 1 addition & 0 deletions libclingo/clingo/control.hh
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ struct clingo_control {
virtual void *claspFacade() = 0;
virtual void add(std::string const &name, Gringo::StringVec const &params, std::string const &part) = 0;
virtual void load(std::string const &filename) = 0;
virtual void load_aspif(Potassco::Span<char const *> files) = 0;
virtual Gringo::Symbol getConst(std::string const &name) const = 0;
virtual bool blocked() = 0;
virtual void assignExternal(Potassco::Atom_t ext, Potassco::Value_t val) = 0;
Expand Down
13 changes: 13 additions & 0 deletions libclingo/src/clingocontrol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ void ClingoControl::load(std::string const &filename) {
parser_->pushFile(std::string(filename), logger_);
parse();
}
void ClingoControl::load_aspif(Potassco::Span<char const*> files) {
using std::begin;
using std::end;
for (auto it = end(files), ib = begin(files); it != ib; --it) {
parser_->pushFile(std::string{*(it - 1)}, logger_);
}
if (!parser_->empty()) {
parser_->parse_aspif(logger_);
}
if (logger_.hasError()) {
throw std::runtime_error("parsing failed");
}
}
bool ClingoControl::hasSubKey(unsigned key, char const *name) const {
unsigned subkey = claspConfig_.getKey(key, name);
return subkey != Clasp::Cli::ClaspCliConfig::KEY_INVALID;
Expand Down
5 changes: 5 additions & 0 deletions libclingo/src/control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,11 @@ extern "C" bool clingo_control_load(clingo_control_t *ctl, char const *file) {
GRINGO_CLINGO_CATCH;
}

extern "C" bool clingo_control_load_aspif(clingo_control_t *ctl, char const **files, size_t size) {
GRINGO_CLINGO_TRY { ctl->load_aspif(Potassco::Span<char const *>{files, size}); }
GRINGO_CLINGO_CATCH;
}

extern "C" bool clingo_control_set_enable_enumeration_assumption(clingo_control_t *ctl, bool value) {
GRINGO_CLINGO_TRY { ctl->useEnumAssumption(value); }
GRINGO_CLINGO_CATCH;
Expand Down
11 changes: 11 additions & 0 deletions libclingo/src/gringo_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ struct IncrementalControl : Control, private Output::ASPIFOutBackend {
parser.pushFile(std::string(filename), logger_);
parse();
}
void load_aspif(Potassco::Span<char const *> files) override {
for (auto it = end(files), ib = begin(files); it != ib; --it) {
parser.pushFile(std::string{*(it - 1)}, logger_);
}
if (!parser.empty()) {
parser.parse_aspif(logger_);
}
if (logger_.hasError()) {
throw std::runtime_error("parsing failed");
}
}
bool blocked() override { return false; }
USolveFuture solve(Assumptions ass, clingo_solve_mode_bitset_t, USolveEventHandler cb) override {
update();
Expand Down
4 changes: 3 additions & 1 deletion libgringo/gringo/input/nongroundparser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public:
int lex(void *pValue, Location &loc);
bool parseDefine(std::string const &define, Logger &log);
ParseResult parse(Logger &log);
void parse_aspif(Logger &log);
bool empty() { return LexerState::empty(); }
void include(String file, Location const &loc, bool inbuilt, Logger &log);
void theoryLexing(TheoryLexing mode);
Expand Down Expand Up @@ -106,6 +107,7 @@ private:
Condition condition() const;
String filename() const;

void aspif_stms_(Location &loc);
Symbol aspif_symbol_(Location &loc);
std::vector<Potassco::Id_t> aspif_ids_(Location &loc);
std::vector<Potassco::Atom_t> aspif_atoms_(Location &loc);
Expand All @@ -131,7 +133,7 @@ private:
void aspif_edge_(Location &loc);
void aspif_theory_(Location &loc);
void aspif_comment_(Location &loc);

void aspif_asp_(Location &loc);

std::set<std::string> filenames_;
bool &incmode_;
Expand Down
11 changes: 11 additions & 0 deletions libgringo/src/input/nongroundlexer.xch
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ uint32_t NonGroundParser::aspif_unsigned_(Location &loc) {

}

void NonGroundParser::aspif_asp_(Location &loc) {
start(loc);
char yych;
/*!re2c
<aspif> "asp" { return; }
<aspif> NL { aspif_error_(loc, format("expected 'asp' but got ", eof() ? "<EOF>" : "<EOL>").c_str()); }
<aspif> WS { aspif_error_(loc, "expected 'asp' but got <SPACE>"); }
<aspif> NOWSNL+ { aspif_error_(loc, format("expected 'asp' but got token ", string()).c_str()); }
*/
}

} } // namespace Input Gringo

#undef YYCTYPE
Expand Down
34 changes: 29 additions & 5 deletions libgringo/src/input/nongroundparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,32 @@ std::vector<Potassco::WeightLit_t> NonGroundParser::aspif_wlits_(Location &loc)
return wlits;
}

void NonGroundParser::aspif_(Location &loc) {
aspif_preamble_(loc);
bck_.beginStep();
void NonGroundParser::parse_aspif(Logger &log) {
if (!empty()) {
log_ = &log;
condition_ = yycaspif;
auto loc = Location(filename(), 1, 1, filename(), 1, 1);
aspif_asp_(loc);
aspif_preamble_(loc);
bck_.beginStep();
do {
aspif_stms_(loc);
pop();
}
while (!empty());
bck_.endStep();
filenames_.clear();
disable_aspif();
}
}

void NonGroundParser::aspif_stms_(Location &loc) {
for (;;) {
auto stm_type = aspif_unsigned_(loc);
switch (stm_type) {
case 0: {
aspif_nl_(loc);
bck_.endStep();
start(loc);
condition(yycnormal);
return;
}
case 1: { aspif_rule_(loc); break; }
Expand All @@ -559,6 +574,15 @@ void NonGroundParser::aspif_(Location &loc) {
}
}

void NonGroundParser::aspif_(Location &loc) {
aspif_preamble_(loc);
bck_.beginStep();
aspif_stms_(loc);
bck_.endStep();
condition(yycnormal);
return;
}

void NonGroundParser::aspif_rule_(Location &loc) {
aspif_ws_(loc);
auto stm_type = Potassco::Head_t(aspif_unsigned_(loc));
Expand Down
Loading

0 comments on commit 3733f33

Please sign in to comment.