Jump to: navigation, search

Detect User Data Changes

The following SCXML strategy shows how to detect changes in user data for an interaction.

<scxml version="1.0" xmlns="http://www.w3.org/2005/07/scxml"
	initial="waitForInteraction">
	<script>
		var userdata = ({});
		function detectChanges( oldObj, newObj, objDiffs )
		{
			if ( null == oldObj || null == newObj || 
				"undefined" ==	typeof( oldObj ) || "undefined" == typeof( newObj ) )
			{
				__Log( 'Invalid	userdata' )
				return false;
			}
	
			var key;
			for ( key in oldObj )
			{
				if ( newObj.hasOwnProperty( key ) )
				{
					if ( oldObj[key] !=	newObj[key] )
					{
						objDiffs.push({ userdata : "updated" , key:	key, oldvalue :	oldObj[key], newvalue :	newObj[key] });
					}
				}
				else
				{
					objDiffs.push({ userdata: "deleted", key : key, value :	oldObj[key] });
				}
			}
			for ( key in newObj )
			{
				if ( !oldObj.hasOwnProperty( key ) )
				{
					objDiffs.push({ userdata: "added", key : key, value : newObj[key] });
				}
			}
	
			return true;
		}
	</script>
	<datamodel>
		<data id="reqid" />
	</datamodel>
	<state id="waitForInteraction">
		<transition event="interaction.added" target="globalstate">	
			<script>
				_data.ixnid = _event.data.interactionid;
			</script>
		</transition>
	</state>
	<state id="globalstate">
		<onentry>
			<script>
				userdata = _genesys.ixn.interactions[_data.ixnid].udata;
				__Log('Userdata initial value = ' + uneval( userdata ) );
			</script>
		</onentry>
		<initial>
			<transition target="addUdata" />
		</initial>
		<transition event="interaction.udata.changed"
			cond="_genesys.ixn.interactions[_data.ixnid].udata.hasOwnProperty( 'timeToExit' )"
			target="exit" />
		<transition event="interaction.udata.changed">
			<script>
				var objDiffs =[];
				if ( detectChanges( userdata, _genesys.ixn.interactions[_data.ixnid].udata, objDiffs ) )
				{
					__Log( uneval( objDiffs ) );
				}
				__Log('Userdata updated to = ' + uneval(
				_genesys.ixn.interactions[_data.ixnid].udata ) )
				userdata =
				_genesys.ixn.interactions[_data.ixnid].udata;
	  </script>
		</transition>
		<state id="addUdata">
			<onentry>
				<script>
					var myData = { name : "Smith, John", age : 45 };
					_genesys.ixn.setuData( myData );
				</script>
			</onentry>
			<transition target="updateUdata" />
		</state>
		<state id="updateUdata">
			<onentry>
				<script>
					_genesys.ixn.setuData( { age : 25 } );
				</script>
			</onentry>
			<transition target="deleteUdata" />
		</state>
		<state id="deleteUdata">
			<onentry>
				<script>
					_genesys.ixn.deleteuData( 'name' );
				</script>
			</onentry>
			<transition target="endInteraction" />
		</state>
		<state id="endInteraction">
			<onentry>
				<script>
					var exitFlag = { timeToExit : "Y" };
					_genesys.ixn.setuData( exitFlag );
				</script>
			</onentry>
		</state>
	</state>
	<final id="exit" />
	<final id="error" />
</scxml>

In this example, the "detectChanges" function is called five times (see sample ORS log below). It is first called when ORS information such as ORDbid, ORSession, and ORUrl are added to the interaction user data. It is called a second time after "name" and "age" are added to the user data during state "addUdata". It is called a third time after the "age" is modified during the state "updateUdata". It is called a fourth time after the "name" is deleted during the state "deleteUdata". It is called a fifth time after "timeToExit" is added to the user data during state "endInteraction", which signals the strategy to end when it transitions to the state "exit".

The inputs to the function "detectChanges" are the userdata object before the "interaction.udata.changed" event, and the userdata object after the "interaction.udata.changed" event. The output is the "objDiffs" object which keeps track of changes such as additions, deletions, and modifications.

METRIC <transition sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='globalstate' event='interaction.udata.changed' line='64' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='[{userdata:"added", key:"ORDbid", value:"143"}, 
       {userdata:"added", key:"ORSession", value:"2M4DTARKVT2MBFSKSCVVHVM08K000001"}, 
       {userdata:"added", key:"ORUrl", value:"http://localhost:17011/scxml"}]' label='' level='1' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='Userdata updated to = ({ORDbid:"143", ORSession:"2M4DTARKVT2MBFSKSCVVHVM08K000001", 
       ORUrl:"http://localhost:17011/scxml"})' label='' level='1' />
...
METRIC <event_queued sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' type='external' />
METRIC <event_processed sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' disposition='transition selected' />
METRIC <transition sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='globalstate' event='interaction.udata.changed' line='64' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='[{userdata:"added", key:"age", value:45}, 
       {userdata:"added", key:"name", value:"Smith, John"}]' label='' level='1' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='Userdata updated to = ({ORDbid:"143", ORSession:"2M4DTARKVT2MBFSKSCVVHVM08K000001", 
       ORUrl:"http://localhost:17011/scxml", age:45, name:"Smith, John"})' label='' level='1' />
...
METRIC <event_queued sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' type='external' />
METRIC <event_processed sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' disposition='transition selected' />
METRIC <transition sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='globalstate' event='interaction.udata.changed' line='64' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='[{userdata:"updated", key:"age", oldvalue:45, newvalue:25}]' label='' level='1' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='Userdata updated to = ({ORDbid:"143", ORSession:"2M4DTARKVT2MBFSKSCVVHVM08K000001", 
       ORUrl:"http://localhost:17011/scxml", age:25, name:"Smith, John"})' label='' level='1' />
...
METRIC <event_queued sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' type='external' />
METRIC <event_processed sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' disposition='transition selected' />
METRIC <transition sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='globalstate' event='interaction.udata.changed' line='64' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='[{userdata:"deleted", key:"name", value:"Smith, John"}]' label='' level='1' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='Userdata updated to = ({ORDbid:"143", ORSession:"2M4DTARKVT2MBFSKSCVVHVM08K000001", 
       ORUrl:"http://localhost:17011/scxml", age:25})' label='' level='1' />
...
METRIC <event_queued sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' type='external' />
METRIC <event_processed sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='interaction.udata.changed' disposition='transition selected' />
METRIC <transition sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' name='globalstate' event='interaction.udata.changed' line='64' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='[{userdata:"added", key:"timeToExit", value:"Y"}]' label='' level='1' />
METRIC <log sid='2M4DTARKVT2MBFSKSCVVHVM08K000001' expr='Userdata updated to = ({ORDbid:"143", ORSession:"2M4DTARKVT2MBFSKSCVVHVM08K000001", 
       ORUrl:"http://localhost:17011/scxml", age:25, timeToExit:"Y"})' label='' level='1' /> 
This page was last edited on September 22, 2017, at 13:06.
Comments or questions about this documentation? Contact us for support!