123

Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

POLITECNICO DI MILANOFacoltà di Ingegneria

Corso di Laurea Triennale in Ingegneria Informatica

Design and implementation of theLow Level Query environment

for PerLa language

Relatore: Ch.mo Prof. Fabio A. SCHREIBERCorrelatore: Ing. Romolo CAMPLANI

Tesi di laurea di:Diego VIGANO'

mat. 668589

Anno Accademico 2009/2010

Page 2: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Contents

1 Introduction 9

1.1 Pervasive systems and Wireless Sensors Networks . . . . . . . . . . . . . . . 9

1.2 Challenges of WSNs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.3 PerLa project: introduction and goals of the project . . . . . . . . . . . . . 12

2 State of the art 15

3 PerLa architecture 19

3.1 The high level interface (language) . . . . . . . . . . . . . . . . . . . . . . . 20

3.1.1 Actuation and High Level Queries . . . . . . . . . . . . . . . . . . . 21

3.1.2 Low Level Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3.1.3 A PerLa query example . . . . . . . . . . . . . . . . . . . . . . . . . 24

3.2 The high level interface (middleware) . . . . . . . . . . . . . . . . . . . . . . 25

3.2.1 Application Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

3.2.2 Logical Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.2.3 Device access (physical) Level . . . . . . . . . . . . . . . . . . . . . . 27

3.3 First PerLa deployment: rockfall prevention . . . . . . . . . . . . . . . . . . 29

4 LLQ Execution Design 33

4.1 The importance of LLQ Execution . . . . . . . . . . . . . . . . . . . . . . . 33

4.2 LLQ Execution design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1

Page 3: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

4.3 DataCollector entity design . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

4.3.1 DataCollector activity . . . . . . . . . . . . . . . . . . . . . . . . . . 42

4.4 Data processing and result computation . . . . . . . . . . . . . . . . . . . . 43

5 FPC interface design 47

5.1 Interface towards DataCollector . . . . . . . . . . . . . . . . . . . . . . . . 48

5.2 Interface towards the Logical Object Registry . . . . . . . . . . . . . . . . . 48

5.2.1 Interface exposing FPC attributes . . . . . . . . . . . . . . . . . . . 48

5.2.2 Interface for query injection . . . . . . . . . . . . . . . . . . . . . . . 49

5.3 Interface towards the abstracted device . . . . . . . . . . . . . . . . . . . . . 49

6 LLQ Execution Implementation 51

6.1 Data structures implementation . . . . . . . . . . . . . . . . . . . . . . . . . 51

6.1.1 Data structures for records . . . . . . . . . . . . . . . . . . . . . . . 51

6.1.2 Data structures for query computation . . . . . . . . . . . . . . . . . 54

6.1.3 Data structure for query analysis . . . . . . . . . . . . . . . . . . . . 63

6.2 Data collection implementation . . . . . . . . . . . . . . . . . . . . . . . . . 68

6.2.1 Communication structure implementation . . . . . . . . . . . . . . . 68

6.2.2 DataCollector class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

7 FPC interface implementation 73

7.1 FPCDataStructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

7.2 FunctionalityProxyComponent class . . . . . . . . . . . . . . . . . . . . . . 74

8 Conclusion and open points 77

Appendix A: PerLa Console 79

Appendix B: Source code 87

2

Page 4: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Bibliography 121

3

Page 5: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

4

Page 6: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

List of Figures

1.1 Pervasive system key components taken from [1] . . . . . . . . . . . . . . . . 10

2.1 PerLa vs. others projects. A comparison . . . . . . . . . . . . . . . . . . . . 17

3.1 PerLa interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.2 Query (quite complex) example, taken from [2] . . . . . . . . . . . . . . . . 24

3.3 Logical object abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.4 PerLa middleware overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.5 An example of a pervasive system [3] . . . . . . . . . . . . . . . . . . . . . . 31

4.1 Example with �dumb� and �smart� devices . . . . . . . . . . . . . . . . . . . 34

4.2 Entities designed to achieve LLQ Execution . . . . . . . . . . . . . . . . . . 36

4.3 LLQ Execution (single query) . . . . . . . . . . . . . . . . . . . . . . . . . . 38

4.4 LLQ Execution example (two simultaneous queries) . . . . . . . . . . . . . . 40

4.5 Data collection uml activity diagram . . . . . . . . . . . . . . . . . . . . . . 42

4.6 LLQ Executor activity uml diagram . . . . . . . . . . . . . . . . . . . . . . 44

4.7 group by clause example, taken from [2] . . . . . . . . . . . . . . . . . . . 45

5.1 FPC relationships with other PerLa key components . . . . . . . . . . . . . 47

6.1 Data structures for records uml diagrams . . . . . . . . . . . . . . . . . . . 53

6.2 Di�erent iteration type on the same bu�er . . . . . . . . . . . . . . . . . . . 56

5

Page 7: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

6.3 uml diagram of the data structures for sampling frequency . . . . . . . . . 57

6.4 uml diagram of LLStack class . . . . . . . . . . . . . . . . . . . . . . . . . . 58

6.5 uml diagrams of classes used to represent information of a query . . . . . . 60

6.6 LLQEnvironment and LLQInformation uml diagram . . . . . . . . . . . . . 62

6.7 ActiveSession, LLQEnvironments and LLStack relationships . . . . . . . . . 63

6.8 uml class diagram of QueryAnalyzer class . . . . . . . . . . . . . . . . . . . 66

6.9 ExpressionAnalyzer uml diagram . . . . . . . . . . . . . . . . . . . . . . . . 67

6.10 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

6.11 Data �ow using pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

6.12 DataCollector uml class diagram . . . . . . . . . . . . . . . . . . . . . . . . 72

7.1 FPC uml diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

8.1 ProtoBuf grammar, an example taken from [4] . . . . . . . . . . . . . . . . . 80

8.2 PerLa console messages structure . . . . . . . . . . . . . . . . . . . . . . . . 81

8.3 Request and response messages de�nition with ProtoBuf . . . . . . . . . . . 83

8.4 TestQueryResponse and TestQueryRequest payload de�nition . . . . . . . . 84

8.5 PerLa console functioning schema . . . . . . . . . . . . . . . . . . . . . . . . 85

6

Page 8: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Thesis objectives

This thesis aimed at the de�nition, design and implementation of the Low Level Execution

environment of PerLa [5, 6] language, which is mainly charged of collecting data from

the underlying pervasive system, preparing them for further computations at upper levels.

As it will be explained in Subsection 3.2.2, this thesis found its location within logical

level, and particularly in logical objects. In details, this thesis intended to accomplish the

following goals:

� Achieve an overall design of the Low Level Query Execution, focusing on the com-

plex aspects regarding the collection of data from the underlying pervasive system,

since a complete design still, in fact, needed to be ful�lled. This objective included

the implementation of those elements which are responsible to make data exchange

possible between FPC and LLQ environment.

� Consolidation and design of the interface between the FPC and the Low Level Query

Execution. The FPC was, in fact, designed in [7] and partially implemented in [8]

but its implementation focused on the interactions with physical devices, while a well

de�ned interface with the LLQ environment was still to be completely designed and

achieved.

7

Page 9: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

8

Page 10: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

1 Introduction

1.1 Pervasive systems and Wireless Sensors Networks

Computing has, particularly in the last years, leapt o� from the desktop to insinuate itself

into everyday life, slowly making real the concept of pervasive computing. According to

[9], this is only the �nal step of a series of changes in computing vision, started in the mid

1970s, when the PC �rst brought computer to the people, making the personal computing

concept popular for the �rst time. Since then, the computing vision has encountered two

important revolutionary changes that can be summarized as follows:

� Distributed computing (mid 1990s). Thanks to the advent of networking, it intro-

duced the user to seamless access to remote information resources and communication

� Mobile computing (late 2000s). Integrating mobile technology into the Web, it has

made possible to access information �anytime anywhere�.

As mentioned before the last step of this change-in-vision is pervasive computing, whose

goal is to ful�ll the vision of an �all the time everywhere� access to information. Nowadays

literature about this argument is vast, but, among many di�erences, one vision is shared

between the experts of this sector: at their core, all models of pervasive computing must

be composed of the following key components [9]:

� a set of devices;

� a pervasive network, used to link devices together;

9

Page 11: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

� a pervasive middleware, playing a central role, making dialogue possible between

the pervasive network and the �nal users;

� a pervasive application to allow the �nal user to interact with the system;

Figure 1.1: Pervasive system key components taken from [1]

Let's now focus on the model just presented. Maybe the most important application of

this concept are the Wireless Sensors Networks. Such pervasive systems are composed of

devices such as RFID tags, sensors, PDA and actuators linked together using a pervasive

network (both cabled and wireless). WSNs have come under footlights, especially in the

recent years, thanks to their rapid di�usion in many environments [1]:

� military applications (e.g.: battle�eld surveillance, monitoring friendly and hostile

forces, etc);

� environmental applications (e.g.: forest �re detection, �ood detection, rockfall pre-

vention [3]);

10

Page 12: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

� home applications (home automation);

� health applications (remote patient surveillance);

� other applications (detecting car thefts, vehicle tracking system, etc).

1.2 Challenges of WSNs

According to many authors [1, 9] the WSNs present a series of challenges that every ad-hoc

pervasive middleware should overcome and where the research is today focusing on:

Power saving support : this challenge is mainly related to the hardware adopted to realize

the devices. In fact, from the energy point of view, the most delicate phase of a device

is represented by the transmission of sampled data, that is the most power consuming

activity among the all ones performable by a device. With power saving support we

refer to the capability of the middleware to provide a series of algorithms and data

structures that globally take care of the quantity of energy consumed by the device,

and try to optimize the behaviour of the device to obtain the best performances. An

example of such algorithms can be found in [10].

While, as said, power saving support is mainly related to hardware, there are several others

challenges that concern the problem of how exploit the pervasive system in an e�cent way,

that are thus mainly related to software.

Heterogeneity support: di�erent sensors networked together imply di�erent technologies

to be managed simultaneously. This requires the developer to make di�erent proto-

cols able to communicate each others. For example, today sensors (Motes, Jennic,

Micaz to cite some examples) di�er each others particularly for computational ca-

pabilities, power supplying methods (power cord, battery, solar power) and data

gathering methods.

11

Page 13: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Con�gurability support: the network, especially if composed by hundreds of sensors,

should be able to be easily con�gured, possibly at runtime if needed.

High level integration: the pervasive middleware that is going to manage the underlying

(pervasive) network must expose an high level interface, allowing the user to easily

write application or to integrate the WSN with information systems.

Flexibility and application reusability: Even if the developer could be able to succeed in

managing heterogeneity, his solution could still be bound to the particular network

he is dealing with. The risk lies in writing an application completely customized

for a particular network, making it unusable for another network although slightly

di�erent.

Low Level support and Data gathering mechanisms: : These problems are somehow

linked the heterogeneity support. Even if a middleware could be able to support

heterogeneity, what about the programming e�ort needed to add a new device and

make data exchange possible? With Low Level Support we indicate the capability

of a pervasive middleware to �assist� the programmer in this e�ort, reducing the

overall quantity of source code to be written, providing data structures, algorithms

and, more in general, facilities that relieve the programmer of this task. Such type

of �assistance� includes the data gathering mechanisms. With these terms we refer

to the capability of the middleware of providing facilities to retrieve data from the

pervasive system; in case of absence of these mechanisms the programmer is otherwise

obliged to design and implement the code necessary to ful�ll this fundamental task.

1.3 PerLa project: introduction and goals of the project

During the recent years, academic interests have taken into consideration the aforemen-

tioned challenges with many proposals explored in details in Chapter 2. PerLa (PERvasive

12

Page 14: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

LAnguage) project �nds its location in this set of proposals. Born within ART DECO

project [11], the project aims at completely ful�lling the previous challenges. PerLa makes

real the possibility of managing systems composed of sensors belonging to di�erent tech-

nologies, allowing devices to be added and removed �on the �y�, without a�ecting the whole

system. Finally PerLa completely hides the complexity which derives from this goal to the

�nal user.

The following chapters deeply analyze the di�erences between PerLa and other projects

in order to underline the strengths points of PerLa compared to the other projects. In

Chapter 3 the internal structure of PerLa will be presented, in order to show where this

thesis �nds its location within PerLa project.

13

Page 15: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

14

Page 16: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

2 State of the art

As said before, PerLa is not the only project aiming at undertaking the challenges exposed

in Chapter 1. PerLa design started, during its �rst phases, searching for the potential lim-

itations of other projects. Among the broad collection of such projects, the concentration

was focused on the followings:

TinyDB [12]: this is the �rst and the most famous proposal to manage pervasive systems.

Its most important feature lies in the view of the pervasive system as a database,

allowing the user to query the system using a normal query language. Since this

approach has been demonstrated being e�cient, PerLa completely shares such vision.

But TinyDB is still limited in dealing with homogeneous devices, and thus only

partially solving the problems exposed in Section 1.2.

GSN [13]: GSN is a scalable, lightweight system which can be easily adapted, even at

run time, to new type of sensors, thus allowing the dynamic recon�guration of the

system. Heterogeneity is supported, but it completely lacks of Low Level software

support.

DSN [14]: DSN uses a completely di�erent approach compared to the other middlewares

proposed. The whole system, in fact, has been built using the declarative language

Snlog (a dialect of Datalog), used both for the data acquisition and for the net-

work and transmission management. This project lacks the important support of

heterogeneity, fundamental in today WSNs.

15

Page 17: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

SWORD [15]: Developed by SIEMENS, SWORD is a solution to control remotely a set

of heterogeneous devices. The sensors (placed on the technological devices to be

monitored) communicate with a central station where the SWORD application is

running, allowing the �nal user to continuously monitor the situation remotely and

to intervene if necessary. Also this project lacks of Low Level Software and, moreover,

it doesn't provide data gathering mechanism since this task is left to the user.

As it can be seen from Table 2.1 many of these projects present some lacks that PerLa

completely supports.

TinyDB GSN DSN SWORD PerLa

Heterogeneity support

Data gathering

mechanisms

High level integration

Flexibility & Reusability

Con�gurability support

Low Level SW support

Power saving support

Table 2.1: Features supported by languages for WSNs.

As mentioned in Chapter 1 the challenge to achieve heterogeneity support is strictly

linked to the concept of Low Level Software Support. It's now worth to analyze this

relationship, with the help of the following �gure:

16

Page 18: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 2.1: PerLa vs. others projects. A comparison

What emerges from this �gure is that being able to support heterogeneity doesn't al-

ways imply to be able to do the same with Low Level Software support. The reader might

remember the physical principle a�rming that, when dealing with two di�erent physical

properties, the more precise one property is measured the less precisely the other will be1.

Although heterogeneity and Low Level software support are not physical properties, this

principle can be used as a metaphor to �rmly understand that struggling to completely

achieve one of the two can lead to fully unsupport the other and viceversa. This was the

case of DNS, GSN, SWORD and TinyDB projects that decided to concentrate the e�orts

only on one of the two features, as shown in Figure 2.1. PerLa project is, instead, able

to support both of them: such important feature will be fully described in the following

chapters.

1The Heisenberg uncertainty principle

17

Page 19: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

The dissertation continues now focusing on PerLa architecture, explaining how all the

previously mentioned challenges are ful�lled.

18

Page 20: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

3 PerLa architecture

This chapter focuses on the PerLa architecture that allows to ful�ll all the challenges

exposed in Chapter 1. The dissertation follows a top-down approach starting from looking

at PerLa from the most general point of view. In this way, we can a�rm that PerLa is

composed by two interfaces, as exposed in the following �gure:

Figure 3.1: PerLa interfaces

� a �high level� interface for the �nal user, through the de�nition of a language able to

manage the pervasive system;

19

Page 21: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

� a �low level� interface to dialogue with the multitude of devices (and technologies)

today available, achieving also the objective of relieving the programmer from the

arduous task of managing the di�erent technologies of such devices. This goal is

ful�lled with the implementation of a middleware.

Following the top-down approach it's now worth to go deeper into details, focusing on

these two, brie�y introduced, interfaces and how their combined action allows PerLa to

ful�ll all the challenges exposed in Section 1.2.

3.1 The high level interface (language)

The language allows to manage the pervasive system and to declare how the user intends

to exploit the WSN. It particularly allows the user to specify:

� What information must be retrieved from the WSN

� When and how such information must be retrieved

� Where the desired information should come from (i.e: to choose which sensors should

be used, if the whole set composing the WSN or only a part of them satisfying a

particular characteristic)

Since PerLa language was �rstly designed [2], the objective of semantic power just ex-

posed has required the design of a syntax that should have to be easy, fast to write and

understandable to the �nal user. At the same time the language needed to be powerful:

PerLa language was in fact designed trying to support both �standard� queries (i.e. queries

written to retrieve some information from the WSN), and actuation queries (i.e. queries

capable to set some parameters on sensors). Using an informatics paradigm it can be said

that PerLa is expected to be able to �read� and �write� on the pervasive system. Moreover,

since PerLa shares the vision of pervasive system as a database, the syntax is SQL-like

20

Page 22: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

although enriched with a series of operators and clauses. In order to achieve the required

semantic power, the language was split in three logic parts:

� Actuation Language, allowing the user to set a number of parameters on one or more

devices;

� Low Level Language, de�ning the behaviour of a single device (or a group of them),

specially focusing on sampling operation and some aggregation operators;

� High Level Language, allowing the manipulation of data stream originated from low

level queries.

It's immediate to understand that each language de�nes a certain number of operators and

generates a proper query type:

� Actuation Queries (AQ)

� Low Level Queries (LLQ)

� High Level Queries (HLQ)

PerLa queries are then composed of a subset of the aforementioned three queries written

in their proper languages, leaving to the user the power and �exibility to choose how the

pervasive system can be exploited. In the following subsections a description of each query

type (and its correspondent language) is given. Finally, in Subsection 3.1.3, a complete

example of a quite complex PerLa query is shown.

3.1.1 Actuation and High Level Queries

Let's focus �rstly on Actuation Queries. As said before, they allow the user to be able

to set a certain number of parameters on a sensor (notice that a parameter can be a

command, used, for example, to start an actuator installed on a node, to turn o� power

supply and even to upgrade the node �rmware). The High Level Queries are instead used

21

Page 23: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

to de�ne data structures that can be used both to contain the �nal results of a query, and

as intermediary structures for other following manipulations. In PerLa two data structures

have been de�ned:

� Stream: it's the common type of data structure and can be seen as an unbound

table that collects records produced by LLQs or by others HLQs (especially when

this data structure is used as intermediary, as mentioned before);

� Snapshot: it's a data structure representing a set of records produced in a given pe-

riod and it is mainly used to implement pilot join operations which will be presented

in the next subsection.

3.1.2 Low Level Queries

The statements composing the LLQs allow to precisely de�ne the behaviour of a single

device, and their main goal is the de�nition of sampling operations and the application of

some SQL operators on sampled data (e.g. aggregation, grouping and �ltering). Their role

within PerLa query is fundamental and every LLQ is composed of at most four sections:

Sampling section

This part speci�es how and when the sampling operation should be performed. The syntax

allows to specify both a time based sampling (i.e. at a certain frequency) and an event

based sampling (e.g. data is sampled only when an event occurs, such as the presence of a

RFID sensor in an established area). Sampling is the operator used to accomplish these

functionalities: it can be followed by an events list whose veri�cation is used as a condition

to execute sampling (event based sampling), or by a if-every-else statements block which

allows to de�ne a precise sampling frequency upon the veri�cation of some conditions (time

based sampling, see Subsection 3.1.3 for an example). The refresh clause allows the user

to specify how often the sampling frequency must be evaluated again during the lifetime

22

Page 24: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

of the query, and can however be used only in time based sampling. Independently from

