CEPState.hpp 2.89 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2021 DBIS Group - TU Ilmenau, All Rights Reserved.
3
4
5
 *
 * This file is part of the PipeFabric package.
 *
6
7
8
 * PipeFabric is free software: you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
9
 *
10
11
12
 * PipeFabric is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
13
 *
14
15
 * You should have received a copy of the GNU General Public License along with PipeFabric. If not,
 * see <http://www.gnu.org/licenses/>.
16
17
 */

Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef CEPState_hpp_
#define CEPState_hpp_

#include <string>
#include <vector>
#include <memory>
#include <iostream>
#include <exception>

#include "fmt/format.h"

namespace pfabric {

  template <typename Tin, typename Tstate>
  class CEPState {
  public:
      typedef CEPState<Tin, Tstate>* CEPStatePtr;

      enum Op { SEQ, OR, NOT, START, END };
      enum StateTag { Start, Stopp, Intermediate };

      struct Expr {
        Op op;
        CEPStatePtr fromState, toState;
        Expr(Op o, CEPStatePtr fptr, CEPStatePtr tptr = nullptr) :
        op(o), fromState(fptr), toState(tptr) {}
      };
      typedef std::vector<Expr> ExprList;

      ExprList& exprTable() { return exprs; }

      typedef std::function<bool(const Tin& tp, const Tstate& related)> Predicate;

      CEPState(Predicate p, StateTag t = Intermediate) : pred(p), tag(t) {
        id = (t == Stopp ? 1000 : ++globalId);
        if (t == Stopp)
          exprs.push_back(Expr(END, this));
      }

      CEPState() : pred(nullptr), id(0), tag(Start) {
        exprs.push_back(Expr(START, this));
      }

      CEPState& operator>> (CEPState& other) {
        other.exprs.insert(other.exprs.end(), exprs.begin(), exprs.end());
        other.exprs.push_back(Expr(SEQ, this, &other));
        return other;
      }
      CEPState& operator|| (CEPState& other) {
        exprs.insert(exprs.end(), other.exprs.begin(), other.exprs.end());
        exprs.push_back(Expr(OR, this, &other));
        return *this;
      }
      CEPState& operator! () {
        exprs.push_back(Expr(NOT, this));
        return *this;
      }

      void print() {
        std::cout << "STATE: " << id << std::endl;
        for (auto& e : exprs) {
          std::cout << e.op << "(" << e.fromState->id;
          if (e.toState)
            std::cout << "," << e.toState->id;
          std::cout << ")" << std::endl;
        }
      }
      Predicate predicate() const { return pred; }
      int ID() const { return id; }

  protected:
    Predicate pred;
    int id;
    StateTag tag;
    ExprList exprs;

    static int globalId;
  };

  template <typename Tin, typename Tstate>
  int CEPState<Tin,Tstate>::globalId = 0;

}

#endif