2020-02-01 17:20:43 +01:00
|
|
|
mod head_type;
|
2020-01-31 17:24:13 +01:00
|
|
|
mod translate_body;
|
2020-01-31 17:19:44 +01:00
|
|
|
|
2020-02-01 17:20:43 +01:00
|
|
|
use head_type::*;
|
|
|
|
use translate_body::*;
|
2020-01-25 12:55:23 +01:00
|
|
|
|
2020-02-01 19:20:46 +01:00
|
|
|
pub struct ScopedFormula
|
|
|
|
{
|
|
|
|
free_variable_declarations: foliage::VariableDeclarations,
|
|
|
|
formula: foliage::Formula,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Definitions
|
|
|
|
{
|
2020-02-01 21:59:36 +01:00
|
|
|
head_atom_parameters: std::rc::Rc<foliage::VariableDeclarations>,
|
2020-02-01 19:20:46 +01:00
|
|
|
definitions: Vec<ScopedFormula>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(rule: &clingo::ast::Rule) -> Result<(), crate::Error>
|
2020-01-31 17:19:44 +01:00
|
|
|
{
|
2020-02-01 21:59:36 +01:00
|
|
|
use super::common::FindOrCreatePredicateDeclaration;
|
|
|
|
|
2020-02-01 19:20:46 +01:00
|
|
|
let mut function_declarations = foliage::FunctionDeclarations::new();
|
|
|
|
let mut predicate_declarations = foliage::PredicateDeclarations::new();
|
|
|
|
let mut variable_declaration_stack = foliage::VariableDeclarationStack::new();
|
2020-02-01 15:32:41 +01:00
|
|
|
|
2020-02-01 17:13:43 +01:00
|
|
|
let head_type = determine_head_type(rule.head(),
|
2020-02-01 21:59:36 +01:00
|
|
|
|name, arity| predicate_declarations.find_or_create(name, arity))?;
|
2020-02-01 19:20:46 +01:00
|
|
|
|
|
|
|
let mut definitions
|
|
|
|
= std::collections::BTreeMap::<std::rc::Rc<foliage::PredicateDeclaration>, Definitions>::new();
|
|
|
|
|
|
|
|
let declare_predicate_parameters = |predicate_declaration: &foliage::PredicateDeclaration|
|
|
|
|
{
|
2020-02-01 21:59:36 +01:00
|
|
|
std::rc::Rc::new((0..predicate_declaration.arity)
|
2020-02-01 19:20:46 +01:00
|
|
|
.map(|_| std::rc::Rc::new(foliage::VariableDeclaration
|
|
|
|
{
|
|
|
|
name: "<anonymous>".to_string(),
|
|
|
|
}))
|
2020-02-01 21:59:36 +01:00
|
|
|
.collect())
|
2020-02-01 19:20:46 +01:00
|
|
|
};
|
2020-02-01 15:32:41 +01:00
|
|
|
|
2020-02-01 17:13:43 +01:00
|
|
|
match head_type
|
2020-02-01 15:32:41 +01:00
|
|
|
{
|
2020-02-01 19:20:46 +01:00
|
|
|
HeadType::SingleAtom(head_atom) =>
|
|
|
|
{
|
|
|
|
if !definitions.contains_key(&head_atom.predicate_declaration)
|
|
|
|
{
|
|
|
|
definitions.insert(std::rc::Rc::clone(&head_atom.predicate_declaration),
|
|
|
|
Definitions
|
|
|
|
{
|
|
|
|
head_atom_parameters: declare_predicate_parameters(&head_atom.predicate_declaration),
|
|
|
|
definitions: vec![],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let definitions = definitions.get(&head_atom.predicate_declaration).unwrap();
|
2020-02-01 21:59:36 +01:00
|
|
|
|
|
|
|
variable_declaration_stack.push(std::rc::Rc::clone(&definitions.head_atom_parameters));
|
|
|
|
|
|
|
|
let translated_body = translate_body(rule.body(), &mut function_declarations,
|
|
|
|
&mut predicate_declarations, &mut variable_declaration_stack);
|
|
|
|
|
|
|
|
variable_declaration_stack.pop();
|
2020-02-01 19:20:46 +01:00
|
|
|
},
|
2020-02-01 21:59:36 +01:00
|
|
|
HeadType::ChoiceWithSingleAtom(_) =>
|
2020-02-01 17:13:43 +01:00
|
|
|
log::debug!("translating choice rule with single atom"),
|
2020-02-01 17:20:43 +01:00
|
|
|
HeadType::IntegrityConstraint =>
|
2020-02-01 17:13:43 +01:00
|
|
|
log::debug!("translating integrity constraint"),
|
2020-02-01 17:20:43 +01:00
|
|
|
HeadType::Trivial =>
|
2020-02-01 17:13:43 +01:00
|
|
|
{
|
|
|
|
log::debug!("skipping trivial rule");
|
|
|
|
return Ok(());
|
|
|
|
},
|
2020-02-01 15:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-01-24 13:32:43 +01:00
|
|
|
}
|