@@ -1,4 +1,4 @@
package org.drools.fipa;
package org.drools.interaction;


import java.util.*;
@@ -7,17 +7,26 @@ import org.drools.expectations.*;
import org.drools.informer.generator.*;
import org.drools.informer.*;

import org.jbpm.task.HumanTaskServiceLookup;
import org.jbpm.task.TaskService;


import org.drools.informer.Progress;





declare InteractionTicket
id : String
interactions : List // <Interaction>
source : Object
end

declare TicketActor
interactionId : String @key
actorId : String @key
ticketId : String
actorId : String
end


@@ -42,6 +51,8 @@ end





query formMarker( String $ticketId, String $type, String $formId )
FormMarker( $ticketId, $type, $formId ; )
end
@@ -50,8 +61,17 @@ query owner( String $formId, String $user )
Alert( $ticket, $user, $formId ; )
end

query ticket( String $className, String $formId, InteractionTicket $ticket )
$ticket := InteractionTicket( id == $formId )
end


query conversationForms( String $ticketId, String $actorId, List $list )
$list := List() from accumulate (
Alert( $ticketId, $actorId, $formId ; )
collectList( $formId )
)
end



@@ -66,53 +86,49 @@ when
$inters : interactions )
$inter : Interaction( /*receivers.size > 0*/ ) from $inters
then
System.out.println( "Inserting interaction " + $inter + " for ticket " + $ticket );
insertLogical( $inter );
end

