souffle  2.0.2-371-g6315b36
Constraint.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 Constraint.h
12  *
13  * Defines a class for evaluating conditions in the Relational Algebra
14  * Machine.
15  *
16  ***********************************************************************/
17 
18 #pragma once
19 
20 #include "ram/Condition.h"
21 #include "ram/Expression.h"
22 #include "ram/Node.h"
23 #include "ram/utility/NodeMapper.h"
27 #include <cassert>
28 #include <memory>
29 #include <sstream>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ram {
34 
35 /**
36  * @class Constraint
37  * @brief Evaluates a binary constraint with respect to two Expressions
38  *
39  * Condition is true if the constraint (a logical operator
40  * such as "<") holds between the two operands
41  *
42  * The following example checks the equality of
43  * the two given tuple elements:
44  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
45  * t0.1 = t1.0
46  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
47  */
48 class Constraint : public Condition {
49 public:
50  Constraint(BinaryConstraintOp op, Own<Expression> l, Own<Expression> r)
51  : op(op), lhs(std::move(l)), rhs(std::move(r)) {
52  assert(lhs != nullptr && "left-hand side of constraint is a null-pointer");
53  assert(rhs != nullptr && "right-hand side of constraint is a null-pointer");
54  }
55 
56  /** @brief Get left-hand side */
57  const Expression& getLHS() const {
58  return *lhs;
59  }
60 
61  /** @brief Get right-hand side */
62  const Expression& getRHS() const {
63  return *rhs;
64  }
65 
66  /** @brief Get operator symbol */
68  return op;
69  }
70 
71  std::vector<const Node*> getChildNodes() const override {
72  return {lhs.get(), rhs.get()};
73  }
74 
75  Constraint* clone() const override {
77  }
78 
79  void apply(const NodeMapper& map) override {
80  lhs = map(std::move(lhs));
81  rhs = map(std::move(rhs));
82  }
83 
84 protected:
85  void print(std::ostream& os) const override {
86  os << "(" << *lhs << " ";
88  os << " " << *rhs << ")";
89  }
90 
91  bool equal(const Node& node) const override {
92  const auto& other = static_cast<const Constraint&>(node);
93  return op == other.op && equal_ptr(lhs, other.lhs) && equal_ptr(rhs, other.rhs);
94  }
95 
96  /** Operator */
97  const BinaryConstraintOp op;
98 
99  /** Left-hand side of constraint*/
101 
102  /** Right-hand side of constraint */
104 };
105 
106 } // namespace souffle::ram
souffle::ram::Constraint::print
void print(std::ostream &os) const override
Print RAM node.
Definition: Constraint.h:92
BinaryConstraintOps.h
souffle::ram::Constraint::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: Constraint.h:98
souffle::ram::Constraint::getRHS
const Expression & getRHS() const
Get right-hand side.
Definition: Constraint.h:69
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
MiscUtil.h
souffle::toBinaryConstraintSymbol
char const * toBinaryConstraintSymbol(const BinaryConstraintOp op)
Converts operator to its symbolic representation.
Definition: BinaryConstraintOps.h:336
souffle::ram::Constraint::getLHS
const Expression & getLHS() const
Get left-hand side.
Definition: Constraint.h:64
souffle::ram
Definition: AstToRamTranslator.h:54
l
var l
Definition: htmlJsChartistMin.h:15
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::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
ContainerUtil.h
Condition.h
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
souffle::ram::Constraint::op
const BinaryConstraintOp op
Operator.
Definition: Constraint.h:104
souffle::BinaryConstraintOp
BinaryConstraintOp
Binary Constraint Operators.
Definition: BinaryConstraintOps.h:41
souffle::ram::Constraint::getOperator
BinaryConstraintOp getOperator() const
Get operator symbol.
Definition: Constraint.h:74
souffle::ram::Constraint::rhs
Own< Expression > rhs
Right-hand side of constraint.
Definition: Constraint.h:110
souffle::ram::Constraint::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Constraint.h:86
souffle::ram::Constraint
Evaluates a binary constraint with respect to two Expressions.
Definition: Constraint.h:55
Node.h
std
Definition: Brie.h:3053
souffle::ram::Constraint::lhs
Own< Expression > lhs
Left-hand side of constraint.
Definition: Constraint.h:107
souffle::ram::Constraint::clone
Constraint * clone() const override
Create a clone (i.e.
Definition: Constraint.h:82
Expression.h
souffle::ram::Constraint::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: Constraint.h:78
souffle::ram::Constraint::Constraint
Constraint(BinaryConstraintOp op, Own< Expression > l, Own< Expression > r)
Definition: Constraint.h:57
souffle::ram::Expression
Abstract class for describing scalar values in RAM.
Definition: Expression.h:33