C H AO Z H A N G
MONGODB WITH DLVHEX PLUGINS
THE MONGODB
INTRODUCTION TO MONGODB
• MongoDB is an open-source document database which provides high performance on big data.
• A record in MongoDB is a document, which is similar to JSON format, called BSON.
ADVANTAGE OF MONGODB
● JSON is native data type in many programming
languages.
● Embedded documents and arrays instead of
joins.
● Dynamic schema.
EMBEDDED VS NORMALIZED
MONGODB IS FAST AND SCALABLE
• Disk seeking and data locality
RELATIONSHIP WITH RDB
EXAMPLE SENSOR DATA
{ "@id": "smart_ontology:record001", "@type": ["smart_ontology:Record"], "smart_ontology:startTime": ["1410114600", "2014-09-07T11:30:00-07:00”], "smart_ontology:endTime": ["1410116400", "2014-09-07T12:00:00-07:00"], "smart_ontology:fromDevice": [ { "@id": "smart_ontology:phone1", "@type": ["smart_ontology:SmartPhone"], "smart_ontology:installedIn": [ { "@id": "smart_ontology:Alice", "@type": ["smart_ontology:User"] } ] } ], "smart_ontology:isValue": [
"run", "run", "run", "walk", "walk", "walk", "walk", "stay", "stay", "stay", "walk", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "stay", "walk", "walk", "walk"]
}
MONGO QUERY: FIND OUT THE FALLING EVENT
Mongodb command:
db.sensor.find({'smart_ontology:isValue.0':'fall’, 'smart_ontology:isTime.1’: { $gt: '2014-09-07T00:00:00-
07:00’} },}, {_id:0, 'smart_ontology:isTime':1})
Output:{ "smart_ontology:isTime" : [ "1410117095", "2014-09-07T12:11:35-07:00" ] }
Query Operation
Output Operation
Collection name & command
MONGO MAPREDUCE (1)
• Count the number of activities in the last 1 hourvar mapper = function () if(this['smart_ontology:fromDevice'][0]['@type'][0] != "smart_ontology:SmartPhone") {return;}if(this['smart_ontology:startTime'][1] < "2014-09-07T11:00:00-07:00") {return;}for(index in this['smart_ontology:isValue']) {emit(this['smart_ontology:isValue'][index], 1);}
}
MONGO MAPREDUCE (2)
reducer = function(previous, current) {var count = 0;for(index in current) {count += current[index];}return count;
}
db.sensor.mapReduce( mapper, reducer, { out: "result" } )
db.result.find()• { "_id" : "NULL", "value" : 19 }• { "_id" : "run", "value" : 3 }• { "_id" : "stay", "value" : 26 }• { "_id" : "walk", "value" : 12 }
THE MONGODB PLUGIN
PLUGIN MODEL: - QUERY (1)
&mongo_query[D, Q, P, <INPUT1,INPUT2...>](Output)- D: database and collection name: eg:
“smart.sensor”- Q: query criteria: eg: “{startTime: {$gt: ‘2014-
09-07’}”- P: query result projection: eg:
“{isValue;fromDevice}” (should be separated by “;”) (or use “{}” to return the whole document)
- <INPUT1, INPUT2 ... >: the $INPUT in the query can be replace by the constant
eg: &mongo_query["test.person","{age: {$gt: $INPUT}}","name", X](NAME), X = 50.
PLUGIN MODEL: - QUERY ARRAY
If the mongo_query result is array, you should run mongo_array plugin to traverse the data in array.
&mongo_array[A](Index, Value)- A: the input of an Array
- Output: Index: the index number of one array
itemValue: the value of one array item
PLUGIN MODEL: - INSERT
&mongo_insert[D, K1, V1,[K2,V2]...]()- D: database and collection name: eg:
“smart.sensor”- K: The field of JSON Element: eg:
“falling_reason”- V: The value of JSON Element: eg: “heart
attack”- The [K, V] should be paired, and can have as
many as you want.
PLUGIN MODEL: - MAPREDUCE
&mongo_mapreduce[D, M, R,[Q](Output)- D: database and collection name
- M: Map function- R: Reduce function- [Q]: Query operation is optional- Output: The mapReduce Output in JSON
EXAMPLE OF FALLING REASONING(1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Part 1: Data Retrive from MongoDB%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The event of Falling has been detected%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
falling_time(T) :-
&mongo_query["smart.example", "{'smart_ontology:isValue.0': 'fall'}",
"smart_ontology:isTime.0"](T).
thirty_minutes_before(T1) :- falling_time(T), T1 = T-1800.
hour_before(T2) :- falling_time(T), T2 = T-3600.
one_minute_before(T3) :- falling_time(T), T3 = T - 60.
EXAMPLE OF FALLING REASONING(2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get the movement data from smartphone sensor in the range
% of 30 minutes before the falling down happened.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
movement_array(S,A) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:phone1',
'smart_ontology:startTime.0': {$lt: '$INPUT'}, 'smart_ontology:endTime.0': {$gt:
'$INPUT'}}", "smart_ontology:isValue;smart_ontology:startTime.0", T,T](A,S),
falling_time(T).
EXAMPLE OF FALLING REASONING(3)
movement_array(S,A) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:phone1',
'smart_ontology:startTime.0': {$lt: '$INPUT'}, 'smart_ontology:endTime.0': {$gt:
'$INPUT'}}", "smart_ontology:isValue;smart_ontology:startTime.0", T1,T1](A,S),
thirty_minutes_before(T1).
movement_total(S,I,V) :- &mongo_array[A](I,V), movement_array(S,A), V != "NULL”.
movement_in_30_minutes(T1,V) :- movement_total(S,I,V), T1= S+I1, I1 = I*60, T1 > T,
thirty_minutes_before(T).
EXAMPLE OF FALLING REASONING(4)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Get the "insulin taken" and "eating" event data from
% camera sensor in one hour before the falling down happened.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
take_insulin_time(T) :-
&mongo_query["smart.example",
"{'smart_ontology:[email protected]':'smart_ontology:Camera',
'smart_ontology:isValue.0':'take_insulin', 'smart_ontology:isTime.0': {$lt: '$INPUT'},
'smart_ontology:isTime.0': {$gt: '$INPUT'}}}", "smart_ontology:isTime.0", T1, T2](T),
falling_time(T1), hour_before(T2).
EXAMPLE OF FALLING REASONING(5)
eat_time(T) :-
&mongo_query["smart.example",
"{'smart_ontology:[email protected]':'smart_ontology:Camera',
'smart_ontology:isValue.0':'eat','smart_ontology:isTime.0': {$lt: '$INPUT'},
'smart_ontology:isTime.0': {$gt: '$INPUT'}}}", "smart_ontology:isTime.0", T1, T2](T),
falling_time(T1), hour_before(T2).
eat_after_insulin_taken :- take_insulin_time(T1), eat_time(T), T > T1.
EXAMPLE OF FALLING REASONING(6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Glucose testing was given every hour
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
glucose(T,V) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:glucose1','smart_ontology:isTime.
0': {$lt: '$INPUT'}, 'smart_ontology:isTime.0': {$gt: '$INPUT'}}}",
"smart_ontology:isTime.0;smart_ontology:isValue.0", T1, T2](T,V), falling_time(T1),
hour_before(T2).
EXAMPLE OF FALLING REASONING(7)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get the heart rate 1 minute before the falling down event
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
heart_rate_array(A) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:heartrate1',
'smart_ontology:startTime.0': {$lt: '$INPUT'}, 'smart_ontology:endTime.0': {$gt:
'$INPUT'}}", "smart_ontology:isValue", T,T](A), one_minute_before(T).
heart_rate(I,V) :- &mongo_array[A](I,V), heart_rate_array(A), V != "NULL".
not_newest_heart_rate(I,V) :- heart_rate(I,V), heart_rate(I1,V1), I < I1.
newest_heart_rate(V) :- heart_rate(I,V), not not_newest_heart_rate(I,V).
EXAMPLE OF FALLING REASONING(8)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part 2: Reasoning Task
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the activities of run is dominant in the previous 30
% minutes, the person has been activiated intensively. That
% might be one reason of falling down.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
intensive_activity_possibility(Y1) :-
&count[movement_in_30_minutes](X1), &count[movement_in_30_minutes,mask,"run"]
(X2), Y = X2*100, Y1 = Y/X1.
reason_of_falling("intensive_activity") :- intensive_activity_possibility(Y), Y > 50.
EXAMPLE OF FALLING REASONING(9)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% If the person has low glucose level and not eat after the
% insulin taking, or the use has two insulin taking in the
% previous hour, the reason of falling might be "insulin shock"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
reason_of_falling("insulin_shock") :- glucose(T,V), V< 50, not eat_after_insulin_taken.
reason_of_falling("insulin_shock") :- glucose(T,V), V< 50, &count[take_insulin_time](C),
C >= 2.
EXAMPLE OF FALLING REASONING(10)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the person has a heart attack that the heart almost stop
% beating, the falling down reason would be "heart attack”
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
reason_of_falling("heart_attack") :- newest_heart_rate(V), V < 20.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Insert the reasoning result back to the database
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
insert_success:- &mongo_insert["smart.reasoning", "reason_of_falling", X](),
reason_of_falling(X).
DEMO