NFAStructure.hpp 10.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 2014-2021 DBIS Group - TU Ilmenau, All Rights Reserved.
 *
 * This file is part of the PipeFabric package.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along with PipeFabric. If not,
 * see <http://www.gnu.org/licenses/>.
 */

aliahmadbutt4's avatar
aliahmadbutt4 committed
18

Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
19
20
21
22
23
24
25
26
27
28
29
30

#ifndef NFAStructure_hpp_
#define NFAStructure_hpp_
#include <vector>
#include "NFAController.hpp"
#include "Instance.hpp"
#include "relatedvalue/RelatedStateValuePrevious.hpp"
#include "state/StartState.hpp"
#include "edge/NFAEdge.hpp"
#include "util/Partition.hpp"

namespace pfabric {
31
template<class TinPtr, class ToutPtr, class TdepPtr>
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
32
struct compare {
33
	compare(KleeneState<TinPtr, ToutPtr, TdepPtr>* val) :
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
34
35
			val_(val) {
	}
36
	bool operator()(const std::pair<KleeneState<TinPtr, ToutPtr, TdepPtr>*, short>& elem) const {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
37
38
39
		return val_ == elem.first;
	}
private:
40
	KleeneState<TinPtr, ToutPtr, TdepPtr>* val_;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
41
42
43
};
//template<class Tin, class Tout>
//class NFAStructure;
44
template<class TinPtr, class ToutPtr, class TdepPtr>
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
45
46
class StructurePool;

47
template<class TinPtr, class ToutPtr, class TdepPtr>
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
48
49
50
51
52
53
54
class NFAStructure {
	mutable boost::atomic<short> refCount;

private:
	/**
	 * store the events of this structure which contribute on complex event
	 */
55
	std::vector<typename Instance<TinPtr, ToutPtr>::InstancePtr> events;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
56
57
58
59

	/**
	 * which state the structure is at
	 */
60
	NFAState<TinPtr>* currentState;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
61
62
63
64
65
66
67
68

	/**
	 * boolean indicating whether the structure is complete to make a match
	 */
	bool complete;
	/**
	 * our working NFA
	 */
69
	typename NFAController<TinPtr, ToutPtr, TdepPtr>::NFAControllerPtr  nfa;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
70
71
72
73

	/**
	 * store related values
	 */
74
	TdepPtr relatedValues;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
75
76
77
78
79
80

	/*
	 * the partition of  this structure
	 */
	Partition<TinPtr>* equality;

81
	std::vector<std::pair<KleeneState<TinPtr, ToutPtr, TdepPtr>*, short> > kleeneState;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
82
83
84
85



public:
86
	typedef boost::intrusive_ptr<NFAStructure<TinPtr, ToutPtr, TdepPtr>> NFAStructurePtr;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
87
88
89
90
	/**
	 * constructor
	 * @param start the current state is set to start state
	 */
91
	NFAStructure(typename NFAController<TinPtr, ToutPtr, TdepPtr>::NFAControllerPtr  nfa);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
92
93
94
95
96
97
98
99
100
101

	/**
	 * virtual destructor
	 */
	virtual ~NFAStructure() { delete equality; }

	/**
	 * Get the current state
	 * @return the current state
	 */
102
	NFAState<TinPtr>* getCurrentState() const { return this->currentState; }
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
103
104
105
106
	/**
	 * set the current state
	 * @param current_state the current state to set
	 */
107
	void setCurrentState(NFAState<TinPtr>* cur) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
108
109
110
111
112
113
114
		assert(currentState);
		this->currentState = cur;
	}
	/**
	 * get all event taht match this structure
	 * @return all event for this structure
	 */
115
	const std::vector<typename Instance<TinPtr, ToutPtr>::InstancePtr>& getEvents() const { return this->events; }
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
116
117
118
119
	/**
	 * set all event ids for this structure
	 * @param events all event ids for this structure
	 */
120
	void setEvents(std::vector<typename Instance<TinPtr, ToutPtr>::InstancePtr> events) {	this->events = events; }
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	/**
	 * Checks whether this structure is ready to make a matcht
	 * @return the complete flag
	 */

	bool isComplete() const { return this->complete; }
	/**
	 * set the complete flag
	 * @param complete the complete flag to set
	 */
	void setComplete(bool complete) { this->complete = complete; }