the chosen sampling type, the sampled values are appended into a local bu�er, until they

are manipulated by the data management section, described in the following.

Data management section

As mentioned before, this section has the role of managing sampled data in order to

compute query results. This goal is achieved through the use of the select clause, which

is performed upon one or more records currently present in the local bu�er: the computed

records are then appended to an output bu�er, ready to be sent to upper levels. In addition

to select clause, standard SQL aggregation operators are part of this section: avg, min,

max, count, group by and having clauses keep partially their SQL semantics [2], and

allow the user to specify how to manage the sampled data when the selection is performed.

Again, in Subsection 3.1.3 an example of selection and aggregation is given.

Execution condition section

This section de�nes the rules to establish if a certain sensor (or a group of sensors) should

participate to query computation. Currently two clauses have been de�ned to accomplish

this goal: execute if and the aforementioned pilot join clauses. Such clauses are

evaluated before the sampling section is executed, avoiding sensors (not involved in query

execution) to waste resources: the sampling activity, in fact, always requires power and this

could represent a problem on battery powered sensors. A dedicated component (see also

Section 3.3) is employed to choose which sensors will be taken into account to compute

query results. One �nal note is relative to the pilot join operation: it allows to de�ne

context-awareness and data tailoring capabilities in PerLa and more information about

this topic can be found in [16].

23

Page 25: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Termination condition section

The termination condition is the last block of an LLQ and allows the user to stop the

query computation when a certain amount of time is elapsed or a given number of sampled

values is collected. The terminate after clause is charged to accomplish such objective.

Complete EBNF of all statements presented here can be found in [2].

3.1.3 A PerLa query example

Having achieved the comprehension of language syntax and semantics, a quite complex

query example related to a wine transport scenario is presented here. The query is used to

keep temperature values under control, using sensors placed into the transport truck that

is currently nearest to a given point P.

Figure 3.2: Query (quite complex) example, taken from [2]

In this example LLQ and HLQ are highlighted with a red and a green rectangle re-

spectively. The reader can �nd all the PerLa keywords in bold, while a blue rectangle

24

Page 26: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

highlights PerLa statements which requires a physical computation to be analyzed: such

elements are called expressions. Every expression can be seen as a set of constant ele-

ments (number, attributes to be sampled, etc) called Constants and a group of operators

(addition, division, AND, OR, XOR, and many more) called Nodes [17].

3.2 The high level interface (middleware)

PerLa language needs to be fully supported by the middleware: a previous work within the

ART DECO project [18] studied a possible design (later chosen as e�ective) which proposes

a middleware architecture composed of three levels. The �rst level is the application level,

whose main purpose is to manage user interaction by receiving queries to be executed. A

logical level is inserted down below, in order to abstract from the underlying set of sensors.

Finally a physical level is used to send and receive data from sensors.

3.2.1 Application Level

This level is the front-end used by applications in order to access data coming from the

physical devices. It's composed of a parser [19], whose main purpose is to transform

the user query (written in text format) to another internal representation, more suitable

to be managed within PerLa middleware. This representation consists in a set of Java

classes [20]: the parser separates the three aforementioned queries types (AQ, HLQ and

LLQ), then a proper Java class is created for each SQL clause. The parser is also able

to recognize also expressions, giving a similar Java representation used for SQL clauses

[17], distinguishing between nodes and constants. The parser signals potential syntactic

errors and the query is ready to be pushed to the next level when the parsing operation is

successfully concluded.

25

Page 27: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

3.2.2 Logical Level

The logical level represents the core of the middleware. Such fundamental role derives from

the fact that this level contains a series of entities called logical objects, representing the

heart of the whole PerLa architecture. The main idea behind logical object concept is to

wrap sets of homogeneous sensors into a physical device abstraction called, indeed, logical

object (see Figure 3.3). As it can be seen from the �gure, logical objects allow interaction

with physical devices and the middleware through the de�nition of a suitable interface,

that provides three main functionalities:

� retrieving attributes

� �ring noti�cation events

� getting the list of supported attributes and events

Figure 3.3: Logical object abstraction

It is important to underline that with the introduction of the logical object concept, het-

erogeneity management challenge is achieved, since pervasive system can now be abstracted

as a group of logical objects also hiding complexity to the end users.

It's now possible to go deeper into logical objects, to better comprehend how its three

functionalities can be realized. In fact the logical objects implementation includes two

important components: a Low Level Query Environment, containing all the data structures

26

Page 28: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

and algorithms needed to compute LLQ result, and a Functionality Proxy Component

(shortened FPC ), charged of exchanging data with physical devices.

Low Level Query Environment

LLQ Environment is going to contain the real �engine� of LLQ execution as well as the

structures needed to accomplish such goal. This component is introduced here to highlight

its role within logical object and its relation with FPC. However more details about this

component can be found in following chapters since this thesis focuses on this important

part.

Functionality Proxy Component (FPC)

FPC is the object that really implements physical device abstraction within a logical ob-

ject. FPC is also the object that is charged to manage devices initialization and their

addition to the system. Such goal is achieved through a device self-description: device

type, capabilities, attributes that can be sampled and many other information are pre-

sented to the system using a xml �le. First studies about FPC component, together with

more details about the xml descriptor and the adopted communication and data exchange

protocols, are reported in [7], while a partial implementation and integration within PerLa

is explained in [8].

3.2.3 Device access (physical) Level

This level is mainly composed of a C library: it should be noticed that most of nowadays

sensors aren't capable of running Java code (and no code at all in the case of RFID

sensors tags). Such library has been designed with the purpose of minimizing the low level

programming e�ort required to the developer user to integrate a new technology in the

middleware: when programmers need to integrate a new device into PerLa, they just need

to extend and recompile the library and to de�ne an xml description �le, in order to make

27

Page 29: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

device and FPC dialogue possible. More information on library architecture can be found

in [6].

As important conclusion, it can be noticed that the logical level exposes interfaces on

