Version 2, last updated by slim at March 10, 2007 06:43 UTC

Matches incoming message with the provided list of values in sequence.

Sample Declaration

<div id=""
     impl=""
     properties=""
     script=""></div>

Input Terminals

Output Terminals

Properties

Demos that use it

How does it work?

Source


/*

 */
BLUEPRINT(
"~019C8E4211725F46bcA63DB5A5DDFFF2C4", 
[
 ["value_in", "onValue", 10]
],
[
 "match_success_value_out",
 "match_failure_value_out"
],
function (props) {
  var Blueprint = function(id, props, bp_uuid)
  {
    this.__init__(id, bp_uuid);
    this._sequence = props["sequence"] || [];
    this._n = this._sequence.length;
    this._on_success = props["match_success_value"];
    this._on_fail = props["match_failure_value"];
    this._curr_i = 0;
  }

  Blueprint.prototype = new Infotron;
    Blueprint.prototype._isObjEqual = function (obj1, obj2) 
    {
	var keys = {}, p, i, n, val1, val2;
	
	if (obj1 && obj1.isEqualTo) {

	    return obj1.isEqualTo(obj2);
	    /*****************************************************************/
	} else if (obj2 && obj2.isEqualTo) {

	    return obj2.isEqualTo(obj1);
	    /*****************************************************************/
	} else if (obj1 && obj2 && obj1.constructor == obj2.constructor) {
	    if (obj1.constructor == Array) {
		if (obj1.length == obj2.length) {
		    n = obj1.length;
		    for (i = 0; i < n; i ++) {
			val1 = obj1[i];
			val2 = obj2[i];

			if (typeof(val1) == "object" ||
			    typeof(val2) == "object") {

			    return this._isObjEqual(val1, val2);
			    /*************************************************/
			} else if (val1 != val2) {
			    
			    return false;			    
			    /*************************************************/
			}
		    }
		} else {

		    return false;
		    /*********************************************************/
		}
	    } else {
		for (p in obj1) {
		    keys[p] = [obj[p]];
		}

		for (p in obj2) {
		    if (keys[p]) {
			val1 = keys[p];
			val2 = obj2[p];
			
			if ((typeof(val1) == "object" ||
			    typeof(val2) == "object") &&
			    !this._isObjEqual(val1, val2)) {

			    return false;
			    /*************************************************/
			} else if (val1 != val2) {
				
			    return false;			    
			    /*************************************************/
			}
		    } else {
			// discrepancy!
			return false;
			/*****************************************************/
		    }
		}
		return false;
		/*************************************************************/
	    }
	} else {
	    
	    return false;
	    /*****************************************************************/
	}

	return true;
	/*********************************************************************/
    }

    Blueprint.prototype.onValue = function(msg)
    {
	var is_match;
	var match_val = this._sequence[this._curr_i];

	if (typeof(msg) == "object") {
	    is_match = this._isObjEqual(msg, match_val);
	} else {
	    is_match = msg == match_val;
	}

	this._curr_i ++;
	this._curr_i %= this._n;

	if (is_match) {
	    this.postMessage("match_success_value_out", 
			     (this._on_success === undefined) ?  msg : this._on_success);
	} else {
	    this.postMessage("match_failure_value_out", 
			     (this._on_fail === undefined) ? msg : this._on_fail);
	}
    };


    return Blueprint;
}, "Sequence Matcher");