	/**
	 * get the nfa controller
	 * @return the nfa
	 */
137
	typename NFAController<TinPtr, ToutPtr, TdepPtr>::NFAControllerPtr getNFA() const { return this->nfa; }
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
138
139
140
141
	/**
	 * set the nfa
	 * @param nfa the nfa to set
	 */
142
	void setNFA(typename NFAController<TinPtr, ToutPtr, TdepPtr>::NFAControllerPtr  nfa);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
143
144
145
146
147
	/**
	 * Adds an event to this structure, and makes necessary updates
	 * @param event the event to be added
	 * @param the current edge
	 */
148
	void addEvent(const TinPtr& event, NFAEdge<TinPtr, ToutPtr, TdepPtr>* currentEdge);
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
149
150
151
152
	/**
	 * Clones the structure
	 * @return new structure
	 */
153
	//typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr clone( StructurePool<TinPtr, ToutPtr, TdepPtr>* pool) ;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
154
155
156
	/**
	 * get a particular event
	 */
157
	typename Instance<TinPtr, ToutPtr>::InstancePtr getEvent(long index) const {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		assert(index < events.size());
		return this->events[index];
	}
	/**
	 * get a time stamp for particulat event
	 */
	Timestamp getEventTimestamp(long index) const {
		assert(index < events.size());
		return this->events[index]->getInstanceTimestamp();
	}
	/**
	 * get the timestamp for the last event for this structure
	 */
	Timestamp getLastEventTimestamp() const { return this->events.back()->getInstanceTimestamp(); }
	/**
	 * get the timestamp for the first event for this structure
	 */
	Timestamp getFirstEventTimestamp() const { return this->events.front()->getInstanceTimestamp(); }

	/**
	 * print some information about this structure
	 * @param os
	 */
	void print(std::ostream & os);
	/**
	 * Equality comparison operator.
	 * @param rhs The input structure for this structure to compare to
	 * @return True iff this structure has the same information as that of rhs
	 */
	bool operator==(const NFAStructure & rhs) { return this == &rhs; }

	/**
	 * Inequality comparison operator.
	 * @param rhs The input structure for this structure to compare to
	 * @return True iff this structure does not the same information as that of rhs
	 */
	bool operator!=(const NFAStructure & rhs) { return this == &rhs; }
	/**
	 * get the sequence or number of events in this structure
	 * @return as above
	 */
	int getSequence() const { return this->events.size(); }

	friend void intrusive_ptr_add_ref(const NFAStructure *x) {
		x->refCount.fetch_add(1, boost::memory_order_relaxed);
	}
	friend void intrusive_ptr_release(const NFAStructure *x) {
		if (x->refCount.fetch_sub(1, boost::memory_order_release) == 1) {
			boost::atomic_thread_fence(boost::memory_order_acquire);
			delete x;
		}
	}

211
	const TdepPtr& getRelatedValue() {return relatedValues;}
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
212
213
214
215
216

	void setEqualityValue(Partition<TinPtr>* par) { this->equality = par; }

	Partition<TinPtr>* getEqualityValue() { return equality; }

217
	short getCurrentKleene(KleeneState<TinPtr, ToutPtr, TdepPtr>* kState) const {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
218
		return std::find_if(kleeneState.begin(), kleeneState.end(),
219
			compare<TinPtr, ToutPtr, TdepPtr>((KleeneState<TinPtr, ToutPtr, TdepPtr>*) (currentState)))->second;
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
220
221
222
223
224
225
226
227
	};

};

}

namespace pfabric {

228
229
template<class TinPtr, class ToutPtr, class TdepPtr>
NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructure(typename NFAController<TinPtr, ToutPtr, TdepPtr>::NFAControllerPtr  nfa) :
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
230
231
232
233
234
235
236
237
238
239
240
		refCount(0), kleeneState(nfa->getKleeneStatesCount()) {

	this->nfa = nfa;
	this->currentState = nfa->getStartState();
	this->complete = false;
	for (int i = 0; i < nfa->getKleeneStatesCount(); i++) {
		kleeneState[i] = std::make_pair(nfa->getKleeneStates()[i].get(), 0);
	}
	relatedValues = this->nfa->init();
}

241
242
243
244
template<class TinPtr, class ToutPtr, class TdepPtr>
void NFAStructure<TinPtr, ToutPtr, TdepPtr>::addEvent(const TinPtr& event,
		NFAEdge<TinPtr, ToutPtr, TdepPtr>* currentEdge) {
	typename Instance<TinPtr, ToutPtr>::InstancePtr inst(new Instance<TinPtr, ToutPtr>(event));
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
245
246
247
248
249
250
251
252

	this->events.push_back(inst);

	inst->setSequenceInComplex(this->events.size());
	inst->setState(this->currentState->getStateName());

	this->nfa->update( relatedValues, currentEdge->getID(), event);

253
	if (currentEdge->getEdgeType() == NFAEdge<TinPtr, ToutPtr, TdepPtr>::Forward) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
254
		this->currentState =
255
256
				((ForwardEdge<TinPtr, ToutPtr, TdepPtr>*) (currentEdge))->getDestState();
	} else if (currentEdge->getEdgeType() == NFAEdge<TinPtr, ToutPtr, TdepPtr>::Loop) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
257

aliahmadbutt4's avatar
aliahmadbutt4 committed
258
		typename std::vector<std::pair<KleeneState<TinPtr, ToutPtr, TdepPtr>*, short> >::iterator res = std::find_if(
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
259
				kleeneState.begin(), kleeneState.end(),
260
				compare<TinPtr, ToutPtr, TdepPtr>((KleeneState<TinPtr, ToutPtr, TdepPtr>*) (currentState)));
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
261
262
		res->second = res->second + 1;
	}