one hand to a Java �world� (that's to say, the application level), and on the other hand to

a C �world� (the device access level, through the FPC) making possible all the objectives

pre�xed in the introduction of the chapter.

28

Page 30: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

3.3 First PerLa deployment: rockfall prevention

Having introduced both the language and the middleware features it's now possible to

give an overview on PerLa functioning, analyzing how these two aspects interact between

themselves. Let's suppose that a query (indicated with a red arrow in Figure 3.4) is injected

in PerLa.

Figure 3.4: PerLa middleware overview

As a preliminary passage, the textual query is analyzed by the parser obtaining the

Java representation explained in Section 3.2.2. LLQs obtained from parser can now be

injected in the proper logical objects using a dedicated component called Logical Object

Registry ( or simply Registry); it contains references to logical objects currently active in

29

Page 31: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

the system, allowing the LLQs injection process to work correctly. Query sampling and

data management sections (blue arrows) are then activated: thanks to the combined action

of the LLQ Environment components and Low Level Software (that's in fact the name of

FPC and C library considered together) the data can be received, computed and grouped

again into an HLQ Executor, where the query �nal result is computed and presented to

the user (hatched blue and light blue arrows).

It's now worth to brie�y analyze where, within a pervasive system, every single com-

ponent of a PerLa query is physically executed. The parsing of the query, as well as the

evaluation of the set of logical objects to be taken into account for query computation

(evaluating the execute if clause), are executed by the highest part of the middleware,

that is typically deployed on the server machine used to monitor the pervasive system.

The high level execution is, at the same way, executed on the same machine, since it ma-

nipulates data streams originated by LLQs (but a distributed version of the HLQ engine

can probably be designed). The location where a LLQ execution is performed is, indeed, a

more delicate issue and the computation capabilities of the nodes composing the pervasive

system must be taken into account. If node capabilities allow the node itself to run a Java

virtual machine, both Low Level Software (i.e.: FPC) and Low Level Query Environment

can be deployed on the node. This is the case of powerful devices, such as netbooks, PDAs

or ad-hoc boards, but this is not the most general case. As an example, consider the

pervasive system that will be employed in [3], where small sensors called Acquisition and

Elaboration Units (shortened AEUs) will be used to monitor and prevent rockfalls. As can

be seen from Figure 3.5, these small devices are linked together using CAN-bus, while a

Local Coordinator acts as a master on the bus, sending collected data to a gateway through

a ZigBee network. The single AEU, as well as every Local Coordinator, are not powerful

enough to run a Java virtual machine: Low Level Execution must than be achieved using

the nearest device (connected to the middleware) that is able to host the Java virtual ma-

chine. In Figure 3.5 such device is called Gateway and it hosts the Low Level Software as

30

Page 32: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

well as the Low Level Query Environment. As mentioned before, the High Level Execution

is �nally achieved on the machine used to monitor the pervasive system, connected to the

Gateway using a 5 GHz TCP/IP radio bridge.

Figure 3.5: An example of a pervasive system [3]

As conclusion, it must be noticed that the situation presented is very common in most

of pervasive systems; thus, as long as the nodes aren't able to compute Low Level Query

Execution, a fully capable machine needs to be introduced in order to supply such lack of

computational capabilities. If, instead, a node is �intelligent� enough to execute the LLQ,

the computation is endorsed by the node itself. Finally notice, as an extreme case, that

nodes could only partially support the execution of a LLQ, executing only a subset of its

four sections: again, the nearest capable device will supply for the sections that can't be

directly executed on the node.

31

Page 33: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

32

Page 34: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

4 LLQ Execution Design

This chapter is focused on the design phase of the Low Level Query Execution. The

dissertation will highlight the need of the Low Level Query Execution within PerLa, �nally

describing the design choices adopted.

4.1 The importance of LLQ Execution

Before entering into LLQ Execution details it's worth to analyze this important aspect.

As said in previous chapters, the FPC component must be placed on the most capable

device nearest to the pervasive system since it's written in Java and thus requires a Java

Virtual Machine to operate. Since FPC is strictly linked with the device, delegating the

LLQ Execution to the FPC component might sound as the better choice to achieve LLQ

Execution. During FPC �rst studies, this component was always designed as the key

component �only� charged of masking the complexity of the pervasive system to the upper

level, following the well known divide-et-impera software engineering approach. This is

why FPC tasks don't include data collection mechanisms nor data processing capabilities.

This fact brings us to the important conclusion that one (or more) PerLa component(s)

must be employed to ful�ll LLQ Execution. The following example enforces this important

statement, and is exposed in Figure 4.1

33

Page 35: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 4.1: Example with �dumb� and �smart� devices

In the previous �gure an hypothetical PerLa instance is used to monitor a sensor network

composed of simple devices able to sample only temperature, using the query depicted

in the same �gure. On the sides of the �gure we �nd two scenarios: on the right we

suppose that the devices composing the network are fully capable devices, i.e. capable

to manipulate data, such as calculating a particular mathematical functions as well as

aggregates (avg,max,min,count etc). Such devices are called �smart�. On the left side,

indeed, we suppose that our devices are only able to sample data and are completely

deprived of any computational power. We call these devices �dumb�. Let's now focus on

the example. The query requires to compute an average value to be successfully completed,

but, as said before, FPC tasks don't include data processing. This combination yields an

important conclusion: if the device is not powerful by means of computational power, the

query cannot be executed. This is why LLQ Execution is so important and necessary

34

Page 36: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

within PerLa: it supplies the computational lacks of the �dumb� sensor. This fact is

even more important since most of nowadays sensors have not (or have partially) the

characteristics that make them a �smart� sensor.

4.2 LLQ Execution design

Now that the importance of LLQ execution has been clari�ed, the LLQ Execution design

can be presented. As mentioned in the previous chapters it can be summarized, from a

general point of view, as composed of two important parts:

� Data collection (from the pervasive system);

� Data processing and result computation.

Each part requires a complete design process whose details are presented in the following

sections. Before entering into such details it is possible to analyze how these two parts

cooperate themselves to achieve LLQ Execution, following the usual topdown approach.

Since it's the �rst to be performed, let's �rstly focus on how to obtain data, executing the

sampling section of an LLQ. Collecting data means, in PerLa, to �ll a local bu�er with a

certain amount of data retrieved from the pervasive system (precise number however di�ers

from query to query). Design phase started studying how this process could be performed,

introducing the idea of the DataCollector entity, that is primarily charged, as suggested

by the name, to the collection and �ltering (where clause) of values from the pervasive

system. Once the local bu�er is �lled with values the data management section of the LLQ

can compute results. This task is completed by another entity called LLQ Executor that

withdraws the local bu�er values, process them and append the �nal results to an output

bu�er. This is obviously the most general view of the LLQ Execution and is highlighted

in the following �gure.

35

Page 37: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 4.2: Entities designed to achieve LLQ Execution

Hereafter a complete example of how LLQ Execution is supposed to operate, as well as

what has just been discussed, is presented. Let's suppose that PerLa is operating on a

pervasive system composed of four nodes, equipped with the following on-board sensors:

� Node A: temperature, pressure;

� Node B: temperature, pressure, humidity;

� Node C: humidity;

� Node D: brightness.

Let's initially suppose that only one query, reported in 4.1, is injected into the system.

36

Page 38: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Algorithm 4.1 First query

CREATE STREAM TempNodeA (TIMESTAMP TS , FLOAT temp ) AS

LOW:EVERY 20 mSELECT TS , AVG( temp ,20 m)SAMPLING EVERY 10 mEXECUTE IF node_id = "Node A"

After the query analysis conducted by the parser, the Logical Object Registry is used to

choose which FPCs will be involved into the query computation, evaluating the execute

if clause presented in previous subsections. In this example the registry will contain

references to four FPCs (i.e. FPC A, FPC B, FPC C and FPC D), corresponding to

the four nodes presented above. Consulting the registry it is possible to discover that only

FPC A satis�es the execute if clause of the query. At this point the computation will

be completed in the following steps:

1. Every 10 minutes a temperature value is sampled (according to the sampling clause)

by FPC A and inserted to a proper local bu�er, along with proper timestamp and

ID values;

2. Every 20 minutes the average value (reported in the select clause) is computed

and inserted into the output stream TempNodeA. Notice that streams (and, more in

general, also snapshots) endorse the role of output bu�er mentioned in Subsection

3.1.2;

Note that points 1 and 2 respectively correspond to DataCollector and Low Level Executor

entities, and they allow the query to be computed correctly. This situation is reported in

Figure 4.3.

37

Page 39: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 4.3: LLQ Execution (single query)

This example has been focused on a single query running into the system: in order

to explain how LLQ Execution deals with multiple queries running simultaneously, let's

suppose that another query, hereafter reported, is injected into the same system of the

previous example.

38

Page 40: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Algorithm 4.2 Second query

CREATE STREAM TempPresHum (TIMESTAMP TS , FLOAT temp , FLOAT pres , FLOAT hum) AS

LOW:EVERY 5 mSELECT TS , temp , pres , humSAMPLING EVERY 5 mEXECUTE IF EXISTS ( p r e s )

As said for the �rst query, the Local Object Registry is used to discover which FPCs

will be involved: since this second query requires to be executed on all the nodes having

a pressure sensor on-board, the registry returns both FPC A and FPC B as result. The

computation of this query then requires the following steps to be completed:

1. Every 5 minutes, temperature, pressure and humidity values are sampled through

both FPC A and FPC B, and inserted in two distinct local bu�ers. Notice that

FPC A doesn't board the humidity sensor but it is anyway included into query

computation since, from the point of view of the Local Object Registry, it boards the

required pressure sensor. The values inserted by FPC A into its proper local bu�er

will then contain a null value in the humidity �eld.

2. Every 5 minutes the data management part is activated on each of two local bu�ers

and results are inserted into the �nal stream TempPresHum.

This situation, that includes two queries running simultaneously, is reported in Figure 4.4

and highlights some important aspects of LLQ Execution, explained after the �gure.

39

Page 41: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 4.4: LLQ Execution example (two simultaneous queries)

40

Page 42: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

In fact, for each FPCs reported by the Local Object Registry, a proper DataCollector

must be used; moreover, for each DataCollector, a proper local bu�er, as well as a proper

LLQ Executor, need to be employed to compute successfully the query result. This �rule�

can be summarized saying that an instance of the tuple <DataCollector, Local Bu�er, LLQ

Executor> needs to be instantiated for each FPC involved into a LLQ computation. As

conclusion, it can be noticed that a LLQ produces a �ow of records potentially coming

from di�erent FPCs, but each of these records is necessarily the result of a computation

made on a set of attributes sampled on the same FPC.

4.3 DataCollector entity design

It's now possible to focus the attention on the design of the DataCollector, starting from

its lifecycle. DataCollector needs to be created and modeled on the LLQ that is going to

serve: sampling method (time/event based), where and termination condition, sampling

frequencies are information that DataCollector must be informed about before it can start

operating. DataCollector modeling is carried out at the beginning of its lifecycle during

query initialization phase thanks to a speci�c PerLa component called Query Analyzer : it

receives the Java representation of the query as it is produced by the parser and it creates

every structure needed to compute the query (see also the following example). Among these

structures an ad-hoc DataCollector is created for each query analyzed. QueryAnalyzer is,

in fact, able to navigate through the received Java structures, searching for terminate

conditions, sampling type (time/event based), refresh and where clause conditions,

and produces as output a DataCollector whose activity diagram is �tted for the query

analyzed. Once DataCollector is created it can start operating: the following subsection

focuses on this aspect.

41

Page 43: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

4.3.1 DataCollector activity

Data collection activity is represented in Figure 4.5 using an uml activity diagram.

Figure 4.5: Data collection uml activity diagram

42

Page 44: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

In this �gure, the DataCollector creation discussed before is represented by the �query

initialization� block. The next step is the execution of sampling section according to the

sampling type:

� Time based sampling: DataCollector waits for next sampled set of values according

to sampling frequency. The if-every-else block is used to decide the sampling

frequency to adopt.

� Event based sampling: the event causing sampling is waited for until it occurs (on

event clause).

Indi�erently from the employed sampling type, a set of sampled attributes (retrieved from

the pervasive system) is available when the sampling section execution is concluded. The

where clause, which is the only clause of data management part that data collection has

to deal with, is then activated, allowing DataCollector to discard values according to a

condition speci�ed in the clause: only those records which satisfy the where condition

are inserted into the local bu�er. DataCollector next step is the management of the

refresh clause, in order to discover potential changes in the sampling frequency. The �nal

DataCollector task is the evaluation of termination condition, to detect if data collection

process has to be terminated. According to this condition, DataCollector is able to stop

its activity; otherwise, a new set of values is waited for and the cycle shown in the Figure

4.5 is repeated again.

4.4 Data processing and result computation

While DataCollector is operating, the sampled and �ltered data are continuously appended

into the local bu�er and are ready to be transformed by the data processing mechanism

into query results. As said in previous sections this task is ful�lled by the LLQ Executor

component, whose activity is reported in the following uml diagram.

43

Page 45: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 4.6: LLQ Executor activity uml diagram

44

Page 46: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

The �rst step is to make persistent the values that are within the local bu�er. At this

point, in fact, sampled values �live� in the volatile memory of the physical machine that is

hosting the running PerLa instance. Persistence is realized by saving the sampled values

in a SQL table of a chosen DBMS running on the same machine. Currently a dedicated

project is dealing with persistence, and the interested reader can �nd more details in [6].

Once that data is made persistent, LLQ Executor evaluates if the selection to be performed

is time or event based. Independently from the selection type detected, the LLQExecutor

enters the core of its activity: the group by clause is evaluated producing the division of

the local bu�er according to the clause condition. An example of how the local bu�er is

split is reported in the following �gure:

Figure 4.7: group by clause example, taken from [2]

After the evaluation of the group by clause, LLQ Executor evaluates the up to clause

in order to discover if there's a maximum number of records to be inserted into the �nal

structure (select clause is identical to the SQL one, and the number of selected records is

unknown when the clause is executed). Finally the having clause acts as �lter to discard

data according to a particular condition. At this point the select clause can be executed

with two potential result types: if no record is produced then the user can choose to insert

anyway a default record into the �nal data structure (insert default) or to take no

45

Page 47: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

actions (insert nothing). The values contained in the local bu�er that have been used

in this selection are now unuseful and can be eliminated. Like DataCollector, the LLQ

Executor cycle must be repeated again if LLQ has not been terminated. In the same way

LLQExecutor is customized for the query that it's going to serve by the Query Analyzer

component introduced before. LLQ Executor and DataCollector �nally share the same

end: when the LLQ is terminated they're eliminated.

46

Page 48: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

5 FPC interface design

From the point of view of the �ow of data, the collection of sampled values presented in the

previous chapter represents the �rst step of a long chain that ends with the presentation of

the �nal results to the �nal user that has injected the query. At this point of dissertation

it should be clear that FPC is linked to three fundamental PerLa components:

1. The device that is abstracting and thus representing

2. The Logical Object Registry

3. The DataCollector entity

Figure 5.1: FPC relationships with other PerLa key components

A well-de�ned interface required to be designed towards every one of these entities, and

is explained in the following subsections.

47

Page 49: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

5.1 Interface towards DataCollector

Let's then focus on the interface used by FPC to communicate with the DataCollector. As

mentioned in Section 4.2 DataCollector lifecycle begins thanks to the activity of the Query

Analyzer component. Since for each FPC involved into query computation an instance of

the tuple <DataCollector, Local Bu�er, LLQ Executor> needs to be created, it requires

the FPC interface to expose the following capabilities:

� the capability of creating a dedicated channel, where sampled data can �ow within,

starting from the FPC and ending with the DataCollector.

� the capability of allow forced sampling. Such capability is required by the refresh

clause, to retrieve a group of records (or a single one) in order to evaluate a new

sampling frequency.

The Section 6.2.1 will explain what kind of problems such capabilities imply.

5.2 Interface towards the Logical Object Registry

The relationships between the FPC and the Logical Object Registry can be divided into

two separate sides: one, when every FPCs is created and another when a particular FPC

is chosen (by the Local Object Registry) to collaborate in computing query results. The

dissertation is then divided for each one of such phases.

5.2.1 Interface exposing FPC attributes

Since the FPC represents the abstraction of every device type in the WSN, the Local

Object Registry (LOR hereafter) needs to be informed about what kind of device the FPC is

currently representing. This aspect is very important, since the execution condition section

of an LLQ might require to query the registry, aiming, for example, at �nding a particular

device which responds to particular characteristics (execute if clause). The reader might

48

Page 50: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

state that this goal could be satis�ed when PerLa �rstly starts up, simply polling the

present FPCs. It must instead be remembered that PerLa supports heterogeneity with a

plug&play fashion: if the user, at instant t0, plugs in a new device that requires a new FPC

not present in the system at t0, this FPC needs to be created and added to the system. A

series of information need then to be communicated to the LOR by the FPC just created

and added. This �ow of information requires the FPC to implement the capability of

communicate what device is representing, by means of communicating how attribute can

be sampled by the abstracted device. All the queries that will be injected at instants t > t0

will then be able to take advantage of the device added.

5.2.2 Interface for query injection

When one (or more) FPC is selected into the set of FPCs needed to compute the result

of a speci�c query, the LLQ must be registered on the FPC. In other words, FPC needs

to expose the capability to �receive� and keep an internal trace of the LLQs at whose

computation is collaborating. Since is the LOR that discriminate which FPCs are meant

to execute an LLQ, this part of the FPC interface must be composed of two capabilities:

� The capability to allow the LOR to �register� the LLQ on the FPC and to signal

it to start computation (i.e. the collection of data and the execution of the data

management part of the LLQ)

� The capability of �unregister� the LLQ from the FPC, according to the conditions

present in the termination condition section of an LLQ (if present)

5.3 Interface towards the abstracted device

As said in Subsection 3.3 the FPC is the component which also deals with the device level

of the middleware. For this important task the FPC interface must provide the following

capabilities:

49

Page 51: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

� The capability to discover the data format adopted by the device (Little/Big Endian,

number of bits adopted to represent a data type, etc) and to convert it into the format

adopted within PerLa

� The capability to establish a channel between the FPC and the abstracted device:

this capability was designed and partially implemented in [8], but a precise interface

needed to be designed.

50

Page 52: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

6 LLQ Execution Implementation

In this chapter the implementation of the design aspects proposed in the previous chapter

is explained. Until now, terms like �query�, �sampled values� and �structure� have been

used in a general fashion and always referring to the functionalities of these components.

Section 6.1 is intended to explain what these components actually are, and which data

structures are involved.

6.1 Data structures implementation

The �rst data structures that are examined in the following are those charged of repre-

senting sampled values generated by FPC.

6.1.1 Data structures for records

Let's concentrate on sampled values: they can be grouped in records, each of them com-

posed of a set of �elds representing a sampled value. A suitable record structure must

be de�ned, starting from the internal structure of each �eld, in order to allow an e�cient

access to the contained data. The classes presented here have been designed trying to

accomplish this purpose.

QueryFieldStructure class

This class contains information about the internal structure of a record �eld

51

Page 53: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

private Class<?> pFieldType;

private String pFieldName;

private int pFieldIndex;

The �rst variable represents the type of the value contained in the �eld; the second one

represents the name of the variable contained in the �eld, while the third is the index

number of the �eld within the record. As an example, suppose that a �eld is used to

represent a temperature. In this case the three variables will assume the following values:

�ConstantFloat.class� (a Constant to represent �oat values [17]), �temperature�, and �0�

(since we're talking about a single �eld in a record). Some methods (setter/getter) have

been added to the class, allowing to read and write the variables.

RecordStructure class

This class de�nes the precise structure of a record and maps the name and position of

each �eld into the record. Mapping is accomplished using HashMap built-in Java class, as

shown.

private HashMap<String, QueryFieldStructure> pRecordStructure;

The �rst argument of the HashMap is the name of the attribute (and so of the �eld)

that has been sampled (e.g. �temperature�). The second argument is the reference to the

�eld structure whose name is given as the �rst argument. An internal method has been

implemented in order to retrieve the index of a �eld given its name (this is very useful, for

example, when we want to obtain the index of the �timestamp� �eld, if present). A similar

method has been implemented in order to retrieve a �eld given its index.

52

Page 54: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Record class

This class is deputed to physically store sampled data. Its state is composed of

private Constant[] pConstant;

private QueryRecordStructure pQueryRecordStructure;

The array contains the constants (i.e., the actual sampled values), while the second

variable is the record structure. Methods to retrieve record contents have been implemented

as shown in the uml diagram in Figure 6.1. This �gure is a summary diagram representing

the interaction among the three classes presented before.

Figure 6.1: Data structures for records uml diagrams

53

Page 55: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

NodeLogicalObjectAttribute class

NodeLogicalObjectAttribute [17] is the object charged of representing attributes (always

contained in expressions) of a query, and it has been designed and implemented only at a

mockup level. A more detailed implementation has been achieved in this project. When a

textual query is injected, the parser creates, for every attribute found, an instance of this

class �lling two variables:

private String pIdenti�er;

private Constant pValue;

Only the attribute name is initially �lled since is the only information known at parsing

time (pValue still needs to be sampled and its value is unknown). The second variable will

be �lled only at LLQ execution time, when the value will be physically retrieved. This

class exposes classic getter/setter methods, that are used during expression evaluation by

an ad-hoc class explained in the Subsection 6.1.3.

6.1.2 Data structures for query computation

Records presented in the above section must be inserted into suitable structures before

being computed (in the case of local bu�er) or sent to the upper level (in the case of

output bu�er). The Bu�er class is used to achieve this goal.

Bu�er class

Even if local and output bu�ers have di�erent roles, their functionalities can be accom-

plished by the same class. The storing function is realized using Java ArrayList class which

allows dynamic insertion and deletion, automatically �tting the size of the bu�er whenever

a record is appended or removed:

54

Page 56: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

private ArrayList<Record> pRecordSet;

The real importance of bu�er class lies in the methods that are provided to navigate

through a set of records. The following methods are de�ned

public Iterator<Record> recordsWindowIteratorByIndexes(int parInit, int parEnd)

public Iterator<Record> getIterator(Timestamp parTimestamp, long parDelta)

The �rst one allows to obtain a set of records (from parInit index and ending with parEnd

index) and, more important, to navigate through it using the iterator functionalities o�ered

by Java. This method is particularly useful when computation of query results needs more

than one record to be completed.

The second method is similar to the previous one, but di�erent parameters types are

used to iterate on the bu�er: a starting timestamp is given as the start point and a set

of records, ending at timestamp parTimestamp + parDelta, is returned. This method is

useful when computation requires to operate on a group of records that have been sampled

within a certain interval (e.g avg(temperature,30 s)). Figure 6.2 shows the behaviour

of these two methods on a bu�er taken as example, coloring in light blue the set of records

returned.

55

Page 57: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 6.2: Di�erent iteration type on the same bu�er

It has been told that, when sampling is time based, sampling frequency can be selected

evaluating the if-else-every clause. SamplingData class has been implemented to satisfy

this need, as well as to represent all the information about sampling timings.

SamplingData class

This class is charged of representing the following information about the sampling:

� The if-else-every clause which allows to dynamically select sampling frequency;

� The refresh clause;

� The behaviour that the system is expected to follow in case of unsupported sample

rate.

The if-else-every clause is represented using

private ArrayList<FrequencyRecord> pRecordSet;

where FrequencyRecord is another class representing a single branch of the if-else-

every statement. Each FrequencyRecord contains a frequency value and the condition

56

Page 58: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

to be veri�ed to choose the sampling rate. The last entry contained in the ArrayList is

relative to the else branch and thus speci�es the sampling behaviour that should be used

when all the if conditions are not satis�ed. Finally, the other two points of the previous

list are represented using the following variables:

private UnsupportedSampleRateOption pBehaviour;

private Node pRefreshClause;

where the class UnsupportedSampleRateOption is a Java enum containing the two cur-

rently designed behaviours to follow[2] (i.e.: slow down and do not sample). This

class obviously exposes a number of getter/setter methods to access all this information.

Figure 6.3: uml diagram of the data structures for sampling frequency

57

Page 59: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 6.4: uml diagram of LLStack class

The dissertation now focuses on classes deputed to represent all the information about

an LLQ within the Low Level Execution Environment.

LLStack

This class represents the tuple <DataCollector, Local Bu�er, LLQExecutor> presented in

4.3.1. Its internal state is thus the following:

private DataCollector pDataCollector;

private Bu�er pLocalBu�er;

private LLExecutor pExecutor1;

The class exposes the usual getter/setter method to retrieve the desired content. Since,

as said in Section 4.2, an instance of the DataCollector entity has to be employed for each

FPC that is included into query results computation, the number of active tuple (namely

LLStack object instances) coincides with the number of FPC returned by the interrogation

on the Local Object Registry.

1Actually this class hasn't been implemented yet, since it's related to the data management part of an LLQ.

58

Page 60: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

QueryInformation class

This class represents the information needed by every query to be successfully computed.

It has been chosen to implement this class as abstract, grouping here all the aspects that

are in common among the three types of query mentioned in Chapter 1. This class, in fact,

simply contains a reference to the Statement class [20] whose information are going to be

represented. This choice o�ers the opportunity to extend this class on need, according to

the type of query whose information are going to be represented.

In the rest of the dissertation only the class representing information of LLQ are dis-

cussed. For completion it can be said that also classes for HLQ and AQ information

(respectively HQInformation and AQInformation classes, both inheriting from QueryInforma-

tion) have been de�ned, but their implementation has still to be achieved since this exuled

from the scopes of this thesis.

LLQDataCollectionInfo class

This class wraps the �general� information of a single LLQ focusing on information regard-

ing the data collection. It obviously inherits from the QueryInformation class presented

above. The reader might ask himself why there's the need of this class, having introduced

the SamplingData class. It must then be remembered that the DataCollector entity requires

other information to ful�ll its objectives further than the information about sampling, in-

cluding, for example, data regarding the refresh clause and data relative to the where

clause. More speci�cally, the class exposes the following information about a LLQ query:

� the expression representing the where clause

� the reference to a SamplingData object (if time based sampling is adopted)

� the reference to the Local bu�er where data are meant to be inserted

59

Page 61: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

� the reference to the FPC, where data will come from.

As usual, setter/getter methods have been implemented to allow retrieving all the infor-

mation mentioned above.

A LLExecutionInfo class has been planned and will be used when the data management

part implementation will be completely achieved.

The following Figure represents the relationship between the classes just mentioned above.

Figure 6.5: uml diagrams of classes used to represent information of a query

60

Page 62: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Environment class

This class can be seen as the container of every query (HLQ, LLQ, AQ) running in the

system. Like for QueryInformation, this class has been realized as abstract, allowing to

represent in one class all the information shared by HLQ, LLQ and AQ as the following

subsection explains.

As for QueryInformation class only the classes regarding LLQ are here discussed. Obvi-

ously a class inheriting from Environment class has been de�ned for HLQs and AQs has

been de�ned but its implementation exuled from the purposes of the thesis.

61

Page 63: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

LLQEnvironment class

This class inherits from the previous one and is deputed to wrap every single LLQ running

in the system (one instance of the class for every LLQ running). The internal state of this

class is the following:

private ArrayList<LLStack> pStacks;

that's to say all the stacks that are currently employed to compute LLQ results. The

class exposes getter/setter methods as can be seen from Figure 6.6.

Figure 6.6: LLQEnvironment and LLQInformation uml diagram

ActiveSession class

This class is the most general container of every Environment currently active in the system.

It has been de�ned as static, enabling it to be called without instantiating the object.

62

Page 64: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

For a better comprehension the following �gure represents what has been discussed until

now, and how the aforementioned classes relates each others.

Figure 6.7: ActiveSession, LLQEnvironments and LLStack relationships

6.1.3 Data structure for query analysis

As mentioned in Subsection 3.2.2 Query Analyzer must be invoked to obtain a set of

DataCollectors. Query Analyzer is a Java class and is presented hereafter.

QueryAnalyzer class

At this point of dissertation something more must be said about Query Analyzer. Ac-

tually its activity is not only limited to DataCollector creation. For every PerLa query

injected into the system, QueryAnalyzer is charged of analyze it to create all the structures

presented before. It essentially performs the following steps:

1. Query type separation phase: analyzes the query (analyzing the structure de�ned

in [20]) separating HLQ, LLQ and AQ.

2. Query information retrieval phase: For each query type identi�ed a proper

63

Page 65: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

QueryInformation object is built (i.e: LLDataCollectionInfo and LLQExecutionInfo for

the LLQs, or HLInformation for HLQs). Notice that for LLQ this step requires the

QueryAnalyzer class to query the Local Object Registry, in order to obtain a list of the

FPCs needed to compute query results.

3. Environment build phase: Once the QueryInformation classes are built, the Query

Analyzer completes its activity creating an appropriate number of Environment class

and adds them to the ActiveSession class.

Obviously the point 2. is the heart of the QueryAnalyzer activity and is hereafter described,

focusing on the creation of the LLQDataCollectionInfo class. To achieve the creation of this

class and its later insertion in a LLQEnvironment class, the Query Analyzer classes employs

a complete set of private methods created ad hoc to analyze queries, each one focusing

on a speci�c aspect. For example getWhereNode() method is used to obtain the where

clause, while getSamplingData() is used to obtain the sampling information. Called in

the right order this methods allow to build the Query Information classes for each query

type identi�ed. It's important to underline that acting this way, the QueryAnalyzer class

summarily endorses the role of a factory : in fact, considering the whole set of exposed

methods, the raw representation of a PerLa query designed in [20] is transformed in a series

of structures ready to cooperate to compute query results. This factory fashion is achieved

de�ning the class methods as static (enabling them to be called without instantiating

a QueryAnalyzer object), and calling the only public method registerQuery() passing as

parameter the the Java object representing the root of a query (as de�ned in [20]). In

the following a set of QueryAnalyzer methods is presented, virtually undertaking the three

points exposed above, simulating the injection of query. Note that parQuery is always the

Java object representing the root of a query. Let's then suppose that a query is injected

in PerLa.

1. Query type separation phase: this phase starts when the method registerQuery(Query

64

Page 66: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

parQuery) is called. Notice that this is the only public method available in the class,

and is used to achieve the factory fashion explained above. Once this method is

called, a set of sub-method is called to identify the three query types:

a) createLLEnvironment(Query parQuery);

b) createHLEnvironment(Query parQuery);

c) createSetEnvironment(Query parQuery);

At this point the original problem of analyzing a single query is divided in three

independent parts. For each one of the methods exposed, the query information

retrieval phase is then activated. This example will focus on what happens after

createLLEnvironment(Query parQuery) is called, since this is the case that is relevant

in this dissertation.

2. Query information retrieval phase: Let's then focus on the method createL-

LEnvironment(Query parQuery). This method, for each LLQ identi�ed, executes the

following steps (always calling proper methods):

a) It builds the object LLQDataCollectionInfo: this step requires a number of sub-

steps:

i. Querying the Local Object Registry in order to receive a list of FPC able to

execute the query;

ii. Building the SamplingData object, which requires to retrieve information

about where, refresh as exposed in the description of this class.

b) It builds the object LLQExecutionInfo.

c) It builds the object LLStack containing a certain number of DataCollector ac-

cording to the response of the Local Object Registry. It must in fact be remem-

bered that a single LLQ can run on multiple FPC, and for each FPC identi�ed

a DataCollector must be employed.

65

Page 67: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

3. Environment build phase: all the Environment classes built above are then in-

serted in the ActiveSession class and the computation of the query starts.

As conclusion uml class diagram is reported in Figure 6.8.

Figure 6.8: uml class diagram of QueryAnalyzer class

ExpressionAnalyzer class

This class is used to analyze expressions within PerLa queries searching for the elements

that must be sampled. Till now NodeLogicalObjectAttribute has been introduced as the

object which represents an attribute, completely ignoring how such object can be created

starting from the user-submitted query. ExpressionAnalyzer can answer this question: since

in [17] expressions are implemented as trees (NodeLogicalObjectAttributes or Constants are

always leaves, while operators are always Nodes) it uses recursion to navigate through the

expression �nding NodeLogicalObjectAttributes and returning them to the ExpressionAna-

lyzer caller.

As shown in Figure 3.7 this class exposes only one public method:

public ArrayList<NodeLogicalObjectAttribute> getNodeArrayList (Node parNode);

66

Page 68: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 6.9: ExpressionAnalyzer uml diagram

where parNode is the root of the expression Java representation. According to the num-

ber of children of the root, a private method (getFields(Node parNode)) is called on each

child. This method checks if a Node is an instance of NodeLogicalObjectAttribute class

and, if so, appends it to a Java ArrayList (checking and avoiding multiple insertion of the

same NodeLogicalObjectAttribute). Instead, if the child is an internal node, getFields(Node

parNode) calls recursively a proper method, depending on the number of children of the

internal node:

public void getFields (Node parFirstNode, Node parSecondNode);

public void getFields (Node parFirstNode, Node parSecondNode, Node parThirdNode);

public void getFields (ArrayList<Node> parArrayListNode);

Such methods di�er each others only for the number of children they deal with, but

they work in the same way: for each child getFields(Node parNode) is called again on all

its children, generating the recursion mentioned above.

ExpressionEvaluator class

This is the static class which physically computes expression evaluation. The static method

67

Page 69: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

public static boolean evaluateExpression(Record parRecord, Node parExpression);

uses ExpressionAnalyzer to retrieve the set of NodeLogicalObjectAttribute from parExpres-

