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

Runs the first stage of the labelling algorithm. More...

#include <MagicSet.h>

Inheritance diagram for souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer:
Collaboration graph

Public Member Functions

NegativeLabellingTransformerclone () 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
 

Static Private Member Functions

static QualifiedName getNegativeLabel (const QualifiedName &name)
 Provide a unique name for negatively-labelled relations. More...
 

Detailed Description

Runs the first stage of the labelling algorithm.

Separates out negated appearances of relations from the main SCC graph, preventing them from affecting stratification once magic dependencies are added.

Definition at line 185 of file MagicSet.h.

Member Function Documentation

◆ clone()

NegativeLabellingTransformer* souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer::clone ( ) const
inlineoverridevirtual

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

Definition at line 191 of file MagicSet.h.

191  {
192  return new NegativeLabellingTransformer();
193  }

◆ getName()

std::string souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer::getName ( ) const
inlineoverridevirtual

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

Definition at line 187 of file MagicSet.h.

187  {
188  return "NegativeLabellingTransformer";
189  }

◆ getNegativeLabel()

QualifiedName souffle::ast::transform::NegativeLabellingTransformer::getNegativeLabel ( const QualifiedName name)
staticprivate

Provide a unique name for negatively-labelled relations.

Definition at line 781 of file MagicSet.cpp.

781  {
782  std::stringstream label;
783  label << "@poscopy_" << count;
784  QualifiedName labelledName(name);
785  labelledName.prepend(label.str());

References souffle::test::count(), and souffle::ast::QualifiedName::prepend().

Here is the call graph for this function:

◆ transform()

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

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

Definition at line 801 of file MagicSet.cpp.

806  {
807  auto* atom = neg.getAtom();
808  auto relName = atom->getQualifiedName();
809  if (contains(relationsToNotLabel, relName)) return;
810  atom->setQualifiedName(getNegativeLabel(relName));
811  relationsToLabel.insert(relName);
812  });
813  visitDepthFirst(program, [&](const Aggregator& aggr) {
814  visitDepthFirst(aggr, [&](const Atom& atom) {
815  auto relName = atom.getQualifiedName();
816  if (contains(relationsToNotLabel, relName)) return;
817  const_cast<Atom&>(atom).setQualifiedName(getNegativeLabel(relName));
818  relationsToLabel.insert(relName);
819  });
820  });
821 
822  // Copy over the rules for labelled relations one stratum at a time
823  for (size_t stratum = 0; stratum < sccGraph.getNumberOfSCCs(); stratum++) {
824  // Check which relations to label in this stratum
825  const auto& stratumRels = sccGraph.getInternalRelations(stratum);
826  std::map<QualifiedName, QualifiedName> newSccFriendNames;
827  for (const auto* rel : stratumRels) {
828  auto relName = rel->getQualifiedName();
829  if (contains(relationsToNotLabel, relName)) continue;
830  relationsToLabel.insert(relName);
831  newSccFriendNames[relName] = getNegativeLabel(relName);
832  }
833 
834  // Negatively label the relations in a new copy of this stratum
835  for (const auto* rel : stratumRels) {
836  if (contains(relationsToNotLabel, rel->getQualifiedName())) continue;
837  for (auto* clause : getClauses(program, rel->getQualifiedName())) {
838  auto neggedClause = souffle::clone(clause);
839  renameAtoms(*neggedClause, newSccFriendNames);
840  clausesToAdd.insert(std::move(neggedClause));
841  }
842  }
843  }
844 
845  // Add in all the relations that were labelled
846  for (const auto& relName : relationsToLabel) {
847  const auto* originalRel = getRelation(program, relName);
848  assert(originalRel != nullptr && "unlabelled relation does not exist");
849  auto labelledRelation = souffle::clone(originalRel);
850  labelledRelation->setQualifiedName(getNegativeLabel(relName));
851  program.addRelation(std::move(labelledRelation));
852  }
853 
854  // Add in all the negged clauses
855  for (const auto& clause : clausesToAdd) {
856  program.addClause(souffle::clone(clause));
857  }
858 
859  return !relationsToLabel.empty();
860 }
861 
862 bool PositiveLabellingTransformer::transform(TranslationUnit& translationUnit) {
863  Program& program = translationUnit.getProgram();
864  const auto& sccGraph = *translationUnit.getAnalysis<analysis::SCCGraphAnalysis>();
865  const auto& precedenceGraph = translationUnit.getAnalysis<analysis::PrecedenceGraphAnalysis>()->graph();
866  const auto& relationsToNotLabel = getRelationsToNotLabel(translationUnit);

The documentation for this class was generated from the following files:
souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::PositiveLabellingTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: MagicSet.cpp:868
souffle::ast::transform::MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer::getNegativeLabel
static QualifiedName getNegativeLabel(const QualifiedName &name)
Provide a unique name for negatively-labelled relations.
Definition: MagicSet.cpp:781
souffle::ast::renameAtoms
bool renameAtoms(Node &node, const std::map< QualifiedName, QualifiedName > &oldToNew)
Rename all atoms hat appear in a node to a given name.
Definition: Utils.cpp:307
souffle::contains
bool contains(const C &container, const typename C::value_type &element)
A utility to check generically whether a given element is contained in a given container.
Definition: ContainerUtil.h:75
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::ast::getRelation
Relation * getRelation(const Program &program, const QualifiedName &name)
Returns the relation with the given name in the program.
Definition: Utils.cpp:101
souffle::test::count
int count(const C &c)
Definition: table_test.cpp:40
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::NegativeLabellingTransformer
MagicSetTransformer::LabelDatabaseTransformer::NegativeLabellingTransformer NegativeLabellingTransformer
Definition: MagicSet.cpp:66
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
souffle::ast::transform::MagicSetTransformer::getRelationsToNotLabel
static std::set< QualifiedName > getRelationsToNotLabel(const TranslationUnit &tu)
Gets the set of relations to not label.
Definition: MagicSet.cpp:270
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086