263
	if (currentState->getStateType() == NFAState<TinPtr>::Final) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
264
265
		this->complete = true;

266
	} else if (currentState->getStateType() == NFAState<TinPtr>::Kleene) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
267
268
269
270
271
272
		//initRelatedValue(event,
			//	((KleeneState*) this->currentState)->getLoopEdge());
	}
}
/*
template<class Tin, class Tout>
273
typename NFAStructure<TinPtr, ToutPtr, TdepPtr>::NFAStructurePtr NFAStructure<TinPtr, ToutPtr, TdepPtr>::clone(StructurePool<TinPtr, ToutPtr, TdepPtr>* pool) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
274
	int counter = 0;
275
276
	std::vector<typename Instance<TinPtr, ToutPtr, TdepPtr>::InstancePtr> vector(this->events.size());
	for (std::vector<typename Instance<TinPtr, ToutPtr, TdepPtr>::InstancePtr>::iterator it = this->events.begin();
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
277
278
279
280
281
282
			it != this->events.end(); it++) {
		vector.at(counter++) = *it;
	}
	counter = 0;
	//vector_related_value related(this->related_values.size());

283
	for (std::vector<typename Instance<TinPtr, ToutPtr, TdepPtr>::InstancePtr>::iterator it = this->events.begin();
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
			it != this->events.end(); it++) {
		vector.at(counter++) = *it;
	}

	NFAStructurePtr cloneStr = this->pool->getStructure(this->nfa, equality->clone());

	cloneStr->setComplete(this->complete);
	cloneStr->setEvents(vector);
	cloneStr->setCurrentState(this->currentState);*/
	//return cloneStr;

//}
/*
template<class Tin, class Tout>
298
void NFAStructure<TinPtr, ToutPtr, TdepPtr>::addStateSource() {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
299
300
301
302
303
304
	for (int i = 0; i < this->nfa->getTransitions().size(); i++) {
		relatedValues.addRelatedValue(
				this->nfa->getTransitions()[i]->getSourceOf());
	}
}
template<class Tin, class Tout>
305
any NFAStructure<TinPtr, ToutPtr, TdepPtr>::getRelatedValue(int edge_id, int index) {
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
306
307
308
309
310

	std::vector<RelatedStateValuePtr> values = relatedValues.at(edge_id);
	return values[index]->getValue();
}
template<class Tin, class Tout>
311
void NFAStructure<TinPtr, ToutPtr, TdepPtr>::updateRelatedValue(const tuple_ptr& inst,
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
312
313
314
315
316
317
318
319
320
321
322
		NFAEdge* edge) {

	if (edge->hasRelatedValues()) {
		const vector<RelatedStateValuePtr>& values = relatedValues.at(
				edge->getID());
		for (int i = 0; i < values.size(); i++) {
			values[i]->updateValue(inst);
		}
	}
}
template<class Tin, class Tout>
323
void NFAStructure<TinPtr, ToutPtr, TdepPtr>::initRelatedValue(const TinPtr& inst,
Kai-Uwe Sattler's avatar
Kai-Uwe Sattler committed
324
325
326
327
328
329
330
331
332
333
334
335
336
		NFAEdge* edge) {

	if (edge->hasRelatedValues()) {
		const vector<RelatedStateValuePtr>& values = relatedValues.at(
				edge->getID());
		for (int i = 0; i < values.size(); i++) {
			values[i]->initValue(inst);
		}
	}
}*/

}
#endif /* NFAStructure_hpp_ */