sion and substitutes any recurrence of the obtained set with their counterparts contained

in parRecord, which is a record of sampled values. Such substitution is achieved �lling the

pValue �eld of NodeLogicalObjectAttribute (as described in Subsection 6.1): at this point

attributes have been substituted and expression computation can be executed calling the

getResult() method of Node class, as implemented in [17]. The (boolean) result of the

evaluation is then returned to the caller.

6.2 Data collection implementation

6.2.1 Communication structure implementation

Pipe class

Pipe class plays the role of a synchronized channel between two elements. Referring to

Figure 6.9 let's explain how synchronization is achieved, supposing that component A has

to send a message for component B.

Figure 6.10: Pipes

Since component B needs to wait for messages at the end of the pipe, a Waiter object has

been de�ned, whose main role is to wait for the next element �traveling� on the pipe: such

messages are called Waitable objects. When Waiter notices that a Waitable object reaches

the end of the pipe, it signals that a new message has arrived to its �nal destination: at

68

Page 70: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

this point the message can be removed from the pipe and sent to upper levels for further

modi�cations. At this time the Waiter begins again waiting for next message on the pipe.

It must be noticed that more than one pipe can be linked with the same Waiter: in fact,

one component can be interested in receiving messages from more than one component

at the same time. Synchronization is then a key element in Pipe, and it is insured by

enqueue() and dequeue() methods implemented within Pipe class: Waiter, in fact, must

manage a certain number of messages on multiple pipes, always avoiding data loss. Pipes,

then, allow two communicating PerLa elements to be decoupled: calling standard ad-hoc

methods to transfer messages introduces the risk of �freezing� the system if a message is

still not ready when the method is called, thus deteriorating global performances. Waiter

has been designed exactly to allow avoiding such a risk: instead of physically retrieving a

message, message itself is simply waited.

The following �gure shows the �ow of data after introducing the concept of pipes.

Figure 6.11: Data �ow using pipes

It's now possible to discuss the class implemented to achieve the data collection, the

DataCollector class, whose design was proposed in 4.3.1

69

Page 71: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

6.2.2 DataCollector class

DataCollector lifecycle as Java object begins with its constructor, which simply needs to

receive the LLDataCollectionInfo object as parameter. DataCollector class has been written

extending Java Thread Class, overriding run() method to implement data collection activ-

ity. The �rst step DataCollector takes is waiting (instantiating a Waiter) a �rst record.

Waiting a record requires DataCollector to open a pipe with FPC, calling the method

getNewOutputPipe() method on the FPC reference contained in the object LLDataCollec-

tionInfo received by the constructor. It's important to highlight that the use of pipes

allows DataCollector to manage time-based and event-based samplings in the same way:

Waiter, in fact, completely ignores which is the cause that generated the record it receives.

This can be easily understood by viewing �time based� sampling as a particular type of

�event based� sampling, with the only di�erence that in a �time based� sampling the ar-

rival instants of records are predictable (according to the adopted sampling frequency),

while they're completely aleatory in the �event based� mode. This particular character-

istic allows DataCollector implementation to manage the two sampling types using the

same portion of code. DataCollector is anyhow aware of sampling types di�erence and,

when in time based mode, it manages the setting of the sampling frequency as well as

the refresh clause. This task is achieved by a dedicate private (thus internal) method

called evaluateFrequency(). This method uses the �rst record received from the pipe to set

the sampling frequency on the device. The reader might be surprised that it's necessary

to wait for a �rst record to evaluate sampling frequency. Instead, it's perfectly reason-

able since evaluating the sampling frequency requires the evaluation of the if branch in

the if-every-else clauses: without a record of sampled data this couldn't be possible.

After that the sampling has been chosen, the evaluateFrequency() method calls setSam-

plingFrequency() method on its relative FPC. Next sampling frequency updates are instead

delegated to the information contained into the refresh clause (if present). This task is

accomplished with the de�nition of an inner class called RefreshHandler. This class extends

70

Page 72: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

the Java Thread class, and is able to read the refresh clause frequency contained into the

LLDataCollectionInfo object. According to this frequency, this class class calls forceSam-

pling() method exposed by the FPC, in order to obtain a set of sampled data. With these

records DataCollector is able to calculate the new sampling frequency and to set it calling

the aforementioned setSamplingFrequency() method. According to Figure 4.5, next step to

be performed is then the evaluation of the where clause using ExpressionEvaluator class:

if it is satis�ed, the record is inserted in the local bu�er (parBu�er) otherwise it's discarded

and terminate condition �nally discriminates if the cycle has to be repeated again or data

collection activity has terminated. A do/while block allows the whole cycle to be repeated

as many time as needed, until data collection activity ends.

71

Page 73: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 6.12: DataCollector uml class diagram

72

Page 74: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

7 FPC interface implementation

This chapter will focus on the implementation aspects regarding the FPC interface. Since

the interface will be used to link together the LLQ Execution with PerLa lower levels it

has been written in Java and consists in two Java classes:

� FunctionalityProxyComponent

� FPCDataStructures

Each class is going to be discussed in the following.

7.1 FPCDataStructure

This class contains a series of fundamental information needed by the FPC. The most

important is surely the couple

public �nal HashMap<Constant, String parName> pAttributes;

public abstract Constant getAttributeByName(String parName);

This couple is used to identify what attributes are abstracted by a the FPC component

and are used by the Local Object Registry to catalog the FPC, in order to later decide if

an FPC must participate into a query results computation.

73

Page 75: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

7.2 FunctionalityProxyComponent class

This is the class implementing the designed interface. This is why it has been created as

abstract as well as the methods inserted. This class must in fact must be extended and

widened dynamically by the FPC factory component according the provided xml �le as

explained in previous chapters. Let's start the description of the class with the internal

data structures:

private �nal FPCDataStructure pFPC�nalStructure;

The FPCDataStructure is the object presented in the previous section.

private �nal Pipe<AdapterFpcMessage> inputPipe;

private �nal Waiter<Pipe<AdapterFpcMessage>�> inputPipeWaiter;

are respectively the Pipe and Waiter objects presented in the Section 6.2.1, and are

used as channels to receive data from the sensor (or sensors) that the FPC is abstracting.

The Waitable object travelling through these Pipe objects is another Java object called

AdapterFpcMessage. This class is part of the complex framework that allows FPC to

communicate with the abstracted device and more details can be found in [7, 8]. For

this dissertation this object can be considered as a Java object encapsulating the value(s)

sampled by the abstracted sensor.

private �nal ArrayList<Pipe<Record>�> outputPipeArrayList;

This ArrayList contains the collection of Pipe objects that are used to link FPC to the

DataCollectors. The collection is necessary since it must be remembered that an FPC

could be involved into the execution of more than one LLQ (and thus, more than one Dat-

aCollector). When a DataCollector requires a new pipe it calls the getNewOutputPipe()

74

Page 76: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

method which has been included in this class.

Before the FPC can start operating, the LLQ that is going to be served must be registered

on the FPC using the method registerQuery(). In the same way when an LLQ has termi-

nated this method counterpart must be invoked calling unregisterQuery().

Once the LLQ has been registered values can be read from the device using a dedicated

method called readMessageFromAdapter(). This method has obviously been left abstract

since every device generates a record according to a set of rules typically decided by the

producer. A typical example is the endianess of the bytes representing a sampled value:

some producers use Little Endian, while others prefer to use Big Endian. Another exam-

ple lies in the number of bits that are adopted to represent an integer value (8,16 or 32

usually): the number varies from producer to producer. Since it's the FPC component

that complies with devices rules and not viceversa, the readMessageFromAdapter() method

must be extended from time to time by the FPC factory according to device abstracted.

The last important method is forceSampling(). This method is the implementation of the

desired ability of the FPC to withdraw a sampled value from the pervasive system to

reevaluate sampling frequencies. This is the only case, and the only reason in PerLa where

it is allowed to obtain a sampled value with the direct call of a method instead of using the

excellent decoupling method provided by pipes. Once a new frequency has been selected

it can be set using the method setSamplingFrequency(). The last two presented methods

are again left abstracted: again every device has its proper rule and these methods must

be expanded and �lled by the FPC factory component.

The class �nally includes start(), stop() and isStarted() methods whose use is evident. In

the following �gure the complete uml diagram is reported.

75

Page 77: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 7.1: FPC uml diagrams

76

Page 78: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

8 Conclusion and open points

The objectives of design proposed at the beginning of the dissertation have been fully

satis�ed with the introduction of the DataCollector and LLQExecutor entities and the

de�nition of a precise interface with lower levels, followed by the implementation of the

proposed design.

Some point still remains open:

1. The LLQExecutor entity has been designed but still needs to be implemented. Future

developments will focus on this aspect.

2. Following to point 1, QueryAnalyzer class still requires the implementation of a certain

number of methods to build LLExecutionInfo class as well as all the other structure

that will be needed. (e.g. to retrieve the set of �elds needed to compute select,

having and group by clauses). These functionalities were, in fact, out of the thesis

scopes.

3. ExpressionEvaluator class is still limited in evaluating a condition using one single

record. A complete implementation must be achieved when data management section

will completed, since it deals with aggregates (e.g. avg, min, group by, etc) whose

computation requires more than one single record to be performed.

4. Logical Object Registry still requires to be completely designed and implemented, as

brie�y mentioned in this dissertation.

77

Page 79: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Future developments should provide answers to the previous questions. Finally the whole

stack should be tested in a real scenario: PerLa is in fact currently scheduled to monitor

rockfalls in Lecco (MI) within Prometeo project [3].

78

Page 80: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Appendix A: PerLa console

The need of remote control

During the implementation phase of this thesis it came clear the need to be able to remotely

control an instance of PerLa. The need arises from a series of considerations, among the

others:

1. PerLa is currently under continuous development: implementing a new component,

or modifying an existing one always requires a test phase which includes to test

component interactions with the others. In PerLa almost every test is conducted by

simulating the injection of a query prototype in order to see if the system reacts as it

is designed to. Again this process requires to manually create a Java executable test

class deputed at creating all the objects necessary (parser, Logical Object Registry,

Query Analyzer, Environments and so on) as well as running the test itself. The

whole process needed to be sped up and easily performable.

2. In most of PerLa real implementation the �nal query results (i.e. the results com-

puted by the HLQs) are not directly exposed to the user but are �redirect� to other

applications for further modi�cations, for example MatLab®.

3. Some PerLa instances operates in physical inaccessible places (or hardly accessible)

by human beings. An example is reported in [3], where the sensors are placed on

a side of mount S.Martino in Lecco (Italy) well known for rockfalls phenomena. In

79

Page 81: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

case of necessary modi�cations (upgrades, fails recovers) the need to control remotely

or to upload/download some data would make the operation free of risks and easily

performable.

The component deputed to this important task is called PerLa console, since it allows to

execute a certain number of remote commands. The console is, however, no more than a

normal client/server application: the server runs on the same machine that is currently

hosting a PerLa instance, while the client side of the application uses TCP/IP to connect

to the server in order to send commands and receive responses. The �critical� design phase

was then about the format to be adopted for the messages that client and server exchange

each others. Google Inc. has been developing, during recent years, an infrastructure called

Protocol Bu�ers [4] which made the �rst console implementation to be ready in few days.

Protocol Bu�ers (shortened ProtoBuf) are a �exible, e�cient and automated mechanism

for serializing structured data, and is currently used by Google itself for almost its internal

RPC protocols. ProtoBuf allow to de�ne how messages are to be structured using a very

simple, but powerful grammar as exposed in Figure:

Figure 8.1: ProtoBuf grammar, an example taken from [4]

80

Page 82: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

The message structure written with this grammar is usually saved in text �les called

�proto �les� (the names derives from the �le extension, .proto)

Google than provides a ProtoBuf parser for these �les that is able to create the Java,

C++ and Python classes that implement the desired structure. The big advantage is

evident: instead of spending time in implementing classes, data structures, access methods

for the desired message, ProtoBuf allows to simply write the desired structure: the classes

created after the parsing phase already contains all the necessary to operate (constructors,

methods, and so on). The advantage is maybe more evident when the messages exchanged

need to be modi�ed: ProtoBuf allow to update the desired message without breaking

deployed programs that are compiled using the �old� method.

PerLa Console using Google ProtoBuf

The messages exchanged between server and client are two:

Request it �travels� from client to server and typically consists in a command to be exe-

cuted

Response is the response to a received command

In both of these messages, the internal structure is composed as exposed in the following

�gure:

Figure 8.2: PerLa console messages structure

The header is used to transmit from client to server the desired command to be exe-

cuted, and from server to client the response type (i.e: the (un)successful execution of the

81

Page 83: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

command). Since it was necessary to obtain an implementation of the console that could

help PerLa development (as exposed in the �rst point of the previous section) the following

commands were the �rst to be included and chosen:

TEST_QUERY it's supposed to be used to test a query without injecting it into the

system, in order to check its syntax

INJECT_QUERY to remotely inject a query

SHOW_REGISTRY to explore the content of the Logical Object Registry

SHOW_QUERY to retrieve all the information about a running query

STOP_QUERY to stop a running query

SHOW_DEVICES to retrieve information about the added devices

INJECT_DESCRIPTOR to manually provide PerLa with the xml �le in order to add a

device that is not able to provide autonomously the xml �le.

Every command may request the presence of a proper payload: this is why a second section

within message structure has been reserved. For example the command TEST_QUERY

requires the textual query that must be checked. In the same way the response payload will

contain the con�rmation of the correctness of the query, or the syntactic error discovered.

The message structure (header and payload considered together) of both requests and

responses were de�ned using the following proto �le (perlamessage.proto). Their structures

using ProtoBuf are exposed in the following �gures:

82

Page 84: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Figure 8.3: Request and response messages de�nition with ProtoBuf

83

Page 85: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

ProtoBuf grammar enables the user to choose if a message has to contain mandatory

or optional �elds (required and optional keywords). In a request the command is obviously

mandatory while, as said before, the payload is optional. The classes dynamically created

by ProtoBuf automatically raise a Java Exception if a mandatory �eld is found empty:

this is another advantage inherent to the use of ProtoBuf instead of manually coding Java

classes.

The �elds highlighted in red are the de�nitions of the payloads and require a further

discussion. The payload of every request/response must have a precise structure, and is

de�ned using ProtoBuf again. In this case the payload of the request containing command

TEST_QUERY is de�ned in a message called TestQueryRequest, while the payload re-

sponse is a message called TestQueryResponse. Both messages structures are reported here

below, since they're written in the same proto �le:

Figure 8.4: TestQueryResponse and TestQueryRequest payload de�nition

It's now clear how all the messages are de�ned. A �rst proto �le describes the structure

of a generic request/response and contains mandatory the command to execute, or the

response to a command. If the command/response requires a payload it must be de�ned

84

Page 86: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

into a separate proto �le that will contain the de�nition of the request/response payloads.

In other words there is only one proto �le to describe request and responses while there's a

single �le, describing eventually the payloads, for each command presented at the beginning

of this section.

Figure 8.5: PerLa console functioning schema

Actual and future works

Currently only the proto �les for the request/response messages and for theTEST_QUERY

command has been written and tested. The console is, in fact, still at an primitive level, as

well as the list of executable commands. The commands themselves are also in a very sim-

ple form. Future works should consolidate the current design, de�ning a precise grammar

for the console also considering all the possible application scenarios.

85

Page 87: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

86

Page 88: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Appendix B: Source code

In the following the source code of the most important implemented class is proposed.

DataCollector class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a c o l l e c t i o n ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . * ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . Node ;

import org . d e i . p e r l a . s y s . d e v i c e . f p c . Func t i ona l i t yProxyComponent ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s . * ;

import org . d e i . p e r l a . u t i l s . p i p e . * ;

import org . d e i . p e r l a . u t i l s . w a i t e r . * ;

/*** Questa c l a s s e r a pp r e s e n t a l ' ogge t to i n c a r i c a t o d e l r e cupe r o d e i d a t i c amp iona t i

da pa r t e* d e l l '< tt>FPC</tt >.*/

