souffle  2.0.2-371-g6315b36
Choice.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, 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 Choice.h
12  *
13  * Defines the Operation of a relational algebra query.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ram/AbstractChoice.h"
20 #include "ram/Condition.h"
21 #include "ram/Node.h"
22 #include "ram/Operation.h"
23 #include "ram/Relation.h"
24 #include "ram/RelationOperation.h"
25 #include "ram/utility/NodeMapper.h"
28 #include <cstddef>
29 #include <iosfwd>
30 #include <memory>
31 #include <ostream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace souffle::ram {
37 
38 /**
39  * @class Choice
40  * @brief Find a tuple in a relation such that a given condition holds.
41  *
42  * Only one tuple is returned (if one exists), even
43  * if multiple tuples satisfying the condition exist.
44  *
45  * For example:
46  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
47  * QUERY
48  * ...
49  * CHOICE t1 IN A WHERE (t1.x, t1.y) NOT IN A
50  * ...
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  */
53 class Choice : public RelationOperation, public AbstractChoice {
54 public:
55  Choice(std::string rel, size_t ident, Own<Condition> cond, Own<Operation> nested,
56  std::string profileText = "")
57  : RelationOperation(rel, ident, std::move(nested), std::move(profileText)),
58  AbstractChoice(std::move(cond)) {}
59 
60  void apply(const NodeMapper& map) override {
63  }
64 
65  Choice* clone() const override {
67  getProfileText());
68  }
69 
70  std::vector<const Node*> getChildNodes() const override {
72  }
73 
74 protected:
75  void print(std::ostream& os, int tabpos) const override {
76  os << times(" ", tabpos);
77  os << "CHOICE t" << getTupleId();
78  os << " IN " << getRelation();
79  os << " WHERE " << getCondition();
80  os << std::endl;
81  RelationOperation::print(os, tabpos + 1);
82  }
83 
84  bool equal(const Node& node) const override {
85  const auto& other = static_cast<const Choice&>(node);
86  return RelationOperation::equal(other) && AbstractChoice::equal(other);
87  }
88 };
89 
90 } // namespace souffle::ram
souffle::ram::NestedOperation::profileText
const std::string profileText
Text used by the profiler.
Definition: NestedOperation.h:93
souffle::ram::AbstractChoice::AbstractChoice
AbstractChoice(Own< Condition > cond)
Definition: AbstractChoice.h:40
souffle::ram::Choice::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: Choice.h:81
souffle::ram::AbstractChoice::apply
void apply(const NodeMapper &map)
Definition: AbstractChoice.h:50
souffle::ram::Choice::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Choice.h:66
souffle::ram::RelationOperation::RelationOperation
RelationOperation(std::string rel, int ident, Own< Operation > nested, std::string profileText="")
Definition: RelationOperation.h:43
souffle::ram::NestedOperation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: NestedOperation.h:75
souffle::ram::NestedOperation::getOperation
Operation & getOperation() const
Get nested operation.
Definition: NestedOperation.h:62
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
MiscUtil.h
AbstractChoice.h
souffle::ram::RelationOperation::getRelation
const std::string & getRelation() const
Get search relation.
Definition: RelationOperation.h:49
RelationOperation.h
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::RelationOperation::relation
const std::string relation
Search relation.
Definition: RelationOperation.h:60
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
souffle::ram::AbstractChoice::equal
bool equal(const Node &node) const
Definition: AbstractChoice.h:59
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
souffle::times
detail::multiplying_printer< T > times(const T &value, unsigned num)
A utility printing a given value multiple times.
Definition: StreamUtil.h:322
souffle::ram::Choice::clone
Choice * clone() const override
Create a clone (i.e.
Definition: Choice.h:71
souffle::ram::Choice::Choice
Choice(std::string rel, size_t ident, Own< Condition > cond, Own< Operation > nested, std::string profileText="")
Definition: Choice.h:61
souffle::ram::NestedOperation::getProfileText
const std::string & getProfileText() const
Get profile text.
Definition: NestedOperation.h:67
Relation.h
Condition.h
souffle::ram::RelationOperation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: RelationOperation.h:54
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
souffle::ram::AbstractChoice::getChildNodes
std::vector< const Node * > getChildNodes() const
Definition: AbstractChoice.h:54
souffle::ram::Choice
Find a tuple in a relation such that a given condition holds.
Definition: Choice.h:59
Node.h
std
Definition: Brie.h:3053
Operation.h
souffle::ram::Choice::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: Choice.h:76
souffle::ram::Choice::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Choice.h:90
StreamUtil.h
souffle::ram::AbstractChoice::condition
Own< Condition > condition
Condition for which a tuple in the relation may hold.
Definition: AbstractChoice.h:65
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ram::NestedOperation::nestedOperation
Own< Operation > nestedOperation
Nested operation.
Definition: NestedOperation.h:90
souffle::ram::AbstractChoice::getCondition
const Condition & getCondition() const
Getter for the condition.
Definition: AbstractChoice.h:45
souffle::ram::NestedOperation::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: NestedOperation.h:80