souffle  2.0.2-371-g6315b36
UserDefinedOperator.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 UserDefinedOperator.h
12  *
13  * Defines a class for evaluating values in the Relational Algebra Machine
14  *
15  ************************************************************************/
16 
17 #pragma once
18 
19 #include "ram/AbstractOperator.h"
20 #include "ram/Expression.h"
21 #include "ram/Node.h"
22 #include "souffle/TypeAttribute.h"
24 #include <cassert>
25 #include <memory>
26 #include <sstream>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 namespace souffle::ram {
32 
33 /**
34  * @class UserDefinedOperator
35  * @brief Operator that represents an extrinsic (user-defined) functor
36  */
37 class UserDefinedOperator : public AbstractOperator {
38 public:
39  UserDefinedOperator(std::string n, std::vector<TypeAttribute> argsTypes, TypeAttribute returnType,
40  bool stateful, VecOwn<Expression> args)
41  : AbstractOperator(std::move(args)), name(std::move(n)), argsTypes(std::move(argsTypes)),
43  assert(argsTypes.size() == args.size());
44  }
45 
46  /** @brief Get operator name */
47  const std::string& getName() const {
48  return name;
49  }
50 
51  /** @brief Get types of arguments */
52  const std::vector<TypeAttribute>& getArgsTypes() const {
53  return argsTypes;
54  }
55 
56  /** @brief Get return type */
58  return returnType;
59  }
60 
61  /** @brief Is functor stateful? */
62  bool isStateful() const {
63  return stateful;
64  }
65 
66  UserDefinedOperator* clone() const override {
67  auto* res = new UserDefinedOperator(name, argsTypes, returnType, stateful, {});
68  for (auto& cur : arguments) {
69  Expression* arg = cur->clone();
70  res->arguments.emplace_back(arg);
71  }
72  return res;
73  }
74 
75 protected:
76  void print(std::ostream& os) const override {
77  os << "@" << name << "_" << argsTypes;
78  os << "_" << returnType;
79  if (stateful) {
80  os << "_stateful";
81  }
82  os << "(" << join(arguments, ",", [](std::ostream& out, const Own<Expression>& arg) { out << *arg; })
83  << ")";
84  }
85 
86  bool equal(const Node& node) const override {
87  const auto& other = static_cast<const UserDefinedOperator&>(node);
88  return AbstractOperator::equal(node) && name == other.name && argsTypes == other.argsTypes &&
89  returnType == other.returnType && stateful == other.stateful;
90  }
91 
92  /** Name of user-defined operator */
93  const std::string name;
94 
95  /** Argument types */
96  const std::vector<TypeAttribute> argsTypes;
97 
98  /** Return type */
100 
101  /** Stateful */
102  const bool stateful;
103 };
104 } // namespace souffle::ram
souffle::ram::UserDefinedOperator::UserDefinedOperator
UserDefinedOperator(std::string n, std::vector< TypeAttribute > argsTypes, TypeAttribute returnType, bool stateful, VecOwn< Expression > args)
Definition: UserDefinedOperator.h:45
souffle::ram::UserDefinedOperator::clone
UserDefinedOperator * clone() const override
Create a clone (i.e.
Definition: UserDefinedOperator.h:72
souffle::ram::AbstractOperator::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: AbstractOperator.h:68
souffle::ram::UserDefinedOperator
Operator that represents an extrinsic (user-defined) functor.
Definition: UserDefinedOperator.h:43
souffle::ram::UserDefinedOperator::stateful
const bool stateful
Stateful.
Definition: UserDefinedOperator.h:108
souffle::ram::UserDefinedOperator::argsTypes
const std::vector< TypeAttribute > argsTypes
Argument types.
Definition: UserDefinedOperator.h:102
TypeAttribute
Type attribute class.
souffle::ram::AbstractOperator::arguments
VecOwn< Expression > arguments
Arguments of user defined operator.
Definition: AbstractOperator.h:74
souffle::ram::UserDefinedOperator::isStateful
bool isStateful() const
Is functor stateful?
Definition: UserDefinedOperator.h:68
souffle::ram::UserDefinedOperator::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: UserDefinedOperator.h:92
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::UserDefinedOperator::returnType
const TypeAttribute returnType
Return type.
Definition: UserDefinedOperator.h:105
souffle::ram::AbstractOperator::AbstractOperator
AbstractOperator(VecOwn< Expression > args)
Definition: AbstractOperator.h:42
souffle::ram::UserDefinedOperator::getReturnType
TypeAttribute getReturnType() const
Get return type.
Definition: UserDefinedOperator.h:63
n
var n
Definition: htmlJsChartistMin.h:15
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::UserDefinedOperator::name
const std::string name
Name of user-defined operator.
Definition: UserDefinedOperator.h:99
souffle::ram::UserDefinedOperator::getArgsTypes
const std::vector< TypeAttribute > & getArgsTypes() const
Get types of arguments.
Definition: UserDefinedOperator.h:58
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::ram::UserDefinedOperator::print
void print(std::ostream &os) const override
Print RAM node.
Definition: UserDefinedOperator.h:82
souffle::ram::Expression::clone
Expression * clone() const override=0
Create a clone (i.e.
Node.h
std
Definition: Brie.h:3053
StreamUtil.h
Expression.h
souffle::ram::UserDefinedOperator::getName
const std::string & getName() const
Get operator name.
Definition: UserDefinedOperator.h:53
souffle::ram::Expression
Abstract class for describing scalar values in RAM.
Definition: Expression.h:33
TypeAttribute.h
AbstractOperator.h