pub l i c c l a s s Da t aCo l l e c t o r extends Thread{

/*** Tutte l e i n f o rm a z i o n i r i g u a r d o l ' a t t i v i t a ' d i data c o l l e c t i n g .*/

p r i v a t e LLDa t aCo l l e c t i o n I n f o p I n f o ;

p r i v a t e Funct i ona l i t yProxyComponent pFPC ;

p r i v a t e Bu f f e r pBu f f e r ;

p r i v a t e Sampl ingData pSampl ingData ;

p r i v a t e Waiter<Pipe<Record>> pRecordWaiter ;

p r i v a t e Node pWhereNode ;

/*** Flag pe r marcare l a s i t u a z i o n e " pr imo r e c o r d r i c e v u t o "*/

p r i v a t e boolean i s F i r s t R e c o r d = t rue ;

/*** Flag pe r marcare che e ' g i a ' a t t i v o un g e s t o r e d i REFRESH*/

87

Page 89: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

p r i v a t e Re f r e s hHand l e r tR e f r e s hHand l e r = nu l l ;

/*** Oggetto che s i occupa d i g e s t i r e l a c l a u s o l a REFRESH*/

p r i v a t e c l a s s Re f r e s hHand l e r implements Runnable {

long pTime ;

pub l i c Re f r e s hHand l e r ( long parTime ) {

t h i s . pTime = parTime ;

}

pub l i c vo id run ( ) {

t r y {

s l e e p ( pTime ) ;

} catch ( I n t e r r u p t e dE x c e p t i o n e ) {

e . p r i n t S t a c kT r a c e ( ) ;

}

pFPC . setSampl ingTime ( readFrequencyFromTable (pFPC . f o r c eSamp l i ng ( ) . ge t (0 ) ) ,

pSampl ingData . g e tBehav i ou r ( ) ) ;

}

}

/*** Co s t r u i s c e un nuovo ogge t to d e l t i p o Da t aCo l l e c t o r* @param p a r I n f o l e i n f o rm a z i o n i d i c u i ha b i sogno i l Da t aCo l l e c t o r*/

pub l i c Da t aCo l l e c t o r ( LLDa t aCo l l e c t i o n I n f o p a r I n f o ) {

t h i s . p I n f o = p a r I n f o ;

t h i s . pBu f f e r = p a r I n f o . g e t L o c a l Bu f f e r ( ) ;

t h i s . pFPC = pa r I n f o . getAssoc ia tedFPC ( ) ;

t h i s . pSampl ingData = pa r I n f o . getSampl ingData ( ) ;

t h i s . pWhereNode = p a r I n f o . ge tWhereCond i t ion ( ) ;

pRecordWaiter = new Waiter<Pipe<Record >>() ;

pRecordWaiter . s t a r t ( ) ;

pRecordWaiter . addWaitab le (pFPC . getNewOutputPipe ( ) . g e tWa i t ab l e ( ) ) ;

// pRecordWaiter . addWaitab le ( ( ( FPCmockup) pFPC) . ge tP ipe ( ( LLStatement ) p a r I n f o .ge tRep r e s en t edSta t ement ( ) ) . g e tWa i t ab l e ( ) ) ;

}

/*** @re tu rn Le i n f o rm a z i o n i d i bas so l i v e l l o a s s o c i a t e a ques to Da t aCo l l e c t o r*/

pub l i c LLDa t aCo l l e c t i o n I n f o g e t LLDa t aCo l l e c t i o n I n f o ( ) {

re tu rn t h i s . p I n f o ;

}

/*** Mette i n e s e c u z i o n e i l Da t aCo l l e c t o r*/

pub l i c vo id run ( ) {

Record tRecord ;

88

Page 90: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

System . out . p r i n t l n ( "*** DATA COLLECTOR s t a r t s ***" ) ;

Waitab le<Pipe<Record>> tTmpWaitable ;

Pipe<Record> tTmpPipe ;

// . . . . c i c l o p r i n c i p a l e d i r a c c o l t a d a t i

do {

tTmpWaitable = pRecordWaiter . wa i tNext ( ) ;

tTmpPipe = tTmpWaitable . getParentComponent ( ) ;

tRecord = tTmpPipe . dequeue ( ) ;

i f ( tRecord == nu l l ) {

System . out . p r i n t l n ( "PIPE CHIUSA" ) ;

tTmpPipe . s top ( ) ;

pRecordWaiter . s top ( ) ;

tRecord = nu l l ;

break ;

}

System . out . p r i n t l n ( "Record l e t t o da "+pFPC . getName ( ) ) ;

i f ( performWhere ( tRecord ) ) {

// . . . i r e c o r d che s i " s a l v ano " dopo l a performWhere ( ) vengono i n s e r i t i n e l l' LocBuf

pBu f f e r . addRecord ( tRecord ) ;

System . out . p r i n t l n ( "Record agg iun to da l DC c o l l e g a t o a "+pFPC . getName ( ) ) ;

System . out . p r i n t l n ( " [ "+pBu f f e r . g e tBu f f e r ( ) . s i z e ( )+" ] "+"<−−− I l r e c o r d ora

n e l b u f f e r c on t i e n e i l v a l o r e : \n" + tRecord ) ;

}

e l s e {

System . out . p r i n t l n ( "*** Record SCARTATO d a l l a WHERE***" ) ;

}

// . . . v a l u t o se ò i l caso d i cambia re l a f r e qu en za d i campionamento . . . .e v a l u a t eF r equenc y ( tRecord , i s F i r s t R e c o r d ) ;

i s F i r s t R e c o r d = f a l s e ;

//TODO i n s e r i r e qu i l a g e s t i o n e d e l l a t e rm i na t e ( ba s ta un " break ")

} whi le ( t rue ) ;

System . out . p r i n t l n ( "*** DATA COLLECTOR ha t e rm ina to ***" ) ;

//TODO b i sogna i n s e r i r e qu i t u t t a l e g e s t i o n e d e l l a t e rm i n a z i o n e d e l l a query}

/*** Metodo h e l p e r : data una t a b e l l a e un r e c o r d agg i o r na l a f r e qu en za d i

campionamento s u l l 'FPC* @param parTab le l a t a b e l l a* @param parRecord i l r e c o r d* @re tu rn TRUE se ok , FALSE a l t r i m e n t i*/

p r i v a t e long readFrequencyFromTable ( Record parRecord ) {

89

Page 91: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

long foundFrequency = 0 ;

f o r ( FrequencyRecord tFreqRec : pSampl ingData . ge tSamp l ingTab leContent ( ) ) {

i f ( E x p r e s s i o nE v a l u a t o r . e v a l u a t e ( parRecord , tFreqRec . g e tF i r s tOpe r and ( ) ) ) {

foundFrequency = ( long ) tFreqRec . ge tF requency ( ) . ge tVa lue ( ) ;

break ;

// bValue = ( ( FPCmockup) pFPC) . setMockupSamplingTime ( ( l ong ) tFreqRec .ge tF requency ( ) . ge tVa lue ( ) *1000 , n u l l , ( LLStatement ) p I n f o .ge tRep r e s en t edSta t ement ( ) ) ;

// bValue = (pFPC . setSampl ingTime ( ( l ong ) tFreqRec . ge tF requency ( ) . ge tVa lue ( )*1000 , pa rTab le . g e tBehav i ou r ( ) , p I n f o . ge tRep re s en t edSta t ement ( ) ) ) ;

// i f ( bValue == t r u e ) break ;}

}

re tu rn foundFrequency ;

}

/*** Metodo h e l p e r pe r l a v a l u t a z i o n e d e l l a c l a u s o l a Where* @param parRecord i l r e c o r d su c u i c o n t r o l l a r e l a c l a u s o l a WHERE* @re tu rn TRUE se i l r e c o r d ò a c c e t t a b i l e , FALSE se va ' s c a r t a t o*/

p r i v a t e boolean performWhere ( Record parRecord ) {

re tu rn Exp r e s s i o nE v a l u a t o r . e v a l u a t e ( parRecord , pWhereNode ) ;

}

/*** Metodo i n t e r n o che s i occupa d e l l a p a r t e r i g u a r d a n t e l e t emp i s t i c h e e l a

c a s i s t i c h e d i samp l i ng* @param tRecord* @param i s F i r s t T im e i n d i c a se e ' l a pr ima v o l t a che i l DC s e t t a l a f r e qu en za .

I n f a t t i i l pr imo r e c o r d v i e n e usa to* pe r s e t t a r e l a f r e qu en za . Al g i r o s u c c e s s i v o e ' compito d e l l a REFRESH

occupa r s ene .* @re tu rn TRUE se t u t t o ok , FALSE o t h e rw i s e .*/

p r i v a t e boolean e va l u a t eF r equenc y ( Record tRecord , boolean i s F i r s t T im e ) {

/*E ' i l pr imo r e c o r d che e ' s t a t o r i c e v u t o ?*/

i f ( i s F i r s t T im e ) {

// . . . a l l o r a s e t t o l a pr ima f r e quen za d i campionamento . .t h i s . pFPC . setSampl ingTime ( readFrequencyFromTable ( tRecord ) , pSampl ingData .

g e tBehav i ou r ( ) ) ;

}

e l s e {

// . . . a ques to punto almeno un s e t t a g g i o d e l l a FC ( f r e quen za d i campionamento )ò s t a t o f a t t o .

// . . devo vede r e se ò i l caso d i f a r e REFRESH e che t i p o d i REFRESH :

i f ( pSampl ingData . g e tR e f r e s hC l a u s e ( ) i n s t anceo f Re f r e shNeve r ) {

// . . l ' u t en t e ha imposto nessun REFRESH . . . l a pr ima f r e qu en za impos ta tar imane pe r sempre .

90

Page 92: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

}

e l s e i f ( pSampl ingData . g e tR e f r e s hC l a u s e ( ) i n s t anceo f Ref r e shEventBased ) {

//Qui e ' t u t t o un open po i n t . Dal mio punto d i v i s t a i o f a r e i c o s i ' :// 1 . a g g i u n g e r e i i n t e r f a c c i a a l l 'FPC per un ' a l t r a p i p e d i Ev en t i// 2 . a s p e t t o g l i e v e n t i che s a l gono e me l i segno// 3 . se t r a q u e l l i che ho segnato c ' ò q u e l l o che mi i n t e r e s s a . . . a gg i o r no

. . .

}

e l s e i f ( pSampl ingData . g e tR e f r e s hC l a u s e ( ) i n s t anceo f RefreshTimeBased ) {

i f ( tR e f r e s hHand l e r == nu l l ) {

tRe f r e s hHand l e r = new Re f r e s hHand l e r ( ( long ) ( ( RefreshTimeBased )

pSampl ingData . g e tR e f r e s hC l a u s e ( ) ) . g e t I n t e r v a l ( ) . ge tVa lue ( ) ) ;

tR e f r e s hHand l e r . run ( ) ;

}

}

}

re tu rn t rue ;

}

}

FunctionalityProxyComponent class

package org . d e i . p e r l a . s y s . d e v i c e . f p c ;

import j a v a . u t i l . * ;

import org . d e i . p e r l a . component . LoggableComponent ;

import org . d e i . p e r l a . component . S t a r t a b l e ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . OnUnsupportedSampleRateOpt ions ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . Nod eLog i c a lOb j e c tA t t r i b u t e ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . Statement ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s . Record ;

import org . d e i . p e r l a . u t i l s . l o g g e r . LogRecord . Type ;

import org . d e i . p e r l a . u t i l s . l o g g e r . LogRecord . V e r b o s i t y ;

import org . d e i . p e r l a . u t i l s . messages . AdapterFpcMessage ;

import org . d e i . p e r l a . u t i l s . p i p e . P ipe ;

import org . d e i . p e r l a . u t i l s . w a i t e r . Wa i tab l e ;

import org . d e i . p e r l a . u t i l s . w a i t e r . Waiter ;

/*** Questa c l a s s e a s t r a e un d i s p o s i t i v o f i s i c o connes so a l s i s t ema .* S i occupa d i t r a d u r r e e comunicare l e r i c h e s t e v e r s o i l* d i s p o s i t i v o e l e r i s p o s t e r i c e v u t e .*/

pub l i c abs t rac t c l a s s Funct i ona l i t yProxyComponent extends LoggableComponent

implements S t a r t a b l e {

/*** La s t r u t t u r a d a t i con t enenen te l e a l t r e s t r u t t u r e d a t i n e c e s s a r i e a l

funz ionamento d e l l a c l a s s e .*/

p r i v a t e f i n a l FPCDataStructure pFPCDataStructure ;

91

Page 93: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

/*** Pipe pe r d a t i i n a r r i v o da l d i s p o s i t i v o*/

p r i v a t e f i n a l Pipe<AdapterFpcMessage> inpu tP i p e ;

/*** Waiter d e i d a t i i n a r r i v o da l d i s p o s i t i v o*/

p r i v a t e f i n a l Waiter<Pipe<AdapterFpcMessage>> inpu tP i p eWa i t e r ;

/*** Gruppo d i p i p e pe r i d a t i i n u s c i t a ( un FPC puo ' s e r v i r e p i u ' Da t aCo l l e c t o r s )*/

p r i v a t e f i n a l Ar r a yL i s t <Pipe<Record>> ou t pu tP i p eA r r a yL i s t ;

/*** I n n e r c l a s s che g e s t i c e l a r i c e z i o n e d e i d a t i d a l d i s p o s i t i v o a s t r a t t o*/

p r i v a t e c l a s s PipeReader implements Runnable {

pub l i c vo id run ( ) {

Waitab le<Pipe<AdapterFpcMessage>> wa i t a b l e ;

Pipe<AdapterFpcMessage> p ip e ;

AdapterFpcMessage message ;

whi le ( t rue ) {

wa i t a b l e = inpu tP i p eWa i t e r . wa i tNext ( ) ;

p i p e = wa i t a b l e . getParentComponent ( ) ;

message = p ip e . dequeue ( ) ;

i f ( message != nu l l ) {

readMessageFromAdapter ( message . ge tPay load ( ) ) ;

/**/} e l s e {

i npu tP i p eWa i t e r . removeWaitab le ( wa i t a b l e ) ;

pu tNu l lOnOutpu tP ipeAr rayL i s t ( ) ;

p i p e . s top ( ) ;

p i p eC l o s edCa l lBa ck ( p i p e ) ;

break ;

}

}

l o g g e rN o t i f y ( Type . I n f o rma t i on , getName ( ) + " t e rm ina t ed ! " , V e r b o s i t y . Low) ;

}

}

/*** I n d i c a se i l componente ò a t tua lmen t e a v v i a t o*/

protected boolean s t a r t e d ;

/*** Metodo pe r i n i z i a l i z z a r e l 'FPC . Viene e s e g u i t o n e l c o s t r u t t o r e

92

Page 94: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/protected abs t rac t vo id i n i t D e f a u l t ( ) ;

/*** Metodo che implementa i l comportamento da s e g u i r e n e l caso l a Pipe d i

comun icaz ione con i l d i s p o s i t i v o f i s i c o* s i a s t a t a c h i u s a ( s i a manualmente , s i a i n caso d i e r r o r e )* @param p ip e*/

protected abs t rac t vo id p i p eC l o s e dCa l lBa ck ( Pipe<AdapterFpcMessage> p ip e ) ;

/*** I n v i a n u l l s u l l e p i p e v e r s o i Da t aCo l l e c t o r pe r s e g n a l a r n e l a c h i u s u r a*/

synchron ized f i n a l protected vo id putNu l lOnOutpu tP ipeAr rayL i s t ( ) {

f o r ( Pipe<?> p : o u t p u tP i p eA r r a yL i s t )

p . enqueue ( nu l l ) ;

}

/*** Conver te uno st ream d i by te i n un r e c o r d ( i n t e r p r e t a )* Viene i n v o c a t o quando l 'FPC r i c e v e un messagg io da i l i v e l l i s o t t o s t a n t i* */

protected abs t rac t vo id readMessageFromAdapter ( byte [ ] pay load ) ;

/*** R i c h i e d e a l l 'FPC una nuova p i p e ( da u s a r e quando c i s i agganc i a un nuovo

Da t aCo l l e c t o r )*/

synchron ized f i n a l pub l i c Pipe<Record> getNewOutputPipe ( ) {

Pipe<Record> p ip e = new Pipe<Record >( t h i s . getName ( ) + "_PIPE_" + (

ou t pu tP i p eA r r a yL i s t . s i z e ( )+1) ) ;

p i p e . s t a r t ( ) ;

o u t p u tP i p eA r r a yL i s t . add ( p i p e ) ;

re tu rn p i p e ;

}

/*** Co s t r u t t o r e* @param parName i l nome da dare a ques to FPC ( e . g : "FPC per i s e n s o r i t i p o A")* @param inpu tP i p e l a p i p e pe r s camb ia r e i d a t i con i l d i s p o t i v o*/

pub l i c Funct i ona l i t yProxyComponent ( S t r i n g parName , Pipe<AdapterFpcMessage>

inputP ipe , FPCDataStructure pa rDa taS t r u c tu r e ) {

super ( parName ) ;

t h i s . s t a r t e d = f a l s e ;

t h i s . i n pu tP i p e = inpu tP i p e ;

t h i s . i n pu tP i p eWa i t e r = new Waiter<Pipe<AdapterFpcMessage >>() ;

t h i s . i n pu tP i p eWa i t e r . addWaitab le ( i n pu tP i p e . g e tWa i t ab l e ( ) ) ;

t h i s . o u t p u tP i p eA r r a yL i s t = new Ar r a yL i s t <Pipe<Record >>() ;

t h i s . pFPCDataStructure = pa rDa taS t r u c tu r e ;

t h i s . i n i t D e f a u l t ( ) ;

}

93

Page 95: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

/*** R e s t i t u i s c e TRUE se l 'FPC ò a t t i v o e f unz i onan t e , FALSE a l t r i m e n t i*/

synchron ized f i n a l pub l i c boolean i s S t a r t e d ( ) {

re tu rn s t a r t e d ;

}

/*** Permette d i a v v i a r e l 'FPC*/

synchron ized pub l i c vo id s t a r t ( ) {

i npu tP i p eWa i t e r . s t a r t ( ) ;

new Thread (new PipeReader ( ) ) . s t a r t ( ) ;

s t a r t e d = t rue ;

l o g g e rN o t i f y ( Type . I n f o rma t i on , getName ( ) + " has j u s t s t a r t e d i t s a c t i v i t y " ,

V e r b o s i t y . Low) ;

}

/*** Permette d i s t oppa r e l 'FPC*/

synchron ized pub l i c vo id s top ( ) {

i npu tP i p e . enqueue ( nu l l ) ;

s t a r t e d = f a l s e ;

l o g g e rN o t i f y ( Type . I n f o rma t i on , getName ( ) + " has j u s t s topped i t s a c t i v i t y " ,

V e r b o s i t y . Low) ;

}

/*** Se t t a i l tempo d i samp l ing*/

pub l i c abs t rac t boolean setSampl ingTime ( long parTime ,

OnUnsupportedSampleRateOpt ions pa rBehav i ou r ) ;

/*** Reg i s t r a una nuova query da e s e g u i r e s u l l 'FPC* @param parAt t rb i u t e sNames l i s t a con t enen t e i nomi d e g l i a t t r i b u t i r i c h i e s t i

d a l l a query* @re tu rn TRUE se ok , FALSE se e r r o r e*/

pub l i c abs t rac t boolean r e g i s t e rQu e r y ( Statement parStatement ) ;

/*** Quando una query t e rm ina va de−r e g i s t r a t a d a l l 'FPC* @re tu rn TRUE se ok , FALSE a l t r i m e n t i*/

pub l i c abs t rac t boolean unReg i s t e rQue r y ( ) ;

/*** R e s t i t u i s c e l ' e l e n c o d i a t t r i b u t i che l 'FPC ò i n grado d i campionare ( ovvero ,

i n a l t r i t e rm i n i , g l i a t t r i b u t i che s t a wrappando )* @deprecated E ' un metodo che va ' s i c u r amen t e e l im i n a t o . Al momento v i e n e

u t i l i z z a t o s o l o d a l l a pr ima v e r s i o n e d e l R e g i s t r y d i Ar thur .

94

Page 96: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*

*/pub l i c abs t rac t Ar r a yL i s t <NodeLog i c a lOb j e c tA t t r i b u t e > g e t A t t r i b u t e L i s t ( ) ;

/*** Normalmente i l f l u s s o d i r e c o r d pa r t e d a l l 'FPC e a t t r a v e r s o l e p i p e a r r i v a a

d e s t i n a z i o n e i n modo i n d i p e nd e n t e* da que s t ' u l t ima ( s i veda l a c l a s s e p i p e . j a v a ) . Tu t t a v i a e ' t a l v o l t a n e c e s s a r i o

f o r z a r e i l campionamento : caso t i p i c o d e l l a* c l a u s o l a REFRESH .*

*/pub l i c abs t rac t Ar r a yL i s t <Record> fo r c eSamp l i ng ( ) ;

}

FPCDataStructure class

package org . d e i . p e r l a . s y s . d e v i c e . f p c ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . Constant ;

import org . d e i . p e r l a . u t i l s . d a t a c o n v e r t e r . DataConve r te r ;

/*** Questa c l a s s e c o n t i e n e t u t t e l e s t r u t t u r e d a t i n e c e s s a r i e a l funz ionamento d e l l '

FPC* Non e ' s t a t a i n s e r i t a a l cuna f u n z i o n e complementare a l l a getAttr ibuteByName , i n

quanto s i* presuppone che i l v a l o r e d e g l i a t t r i b u t i c o n t e n u t i n e l l e s t r u t t u r e d a t i s i a

impos ta to* e s c l u s i v amen t e d a l l a f u n z i o n e d e s e r i a l i z eD a t a , l a qua l e d e s e r i a l i z z a l o st ream

d i Byte* i n a r r i v o da l d i s p o s i t i v o f i s i c o e mod i f i c a i v a l o r i d e l l e s t r u t t u r e

agg io rnandone i l contenuto* con i d a t i a t t u a l i .* TODO: Va l u t a r e se l ' a s s un z i o n e che una setAtt r ibuteByName non s e r v a s i a c o r r e t t a*/

pub l i c abs t rac t c l a s s FPCDataStructure {

/*** Metodo da u t i l i z z a r s i pe r r e c u p e r a r e i l v a l o r e d i un s i n g o l o a t t r i b u t o* a p a r t i r e d a l nome d i ques to . Nel caso i l nome d e l l ' a t t r i b u t o non appar tenga* a l l 'FPC v e r r a ' r e s t i t u i t o n u l l .* @param parName nome d e l l ' a t t r i b u t o r i c h i e s t o* @re tu rn Va l o r e d e l l ' a t t r i b u t o r i c h i e s t o . V iene r e s t i t u i t o n u l l n e l caso l 'FPC* non contenga l ' a t t r i b u t o r i c h i e s t o*/

pub l i c abs t rac t Constant getAtt r ibuteByName ( S t r i n g parName ) ;

/*** Questo metodo con sen t e d i popo l a r e una d e l l e s t r u t t u r e d a t i p r e s e n t i n e l l a

c l a s s e* a p a r t i r e d a l l o s t ream d i d a t i r i c e v u t o da l d i s p o s i t i v o f i s i c o d i c u i l 'FPC e '

l ' a s t r a z i o n e* @param p a r S t r u c t u r e I d e n t i f i e r i d e n t i f i c a t o r e d e l l a c l a s s e da popo l a r e* @param parRawDataArray a r r a y con t enen t e i d a t i con c u i c a r i c a r e l a c l a s s e

95

Page 97: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c vo id d e s e r i a l i z e D a t a ( S t r i n g p a r S t r u c t u r e I d e n t i f i e r , Byte [ ] parRawDataArray

) {

Object s t r u c t u r e = t h i s . g e tDa t aS t r u c t u r e ( p a r S t r u c t u r e I d e n t i f i e r ) ;

i f ( s t r u c t u r e == nu l l ) {

throw new I l l e g a lA r g umen tE x c e p t i o n ( "La s t r u t t u r a d a t i " +

p a r S t r u c t u r e I d e n t i f i e r +

" non e ' s t a t a t r o v a t a ! " ) ;

}

DataConve r te r . f romByteArray ( s t r u c t u r e , parRawDataArray ) ;

}

/*** Questo metodo permet te d i c o n v e r t i r e una s t r u t t u r a d a t i i n un a r r a y d i Byte

u t i l i z z a b i l e* pe r l ' i n v i o su r e t e* @param p a r S t r u c t u r e I d e n t i f i e r i d e n t i f i c a t o r e d e l l a s t r u t t u r a d a t i da

s e r i a l i z z a r e* @re tu rn a r r a y d i by te c o r r i s p o n d e n t e a l l a s e r i a l i z z a z i o n e d e l l a c l a s s e*/

pub l i c Byte [ ] s e r i a l i z e D a t a ( S t r i n g p a r S t r u c t u r e I d e n t i f i e r ) throws

I l l e g a lA r g umen tE x c e p t i o n {

Object s t r u c t u r e = t h i s . g e tDa t aS t r u c t u r e ( p a r S t r u c t u r e I d e n t i f i e r ) ;

i f ( s t r u c t u r e == nu l l ) {

throw new I l l e g a lA r g umen tE x c e p t i o n ( "La s t r u t t u r a d a t i " +

p a r S t r u c t u r e I d e n t i f i e r +

" non e ' s t a t a t r o v a t a ! " ) ;

}

re tu rn DataConve r te r . toByteAr ray ( s t r u c t u r e ) ;

}

/*** Metodo pe r i l r e cupe r o d i una s t r u t t u r a d a t i memor izzata a l l ' i n t e r n o d e l l a

c l a s s e .* Puo ' e s s e r e implementato come una s emp l i c e t a b e l l a d i l ookup . L ' i d e n t i f i c a t o r e

puo '* e s s e r e i l nome s t e s s o d e l l a s t r u t t u r a d a t i da r e c u p e r a r e* @param p a r S t r u c t u r e I d e n t i f i e r i d e n t i f i c a t o r e d e l l a s t r u t t u r a d a t i da

r e c u p e r a r e* @re tu rn s t r u t t u r a d a t i r i c h i e s t a . Nel caso l ' i d e n t i f i c a t o r e non c o r r i s p o n d a a

nessuna* s t r u t t u r a d a t i p r e s e n t e n e l l a c l a s s e deve e s s e r e r e s t i t u i t o n u l l*/

protected abs t rac t Object g e tDa t aS t r u c t u r e ( S t r i n g p a r S t r u c t u r e I d e n t i f i e r ) ;

}

SamplingData class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s ;

import j a v a . u t i l . A r r a y L i s t ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . OnUnsupportedSampleRateOpt ions ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . R e f r e s hC l a u s e ;

96

Page 98: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

/*** Questa c l a s s e r a pp r e s e n t a t u t t e l e i n f o rm a z i o n i s u l samp l i ng (TABELLA, REFRESH,

UNSUPPORTEDSAMPLERATE, e t c )* @author Camplani , Fortunato , Ma r e l l i , Rota e t a l .*/

pub l i c c l a s s Sampl ingData {

/*** A r r a y L i s t con t enen t e i s i n g o l i r e c o r d d e l l a t a b e l l a*/

p r i v a t e Ar r a yL i s t <FrequencyRecord> pRecords ;

/*** Comportamento da t e n e r e n e l caso i n c u i i l d i s p o s i t i v o non suppo r t a una c e r t a

v e l o c i t a ' d i campionamento*/

p r i v a t e OnUnsupportedSampleRateOpt ions pBehav iour ;

/*** Campo per l a c l a u s o l a REFRESH*/

p r i v a t e Re f r e s hC l a u s e pRe f r e s hC l au s e ;

/*** Co s t r u t t o r e d e l l a t a b e l l a , i n i z i a l i z z a n d o l a con g ià un i n s i eme d i r e c o r d . Di

t a l e i n s i eme vengono t e n u t i* s o l o i r e c o r d v a l i d i ( ovve ro q u e l l i che sono c o r r e t t i ) . Se l ' i n s i eme ò vuoto

s i r i c a d e n e l c o s t r u t t o r e bana l e* @param pa rA r r a y L i s t l ' i n s i eme d i r e c o r d*/

pub l i c Sampl ingData ( A r r a yL i s t <FrequencyRecord> pa rA r r a yL i s t ,

OnUnsupportedSampleRateOpt ions parBehav iour , R e f r e s hC l a u s e p a rRe f r e s h ) {

t h i s . pBehav iour = pa rBehav i ou r ;

t h i s . pRe f r e s hC l au s e = pa rRe f r e s h ;

pRecords = new Ar r a yL i s t <FrequencyRecord >() ;

i f ( p a rA r r a y L i s t == nu l l ) re tu rn ; // anche se i o l a n c e r e i l ' e c c e z i o n e data l 'impor tanza ;

f o r ( FrequencyRecord tRecord : p a rA r r a y L i s t ) {

i f ( checkRecord ( tRecord ) ) {

t h i s . pRecords . add ( tRecord ) ;

}

}

}

/*** @re tu rn I l comportamento i n caso d i f r e qu en za non suppo r t a t a d a l l a FPC*/

pub l i c OnUnsupportedSampleRateOpt ions ge tBehav i ou r ( ) {

97

Page 99: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

re tu rn t h i s . pBehav iour ;

}

/*** Metodo h e l p e r p r i v a t o pe r i l c o n t r o l l o d e l l a v a l i d i t à d i un r e c o r d* @param parRecordToCheck i l r e c o r d da c o n t r o l l a r e* @re tu rn t r u e se ò v a l i d o , f a l s e a l t r i m e n t i*/

p r i v a t e boolean checkRecord ( FrequencyRecord parRecordToCheck ) {

//TODO Eccez i one

i f ( parRecordToCheck . ge tF requency ( ) . ge tVa lue ( )>=0) {

re tu rn t rue ;

} e l s e {

System . out . p r i n t l n ( "*** Record non i n s e r i t o i n t a b e l l a ( E r r o r e n e l l a

f r e quen za : ) : "+parRecordToCheck . t o S t r i n g ( )+" ***\n" ) ;

re tu rn f a l s e ;

}

}

/*** @re tu rn l a c l a u s o l a REFRESH per ques to samp l i ng*/

pub l i c Re f r e s hC l a u s e g e tR e f r e s hC l a u s e ( ) {

re tu rn t h i s . pRe f r e s hC l au s e ;

}

/*** R i t o r na una cop i a ( pe r s i c u r e z z a ) d e l contenuto d e l l a t a b e l l a*/

pub l i c Ar r a yL i s t <FrequencyRecord> getSamp l ingTab leContent ( ) {

re tu rn t h i s . pRecords ;

}

/*** Metodo pe r stampare a v i d eo i l contenuto d e l l a t a b e l l a*/

pub l i c S t r i n g t o S t r i n g ( ) {

S t r i n g t S t r i n g = new S t r i n g ( ) ;

f o r ( FrequencyRecord tRecord : pRecords ) {

t S t r i n g += tRecord . g e tF i r s tOpe r and ( ) . t o S t r i n g ( )+" | "+

tRecord . ge tF requency ( ) . t o S t r i n g ( )+"\n" ;

}

re tu rn t S t r i n g ;

98

Page 100: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

}

}

FrequencyRecord class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . * ;

import org . d e i . p e r l a . p a r s e r . u t i l . Dura t i on ;

/*** C l a s s e che r a pp r e s e n t a un s i n g o l o r e c o r d d e l l a t a b e l l a con i v a l o r i d i

campionamento* @author Camplani , Fortunato , Ma r e l l i , Rota e t a l .*/

pub l i c c l a s s FrequencyRecord {

/*** Campi d e l s i n g o l o r e c o r d* |NODO_DA_TESTARE|CONDIZIONE_DI_CONFRONTO|FREQUENZA_SAMPLING|*/

p r i v a t e Node pF i r s tNode ;

p r i v a t e Durat i on pFrequency ;

/*** Co s t r u t t o r e n e l caso i l c on f r on t o s i a sempre f a t t o con TRUE* @param p a r F i r s t L ' e s p r e s s i o n e che deve v e r i f i c a r s i* @param pa rDura t i on l a f r e quen za d i campionamento a s s o c i a t a a l v e r i f i c a r s i d i

p a r F i r s t*/

pub l i c FrequencyRecord (Node p a r F i r s t , Dura t i on pa rDura t i on ) {

t h i s . pF i r s tNode = p a r F i r s t ;

t h i s . pFrequency = pa rDura t i on ;

}

/*** @re tu rn i l pr imo f i e l d d e l r e c o r d ( e s p r e s s i o n e da v e r i f i c a r e )*/

pub l i c Node ge tF i r s tOpe r and ( ) {

re tu rn t h i s . pF i r s tNode ;

}

/*** @re tu rn l ' ogge t to Dura t i on a s s o c i a t o a l v e r i f i c a r s i d e l r e c o r d*/

pub l i c Durat i on ge tF requency ( ) {

re tu rn t h i s . pFrequency ;

}

/**

99

Page 101: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

* S e t t e r d e l nodo da v e r i f i c a r e* @param parNode i l nodo da v e r i f i c a r e*/

pub l i c vo id s e t F i r s tNod e (Node parNode ) {

t h i s . pF i r s tNode = parNode ;

}

/*** R e s t i t u i s c e l a s t r i n g a che r a pp r e s e n t a i l contenuto d e l r e c o r d*/

pub l i c S t r i n g t o S t r i n g ( ) {

re tu rn pF i r s tNode . t o S t r i n g ( )+" | "+pFrequency . ge tVa lue ( )+pFrequency . getTimeUnit ( )

. t o S t r i n g ( ) ;

}

}

SamplingData class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s ;

import j a v a . u t i l . A r r a y L i s t ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . OnUnsupportedSampleRateOpt ions ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . R e f r e s hC l a u s e ;

/*** Questa c l a s s e r a pp r e s e n t a t u t t e l e i n f o rm a z i o n i s u l samp l i ng (TABELLA, REFRESH,

UNSUPPORTEDSAMPLERATE, e t c )* @author Camplani , Fortunato , Ma r e l l i , Rota e t a l .*/

pub l i c c l a s s Sampl ingData {

/*** A r r a y L i s t con t enen t e i s i n g o l i r e c o r d d e l l a t a b e l l a*/

p r i v a t e Ar r a yL i s t <FrequencyRecord> pRecords ;

/*** Comportamento da t e n e r e n e l caso i n c u i i l d i s p o s i t i v o non suppo r t a una c e r t a

v e l o c i t a ' d i campionamento*/

p r i v a t e OnUnsupportedSampleRateOpt ions pBehav iour ;

/*** Campo per l a c l a u s o l a REFRESH*/

p r i v a t e Re f r e s hC l a u s e pRe f r e s hC l au s e ;

/**

100

Page 102: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

* Co s t r u t t o r e d e l l a t a b e l l a , i n i z i a l i z z a n d o l a con g ià un i n s i eme d i r e c o r d . Dit a l e i n s i eme vengono t e n u t i

* s o l o i r e c o r d v a l i d i ( ovve ro q u e l l i che sono c o r r e t t i ) . Se l ' i n s i eme ò vuotos i r i c a d e n e l c o s t r u t t o r e bana l e

* @param pa rA r r a y L i s t l ' i n s i eme d i r e c o r d*/

pub l i c Sampl ingData ( A r r a yL i s t <FrequencyRecord> pa rA r r a yL i s t ,

OnUnsupportedSampleRateOpt ions parBehav iour , R e f r e s hC l a u s e p a rRe f r e s h ) {

t h i s . pBehav iour = pa rBehav i ou r ;

t h i s . pRe f r e s hC l au s e = pa rRe f r e s h ;

pRecords = new Ar r a yL i s t <FrequencyRecord >() ;

i f ( p a rA r r a y L i s t == nu l l ) re tu rn ; // anche se i o l a n c e r e i l ' e c c e z i o n e data l 'impor tanza ;

f o r ( FrequencyRecord tRecord : p a rA r r a y L i s t ) {

i f ( checkRecord ( tRecord ) ) {

t h i s . pRecords . add ( tRecord ) ;

}

}

}

/*** @re tu rn I l comportamento i n caso d i f r e qu en za non suppo r t a t a d a l l a FPC*/

pub l i c OnUnsupportedSampleRateOpt ions ge tBehav i ou r ( ) {

re tu rn t h i s . pBehav iour ;

}

/*** Metodo h e l p e r p r i v a t o pe r i l c o n t r o l l o d e l l a v a l i d i t à d i un r e c o r d* @param parRecordToCheck i l r e c o r d da c o n t r o l l a r e* @re tu rn t r u e se ò v a l i d o , f a l s e a l t r i m e n t i*/

p r i v a t e boolean checkRecord ( FrequencyRecord parRecordToCheck ) {

//TODO Eccez i one

i f ( parRecordToCheck . ge tF requency ( ) . ge tVa lue ( )>=0) {

re tu rn t rue ;

} e l s e {

System . out . p r i n t l n ( "*** Record non i n s e r i t o i n t a b e l l a ( E r r o r e n e l l a

f r e qu en za : ) : "+parRecordToCheck . t o S t r i n g ( )+" ***\n" ) ;

re tu rn f a l s e ;

}

}

/*** @re tu rn l a c l a u s o l a REFRESH per ques to samp l i ng

101

Page 103: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c Re f r e s hC l a u s e g e tR e f r e s hC l a u s e ( ) {

re tu rn t h i s . pRe f r e s hC l au s e ;

}

/*** R i t o r na una cop i a ( pe r s i c u r e z z a ) d e l contenuto d e l l a t a b e l l a*/

pub l i c Ar r a yL i s t <FrequencyRecord> getSamp l ingTab leContent ( ) {

re tu rn t h i s . pRecords ;

}

/*** Metodo pe r stampare a v i d eo i l contenuto d e l l a t a b e l l a*/

pub l i c S t r i n g t o S t r i n g ( ) {

S t r i n g t S t r i n g = new S t r i n g ( ) ;

f o r ( FrequencyRecord tRecord : pRecords ) {

t S t r i n g += tRecord . g e tF i r s tOpe r and ( ) . t o S t r i n g ( )+" | "+

tRecord . ge tF requency ( ) . t o S t r i n g ( )+"\n" ;

}

re tu rn t S t r i n g ;

}

}

LLStack class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a c o l l e c t i o n ;

/*** Rappresenta l a copp i a Da t aCo l l e c t o r − Executo r*/

pub l i c c l a s s LLStack {

p r i v a t e Da t aCo l l e c t o r pDa t aCo l l e c t o r ;

p r i v a t e Object pExecuto r ;

pub l i c LLStack ( Da t aCo l l e c t o r pa rDa t aCo l l e c t o r , Object pa rExecu to r ) {

t h i s . pDa t aCo l l e c t o r = pa rDa t aCo l l e c t o r ;

t h i s . pExecuto r = pa rExecu to r ;

}

pub l i c Da t aCo l l e c t o r g e tDa t aCo l l e c t o r ( ) {

re tu rn t h i s . pDa t aCo l l e c t o r ;

}

pub l i c Object ge tLLExecuto r ( ) {

re tu rn t h i s . pExecuto r ;

102

Page 104: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

}

pub l i c vo id s t a r t ( ) {

t h i s . pDa t aCo l l e c t o r . s t a r t ( ) ;

// t h i s . pExecuto r . s t a r t ( ) ;}

}

QueryInformation class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . Statement ;

pub l i c abs t rac t c l a s s Que ry I n f o rmat i on {

/*** Lo s ta tement d i c u i s i r a pp r e s en t ano l e i n f o*/

p r i v a t e Statement pStatement ;

/*** @re tu rn Lo s ta tement d i c u i s i r a pp r e s en t ano l e i n f o*/

pub l i c Statement ge tRep re s en t edSta t ement ( ) {

re tu rn t h i s . pStatement ;

}

/*** Co s t r u t t o r e* @param parStatement Lo s ta tement d i c u i s i r a pp r e s e n t e r a nno l e i n f o rmaz i o n*/

pub l i c Que ry I n f o rmat i on ( Statement parStatement ) {

t h i s . pStatement = parStatement ;

}

}

LLDataCollectionInfo class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a c o l l e c t i o n ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . Node ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . Statement ;

import org . d e i . p e r l a . s y s . d e v i c e . f p c . * ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . Que r y I n f o rmat i on ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s . * ;

/*** Questa c l a s s e wrappa t u t t e l e i n f o rma z i o n e n e c e s s a r i e pe r e f f e t t u a r e

c o r r e t t amen t e i l

103

Page 105: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

* samp l i ng d e i d a t i d a l s i s t ema p e r v a s i v e* @author S ch r e i b e r , Camplani , Fortunato , Ma r e l l i , Rota e t . a l .*/

pub l i c c l a s s LLDa t aCo l l e c t i o n I n f o extends Que ry I n f o rmat i on {

/*** L ' ogge t to con t enen t e l e t emp i s t i c h e s u l campionamento (REFRESH, IF−EVERY−ELSE ,

UNSUPPORTED SAMPLE RATE)*/

p r i v a t e Sampl ingData pSampData ;

/*** L ' ogge t to Node che r a pp r e s e n t a l a c l u s o l a WHERE*/

p r i v a t e Node pWhere ;

/*** I l b u f f e r i n c u i i n s e r i r e i d a t i appena r a c c o l t i ( e f i l t r a t i d a l l a WHERE)*/

p r i v a t e Bu f f e r pLo c a lBu f f e r ;

/*** L 'FPC da c u i i d a t i da campionare devono p r o v e n i r e*/

p r i v a t e Funct i ona l i t yProxyComponent pFPC ;

/*** Co s t r u t t o r e* @param parStatement I l LLStatemente d i c u i s i s tanno r app r e s en t ando l e i n f o* @param parFPC L 'FPC da cu i i d a t i da campionare devono p r o v e n i r e* @param parSampData L ' ogge t to con t enen t e l e t emp i s t i c h e s u l campionamento (

REFRESH, IF−EVERY−ELSE , UNSUPPORTED SAMPLE RATE)* @param parWhere L ' ogge t to Node che r a pp r e s e n t a l a c l u s o l a WHERE* @param pa rBu f f e r I l b u f f e r i n c u i i n s e r i r e i d a t i appena r a c c o l t i ( e f i l t r a t i

d a l l a WHERE)*/

pub l i c LLDa t aCo l l e c t i o n I n f o ( Statement parStatement , Func t i ona l i t yProxyComponent

parFPC , Sampl ingData parSampData , Node parWhere , Bu f f e r p a rBu f f e r ) {

super ( parStatement ) ;

t h i s . pSampData = parSampData ;

t h i s . pWhere = parWhere ;

t h i s . p L o c a lBu f f e r = pa rBu f f e r ;

t h i s . pFPC = parFPC ;

}

/*** @re tu rn I l b u f f e r i n c u i i n s e r i r e i d a t i appena r a c c o l t i ( e f i l t r a t i d a l l a

WHERE)*/

pub l i c Bu f f e r g e t L o c a l B u f f e r ( ) {

re tu rn t h i s . p L o c a lBu f f e r ;

}

/*** @re tu rn L 'FPC da c u i i d a t i da campionare devono p r o v e n i r e*/

104

Page 106: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

pub l i c Funct i ona l i t yProxyComponent getAssoc ia tedFPC ( ) {

re tu rn t h i s . pFPC ;

}

/*** @re tu rn L ' ogge t to con t enen t e l e t emp i s t i c h e s u l campionamento (REFRESH, IF−

EVERY−ELSE , UNSUPPORTED SAMPLE RATE)*/

pub l i c Sampl ingData getSampl ingData ( ) {

re tu rn t h i s . pSampData ;

}

/*** @re tu rn L ' ogge t to Node che r a pp r e s e n t a l a c l u s o l a WHERE*/

pub l i c Node getWhereCond i t ion ( ) {

re tu rn t h i s . pWhere ;

}

}

Bu�er class

package org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s ;

import j a v a . s q l . Timestamp ;

import j a v a . u t i l . A r r a y L i s t ;

import j a v a . u t i l . C o l l e c t i o n s ;

import j a v a . u t i l . I t e r a t o r ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . ConstantTimestamp ;

pub l i c c l a s s Bu f f e r {

p r i v a t e Ar r a yL i s t <Record> pRecordSet ;

/*** Co s t r u i s c e l ' ogge t to Bu f f e r , i n i z i a l i z z a n d o i l b u f f e r i n t e r n o con 0 r e c o r d*/

pub l i c Bu f f e r ( ) {

pRecordSet = new Ar r a yL i s t <Record >() ;

}

/*** Co s t r u i s c e l ' ogge t to Bu f f e r , i n i z i a l i z z a n d o i l b u f f e r i n t e r n o con s p a z i o pe r

t S i z e r e c o r d* @param t S i z e Dimens ione i n i z i a l e d e l b u f f e r*/

pub l i c Bu f f e r ( i n t t S i z e ) {

pRecordSet = new Ar r a yL i s t <Record >( t S i z e ) ;

}

105

Page 107: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

/*** Co s t r u i s c e l ' ogge t to Bu f f e r , i n i z i a l i z z a n d o i l b u f f e r i n t e r n o con un s e t d i

Record* @param parRecordSet Set d i r e c o r d*/

pub l i c Bu f f e r ( A r r a yL i s t <Record> parReco rdSet ) {

pRecordSet = new Ar r a yL i s t <Record >(parReco rdSet ) ;

}

/*** Aggiunge un r e c o r d a l b u f f e r* @param parRecord* @re tu rn t r u e se l ' ogge t to ò s t a t o agg iung to co r r e t t amente , f a l s e a l t r i m e n t i*/

pub l i c boolean addRecord ( Record parRecord ) {

re tu rn pRecordSet . add ( parRecord ) ;

}

/*** E l im ina un r e c o r d da l b u f f e r* @param parRecord I l r e c o r d da e l i m i n a r e* @re tu rn t r u e se i l r e c o r d ò s t a t o e l im i n a t o co r r e t t amente , f a l s e a l t r i m e n t i*/

pub l i c boolean de lReco rd ( Record parRecord ) {

re tu rn pRecordSet . remove ( parRecord ) ;

}

/*** R i t o r na l ' i n d i c e d e l r e c o r d aven te i l t imestamp s p e c i f i c a t o* r i t o r n a −1 se i l r e c o r d non ò s t a t o t r o v a t o*/

p r i v a t e i n t getIndexByTimestamp ( Timestamp parTimestamp ) {

Record tRecord ;

I t e r a t o r <Record> t I t e r a t o r R e c o r d S e t = pRecordSet . i t e r a t o r ( ) ;

// I t e r a t o r <Constant> t I t e r a t o r C o n s t a n t ;ConstantTimestamp tF ie ldT imestamp ;

//Record tRecord ;

whi le ( t I t e r a t o r R e c o r d S e t . hasNext ( ) ) {

tRecord = ( Record ) t I t e r a t o r R e c o r d S e t . nex t ( ) ;

tF i e ldT imestamp = tRecord . getTimestamp ( ) ;

i f ( tF i e ldT imestamp . getValueTimestamp ( ) == parTimestamp . getTime ( ) )

re tu rn pRecordSet . i ndexOf ( tRecord ) ;

}

re tu rn −1;

}

106

Page 108: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

/*** R i t o r na l ' i t e r a t o r e d i una f i n e s t r a d i r e c o r d* @param p a r I n i t i n d i c e i n i z i a l e d e l l a f i n e s t r a* @param parEnd i n d i c e f i n a l e d e l l a f i n e s t r a* @re tu rn I t e r a t o r*/

p r i v a t e I t e r a t o r <Record> r e co r d sW indow I t e r a t o rBy I nd e x e s ( i n t p a r I n i t , i n t parEnd )

{

A r r a y L i s t <Record> tReco rdWindowI t e ra to r ;

i f ( p a r I n i t >=0 && parEnd < pRecordSet . s i z e ( ) && p a r I n i t <= parEnd ) {

tReco rdWindowI t e ra to r = new Ar r a yL i s t <Record >(pRecordSet . s u b L i s t ( p a r I n i t ,

parEnd ) ) ;

re tu rn tReco rdWindowI t e ra to r . i t e r a t o r ( ) ;

}

e l s e

re tu rn n u l l ;

}

/*** R i t o r na i l r e c o r d d i i n d i c e s p e c i f i c a t o*/

p r i v a t e Record r e co rdBy Index ( i n t pa r I nd e x ) {

i f ( pa r I nd e x >=0 && pa r I nd e x < pRecordSet . s i z e ( ) )

re tu rn ( Record ) pRecordSet . ge t ( pa r I nd e x ) ;

e l s e

re tu rn n u l l ;

}

pub l i c I t e r a t o r <Record> g e t I t e r a t o r ( Timestamp parTimestamp , long pa rDe l t a ) {

i n t t I n d e x I n i t = getIndexByTimestamp ( parTimestamp ) ;

i n t t IndexEnd = getIndexByTimestamp (new Timestamp ( parTimestamp . getTime ( ) +

pa rDe l t a ) ) ;

re tu rn r e c o r d sW indow I t e r a t o rBy I nd e x e s ( t I n d e x I n i t , t IndexEnd+1) ;

}

pub l i c I t e r a t o r <Record> g e t I t e r a t o r ( Timestamp parTimestamp , i n t parNumRecord ) {

i n t t I n d e x I n i t = getIndexByTimestamp ( parTimestamp ) ;

re tu rn r e c o r d sW indow I t e r a t o rBy I nd e x e s ( t I n d e x I n i t , t I n d e x I n i t + parNumRecord ) ;

}

/*** R i t o r na i l contenuto d e l b u f f e r*/

pub l i c Ar r a yL i s t <Record> g e tBu f f e r ( ) {

re tu rn t h i s . pRecordSet ;

}

/*** R e s t i t u i s c e l a s t r i n g a che r a c c o g l i e i l contenuto d e l b u f f e r

107

Page 109: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c S t r i n g t o S t r i n g ( ) {

S t r i n g t S t r i n g = "" ;

I t e r a t o r <Record> t I t e r a t o r = pRecordSet . i t e r a t o r ( ) ;

whi le ( t I t e r a t o r . hasNext ( ) )

t S t r i n g = t S t r i n g + ( ( Record ) t I t e r a t o r . nex t ( ) ) . t o S t r i n g ( ) + "\n" ;

re tu rn t S t r i n g ;

}

}

ActiveSession class

package org . d e i . p e r l a . s y s . env i r onment s ;

import j a v a . u t i l . * ;

/*** Questa c l a s s e r a pp r e s e n t a una s e s s i o n e d i PerLa a t tua lmen t e a t t i v a*/

pub l i c c l a s s Ac t i v e S e s s i o n {

/*** Elenco d e g l i env i ronment a t t i v i*/

p r i v a t e s t a t i c Ar r a yL i s t <Environment> pEnvsArray = new Ar r a yL i s t <Environment >() ;

/*** Aggiunge ( e a t t i v a ) una query , g i a ' p a r s a t a e p ronta pe r e s s e r e e s e g u i t a* @param parEnv i ronment* @re tu rn TRUE agg iun ta co r r e t t amen t e e f a t t a p a r t i r e , FALSE a l t r i m e n t i*/

pub l i c s t a t i c boolean addToSess ion ( Env i ronment parEnv i ronment ) {

// parEnv i ronment . s t a r t ( ) ; // t o g l i e r e i commenti deve p a r t i r e appena agg iun tore tu rn pEnvsArray . add ( parEnv i ronment ) ;

}

/*** @re tu rn L'<code>I t e r a t o r </code> per " s f o g l i a r e " g l i env i r onment s a t t i v i*/

pub l i c s t a t i c I t e r a t o r <Environment> ge tAc t i v eEnv i r onmen t s ( ) {

re tu rn pEnvsArray . i t e r a t o r ( ) ;

}

/*** @re tu rn I l numero d i Statement a t tua lmen t e a t t i v i*/

pub l i c s t a t i c i n t getEnvironmentsNumber ( ) {

re tu rn pEnvsArray . s i z e ( ) ;

}

/*** @re tu rn L ' e l e n c o completo d e g l i env i r onment s a t t i v i

108

Page 110: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c s t a t i c Ar r a yL i s t <Environment> getEnv i ronment s ( ) {

re tu rn pEnvsArray ;

}

}

Environment class

package org . d e i . p e r l a . s y s . env i r onment s ;

import org . d e i . p e r l a . component . S t a r t a b l e ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . Statement ;

pub l i c abs t rac t c l a s s Envi ronment implements S t a r t a b l e {

/*** @re tu rn Ogni s o t t o c l a s s e d i Env i ronment deve r e s t i t u i r e l a Query r a p p r e s e n t a t a* n e l modo che r i t i e n e p i u ' opportuno*/

pub l i c abs t rac t Statement ge tRep re sen tedQue ry ( ) ;

}

LLEnvironment class

package org . d e i . p e r l a . s y s . env i r onment s ;

import j a v a . u t i l . * ;

import org . d e i . p e r l a . component . S t a r t a b l e ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . Statement ;

import org . d e i . p e r l a . s y s . d e v i c e . f p c . Func t i ona l i t yProxyComponent ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a c o l l e c t i o n . LLStack ;

pub l i c c l a s s LLEnvironment extends Envi ronment implements S t a r t a b l e {

p r i v a t e Ar r a yL i s t <LLStack> pStacks ;

pub l i c LLEnvironment ( ) {

pStacks = new Ar r a yL i s t <LLStack >() ;

}

@Over r ide

pub l i c Statement ge tRep re sen tedQue ry ( ) {

re tu rn t h i s . pStacks . ge t (0 ) . g e tDa t aCo l l e c t o r ( ) . g e t LLDa t aCo l l e c t i o n I n f o ( ) .

g e tRep r e s en t edSta t ement ( ) ;

}

pub l i c I t e r a t o r <Funct iona l i t yProxyComponent> getAct iveFPCs ( ) {

A r r a yL i s t <Funct iona l i t yProxyComponent> tAr r a y = new Ar r a yL i s t <

Funct iona l i t yProxyComponent >() ;

f o r ( LLStack tS tack : pStacks ) {

tA r r a y . add ( tS tack . g e tDa t aCo l l e c t o r ( ) . g e t LLDa t aCo l l e c t i o n I n f o ( ) .

getAssoc ia tedFPC ( ) ) ;

109

Page 111: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

}

re tu rn tA r r a y . i t e r a t o r ( ) ;

}

pub l i c Ar r a yL i s t <LLStack> ge tAc t i v eLLS ta ck s ( ) {

re tu rn t h i s . pStacks ;

}

pub l i c vo id addStackToEnvironment ( LLStack pa rStack ) {

t h i s . pStacks . add ( pa rStack ) ;

}

pub l i c boolean i s S t a r t e d ( ) {

re tu rn f a l s e ;

}

pub l i c vo id s t a r t ( ) {

f o r ( LLStack tS tack : pStacks ) {

tS tack . s t a r t ( ) ;

}

}

pub l i c vo id s top ( ) {

// TODO Auto−gene r a t ed method s tub

}

}

QueryAnalyzer class

package org . d e i . p e r l a . s y s . env i r onment s ;

import j a v a . u t i l . A r r a y L i s t ;

import org . d e i . p e r l a . component . * ;

import org . d e i . p e r l a . p a r s e r . c l a u s e s . * ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . * ;

import org . d e i . p e r l a . p a r s e r . s t a t emen t s . * ;

import org . d e i . p e r l a . p a r s e r . u t i l . Dura t i on ;

import org . d e i . p e r l a . s y s . d e v i c e . f p c . Func t i ona l i t yProxyComponent ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a c o l l e c t i o n . * ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . d a t a s t r u c t u r e s . * ;

import org . d e i . p e r l a . s y s . que ry . l l q r u n t im e . e x e c u t i o n . LLExe cu t i on I n f o ;

import org . d e i . p e r l a . s y s . que ry . sq run t ime . S e t I n f o rma t i o n ;

import org . d e i . p e r l a . s y s . r e g i s t r y . MapReg is t ry ;

import org . d e i . p e r l a . s y s . r e g i s t r y . R e g i s t r y ;

import org . d e i . p e r l a . u t i l s . l o g g e r . LogRecord . * ;

/*** Questa c l a s s e s i occupa d e l l a c r e a z i o n e d i t u t t e l e s t r u t t u r e d a t i n e c e s s a r i e

a l l ' e s e c u z i o n e d i una* query i n PerLa .* @author Camplani , Fortunato , Ma r e l l i , Rota e t a l .

110

Page 112: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c c l a s s QueryAna lyze r extends Component {

/*** Co s t r u t t o r e p r i v a t e , pe r e s s e r e s i n g l e t o n* @param parName I l nome d e l componente ( QueryAna lyze r ) da p a s s a r e a Component*/

p r i v a t e QueryAna lyze r ( S t r i n g parName ) {

super ( parName ) ;

}

/*** Re f e r en c e a l l ' un i c a i s t a n z a* @see c o s t r u t t o r e*/

p r i v a t e s t a t i c QueryAna lyze r p I n s t a n c e = nu l l ;

/*** Usato pe r n o t i f i c a r e a l s i s t ema c i o ' che v i e n e compiuto da l QueryAna lyze r* @param parMessageType I l t i p o d e l messagg io* @param parMessage I l messagg io* @param pa rV e r b o s i t y La v e r b o s i t à minima d e l messagg io pe r e s s e r e stampato a

v i d eo* @see Logger*/

p r i v a t e s t a t i c vo id no t i f yToLogge r ( Type parMessageType , S t r i n g parMessage ,

V e r b o s i t y p a rV e r b o s i t y ) {

g e t I n s t a n c e ( ) . l o g g e rN o t i f y ( parMessageType , parMessage , p a rV e r b o s i t y ) ;

}

/*** @re tu rn L ' un i c a i n s t a n z a d e l QueryAna lyze r*/

pub l i c s t a t i c QueryAna lyze r g e t I n s t a n c e ( ) {

i f ( p I n s t a n c e == nu l l ) {

p I n s t a n c e = new QueryAna lyze r ( "QueryAna lyze r " ) ;

re tu rn p I n s t a n c e ;

}

e l s e re tu rn p I n s t a n c e ;

}

/*** Reg i s t r a l a query d i PerLa n e l s i s t ema . Per ogn i Query da e s e g u i r e e '

s u f f i c e n t e* ch iamare ques to metodo che c r e a t u t t o i l n e c e s s a r i o e l o mette i n opera .*/

pub l i c s t a t i c vo id r e g i s t e rQu e r y ( Query parQuery ) {

no t i f yToLogge r ( Type . I n f o rma t i on , "Agg iunta query i n co r s o " , V e r b o s i t y . High ) ;

c r ea t eLLSta t ement s ( parQuery . getLLStatements ( ) ) ;

c r ea teHLStatements ( parQuery . getHLStatements ( ) ) ;

c r e a t eSe tPa r ame t e r sEnv i r onmen t s ( parQuery . ge tSe tS ta t ement s ( ) ) ;

}

/*** Metodo che s i occupa d i c r e a r e g l i <code>Statement</code> d i bas so l i v e l l o* @param p a r L i s t Una l i s t a d i s ta tement

111

Page 113: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/p r i v a t e s t a t i c vo id c r ea t eLLSta t ement s ( LLSta t ementL i s t p a r L i s t ) {

// . . . qua l che d i c h i a r a z i o n eSampl ingData tTab l e ;

Node tWhere ;

LLEnvironment tEnv i ronment ;

L LDa t aCo l l e c t i o n I n f o tDCInfo ;

LLExe cu t i on I n f o tEXIn fo ;

Bu f f e r t B u f f e r ;

// . . pe r ogn i s ta t ement d i bas so l i v e l l of o r ( LLStatement tStatement : p a r L i s t ) {

// . . . t u t t e l e cose c o n d i v i s e d a i v a r i L LDa t aCo l l e c t i o n I n f otEnv i ronment = new LLEnvironment ( ) ;

tTab l e = ge tF r e qu en c i e sTab l e ( tStatement ) ;

tWhere = getWhereCond i t i on ( tStatement ) ;

f o r ( Func t i ona l i t yProxyComponent tFPC : MapRegis t ry . g e t I n s t a n c e ( ) . que ry (

tStatement . g e t E x e c u t e I f C l a u s e ( ) . g e tCond i t i o n ( ) ) ) {

tBu f f e r = new Bu f f e r ( ) ;

tDCInfo = new LLDa t aCo l l e c t i o n I n f o ( tStatement , tFPC , tTable , tWhere , t B u f f e r ) ;

tEXIn fo = new LLExe cu t i on I n f o ( tStatement , t B u f f e r ) ;

// . . c r eo l o s t a c k d i bas so l i v e l l otEnv i ronment . addStackToEnvironment (new LLStack (new Da t aCo l l e c t o r ( tDCInfo ) ,

new Object ( ) ) ) ;

tFPC . r e g i s t e rQu e r y ( tStatement ) ;

}

f o r ( Func t i ona l i t yProxyComponent tFPC : R e g i s t r y . a skReg i s t r yFo rFPCs (

tStatement ) ) {

tFPC . g e tC l a s s ( ) ;

// . . . pe r ogn i FPC o t t enu to da l r e g i s t r y s e r v e un DC, un LLQExec e un LBt B u f f e r = new Bu f f e r ( ) ;

tDCInfo = new LLDa t aCo l l e c t i o n I n f o ( tStatement , tFPC , tTable , tWhere , t B u f f e r ) ;

tEXIn fo = new LLExe cu t i on I n f o ( tStatement , t B u f f e r ) ;

tEXIn fo . hashCode ( ) ; //Rimuovere , s e r v e pe r t o g l i e r e warn ing

// . . c r eo l o s t a c k d i bas so l i v e l l otEnv i ronment . addStackToEnvironment (new LLStack (new Da t aCo l l e c t o r ( tDCInfo ) ,

new Object ( ) ) ) ;

}

// . . . e agg iungo i l t u t t o a l c on t e s t oAc t i v e S e s s i o n . addToSess ion ( tEnv i ronment ) ;

}

no t i f yToLogge r ( Type . I n f o rma t i on , "Terminata c r e a z i o n e d e i LL Env i ronments " ,

V e r b o s i t y . Low) ;

}

/*** Co s t r u i s c e l a t a b e l l a con l e c l a u s o l e IF−EVERY−ELSE

112

Page 114: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/p r i v a t e s t a t i c Sampl ingData g e tF r e qu en c i e sTab l e ( LLStatement parStatement ) {

Samp l ingC lause tC l a u s e = parStatement . ge tSamp l i ngC lau se ( ) ;

i f ( tC l a u s e i n s t anceo f SamplingTimeBased ) {

no t i f yToLogge r ( Type . I n f o rma t i on , " Sampl ing i s TIME based " , V e r b o s i t y . Low) ;

A r r a yL i s t <FrequencyRecord> tA r r a y L i s t = new Ar r a yL i s t <FrequencyRecord >() ;

f o r ( Samp l i n g I f E v e r y tSampl ing : ( ( SamplingTimeBased ) tC l a u s e ) . g e tP e r i o d s ( )

) {

Dura t i on tDu ra t i on = nu l l ;

t r y {

tDu ra t i on = new Durat i on ( tSampl ing . ge tSampl ingRate ( ) . g e tR e s u l t ( ) .

g e tVa l u eF l o a t ( ) , tSampl ing . getTimeUnit ( ) ) ;

}

catch ( Excep t i on e ) {

no t i f yToLogge r ( Type . E r ro r , " E r r o r e n e l r e cupe r o d e l v a l o r e d e l l a

f r e qu en za " , V e r b o s i t y . Low) ;

}

t A r r a y L i s t . add (new FrequencyRecord ( tSampl ing . g e tCond i t i o n ( ) , tDu ra t i on ) ) ;

}

re tu rn new Sampl ingData ( tA r r a y L i s t , ( ( SamplingTimeBased ) tC l a u s e ) . g e tOpe ra t i on ( )

, ( ( SamplingTimeBased ) tC l a u s e ) . g e tR e f r e s hC l a u s e ( ) ) ;

} e l s e {

no t i f yToLogge r ( Type . I n f o rma t i on , " Sampl ing i s EVENT based , no samp l i ng data

needed " , V e r b o s i t y . Low) ;

re tu rn n u l l ;

}

}

/*** Metodo i n t e r n o pe r i l r e c upe r o d e l l a c l a u s o l a WHERE*/

p r i v a t e s t a t i c Node getWhereCond i t ion ( LLStatement parStatement ) {

re tu rn parStatement . ge tSamp l i ngC lau se ( ) . getWhereClause ( ) . g e tCond i t i o n ( ) ;

}

/*** Metodo che s i occupa d i c r e a r e g l i <code>Statement</code> d i a l t o l i v e l l o* @param p a r L i s t Una l i s t a d i s ta tement*/

p r i v a t e s t a t i c vo id c rea teHLStatements ( HLStatementL i s t p a r L i s t ) {

}

/*** Metodo che s i occupa d i c r e a r e g l i <code>Statement</code> per l e Ac tua t i on

Que r i e s* @param p a r L i s t l a l i s t a d i Se tParamete r s Query

113

Page 115: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/p r i v a t e s t a t i c vo id c r e a t eSe tPa r ame t e r sEnv i r onmen t s ( Se tPa r ame t e r sS t a t emen tL i s t

p a r L i s t ) {

f o r ( SetParamete r sSta tement tStatement : p a r L i s t ) {

A c t i v e S e s s i o n . addToSess ion (new SetEnv i ronment (new Se t I n f o rma t i o n ( tStatement

, tStatement . g e tA t t r i b u t e sToSe t ( ) , tStatement . ge t IDs ( ) ) ) ) ;

}

}

}

ExpressionAnalyzer class

package org . d e i . p e r l a . s y s . env i r onment s ;

import j a v a . u t i l . A r r a y L i s t ;

import org . d e i . p e r l a . p a r s e r . e x p r e s s i o n s . * ;

/*** Permette d i a n a l i z z a r e l e e s p r e s s i o n i a l l a r i c e r c a d i campi ogge t to l o g i c o .* @author Camplani , Fortunato , Ma r e l l i , Rota e t a l .*

*/pub l i c c l a s s Exp r e s s i o nAna l y z e r {

p r i v a t e Ar r a yL i s t <NodeLog i c a lOb j e c tA t t r i b u t e > tNodeAr r a yL i s t = new Ar r a yL i s t <

NodeLog i c a lOb j e c tA t t r i b u t e >() ;

/*** Co s t r u t t o r e d e l l a c l a s s e E xp r e s s i o nAna l y z e r*/

pub l i c Exp r e s s i o nAna l y z e r ( ) {

}

/*** Metodo h e l p e r p r i v a t o che s i occupa d i a n a l i z z a r e se un s i n g o l o nodo ò un

campo ogge t to l o g i c o* @param parNode I l nodo da a n a l i z z a r e*/

p r i v a t e vo id g e t F i e l d s (Node parNode ) {

i f ( parNode i n s t anceo f NodeOperat ion ) {

t h i s . g e t F i e l d s ( ( ( NodeOperat ion ) parNode ) . g e tF i r s tNode ( ) , ( ( NodeOperat ion )

parNode ) . getSecondNode ( ) ) ;

}

e l s e i f ( parNode i n s t anceo f NodeAggregat ion ) {

t h i s . g e t F i e l d s ( ( ( NodeAggregat ion ) parNode ) . g e tExp r e s s i onToAgg r ega t e ( ) ) ;

}

e l s e i f ( parNode i n s t anceo f NodeBetween ) {

114

Page 116: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

t h i s . g e t F i e l d s ( ( ( NodeBetween ) parNode ) . g e tF i r s tNode ( ) , ( ( NodeBetween ) parNode

) . getSecondNode ( ) , ( ( NodeBetween ) parNode ) . getThi rdNode ( ) ) ;

}

e l s e i f ( parNode i n s t anceo f NodeFunct ion ) {

t h i s . g e t F i e l d s ( ( ( NodeFunct ion ) parNode ) . g e tFunc t i onPa r ( ) ) ;

}

e l s e i f ( parNode i n s t anceo f NodeLog i c a lOb j e c tA t t r i b u t e && ! t h i s . tNodeAr r a yL i s t .

c o n t a i n s ( parNode ) ) {

t h i s . tNodeAr r a yL i s t . add ( ( NodeLog i c a lOb j e c tA t t r i b u t e ) parNode ) ;

}

}

/*** Metodo h e l p e r che s i occupa d i a n a l i z z a r e un nodo con due " f i g l i " a l l a r i c e r c a

d i campi* @param pa rF i r s tNode I l pr imo nodo* @param parSecondNode I l secondo nodo*/

p r i v a t e vo id g e t F i e l d s (Node pa rF i r s tNode , Node parSecondNode ) {

t h i s . g e t F i e l d s ( pa rF i r s tNode ) ;

i f ( parSecondNode != nu l l ) { t h i s . g e t F i e l d s ( parSecondNode ) ; }

}

/*** Metodo h e l p e r che a n a l i z z a i Nodi con t r e f i g l i ( e . g . NodeBetween )* @param pa rF i r s tNode I l pr imo nodo* @param parSecondNode I l secondo nodo* @param parThirdNode I l t e r z o nodo*/

p r i v a t e vo id g e t F i e l d s (Node pa rF i r s tNode , Node parSecondNode , Node parThirdNode )

{

t h i s . g e t F i e l d s ( pa rF i r s tNode ) ;

t h i s . g e t F i e l d s ( parSecondNode ) ;

t h i s . g e t F i e l d s ( parThirdNode ) ;

}

/*** Ott i en e l ' a r r a y L i s t d i t u t t i i " campi ogge t to l o g i c o " d e i nod i c o n t e n u t i i n un

a r r a y l i s t* @param pa rAr r ayL i s tNode l ' a r r a y L i s t con t en t e i nod i*/

pub l i c Ar r a yL i s t <NodeLog i c a lOb j e c tA t t r i b u t e > g e t F i e l d s ( A r r a yL i s t <Node>

pa rAr r ayL i s tNode ) {

f o r (Node tNode : pa rA r r ayL i s tNode ) {

115

Page 117: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

t h i s . g e t F i e l d s ( tNode ) ;

}

re tu rn t h i s . tNodeAr r a yL i s t ;

}

/*** Metodo pe r o t t e n e r e l ' A r r a y L i s t d i t u t t i i " campi ogge t to l o g i c o " pa r t endo da

un nodo r a d i c e* @param parNode I l nodo r a d i c e d e l l ' e s p r e s s i o n e* @re tu rn A r r a y L i s t con t enen t e i " campi ogge t to l o g i c o " t r o v a t i*/

pub l i c Ar r a yL i s t <NodeLog i c a lOb j e c tA t t r i b u t e > ge tNodeAr r a yL i s t (Node parNode ) {

g e t F i e l d s ( parNode ) ;

re tu rn t h i s . tNodeAr r a yL i s t ;

}

}

Pipe class

/* Copy r i gh t (C) 2008 Romolo Camplani , Marco Fortunato , Marco Ma r e l l i , Guido Rota ,Fab io A . S c h r e i b e r e t a l .

*

* This program i s f r e e s o f twa r e ; you can r e d i s t r i b u t e i t and/ or* modi fy i t under the terms o f the GNU Gene ra l Pub l i c L i c e n s e* as p ub l i s h e d by the Free So f tware Foundat ion ; e i t h e r v e r s i o n 2* o f the L i c en s e , o r ( a t your op t i on ) any l a t e r v e r s i o n .*

* This program i s d i s t r i b u t e d i n the hope tha t i t w i l l be u s e f u l ,* but WITHOUT ANY WARRANTY; w i thout even the imp l i e d war ran ty o f* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU Gene ra l Pub l i c L i c e n s e f o r more d e t a i l s .*

* You shou ld have r e c e i v e d a copy o f the GNU Gene ra l Pub l i c L i c e n s e* a long wi th t h i s program ; i f not , w r i t e to the Free So f tware* Foundat ion , I n c . , 51 F r a n k l i n S t r e e t , F i f t h F loo r , Boston , MA 02110−1301 , USA .*/

package org . d e i . p e r l a . u t i l s . p i p e ;

import j a v a . u t i l . L i n k e d L i s t ;

import j a v a . u t i l . Queue ;

import org . d e i . p e r l a . component . Component ;

import org . d e i . p e r l a . u t i l s . w a i t e r . Wa i tab l e ;

/*** Coda g e n e r i c a d i messagg i . La coda g e s t i s c e l a s i n c r o n i z z a z i o n e t r a i p r o d u t t o r i* ( anche p iu ' d i uno ) ed i l consumatore ( sempre e s o l o uno ) .* Le o p e r a z i o n i d i e s t r a z i o n e d a l l a coda sono non b l o c c a n t i ( v i e n e r i t o r n a t o n u l l

s e* non c i sono d a t i i n coda ) mentre l e o p e r a z i o n i d i i n s e r im e n t o sono b l o c c a n t i (

n e l* caso i n c u i l a coda r i s u l t i p i ena ) .

116

Page 118: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

*/pub l i c c l a s s Pipe<E> extends Component {

/*** L i s t a d e i messagg i p r e s e n t i n e l l a Pipe*/

p r i v a t e f i n a l Queue<E> pMessageQueue ;

/*** Numero massimo d i messagg i che possono e s s e r e a c c o d a t i (0 = i n f i n i t o )*/

p r i v a t e f i n a l i n t pMessageQueueMaxLenght ;

/*** Oggetto Wai tab l e pe r n o t i f i c a r e ad un e v e n t u a l e Waiter l a p r e s en za d i* messagg i n e l l a coda*/

p r i v a t e f i n a l Waitab le<Pipe<E>> pWaitab le ;

/*** Co s t r u t t o r e ( c o s t r u i s c e una p i p e i l l i m i t a t a )*/

pub l i c Pipe ( S t r i n g parName ) {

t h i s ( parName , 0) ;

}

/*** Co s t r u t t o r e* @param parMessageQueueLenght Massimo numero d i messagg i che l a Pipe pot rÃ

con t en e r e (0 = i n f i n i t o )*/

pub l i c Pipe ( S t r i n g parName , i n t parMessageQueueMaxLenght ) {

// Imposta i l nome d e l componentesuper ( parName ) ;

// Memorizza i l massimo numero d i messagg i che l a Pipe pot rà con t en e r ei f ( parMessageQueueMaxLenght < 0) {

parMessageQueueMaxLenght = 0 ;

}

pMessageQueueMaxLenght = parMessageQueueMaxLenght ;

// I s t a n z i a l a coda i n c u i v e r r anno i n s e r i t i i messagg ipMessageQueue = new L i n k edL i s t <E>() ;

// I s t a n z i a l ' ogge t to Wai tab l epWai tab le = new Waitab le<Pipe<E>>( t h i s ) ;

}

/*** Avv ia i l componente p i p e*/

@Over r ide

pub l i c vo id s t a r t ( ) {

synchron ized ( t h i s . pMessageQueue ) {

i f ( ! i s S t a r t e d ( ) ) {

// Segna la che l a p i p e e ' a v v i a t a

117

Page 119: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

s e t S t a r t e d ( t rue ) ;

}

}

}

/*** Ar r e s t a i l componente p i p e*/

@Over r ide

pub l i c vo id s top ( ) {

synchron ized ( pMessageQueue ) {

i f ( i s S t a r t e d ( ) ) {

// Svuota l a p i p epMessageQueue . c l e a r ( ) ;

// R i s v e g l i a e v e n t u a l i t h r e ad i n a t t e s apMessageQueue . n o t i f y A l l ( ) ;

// Re s e t t a i l s e g n a l e n e l l ' ogge t to Wai tab l epWai tab le . r e s e tWa i t e r ( ) ;

// Segna la che l a p i p e non e ' p i u ' a v v i a t as e t S t a r t e d ( f a l s e ) ;

}

}

}

/*** Accoda un nuovo messagg io a l l a P ipe .* Se l a Pipe r i s u l t a p iena , l a ch iamata e ' b l o c c an t e*/

pub l i c vo id enqueue (E parMessageToEnqueue ) {

synchron ized ( pMessageQueue ) {

// V e r i f i c a che l a p i p e s i a a v v i a t ai f ( ! t h i s . i s S t a r t e d ( ) )

re tu rn ;

// Se e ' p r e v i s t a una lunghezza massima d e l l a coda . .i f ( pMessageQueueMaxLenght > 0) {

// . . e que s ta l unghezza e ' s t a t a r a g g i u n t a . .whi le ( pMessageQueue . s i z e ( ) >= pMessageQueueMaxLenght ) {

// . . s i mette i n a t t e s at r y {

pMessageQueue . wa i t ( ) ;

}

catch ( Excep t i on pa rExc ep t i on ) {

// TODO: met te r e i l r e l a y d i I n t e r r u p t e dE x c e p t i o n}

}

}

// Aggiunge i l nuovo messagg io a l l a codapMessageQueue . add ( parMessageToEnqueue ) ;

118

Page 120: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

// Segna la a g l i Waite r i n a t t e s a che c i sono messagg i i n codapWai tab le . n o t i f yWa i t e r ( ) ;

}

}

/*** Es t r a e d a l l a p i p e i l messagg io meno r e c e n t e .* Se l a Pipe e ' vuota , l a ch iamata r i t o r n a n u l l .* @re tu rn ogge t to r e c up e r a t o d a l l a Pipe , n u l l s e l a Pipe e ' vuota .*/

pub l i c E dequeue ( ) {

E tDequeuedMessage = nu l l ;

synchron ized ( pMessageQueue ) {

// V e r i f i c a che l a p i p e s i a a v v i a t ai f ( ! i s S t a r t e d ( ) )

re tu rn n u l l ;

// Se l a p i p e non e ' vuota . .i f ( ! pMessageQueue . i sEmpty ( ) ) {

// . . t o g l i e d a l l a p i p e i l messagg io meno r e c e n t etDequeuedMessage = pMessageQueue . p o l l ( ) ;

// . . r i s v e g l i a e v e n t u a l i t h r ead i n a t t e s a d i po t e r i n s e r i r epMessageQueue . n o t i f y A l l ( ) ;

// . . s e l a p i p e s i e ' s vuota ta , r e s e t t a i l s e g n a l e n e l l ' ogge t to Wai tab l ei f ( pMessageQueue . i sEmpty ( ) ) {

pWai tab le . r e s e tWa i t e r ( ) ;

}

}

}

// R e s t i t u i s c e i l messagg io che e ' s t a t o t o l t o d a l l a p i p ere tu rn tDequeuedMessage ;

}

