otm:the_matching_of_causal_sequences:example1

Analyzing Tweet Popularity

The following example shows two independent Web applications that constitute a social network application. A user can publish a tweet and this tweet can be retweeted by the other user because both users follow each other. A OTM distributed stateful aspect observes and counts each retweet of a user.

Toggle between the code and example

The popularity of a tweet is carried out by the following sequence:

var countRetweet = causalSeq(callTweet, repeatUntil(eventRetweet, event("midnight")))

This sequence matches and counts all retweets of a tweet until the midnight event happens. The retweets that are considered must be caused by a tweet received. This sequence begins the matching process when the callTweet pointcut matches:

var callTweet = call(tweet).and(function(jp, env){
    return env.bind("idT", jp.args[0]).bind("counter", 0);
});

This pointcut matches the call of the tweet function and returns an environment that contains the id of the tweet and a counter initialized to zero. This counter is used to count the retweets of a tweet. Whenever callTweet matches, the eventRetweet pointcut could match several times:

var eventRetweet = function (jp, env){
    return jp.isCustom("retweet") && geTweet(jp) == env.tweet ? 
             env.bind("counter", env.counter + 1) : false;
};    

This pointcut matches the retweet event. This event is the notification of a call to the retweet function that happened in another machine/application. The tweet of this event must be the same (ie the same string) that the environment contains. If this pointcut matches, it returns an environment that contains the counter incremented by one. This pointcut matches retweet events until the midnight event happens, meaning that the sequence matches.

A user commonly publishes several tweets, then it is necessary to have several sequences, ie. one sequence for every different tweet. For example, if a user publishes the tweet m1 twice and m2 once, we only need two sequences: one for m1 and a sequence for m2 to analyze the popularity of all tweets of the user. To achieve this goal, it is necessary to spawn a new sequence for every different tweet. WeCa, through OTM , allows us to spawn a new sequence when a different tweet is published:

var newPosts = function(steps, index, env, jp){
    var currentEnvs = this.getEnvsOfCurrentSeqs(index);
 
    return !currentEnvs.some(function(currentEnv){ //returns true if some condition is not satisfied
        return getTweet(currentEnv.idT) == getTweet(env.idT);
    });
};

Finally, the definition and deployment of the sAspTweetPopularity stateful aspect that determines the popularity of tweets is:

var sAspTweetPopularity = {
    kind: AFTER,
    sequence: countRetweet,
    advice: function(jp, env){
        addPopularity(env.idT, env.counter);
    },
    spawn: newPosts
};
WeCa.OTM.deploy(sAspTweetPopularity);

Go the parent Web page.

  • otm/the_matching_of_causal_sequences/example1.txt
  • Last modified: 2012/02/06 19:22
  • by aspectscript