souffle  2.0.2-371-g6315b36
PrecedenceGraph.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file PrecedenceGraph.cpp
12  *
13  * Implements method of precedence graph to build the precedence graph,
14  * compute strongly connected components of the precedence graph, and
15  * build the strongly connected component graph.
16  *
17  ***********************************************************************/
18 
20 #include "GraphUtils.h"
21 #include "ast/Argument.h"
22 #include "ast/Atom.h"
23 #include "ast/Clause.h"
24 #include "ast/Literal.h"
25 #include "ast/Program.h"
26 #include "ast/QualifiedName.h"
27 #include "ast/Relation.h"
28 #include "ast/TranslationUnit.h"
30 #include "ast/utility/Visitor.h"
31 #include <set>
32 #include <string>
33 #include <vector>
34 
35 namespace souffle::ast::analysis {
36 
37 void PrecedenceGraphAnalysis::run(const TranslationUnit& translationUnit) {
38  /* Get relations */
39  Program& program = translationUnit.getProgram();
40  const auto& relationDetail = *translationUnit.getAnalysis<RelationDetailCacheAnalysis>();
41 
42  for (const auto* r : program.getRelations()) {
43  backingGraph.insert(r);
44  for (const auto& c : relationDetail.getClauses(r)) {
45  visitDepthFirst(c->getBodyLiterals(), [&](const Atom& atom) {
46  backingGraph.insert(relationDetail.getRelation(atom.getQualifiedName()), r);
47  });
48  visitDepthFirst(c->getHead()->getArguments(), [&](const Atom& atom) {
49  backingGraph.insert(relationDetail.getRelation(atom.getQualifiedName()), r);
50  });
51  }
52  }
53 }
54 
55 void PrecedenceGraphAnalysis::print(std::ostream& os) const {
56  /* Print dependency graph */
57  std::stringstream ss;
58  ss << "digraph {\n";
59  /* Print node of dependence graph */
60  for (const Relation* rel : backingGraph.vertices()) {
61  if (rel != nullptr) {
62  ss << "\t\"" << rel->getQualifiedName() << "\" [label = \"" << rel->getQualifiedName()
63  << "\"];\n";
64  }
65  }
66  for (const Relation* rel : backingGraph.vertices()) {
67  if (rel != nullptr) {
68  for (const Relation* adjRel : backingGraph.successors(rel)) {
69  if (adjRel != nullptr) {
70  ss << "\t\"" << rel->getQualifiedName() << "\" -> \"" << adjRel->getQualifiedName()
71  << "\";\n";
72  }
73  }
74  }
75  }
76  ss << "}\n";
77  printHTMLGraph(os, ss.str(), getName());
78 }
79 
80 } // namespace souffle::ast::analysis
TranslationUnit.h
RelationDetailCache.h
souffle::ast::Relation
Defines a relation with a name, attributes, qualifiers, and internal representation.
Definition: Relation.h:49
Relation.h
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
souffle::printHTMLGraph
void printHTMLGraph(std::ostream &out, const std::string &dotSpec, const std::string &id)
Definition: GraphUtils.h:226
souffle::ast::analysis::Analysis::getName
virtual const std::string & getName() const
get name of the analysis
Definition: Analysis.h:50
Argument.h
souffle::Relation
Object-oriented wrapper class for Souffle's templatized relations.
Definition: SouffleInterface.h:49
Atom.h
Literal.h
GraphUtils.h
souffle::ast::analysis::PrecedenceGraphAnalysis::backingGraph
Graph< const Relation *, NameComparison > backingGraph
Adjacency list of precedence graph (determined by the dependencies of the relations)
Definition: PrecedenceGraph.h:60
QualifiedName.h
Program.h
souffle::ast::analysis::PrecedenceGraphAnalysis::run
void run(const TranslationUnit &translationUnit) override
run analysis for a Ast translation unit
Definition: PrecedenceGraph.cpp:45
Visitor.h
Clause.h
souffle::ast::analysis
Definition: Aggregate.cpp:39
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
PrecedenceGraph.h
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ast::analysis::PrecedenceGraphAnalysis::print
void print(std::ostream &os) const override
Output precedence graph in graphviz format to a given stream.
Definition: PrecedenceGraph.cpp:63
souffle::profile::ss
class souffle::profile::Tui ss
Definition: Tui.h:336