/*** R e s t i t u i s c e un r i f e r i m e n t o a l l ' ogge t to Wai tab l e*/

pub l i c Waitab le<Pipe<E>> getWa i t ab l e ( ) {

re tu rn pWai tab le ;

}

}

119

Page 121: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

120

Page 122: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

Bibliography

[1] Ian F. Akyildiz Tommaso Melodia Kaushik R. Chowdhury. A survey on wireless

multimedia sensor networks. Comput. Netw.,, 51:921�960, 2007.

[2] M. Fortunato M. Marelli. "Design of a declarative language for pervasive systems".

Master's thesis, Politecnico di Milano, 2006.

[3] Prometeo Project Website. http://www.prometeo.polimi.it, 2009.

[4] Google Protocol Bu�ers Website. http://code.google.com/p/protobuf/, 2010.

[5] F.A. Schreiber R. Camplani M. Fortunato M. Marelli F. Paci�ci. "PerLa: A data

language for pervasive systems". In Proc. of the Sixth Annual IEEE International

Conference on Pervasive Computing and Communication (Percom 2008), pages 282�

287, Piscataway, NJ, USA, 2008. IEEE.

[6] PerLa Website. http://perla.dei.polimi.it, 2009.

[7] A. Pierantozzi R. Puricelli S. Vavassori. "PERLA: Functionality Proxy Component.".

