souffle  2.0.2-371-g6315b36
Data Structures | Public Member Functions | Private Member Functions | Private Attributes
souffle::ast::analysis::NormalisedClause Class Reference

#include <ClauseNormalisation.h>

Collaboration diagram for souffle::ast::analysis::NormalisedClause:
Collaboration graph

Data Structures

struct  NormalisedClauseElement
 

Public Member Functions

const std::set< std::string > & getConstants () const
 
const std::vector< NormalisedClauseElement > & getElements () const
 
const std::set< std::string > & getVariables () const
 
bool isFullyNormalised () const
 
 NormalisedClause ()=default
 
 NormalisedClause (const Clause *clause)
 

Private Member Functions

void addClauseAtom (const std::string &qualifier, const std::string &scopeID, const Atom *atom)
 Parse an atom with a preset name qualifier into the element list. More...
 
void addClauseBodyLiteral (const std::string &scopeID, const Literal *lit)
 Parse a body literal into the element list. More...
 
std::string normaliseArgument (const Argument *arg)
 Return a normalised string repr of an argument. More...
 

Private Attributes

size_t aggrScopeCount {0}
 
std::vector< NormalisedClauseElementclauseElements {}
 
std::set< std::string > constants {}
 
bool fullyNormalised {true}
 
std::set< std::string > variables {}
 

Detailed Description

Definition at line 46 of file ClauseNormalisation.h.

Constructor & Destructor Documentation

◆ NormalisedClause() [1/2]

souffle::ast::analysis::NormalisedClause::NormalisedClause ( )
default

◆ NormalisedClause() [2/2]

souffle::ast::analysis::NormalisedClause::NormalisedClause ( const Clause clause)

Definition at line 51 of file ClauseNormalisation.cpp.