rule "Interactions"
when
$ticket : InteractionTicket( $id : id,
$ticket : InteractionTicket( $ticketId : id,
$inters : interactions )
Interaction( $type : type, this memberOf $inters, receivers.size > 0, $receiver : receivers[0] )
formMarker( $id, $type, $formId ; )
formMarker( $ticketId, $type, $formId ; )
then
System.out.println(" Form is ready " + $formId );
insert( new Alert( $id, $receiver.toString(), $formId ) );
insert( new Alert( $ticketId, $receiver.toString(), $formId ) );
end

rule "System Interactions"
when
$ticket : InteractionTicket( $id : id,
$ticket : InteractionTicket( $ticketId : id,
$inters : interactions )
Interaction( $type : type, this memberOf $inters, receivers.size == 0 )
formMarker( $id, $type, $formId ; )
formMarker( $ticketId, $type, $formId ; )
then
System.out.println(" Form is ready " + $formId );
end


rule "Alerts"
no-loop
salience 1000
when
$al : Alert( $id, $dest, $formId ; )
not TicketActor( $id, $dest ; )
$al : Alert( $ticketId, $dest, $formId ; )
not TicketActor( $convoId, $ticketId, $dest ; )
then
System.out.println( " ******************************* " + $al );
insert( new TicketActor( $id, $dest ) );
System.out.println( " ************************leads to TicketActor( Convoid, " + $ticketId +" , " + $dest + ") " );
insert( new TicketActor( java.util.UUID.randomUUID().toString(), $ticketId, $dest ) );
end

rule "Dispatch"
salience -200
when
TicketActor( $ticketId, $actorId ; )
$list : List() from accumulate (
Alert( $id, $actorId, $formId ; )
collectList( $formId )
)
then
// Alert manager call goes HERE
System.out.println( " ******************************* Deliver to " + $actorId + " : >> " + $list );
end








@@ -158,20 +174,31 @@ end







//




rule "Start"
when
org.drools.informer.Progress( $formId, 0 ; )
Progress( $formId, 0 ; )
?owner( $formId, $user ; )
$taskService : TaskService() from HumanTaskServiceLookup.getInstance().lookup();
then
// start HT
System.out.println( "********************** START HUMAN TASK " + $formId + " FOR USER " +$user + " using " + $taskService );
end

rule "Complete"
when
org.drools.informer.Progress( $formId, 100 ; )
Progress( $formId, 100 ; )
?owner( $formId, $user ; )
$taskService : TaskService() from HumanTaskServiceLookup.getInstance().lookup();
then
// complete HT
System.out.println( "********************** COMPLETE HUMAN TASK " + $formId + " FOR USER " +$user + " using " + $taskService );
end

//rule "Cancel"
@@ -199,6 +226,8 @@ when
DroolsQuery( name == "formMarker", $args : elements )
not FormMarker( $args[0], $args[1] ; )
then
System.out.println(" Query " + java.util.Arrays.asList($args) );

System.out.println(" Will create a form for " + $args[0] + " / " + $args[1] );
String formId = java.util.UUID.randomUUID().toString();

@@ -214,34 +243,6 @@ end



/*

declare EMRToken
id : String
end

rule "EMR"
salience 99999999
when
FactType( $patientId : patientId.value )
not EMRToken( id == $patientId )
then
System.out.println( " << Initializing session >> " );
// factService.load( $patientId )
insert( new EMRToken( $patientId ) );
end


rule "LogEMR"
when
EMRToken( $patientId : id )
then
System.out.println(" << Session initialized for patient " + $patientId + " >>");
end
*/






@@ -1,46 +1,82 @@

package org.kmr2.mock;

/*
import org.drools.fipa.body.content.Action;
import org.drools.fipa.*;
import org.drools.test.RiskThreshold;
import org.drools.test.RiskValue;

declare EMRToken
id : String
end
import org.drools.interaction.*;

rule "EMR"
salience 99999999
when
FactType( $patientId : patientId.value )
not EMRToken( id == $patientId )
then
System.out.println( " << Initializing session >> " );
// factService.load( $patientId )
insert( new EMRToken( $patientId ) );
end
import org.drools.informer.generator.Surveyable;

import org.drools.informer.generator.annotations.Questionable;
import org.drools.informer.generator.annotations.QuestionMark;
import org.drools.informer.generator.annotations.AllowedAnswers;

rule "LogEMR"
when
EMRToken( $patientId : id )
then
System.out.println(" << Session initialized for patient " + $patientId + " >>");
import org.drools.informer.generator.FormRegistry;


import org.drools.dssagentserver.helpers.*;
import java.util.List;




declare PatientInfo
patientName : String @key
end
*/

declare UserInfo
userName : String @key
end



query patientName( String $pat )
PatientInfo( $pat := patientName )
end


query userName( String $pro )
UserInfo( $pro := userName )
end




//rule "Log"

//
//
//declare PatientAcknowledgment extends Surveyable
//@Questionable( label = "Please acknowledge the above message", language = "EN" )
//
// questionnaireId : String
//
// ack : String
// @QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Do you acknowledge the content above ? ", required = false )
// @AllowedAnswers( values = { "accept=Accept", "reject=Reject", "hold=Hold" } )
//
//end
//
//declare ProviderAcknowledgment extends Surveyable
//@Questionable( label = "Please acknowledge the above message", language = "EN" )
//
// questionnaireId : String
//
// ack : String
// @QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Do you acknowledge the content above ? ", required = false )
// @AllowedAnswers( values = { "accept=Accept", "reject=Reject", "hold=Hold", "ignore=Ignore", "escalate=Escalate" } )
//
//end
//
//
//rule "Install"
//salience 9999
//when
// $o : Object()
//then
//// if ( ! $o.getClass().getPackage().getName().contains("Expect") ) {
// System.err.println( $o );
//// }
// FormRegistry.register( PatientAcknowledgment.class );
// FormRegistry.register( ProviderAcknowledgment.class );
//end


@@ -51,15 +87,88 @@ end



rule "Clear pat"
when
$act : Action( $prov : this["userId"], $pat : this["patientId"] )
$pin : PatientInfo( patientName != $pat )
then
retract( $pin );
end

rule "Clear pro"
when
$act : Action( $prov : this["userId"], $pat : this["patientId"] )
$pin : UserInfo( userName != $prov )
then
retract( $pin );
end

rule "Update pat"
when
$act : Action( $prov : this["userId"], $pat : this["patientId"] )
then
insert( new UserInfo( (String) $prov ) );
insert( new PatientInfo( (String) $pat ) );
end


//
//rule "At risk"
//when
// RiskThreshold( $ctx : modelId, $thold : threshold )
// $risk : RiskValue( context == $ctx, value > ($thold / 100.0) )
// ?patientName( $pat ; )
// ?userName( $pro ; )
//then
// System.err.println(" AT RISK ------------ of " + $ctx + " contact " + $pat + " and " + $pro );
//
// InteractionTicket ticket = new InteractionTicket( java.util.UUID.randomUUID().toString(),
// new java.util.ArrayList(),
// $risk );
//
// System.out.println("Just created a ticket " + ticket );
//
// ticket.getInteractions().add( new Interaction( "org.kmr2.mock.PatientAcknowledgment",
// new java.util.LinkedList( java.util.Arrays.asList( $pat ) ) ) );
// ticket.getInteractions().add( new Interaction( "org.kmr2.mock.ProviderAcknowledgment",
// new java.util.LinkedList( java.util.Arrays.asList( $pro, "eafry" ) ) ) );
// insert( ticket );
//end
//
//
//
//






/*

declare EMRToken
id : String
end

rule "EMR"
salience 99999999
when
FactType( $patientId : patientId.value )
not EMRToken( id == $patientId )
then
System.out.println( " << Initializing session >> " );
// factService.load( $patientId )
insert( new EMRToken( $patientId ) );
end


rule "LogEMR"
when
EMRToken( $patientId : id )
then
System.out.println(" << Session initialized for patient " + $patientId + " >>");
end
*/



@@ -80,11 +189,62 @@ end



//rule "Log"
//salience 9999
//rule "Dispatch"
//salience -200
//when
// $o : Object()
// $ticket : InteractionTicket( $ticketId : id,
// $inters : interactions,
// $source : source )
// $convo : TicketActor( $convoId, $ticketId, $actorId ; )
// ?conversationForms( $ticketId, $actorId, $list ; )
// RiskValue( $model, $value ; ) from $source
// ?patientName( $pat ; )
// ?userName( $pro ; )
//then
// System.out.println("PATIENT #SURVEY >>> " + $o);
// System.out.println( " ******************************* Deliver to " + $actorId + " : >> " + $list + " in " + $convo );
//// SynchronousRequestHelper helper = new SynchronousRequestHelper( new java.net.URL("TODO NEED THE EXACT URL") );
//
// java.util.LinkedHashMap map = new java.util.LinkedHashMap();
// map.put("refId", $ticketId );
// map.put("conversationId", $convoId );
// map.put("subjectAbout", new String[] { $pat, $pro } ); // Array of ids, current alert creation will grab the first id as the ptUnitNumber.
// map.put("sender", $pro ); // id
// map.put("mainRecipients", new String[] { $actorId } ); // Array of Ids
// map.put("secondaryRecipients", new String[0] ); // Array of Ids
// map.put("hiddenRecipients", new String[0] ); // Array of Ids
// map.put("type", "ALERT" );
// map.put("header", "Risk threshold exceeded : " + $model );
// map.put("body", "You are in trouble " + $value );
// map.put("priority", "Critical" );
// map.put("deliveryDate", new java.util.Date() );
// map.put("status", "New" ); // [ "New" | "Read" | "Unread" | "Escalated" | ... ]
//
// System.out.println( map );
//
//
//// helper.invokeRequest("deliverMessage",args);
//end
////
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//








@@ -26,11 +26,8 @@
<resource type="DRL" source="classpath:default/interaction.drl" />



<resource type="DRL" source="classpath:declares/risk_declares.drl" />
<resource type="DRL" source="classpath:declares/dxGuide_declares.drl" />
<!-- TODO: fix? issue in KnowledgeAgent to load this dynamically -->
<resource type="DRL" source="classpath:org/kmr2/mock/mock_ptsd_decision.drl" name="MockDecision" description="Post Traumatic Stress Disorder" />



@@ -52,12 +49,19 @@

<resource type="DRL" source="classpath:services/setRiskThreshold.drl" />
<resource type="DRL" source="classpath:services/getRiskModelsDetail.drl" />
<resource type="DRL" source="classpath:services/getRiskModels.drl" />



<resource type="DRL" source="classpath:default/subsession_default.drl" />


<resource type="DRL" source="classpath:default/subsession_default.drl" />



<!-- TODO: fix? issue in KnowledgeAgent to load this dynamically -->
<resource type="DRL" source="classpath:org/kmr2/mock/mock_ptsd_decision.drl" name="MockDecision" description="Post Traumatic Stress Disorder" />
<resource type="DRL" source="classpath:default/usecase_1.drl" />


</add>
@@ -0,0 +1,168 @@

package org.kmr2.mock;

import org.drools.fipa.body.content.Action;
import org.drools.fipa.*;
import org.drools.test.RiskThreshold;
import org.drools.test.RiskValue;

import org.drools.interaction.*;

import org.drools.informer.generator.Surveyable;

import org.drools.informer.generator.annotations.Questionable;
import org.drools.informer.generator.annotations.QuestionMark;
import org.drools.informer.generator.annotations.AllowedAnswers;

import org.drools.informer.generator.FormRegistry;

import org.mvel2.templates.*;

import org.drools.dssagentserver.helpers.*;
import java.util.List;



declare PatientAcknowledgment extends Surveyable
@Questionable( label = "Please acknowledge the above message", language = "EN" )

questionnaireId : String

ack : String
@QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Do you acknowledge the content above ? ", required = false )
@AllowedAnswers( values = { "accept=Accept", "reject=Reject", "hold=Hold" } )

end

declare ProviderAcknowledgment extends Surveyable
@Questionable( label = "Please acknowledge the above message", language = "EN" )

questionnaireId : String

ack : String
@QuestionMark( type = Question.QuestionType.TYPE_TEXT, label = " Do you acknowledge the content above ? ", required = false )
@AllowedAnswers( values = { "accept=Accept", "reject=Reject", "hold=Hold", "ignore=Ignore", "escalate=Escalate" } )

end


rule "Install"
salience 9999
when
then
FormRegistry.register( PatientAcknowledgment.class );
FormRegistry.register( ProviderAcknowledgment.class );
end






rule "At risk"
when
RiskThreshold( $ctx : modelId, $thold : threshold )
$risk : RiskValue( context == $ctx, value > ($thold / 100.0) )
?patientName( $pat ; )
?userName( $pro ; )
then
System.err.println(" AT RISK ------------ of " + $ctx + " contact " + $pat + " and " + $pro );

InteractionTicket ticket = new InteractionTicket( java.util.UUID.randomUUID().toString(),
new java.util.ArrayList(),
$risk );

System.out.println("Just created a ticket " + ticket );

ticket.getInteractions().add( new Interaction( "org.kmr2.mock.PatientAcknowledgment",
new java.util.LinkedList( java.util.Arrays.asList( $pat ) ) ) );
ticket.getInteractions().add( new Interaction( "org.kmr2.mock.ProviderAcknowledgment",
new java.util.LinkedList( java.util.Arrays.asList( $pro, "eafry" ) ) ) );
insert( ticket );
end














rule "Dispatch"
salience -200
when
$ticket : InteractionTicket( $ticketId : id,
$inters : interactions,
$source : source )
$convo : TicketActor( $convoId, $ticketId, $actorId ; )
?conversationForms( $ticketId, $actorId, $list ; )
RiskValue( $value, $model ; ) from $source
?patientName( $pat ; )
?userName( $pro ; )
then
System.out.println( " ******************************* Deliver to " + $actorId + " : >> " + $list + " in " + $convo );
// SynchronousRequestHelper helper = new SynchronousRequestHelper( new java.net.URL("TODO NEED THE EXACT URL") );


java.util.Map vars = new java.util.HashMap(10);
vars.put("title", $model );
vars.put("patient", $pat );
vars.put("disease", $model );
vars.put("risk", 100*$value );
vars.put("provider", $pro );
vars.put("nonRisk", (100-100*$value) );
vars.put("surveyId", $list.get(0) );

CompiledTemplate template = (CompiledTemplate) org.kmr2.TemplateBuilder.getRegistry().getNamedTemplate( "risk_alert.template" );
String body = ( TemplateRuntime.execute ( template, null, vars ).toString() );

java.util.LinkedHashMap map = new java.util.LinkedHashMap();
map.put("refId", $ticketId );
map.put("conversationId", $convoId );
map.put("subjectAbout", new String[] { $pat, $pro } ); // Array of ids, current alert creation will grab the first id as the ptUnitNumber.
map.put("sender", $pro ); // id
map.put("mainRecipients", new String[] { $actorId } ); // Array of Ids
map.put("secondaryRecipients", new String[0] ); // Array of Ids
map.put("hiddenRecipients", new String[0] ); // Array of Ids
map.put("type", "ALERT" );
map.put("header", "Risk threshold exceeded : " + $model );
map.put("body", body );
map.put("priority", "Critical" );
map.put("deliveryDate", new java.util.Date() );
map.put("status", "New" ); // [ "New" | "Read" | "Unread" | "Escalated" | ... ]

System.out.println( map );


// helper.invokeRequest("deliverMessage",args);
end
//























@@ -24,7 +24,7 @@ end
declare ModelList
patientId : String @key
models : List
success : boolean
successStatus : boolean
successMessage : String
end

@@ -56,15 +56,15 @@ end
rule "Combine with threshold"
when
InternalResource( $id : name, $type : description, tags contains "Risk" )
RiskThreshold( modelId == $id, $thold : threshold )
RiskThreshold( modelId == $id, type == "Alert", $thold : threshold )
then
insertLogical( new ModelElement( $id, $type, $type, $thold ) );
end

rule "Default threshold"
when
InternalResource( $id : name, $type : description, tags contains "Risk" )
not RiskThreshold( modelId == $id )
not RiskThreshold( modelId == $id, type == "Alert" )
?defaultRiskThreshold( $def ; )
then
insertLogical( new ModelElement( $id, $type, $type, $def ) );
@@ -90,9 +90,9 @@ end
rule "Update Risk I"
when
$rmd : RiskModelDetail( $mid : modelId )
$fld : PMML4Field( context == $mid, name == "Risk" )
$fld : PMML4Field( context == $mid, name matches ".*Risk$" )
then
System.out.println(" DONNING A RISK VALUE ");
System.out.println(" DONNING A RISK VALUE " + $fld );
RiskValue val = don( $fld, RiskValue.class, true );
end

@@ -108,9 +108,9 @@ end
rule "Threshold"
when
$rmd : RiskModelDetail( $mid : modelId )
getRiskThreshold( $mid, $value ; )
getRiskThreshold( $mid, "Alert", $value ; )
then
$rmd.setDisplayThreshold( $value );
$rmd.setAlertThreshold( $value );
end


@@ -13,9 +13,10 @@ when
$userId : this["userId"],
$patientId : this["patientId"],
$modelId : this["modelId"],
$type : this["type"],
$value : this["threshold"]
)
$h : RiskThreshold( modelId == $modelId )
$h : RiskThreshold( modelId == $modelId, type == $type )
then
retract( $a );
$h.setThreshold( Integer.valueOf( (String) $value ) );
@@ -28,11 +29,12 @@ when
$userId : this["userId"],
$patientId : this["patientId"],
$modelId : this["modelId"],
$type : this["type"],
$value : this["threshold"]
)
not RiskThreshold( modelId == $modelId )
not RiskThreshold( modelId == $modelId, type == $type )
then
insert( new RiskThreshold( (String) $modelId, Integer.valueOf( (String) $value ) ) );
insert( new RiskThreshold( (String) $modelId, (String) $type, Integer.valueOf( (String) $value ) ) );
retract( $a );
end

