souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions
souffle::ast::transform::ProvenanceTransformer Class Reference

Transformation pass to add provenance information. More...

#include <Provenance.h>

Inheritance diagram for souffle::ast::transform::ProvenanceTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::ProvenanceTransformer:
Collaboration graph

Public Member Functions

ProvenanceTransformerclone () const override
 
std::string getName () const override
 
- Public Member Functions inherited from souffle::ast::transform::Transformer
bool apply (TranslationUnit &translationUnit)
 
virtual ~Transformer ()=default
 

Private Member Functions

bool transform (TranslationUnit &translationUnit) override
 
bool transformMaxHeight (TranslationUnit &translationUnit)
 

Detailed Description

Transformation pass to add provenance information.

Definition at line 34 of file Provenance.h.

Member Function Documentation

◆ clone()

ProvenanceTransformer* souffle::ast::transform::ProvenanceTransformer::clone ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 46 of file Provenance.h.

◆ getName()

std::string souffle::ast::transform::ProvenanceTransformer::getName ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 42 of file Provenance.h.

◆ transform()

bool souffle::ast::transform::ProvenanceTransformer::transform ( TranslationUnit translationUnit)
overrideprivatevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 355 of file Provenance.cpp.

◆ transformMaxHeight()

bool souffle::ast::transform::ProvenanceTransformer::transformMaxHeight ( TranslationUnit translationUnit)
private

Definition at line 273 of file Provenance.cpp.

277  : program.getRelations()) {
278  // generate info relations for each clause
279  // do this before all other transformations so that we record
280  // the original rule without any instrumentation
281  for (auto clause : getClauses(program, *relation)) {
282  if (!isFact(*clause)) {
283  // add info relation
284  program.addRelation(
285  makeInfoRelation(*clause, getClauseNum(&program, clause), translationUnit));
286  }
287  }
288 
289  relation->addAttribute(mk<Attribute>(std::string("@rule_number"), QualifiedName("number")));
290  relation->addAttribute(mk<Attribute>(std::string("@level_number"), QualifiedName("number")));
291 
292  for (auto clause : getClauses(program, *relation)) {
293  size_t clauseNum = getClauseNum(&program, clause);
294 
295  // mapper to add two provenance columns to atoms
296  struct M : public NodeMapper {
297  using NodeMapper::operator();
298 
299  Own<Node> operator()(Own<Node> node) const override {
300  // add provenance columns
301  if (auto atom = dynamic_cast<Atom*>(node.get())) {
302  atom->addArgument(mk<UnnamedVariable>());
303  atom->addArgument(mk<UnnamedVariable>());
304  } else if (auto neg = dynamic_cast<Negation*>(node.get())) {
305  auto atom = neg->getAtom();
306  atom->addArgument(mk<UnnamedVariable>());
307  atom->addArgument(mk<UnnamedVariable>());
308  }
309 
310  // otherwise - apply mapper recursively
311  node->apply(*this);
312  return node;
313  }
314  };
315 
316  // add unnamed vars to each atom nested in arguments of head
317  clause->getHead()->apply(M());
318 
319  // if fact, level number is 0
320  if (isFact(*clause)) {
321  clause->getHead()->addArgument(mk<NumericConstant>(0));
322  clause->getHead()->addArgument(mk<NumericConstant>(0));
323  } else {
324  std::vector<Argument*> bodyLevels;
325 
326  for (size_t i = 0; i < clause->getBodyLiterals().size(); i++) {
327  auto lit = clause->getBodyLiterals()[i];
328 
329  // add unnamed vars to each atom nested in arguments of lit
330  lit->apply(M());
331 
332  // add two provenance columns to lit; first is rule num, second is level num
333  if (auto atom = dynamic_cast<Atom*>(lit)) {
334  atom->addArgument(mk<UnnamedVariable>());
335  atom->addArgument(mk<ast::Variable>("@level_num_" + std::to_string(i)));
336  bodyLevels.push_back(new ast::Variable("@level_num_" + std::to_string(i)));
337  }
338  }
339 
340  // add two provenance columns to head lit
341  clause->getHead()->addArgument(mk<NumericConstant>(clauseNum));
342  clause->getHead()->addArgument(getNextLevelNumber(bodyLevels));
343  }
344  }
345  }
346  return true;
347 }
348 
349 bool ProvenanceTransformer::transform(TranslationUnit& translationUnit) {
350  return ProvenanceTransformer::transformMaxHeight(translationUnit);
351 }
352 
353 } // namespace souffle::ast::transform

The documentation for this class was generated from the following files:
TCB_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.h:198
souffle::ast::transform::ProvenanceTransformer::transformMaxHeight
bool transformMaxHeight(TranslationUnit &translationUnit)
Definition: Provenance.cpp:273
souffle::ast::getClauseNum
size_t getClauseNum(const Program *program, const Clause *clause)
Returns the index of a clause within its relation, ignoring facts.
Definition: Utils.cpp:150
relation
Relation & relation
Definition: Reader.h:130
souffle::ast::isFact
bool isFact(const Clause &clause)
Returns whether the given clause is a fact.
Definition: Utils.cpp:214
i
size_t i
Definition: json11.h:663
souffle::ast::getClauses
std::vector< Clause * > getClauses(const Program &program, const QualifiedName &relationName)
Returns a vector of clauses in the program describing the relation with the given name.
Definition: Utils.cpp:77
souffle::ast::transform::makeInfoRelation
Own< Relation > makeInfoRelation(Clause &originalClause, size_t originalClauseNum, TranslationUnit &translationUnit)
Definition: Provenance.cpp:78
souffle::ast::transform::ProvenanceTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: Provenance.cpp:355