52  {.name = name, .params = headVars});
53 
54  // body
55  for (const auto* lit : clause->getBodyLiterals()) {
56  addClauseBodyLiteral("@min:scope:0", lit);
57  }
58 }
59 
61  const std::string& qualifier, const std::string& scopeID, const Atom* atom) {
62  QualifiedName name(atom->getQualifiedName());
63  name.prepend(qualifier);
64 

Member Function Documentation

◆ addClauseAtom()

void souffle::ast::analysis::NormalisedClause::addClauseAtom ( const std::string &  qualifier,
const std::string &  scopeID,
const Atom atom 
)
private

Parse an atom with a preset name qualifier into the element list.

Definition at line 66 of file ClauseNormalisation.cpp.

67  : atom->getArguments()) {
68  vars.push_back(normaliseArgument(arg));
69  }
70  clauseElements.push_back({.name = name, .params = vars});
71 }
72 
73 void NormalisedClause::addClauseBodyLiteral(const std::string& scopeID, const Literal* lit) {
74  if (const auto* atom = dynamic_cast<const Atom*>(lit)) {
75  addClauseAtom("@min:atom", scopeID, atom);
76  } else if (const auto* neg = dynamic_cast<const Negation*>(lit)) {
77  addClauseAtom("@min:neg", scopeID, neg->getAtom());

References normaliseArgument().

Here is the call graph for this function:

◆ addClauseBodyLiteral()

void souffle::ast::analysis::NormalisedClause::addClauseBodyLiteral ( const std::string &  scopeID,
const Literal lit 
)
private

Parse a body literal into the element list.

Definition at line 79 of file ClauseNormalisation.cpp.

80  :operator");
81  std::vector<std::string> vars;
82  vars.push_back(scopeID);
83  vars.push_back(normaliseArgument(bc->getLHS()));
84  vars.push_back(normaliseArgument(bc->getRHS()));
85  clauseElements.push_back({.name = name, .params = vars});
86  } else {
87  assert(lit != nullptr && "unexpected nullptr lit");
88  fullyNormalised = false;
89  std::stringstream qualifier;
90  qualifier << "@min:unhandled:lit:" << scopeID;
91  QualifiedName name(toString(*lit));
92  name.prepend(qualifier.str());
93  clauseElements.push_back({.name = name, .params = std::vector<std::string>()});
94  }
95 }
96 
97 std::string NormalisedClause::normaliseArgument(const Argument* arg) {
98  if (auto* stringCst = dynamic_cast<const StringConstant*>(arg)) {
99  std::stringstream name;
100  name << "@min:cst:str" << *stringCst;
101  constants.insert(name.str());

◆ getConstants()

const std::set<std::string>& souffle::ast::analysis::NormalisedClause::getConstants ( ) const
inline

Definition at line 65 of file ClauseNormalisation.h.

66  :
67  bool fullyNormalised{true};

◆ getElements()

const std::vector<NormalisedClauseElement>& souffle::ast::analysis::NormalisedClause::getElements ( ) const
inline

Definition at line 69 of file ClauseNormalisation.h.

69  {};
70  std::set<std::string> constants{};
71  std::vector<NormalisedClauseElement> clauseElements{};

◆ getVariables()

const std::set<std::string>& souffle::ast::analysis::NormalisedClause::getVariables ( ) const
inline

Definition at line 61 of file ClauseNormalisation.h.

62  {
63  return clauseElements;

References clauseElements.

◆ isFullyNormalised()

bool souffle::ast::analysis::NormalisedClause::isFullyNormalised ( ) const
inline

Definition at line 57 of file ClauseNormalisation.h.

58  {
59  return constants;

References constants.

◆ normaliseArgument()

std::string souffle::ast::analysis::NormalisedClause::normaliseArgument ( const Argument arg)
private

Return a normalised string repr of an argument.

Definition at line 103 of file ClauseNormalisation.cpp.

103  {
104  std::stringstream name;
105  name << "@min:cst:num:" << *numericCst;
106  constants.insert(name.str());
107  return name.str();
108  } else if (isA<NilConstant>(arg)) {
109  constants.insert("@min:cst:nil");
110  return "@min:cst:nil";
111  } else if (auto* var = dynamic_cast<const ast::Variable*>(arg)) {
112  auto name = var->getName();
113  variables.insert(name);
114  return name;
115  } else if (dynamic_cast<const UnnamedVariable*>(arg)) {
116  static size_t countUnnamed = 0;
117  std::stringstream name;
118  name << "@min:unnamed:" << countUnnamed++;
119  variables.insert(name.str());
120  return name.str();
121  } else if (auto* aggr = dynamic_cast<const Aggregator*>(arg)) {
122  // Set the scope to uniquely identify the aggregator
123  std::stringstream scopeID;
124  scopeID << "@min:scope:" << ++aggrScopeCount;
125  variables.insert(scopeID.str());
126 
127  // Set the type signature of this aggregator
128  std::stringstream aggrTypeSignature;
129  aggrTypeSignature << "@min:aggrtype";
130  std::vector<std::string> aggrTypeSignatureComponents;
131 
132  // - the operator is fixed and cannot be changed
133  aggrTypeSignature << ":" << aggr->getBaseOperator();
134 
135  // - the scope can be remapped as a variable
136  aggrTypeSignatureComponents.push_back(scopeID.str());
137 
138  // - the normalised target expression can be remapped as a variable
139  if (aggr->getTargetExpression() != nullptr) {
140  std::string normalisedExpr = normaliseArgument(aggr->getTargetExpression());
141  aggrTypeSignatureComponents.push_back(normalisedExpr);
142  }
143 
144  // Type signature is its own special atom
145  clauseElements.push_back({.name = aggrTypeSignature.str(), .params = aggrTypeSignatureComponents});
146 
147  // Add each contained normalised clause literal, tying it with the new scope ID
148  for (const auto* literal : aggr->getBodyLiterals()) {
149  addClauseBodyLiteral(scopeID.str(), literal);
150  }
151 
152  // Aggregator identified by the scope ID
153  return scopeID.str();
154  } else {
155  fullyNormalised = false;
156  return "@min:unhandled:arg";
157  }
158 }
159 
160 void ClauseNormalisationAnalysis::run(const TranslationUnit& translationUnit) {
161  const auto& program = translationUnit.getProgram();
162  for (const auto* clause : program.getClauses()) {
163  assert(!contains(normalisations, clause) && "clause already processed");
164  normalisations[clause] = NormalisedClause(clause);

Referenced by addClauseAtom().

Field Documentation

◆ aggrScopeCount

size_t souffle::ast::analysis::NormalisedClause::aggrScopeCount {0}
private

Definition at line 75 of file ClauseNormalisation.h.

◆ clauseElements

std::vector<NormalisedClauseElement> souffle::ast::analysis::NormalisedClause::clauseElements {}
private

Definition at line 78 of file ClauseNormalisation.h.

Referenced by getVariables().

◆ constants

std::set<std::string> souffle::ast::analysis::NormalisedClause::constants {}
private

Definition at line 77 of file ClauseNormalisation.h.

Referenced by isFullyNormalised().

◆ fullyNormalised

bool souffle::ast::analysis::NormalisedClause::fullyNormalised {true}
private

Definition at line 74 of file ClauseNormalisation.h.

◆ variables

std::set<std::string> souffle::ast::analysis::NormalisedClause::variables {}
private

Definition at line 76 of file ClauseNormalisation.h.


The documentation for this class was generated from the following files:
souffle::ast::analysis::NormalisedClause::fullyNormalised
bool fullyNormalised
Definition: ClauseNormalisation.h:74
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::ast::analysis::ClauseNormalisationAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: ClauseNormalisation.cpp:166
souffle::ast::analysis::NormalisedClause::NormalisedClause
NormalisedClause()=default
str
const std::string & str
Definition: json11.h:662
souffle::ast::analysis::NormalisedClause::variables
std::set< std::string > variables
Definition: ClauseNormalisation.h:76
souffle::ast::analysis::NormalisedClause::clauseElements
std::vector< NormalisedClauseElement > clauseElements
Definition: ClauseNormalisation.h:78
souffle::ast::analysis::NormalisedClause::constants
std::set< std::string > constants
Definition: ClauseNormalisation.h:77
souffle::ast::analysis::NormalisedClause::aggrScopeCount
size_t aggrScopeCount
Definition: ClauseNormalisation.h:75
souffle::ast::analysis::NormalisedClause::addClauseBodyLiteral
void addClauseBodyLiteral(const std::string &scopeID, const Literal *lit)
Parse a body literal into the element list.
Definition: ClauseNormalisation.cpp:79
souffle::ast::analysis::NormalisedClause::normaliseArgument
std::string normaliseArgument(const Argument *arg)
Return a normalised string repr of an argument.
Definition: ClauseNormalisation.cpp:103
souffle::ast::analysis::NormalisedClause::addClauseAtom
void addClauseAtom(const std::string &qualifier, const std::string &scopeID, const Atom *atom)
Parse an atom with a preset name qualifier into the element list.
Definition: ClauseNormalisation.cpp:66