FirstMatchEngine.hpp 3.83 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 FirstMatchEngine_hpp_
#define FirstMatchEngine_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 FirstMatchEngine: 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
	FirstMatchEngine(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
	 */
	~FirstMatchEngine() {}

	/**
	 * 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 FirstMatchEngine<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
80

			if (str->isComplete()) { //final state
81
				this->manager->publishResultMatches(str);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
82
83
84
85
86
87
88
89
90
				this->counter++;
				this->pool->clear();

			} else {
			}
		} else {
			this->pool->clear();
		}
	}
91
	else if(result==-1 && type == NFAState<TinPtr>::Negation) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
92
93
94
		this->pool->clear();
	}
 }
95
96
template<class TinPtr, class ToutPtr, class TdepPtr>
void FirstMatchEngine<TinPtr, ToutPtr, TdepPtr>::runEngine(const TinPtr& event) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
97
98
99
100
	//std::cout << event << std::endl;
	if (this->pool->size() == 0)
		this->createStartStructure(event);
	else {
101
		typename ValueIDMultimap<typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr, TinPtr>::MultimapConstIterator it =this->pool->beginConstIterator();
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
102
103
104
		engineProcess(event, it->second);
	}
}
105
106
107
template<class TinPtr, class ToutPtr, class TdepPtr>
void FirstMatchEngine<TinPtr, ToutPtr, TdepPtr>::printNumMatches(std::ostream& os) {
	CEPEngine<TinPtr, ToutPtr, TdepPtr>::printNumMatches(os);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
108
109
110
111
112
113
114
	os << "number of matches using 'first match' approach = " << this->counter
			<< std::endl;
}

}

#endif /* FirstMatchEngine_hpp_ */