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

Transformation pass to remove all empty relations and rules that use empty relations. More...

#include <RemoveEmptyRelations.h>

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

Public Member Functions

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

Static Public Member Functions

static bool removeEmptyRelations (TranslationUnit &translationUnit)
 Eliminate all empty relations (and their uses) in the given program. More...
 

Private Member Functions

bool transform (TranslationUnit &translationUnit) override
 

Static Private Member Functions

static bool removeEmptyRelationUses (TranslationUnit &translationUnit, const QualifiedName &emptyRelation)
 Eliminate rules that contain empty relations and/or rewrite them. More...
 

Detailed Description

Transformation pass to remove all empty relations and rules that use empty relations.

Definition at line 31 of file RemoveEmptyRelations.h.

Member Function Documentation

◆ clone()

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

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

Definition at line 49 of file RemoveEmptyRelations.h.

◆ getName()

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

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

Definition at line 37 of file RemoveEmptyRelations.h.

41  {

◆ removeEmptyRelations()

bool souffle::ast::transform::RemoveEmptyRelationsTransformer::removeEmptyRelations ( TranslationUnit translationUnit)
static

Eliminate all empty relations (and their uses) in the given program.

Parameters
translationUnitthe program to be processed
Returns
whether the program was modified

Definition at line 40 of file RemoveEmptyRelations.cpp.

41  : program.getRelations()) {
42  if (!getClauses(program, *rel).empty() || ioTypes->isInput(rel)) {
43  continue;
44  }
45  emptyRelations.insert(rel->getQualifiedName());
46 
47  bool usedInAggregate = false;
48  visitDepthFirst(program, [&](const Aggregator& agg) {
49  for (const auto lit : agg.getBodyLiterals()) {
50  visitDepthFirst(*lit, [&](const Atom& atom) {
51  if (getAtomRelation(&atom, &program) == rel) {
52  usedInAggregate = true;
53  }
54  });
55  }
56  });
57 
58  if (!usedInAggregate && !ioTypes->isOutput(rel)) {
59  removeRelation(translationUnit, rel->getQualifiedName());
60  changed = true;
61  }
62  }
63 
64  for (const auto& relName : emptyRelations) {
65  changed |= removeEmptyRelationUses(translationUnit, relName);
66  }
67 
68  return changed;
69 }
70 
72  TranslationUnit& translationUnit, const QualifiedName& emptyRelationName) {
73  Program& program = translationUnit.getProgram();

References souffle::ast::Aggregator::getBodyLiterals(), souffle::ast::getClauses(), rel(), and souffle::ast::visitDepthFirst().

Here is the call graph for this function:

◆ removeEmptyRelationUses()

bool souffle::ast::transform::RemoveEmptyRelationsTransformer::removeEmptyRelationUses ( TranslationUnit translationUnit,
const QualifiedName emptyRelation 
)
staticprivate

Eliminate rules that contain empty relations and/or rewrite them.

Parameters
translationUnitthe program to be processed
emptyRelationrelation that is empty
Returns
whether the program was modified

Definition at line 75 of file RemoveEmptyRelations.cpp.

82  { clauses.push_back(&cur); });
83 
84  // clean all clauses
85  for (const Clause* cl : clauses) {
86  // check for an atom whose relation is the empty relation
87 
88  bool removed = false;
89  for (Literal* lit : cl->getBodyLiterals()) {
90  if (auto* arg = dynamic_cast<Atom*>(lit)) {
91  if (arg->getQualifiedName() == emptyRelationName) {
92  program.removeClause(cl);
93  removed = true;
94  changed = true;
95  break;
96  }
97  }
98  }
99 
100  if (!removed) {
101  // check whether a negation with empty relations exists
102 
103  bool rewrite = false;
104  for (Literal* lit : cl->getBodyLiterals()) {
105  if (auto* neg = dynamic_cast<Negation*>(lit)) {
106  if (neg->getAtom()->getQualifiedName() == emptyRelationName) {
107  rewrite = true;
108  break;
109  }
110  }
111  }
112 
113  if (rewrite) {
114  // clone clause without negation for empty relations
115 
116  auto res = Own<Clause>(cloneHead(cl));
117 
118  for (Literal* lit : cl->getBodyLiterals()) {
119  if (auto* neg = dynamic_cast<Negation*>(lit)) {
120  if (neg->getAtom()->getQualifiedName() != emptyRelationName) {
121  res->addToBody(souffle::clone(lit));
122  }
123  } else {
124  res->addToBody(souffle::clone(lit));
125  }
126  }
127 
128  program.removeClause(cl);
129  program.addClause(std::move(res));
130  changed = true;
131  }
132  }
133  }
134 
135  return changed;
136 }
137 
138 } // namespace souffle::ast::transform

References clauses.

◆ transform()

bool souffle::ast::transform::RemoveEmptyRelationsTransformer::transform ( TranslationUnit translationUnit)
inlineoverrideprivatevirtual

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

Definition at line 54 of file RemoveEmptyRelations.h.


The documentation for this class was generated from the following files:
souffle::ast::cloneHead
Clause * cloneHead(const Clause *clause)
Returns a clause which contains head of the given clause.
Definition: Utils.cpp:254
souffle::ast::removeRelation
void removeRelation(TranslationUnit &tu, const QualifiedName &name)
Remove relation and all its clauses from the program.
Definition: Utils.cpp:105
souffle::ast::transform::RemoveEmptyRelationsTransformer::removeEmptyRelationUses
static bool removeEmptyRelationUses(TranslationUnit &translationUnit, const QualifiedName &emptyRelation)
Eliminate rules that contain empty relations and/or rewrite them.
Definition: RemoveEmptyRelations.cpp:75
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
clauses
std::vector< Own< Clause > > clauses
Definition: ComponentInstantiation.cpp:67
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::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the ast rooted by the given node recursively in a depth-...
Definition: Visitor.h:273
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086