Technologies for Information Systems project report, 2008.

[8] A. Maesani C. Magni E. Padula. "Low Level Architecture of the PerLa middleware".

Technologies for Information Systems project report, Politecnico di Milano, 2009.

[9] Debasis Saha Amitava Mukherjee. Pervasive computing: a paradigm for the 21st

century.

121

Page 123: Design and implementation of the Low Level Query ...perlawsn.sourceforge.net/files/projects/llq_executor.pdf · POLITECNICO DI MILANO Facoltà di Ingegneria Corso di Laurea Triennale

[10] Cappiello Cinzia Schreiber A. Fabio. Quality and energy-aware data compression by

aggregation in WSN data streams. In PerCom Workshops, pages 1�6, 2009.

[11] ART DECO Website. http://artedeco.elet.polimi.it, 2008.

[12] TinyOs Website. http://www.tinyos.com, 2008.

[13] K. Aberer, M. Hauswirth and A. Saleni. "Global Sensor Network". Technical Report

LSIR-REPORT-2006-001, School of Computer and Communication Sciences Ecole

Polytechnique Federale de Lausanne (EPFL), 2006.

[14] D. Chu, A. Tavakoli, L. Popa and J. Hellerstein. "Enterely declarative sensor network

systems". In Proc. VLDB '06, pages 1203�1206, 2006.

[15] Website. http://webdoc.siemens.it/cp/sis/press/sword.htm, 2007.

[16] M. Marino. "A proposal for a context-aware extension of PerLa language". Technolo-

gies for Information Systems project report, Politecnico di Milano, 2009.

[17] S. Vettor. "De�nizione ed implementazione della rappresentazione interna delle espres-

sioni del linguaggio PERLA.". Undergraduate thesis, Politecnico di Milano, 2008.

[18] L. Baresi D. Braga M. Comuzzi F. Paci�ci P. Plebani. "A service infrastructure

for advanced logistics". In IW-SOWE '07: 2nd International Workshop on Service

Oriented Software Engineering, pages 47�53, New York, NY, USA, 2007. ACM press.

[19] P. Prazzoli G. Rota. "Parser design for PERLA query language". Technologies for

Information Systems project report, Politecnico di Milano, 2007.

[20] A. Perrucci. "De�nizione di una struttura ad oggetti per la rappresentazione interna

delle interrogazioni del linguaggio PERLA.". Master's thesis, Politecnico di Milano,

2006.

122