NextMatchEngine.hpp 4.77 KB
Newer Older
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
1
/*
2
 * Copyright (C) 2014-2021 DBIS Group - TU Ilmenau, All Rights Reserved.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
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.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
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.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
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/>.
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
16
17
18
19
20
21
22
23
24
25
26
27
 */

#ifndef NextMatchEngine_hpp_
#define NextMatchEngine_hpp_
#include "../Instance.hpp"
#include "../CEPEngine.hpp"
#include "../MatchProducer.hpp"
/**
 * Engine to process events according to first match approach
 */
namespace pfabric {

28
29
template<class TinPtr, class ToutPtr, class TdepPtr>
class NextMatchEngine: public CEPEngine<TinPtr, ToutPtr, TdepPtr> {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
30
31
32
33
34
35
36
private:
	/**
	 * process the current event for a particular structure by checking whether this event can fit
	 * in this structure or not according to first match approach
	 * @param event the current event
	 * @param str the structure
	 */
37
	void engineProcess(const TinPtr& event, const typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr& str);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
38
39
40
41
42
43
44
45
46
47
public:
	/**
	 * the main function, run the main engine to process the event
	 * @param event the current event
	 */
	void runEngine(const TinPtr & event);
	/**
	 * constructor to receive the CEP manager to publish new match
	 * @param manager
	 */
48
	NextMatchEngine(Matcher<TinPtr, ToutPtr, TdepPtr>* manager) :	CEPEngine<TinPtr, ToutPtr, TdepPtr>(manager){}
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	/**
	 * destructor: nothing to do because of using boost library
	 */
	~NextMatchEngine() {}

	/**
	 * print number of matches
	 * @param os the output stream object
	 */
	void printNumMatches(std::ostream& os);
};
}

namespace pfabric {


65
66
template<class TinPtr, class ToutPtr, class TdepPtr>
void NextMatchEngine<TinPtr, ToutPtr, TdepPtr>::engineProcess(const TinPtr& event, const typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr& str) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
67
68
69

	int result = -1;
		bool wind = true;
70
		typename NFAState<TinPtr>::StateType type = NFAState<TinPtr>::Normal;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
71
72
73
74
75
76
77
		result = this->checkPredicate(event, str, type); // check predicate
		if (result != -1) { // the predicate if ok.
			if (this->hasWindow())
				wind = this->checkWindowTime(event, str); // the time window is ok
			if (wind != false) { // predicate and time window are ok
				//instance_ptr inst(new instance(event));
				str->addEvent(event,
78
						( (NormalState<TinPtr, ToutPtr, TdepPtr>*)(str->getCurrentState()))->getForwardEdgeByIndex(result));
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
79
				if (str->isComplete()) { //final state
80
					this->manager->publishResultMatches(str);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
81
82
83
84
85
86
87
88
					this->counter++;
					this->deletedStructures.push_back(str);

				} else {
				}
			} else {
				this->deletedStructures.push_back(str);
			}
89
		} else if (result==-1 && type == NFAState<TinPtr>::Negation) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
90
91
92
93
			this->deletedStructures.push_back(str);
		}

 }
94
95
template<class TinPtr, class ToutPtr, class TdepPtr>
void NextMatchEngine<TinPtr, ToutPtr, TdepPtr>::runEngine(const TinPtr& event) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
96
97
98
99
100
101
102
	if (this->equalityPar->getType() == Partition<TinPtr>::Attribute) {
		   this->equalityPar->generateValues(event);

			//gc->set_parameters(event, equality_par);
			//cg_indicator = true;

			//std::cout << "signal done ... cep" << std::endl;
103
			typename ValueIDMultimap<typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr, TinPtr>::MultimapPair iterPair =
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
104
105
					this->pool->getValue(this->equalityPar);

106
			typename ValueIDMultimap<typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr, TinPtr>::MultimapConstIterator it =
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
107
108
109
110
111
112
113
114
					iterPair.first;
			for (; it != iterPair.second; ++it) {
				engineProcess(event, it->second);
			}
			//while (cg_indicator == true) {
				//	}
				//	std::cout << "wait done ... cep" << std::endl;
		} else if (this->equalityPar->getType() == Partition<TinPtr>::Sequence) {
115
			for (typename ValueIDMultimap<typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr, TinPtr>::MultimapConstIterator it =
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
116
117
118
119
120
121
122
123
124
					this->pool->beginConstIterator();
					it != this->pool->endConstIterator(); it++) {
				engineProcess(event, it->second);
			}
		}
		this->createStartStructure(event);
		if (this->deletedStructures.size() > 0)
			this->runGCstructures();
}
125
126
127
template<class TinPtr, class ToutPtr, class TdepPtr>
void NextMatchEngine<TinPtr, ToutPtr, TdepPtr>::printNumMatches(std::ostream& os) {
	CEPEngine<TinPtr, ToutPtr, TdepPtr>::printNumMatches(os);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
128
129
130
131
132
133
134
	os << "number of matches using 'next match' approach = " << this->counter
			<< std::endl;
}

}

#endif /* NextMatchEngine_hpp_ */