@@ -44,10 +46,10 @@ end
rule "Support Threshold"
when
ModelMarker( $mid : modelName );
not RiskThreshold( modelId == $mid )
not RiskThreshold( modelId == $mid, type == "Alert" )
?defaultRiskThreshold( $def ; )
then
insert( new RiskThreshold( $mid, $def ) );
insert( new RiskThreshold( $mid, "Alert", $def ) );
end

rule "Clean Threshold"
@@ -61,6 +63,6 @@ end



query setRiskThreshold( String $userId, String $patientId, String $modelId, String $threshold, int $return )
RiskThreshold( $modelId, $return ; )
query setRiskThreshold( String $userId, String $patientId, String $modelId, String $type, String $threshold, int $return )
RiskThreshold( $modelId, $type, $return ; )
end
@@ -0,0 +1,32 @@

@declare{'riskAlert'}

<h2>@{title}</h2>
(This is free form HTML with an optionally embedded survey)

Dear @{patient},

<p>
this is a gentle reminder that you are in a high risk condition
to develop the disease known as @{disease}.
</p>

<p>
As should be clear from the following chart, you have an
estimated probability of @{risk}%, which is above the acceptable
threshold set by your provider, doctor @{provider}.
</p>

(Then, later on down the page in the same HTML, possibly a chart...)
<p class='agentEmbed' type='chart' type='chart' cht='v' chtt='@{disease}' chd='@{risk},@{nonRisk}' />

@{title}:
<p class='agentEmbed' type='survey' id='@{surveyId}' /p>

<br/>
Thank you very much, <br/>
<p>Your Friendly Clinical Decision Support Agent</p>

@end{}

@includeNamed{'riskAlert'}
@@ -0,0 +1,28 @@

@declare{'riskAlert'}

<h2>@{disease}</h2>
(This is free form HTML with an optionally embedded survey)

Dear @{provider},

<p>
Your patient @{patient} has a high risk of developing the disease known as @{disease}.
The estimated rate is around @{value}.
</p>

<p>
They will contact you shortly.
</p>


@{title}:
<p class='agentEmbed' type='survey' id='@{surveyId}' /p>

<br/>
Thank you very much, <br/>
<p>Your Friendly Clinical Decision Support Agent</p>

@end{}

@includeNamed{'riskAlert'}

This file was deleted.

Large diffs are not rendered by default.