souffle  2.0.2-371-g6315b36
Atom.h
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 Atom.h
12  *
13  * Defines the atom class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Argument.h"
20 #include "ast/Literal.h"
21 #include "ast/Node.h"
22 #include "ast/QualifiedName.h"
23 #include "ast/utility/NodeMapper.h"
24 #include "parser/SrcLocation.h"
27 #include <algorithm>
28 #include <cstddef>
29 #include <iostream>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ast {
36 
37 /**
38  * @class Atom
39  * @brief An atom class
40  *
41  * An atom representing the use of a relation
42  * either in the head or in the body of a clause,
43  * e.g., parent(x,y), !parent(x,y), ...
44  */
45 class Atom : public Literal {
46 public:
47  Atom(QualifiedName name = {}, VecOwn<Argument> args = {}, SrcLocation loc = {})
48  : Literal(std::move(loc)), name(std::move(name)), arguments(std::move(args)) {}
49 
50  /** Return qualified name */
52  return name;
53  }
54 
55  /** Return arity of the atom */
56  size_t getArity() const {
57  return arguments.size();
58  }
59 
60  /** Set qualified name */
62  name = std::move(n);
63  }
64 
65  /** Add argument to the atom */
66  void addArgument(Own<Argument> arg) {
67  arguments.push_back(std::move(arg));
68  }
69 
70  /** Return arguments */
71  std::vector<Argument*> getArguments() const {
73  }
74 
75  Atom* clone() const override {
76  return new Atom(name, souffle::clone(arguments), getSrcLoc());
77  }
78 
79  void apply(const NodeMapper& map) override {
80  for (auto& arg : arguments) {
81  arg = map(std::move(arg));
82  }
83  }
84 
85  std::vector<const Node*> getChildNodes() const override {
86  std::vector<const Node*> res;
87  for (auto& cur : arguments) {
88  res.push_back(cur.get());
89  }
90  return res;
91  }
92 
93 protected:
94  void print(std::ostream& os) const override {
95  os << getQualifiedName() << "(" << join(arguments) << ")";
96  }
97 
98  bool equal(const Node& node) const override {
99  const auto& other = static_cast<const Atom&>(node);
100  return name == other.name && equal_targets(arguments, other.arguments);
101  }
102 
103  /** Name of atom */
105 
106  /** Arguments of atom */
108 };
109 
110 } // namespace souffle::ast
souffle::ast::Atom::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Atom.h:100
souffle::ast::Atom::getArguments
std::vector< Argument * > getArguments() const
Return arguments.
Definition: Atom.h:77
souffle::ast::Atom::addArgument
void addArgument(Own< Argument > arg)
Add argument to the atom.
Definition: Atom.h:72
souffle::ast::Atom::clone
Atom * clone() const override
Create a clone (i.e.
Definition: Atom.h:81
SrcLocation.h
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
souffle::ast::Atom
An atom class.
Definition: Atom.h:51
n
var n
Definition: htmlJsChartistMin.h:15
NodeMapper.h
souffle::ast::Atom::setQualifiedName
void setQualifiedName(QualifiedName n)
Set qualified name.
Definition: Atom.h:67
Argument.h
souffle::ast::Atom::getQualifiedName
const QualifiedName & getQualifiedName() const
Return qualified name.
Definition: Atom.h:57
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::ast::Atom::arguments
VecOwn< Argument > arguments
Arguments of atom.
Definition: Atom.h:113
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
souffle::equal_targets
bool equal_targets(const Container &a, const Container &b, const Comparator &comp)
A function testing whether two containers are equal with the given Comparator.
Definition: ContainerUtil.h:433
souffle::ast::Atom::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Atom.h:91
souffle::ast::Atom::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Atom.h:85
Literal.h
souffle::ast::Atom::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Atom.h:104
souffle::ast::Atom::Atom
Atom(QualifiedName name={}, VecOwn< Argument > args={}, SrcLocation loc={})
Definition: Atom.h:53
Node.h
souffle::ast::Atom::getArity
size_t getArity() const
Return arity of the atom.
Definition: Atom.h:62
QualifiedName.h
StreamUtil.h
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
souffle::ast::Atom::name
QualifiedName name
Name of atom.
Definition: Atom.h:110
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45