Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Informer : make Questionnaire @propertyReactive
Browse files Browse the repository at this point in the history
  • Loading branch information
sotty committed Aug 25, 2012
1 parent 684c2b2 commit 0caefe3
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 61 deletions.
Expand Up @@ -71,13 +71,13 @@ public void setContext(String context) {
this.context = context;
}

/**
* For debugging purposes.
*/
@Override
public String toString() {
return super.toString() + " label=" + getLabel() + " context=" +getContext();
}
@Override
public String toString() {
return "Note{" +
"label='" + label + '\'' +
", context='" + context + '\'' +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
Expand Down
Expand Up @@ -18,6 +18,8 @@
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import org.drools.definition.type.Modifies;
import org.drools.definition.type.PropertyReactive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,6 +39,7 @@
*
* @author Damon Horrell
*/
@PropertyReactive
public class Questionnaire extends Group {

private static final String DEFAULT_CLIENT_DATE_FORMAT = "dd/mm/yyyy";
Expand Down Expand Up @@ -156,6 +159,7 @@ public String toString() {
* @param newActiveItem
* @return
*/
@Modifies( { "navigationStack", "items", "activeItem", "completionAction" } )
public void navigationBranch(String[] newItems, String newActiveItem) {
navigationBranch(newItems, newActiveItem, COMPLETION_ACTION_RETURN);
}
Expand All @@ -169,6 +173,7 @@ public void navigationBranch(String[] newItems, String newActiveItem) {
*
* @return
*/
@Modifies( { "navigationStack", "items", "activeItem", "completionAction" } )
public void navigationBranch(String[] newItems, String newActiveItem, String newCompletionAction) {
if ((newItems == null) || (newItems.length == 0)) {
throw new IllegalArgumentException("There are no valid new items to push onto stack.");
Expand All @@ -185,6 +190,7 @@ public void navigationBranch(String[] newItems, String newActiveItem, String new
*
* @return
*/
@Modifies( { "navigationStack", "items", "activeItem", "completionAction" } )
public void navigationReturn() {
if (navigationStack.size() == 0) {
throw new IllegalStateException();
Expand Down Expand Up @@ -278,6 +284,7 @@ public void setAvailableItems(Collection<String> availableItems) {
}


@Modifies( "availableItems" )
public void addAvailableItems(List<String> availableItems) {
this.availableItems.addAll(availableItems);
}
Expand Down Expand Up @@ -325,4 +332,46 @@ public String getDefaultLanguage() {
public void setDefaultLanguage(String defaultLanguage) {
this.defaultLanguage = defaultLanguage;
}



@Modifies( "items" )
public void addItem(String itemId) {
super.addItem(itemId);
}

@Modifies( "items" )
public void insertItem(String itemId, String beforeItemId) {
super.insertItem(itemId, beforeItemId);
}

@Modifies( "items" )
public void appendItem(String itemId, String afterItemId) {
super.appendItem(itemId, afterItemId);
}

@Modifies( "items" )
public int removeItem(String itemId) {
return super.removeItem(itemId);
}

@Modifies( "presentationStyles" )
public void setPresentationStyles(String[] presentationStyles) {
super.setPresentationStyles(presentationStyles);
}

@Modifies( "presentationStyles" )
public void setPresentationStyles(Object[] presentationStyles) {
super.setPresentationStyles(presentationStyles);
}

@Modifies( "presentationStyles" )
public void addPresentationStyle(String presentationStyle) {
super.addPresentationStyle(presentationStyle);
}

@Modifies( "presentationStyles" )
public void removePresentationStyle(String presentationStyle) {
super.removePresentationStyle(presentationStyle);
}
}
Expand Up @@ -24,13 +24,19 @@ public abstract class Surveyable implements ISurveyable {
public abstract void setQuestionnaireId( String id );


protected boolean surveyEnabled = true;
protected boolean surveyEnabled;

protected boolean surveyLocked = false;
protected boolean surveyLocked;

protected boolean stateful = false;
protected boolean stateful;


public Surveyable() {
surveyEnabled = true;
surveyLocked = false;
stateful = false;
}

public void enableSurvey() {
surveyEnabled = false;
}
Expand Down
Expand Up @@ -30,9 +30,8 @@ import java.util.Set;


rule "build list of available items"
no-loop
when
$questionnaire : Questionnaire( $oldAvailableItems : availableItemSet, $items : itemList )
$questionnaire : Questionnaire( $oldAvailableItems : availableItemSet, $items : itemList ) @watch( items )
$newItems : Set( this != $oldAvailableItems ) from accumulate (
Item( $id : id != $questionnaire.id,
id memberOf $items,
Expand Down Expand Up @@ -60,7 +59,6 @@ then
end

rule "activate top-level item (if Questionnaire.activeItem is used)"
no-loop
salience 4 // before: "activate/deactivate lower-level items (if Questionnaire.activeItem is used)"
when
$questionnaire : Questionnaire( activeItem != null )
Expand All @@ -74,7 +72,6 @@ then
end

rule "deactivate top-level items (if Questionnaire.activeItem is used)"
no-loop
salience 4 // before: "activate/deactivate lower-level items (if Questionnaire.activeItem is used)"
when
$questionnaire : Questionnaire( activeItem != null )
Expand All @@ -90,10 +87,9 @@ end


rule "activate lower-level items (if Questionnaire.activeItem is used)"
no-loop
salience 3 // before: "activate/deactivate InvalidAnswers"
when
$questionnaire : Questionnaire(activeItem != null )
$questionnaire : Questionnaire( activeItem != null )
$item : Item( id != $questionnaire.id,
id not memberOf $questionnaire.itemList,
active == false )
Expand All @@ -106,10 +102,9 @@ then
end

rule "deactivate lower-level items (if Questionnaire.activeItem is used)"
no-loop
salience 3 // before: "activate/deactivate InvalidAnswers"
when
$questionnaire : Questionnaire(activeItem != null )
$questionnaire : Questionnaire( activeItem != null )
$item : Item( id != $questionnaire.id,
id not memberOf $questionnaire.itemList,
active == true )
Expand All @@ -126,14 +121,10 @@ end







rule "activate everything (if Questionnaire.activeItem is not used)"
salience 3 // before: "activate/deactivate InvalidAnswers"
when
$questionnaire : Questionnaire(activeItem == null )
$questionnaire : Questionnaire( activeItem == null )
$item : Item( id != $questionnaire.id, active == false )
then
modify ( $item ) {
Expand All @@ -157,7 +148,7 @@ salience -1000 // ensure this is the last rule in the Agenda Group to fire
when
$invalidAnswer : InvalidAnswer();
$question : Question( $id : id == $invalidAnswer.questionId, active == false )
$questionnaire : Questionnaire( enableActionValidation == true, itemList contains $id )
$questionnaire : Questionnaire( enableActionValidation == true, itemList contains $id ) @watch( items )
then
retract( $invalidAnswer );
end
Expand Down
Expand Up @@ -21,7 +21,7 @@ rule "IncludeNotes"
no-loop
when
$n : Note( $ctx : context, $noteId : id )
$quest : Questionnaire( id == $ctx, itemList not contains $noteId )
$quest : Questionnaire( id == $ctx, itemList not contains $noteId ) @watch( items )
then
modify ( $quest ) {
addItem( $noteId );
Expand All @@ -33,7 +33,7 @@ end
rule "RemoveUnusedNotes"
no-loop
when
$quest : Questionnaire( $itemz : itemList )
$quest : Questionnaire( $itemz : itemList ) @watch( items )
$itemId : String() from $itemz;
not Item( id == $itemId )
then
Expand Down
Expand Up @@ -84,7 +84,7 @@ end
rule "Questionnaire Size"
when
$quest : Questionnaire( $formId : id )
?size( $formId, $num ; )
size( $formId, $num ; )
then
insertLogical( new Size( $formId, $num ) );
end
Expand Down Expand Up @@ -130,8 +130,9 @@ when
$pro : Progress( $formId, $latestPerc ; )
$state : ProgressStatus( $formId ; percentage != $latestPerc )
then
$state.setPercentage( $latestPerc );
update( $state );
modify ( $state ) {
setPercentage( $latestPerc );
}
end

rule "Override Progress"
Expand All @@ -150,10 +151,4 @@ then
retract( $p );
end

rule "Clean status"
when
$p : ProgressStatus( $id : formId )
not Questionnaire( id == $id )
then
retract( $p );
end

Expand Up @@ -20,20 +20,22 @@ rule "Attach @{questType} in @{context}"
no-loop
when
$Quest : org.drools.informer.Questionnaire( $ctx : context == "@{context}",
$itemList : itemList )
$itemList : itemList ) @watch( items )
$quest : org.drools.informer.Question( type == "@{questType}", context == $ctx,
$id : id not memberOf $itemList )
$hold : @{klass}( questionnaireId == "@{context}", @{whenClause} )
then
System.err.println("(Re)attaching question @{questType} as it is relevant again");
$Quest.addItem($id);

org.drools.informer.DomainModelAssociation dma = new org.drools.informer.DomainModelAssociation();
dma.setObject( $hold );
dma.setQuestionId( $id );
dma.setContext( "@{context}" );
dma.setProperty("@{questType}");
insert(dma);
update($Quest);
modify( $Quest ){
addItem($id);
}
end

@end{}
Expand Down
Expand Up @@ -20,17 +20,18 @@ rule "Detach @{questType} in @{context}"
no-loop
when
$Quest : org.drools.informer.Questionnaire( $ctx : context == "@{context}",
$itemList : itemList )
$itemList : itemList ) @watch( items )
$quest : org.drools.informer.Question( type == "@{questType}", context == $ctx,
$id : id memberOf $itemList )
$dom : org.drools.informer.DomainModelAssociation( questionId == $id )
$hold : @{klass}( questionnaireId == "@{context}" )
not @{klass}( questionnaireId == "@{context}", @{whenClause} )
then
System.err.println("Detaching question @{questType} " + $id + " as it is not longer relevant");
$Quest.removeItem($id);
retract($dom);
update($Quest);
retract( $dom );
modify ( $Quest ) {
removeItem( $id );
}
end

@end{}
Expand Down
Expand Up @@ -107,7 +107,7 @@ rule "Add_GUI_Questions"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$q : Question( $qid : id, $qLab : preLabel, $post : postLabel, $req : required, $type : answerType, $qName : type )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
not QuestionGUIAdapter( itemId == $qid ) from $guiQuestions
then
QuestionGUIAdapter qga = new QuestionGUIAdapter();
Expand All @@ -129,7 +129,7 @@ rule "Valid_GUI_Questions"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$q : Question( $qid : id, $qLab : preLabel, $post : postLabel, $req : required, $type : answerType, $qName : type, $last : lastAnswer != null )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid )
not InvalidAnswer( questionId == $qid )
then
Expand All @@ -151,7 +151,7 @@ rule "InvalidOrMissing_GUI_Questions"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$q : Question( $qid : id, $qLab : preLabel, $post : postLabel, $req : required, $type : answerType, $qName : type )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid )
InvalidAnswer( questionId == $qid, $failType : type )
then
Expand All @@ -162,7 +162,7 @@ rule "Final_GUI_Questions"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$q : Question( $qid : id, $qLab : preLabel, $post : postLabel, $req : required, $type : answerType, $qName : type, $last : lastAnswer != null, finalAnswer == true )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid )
then
$qga.setAction( "disable" );
Expand Down Expand Up @@ -202,7 +202,7 @@ salience 100
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$qst : MultipleChoiceQuestion( $qid : id, $poss : possibleAnswers, $single : singleAnswer )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid, $possAnswers : possibleAnswers == null )
then
$qga.setPossibleAnswers(new java.util.LinkedList());
Expand All @@ -216,7 +216,7 @@ rule "Add_MultipleOptions"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$qst : MultipleChoiceQuestion( $qid : id, $poss : possibleAnswers, $single : singleAnswer )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid, $possAnswers : possibleAnswers != null )
$pa : MultipleChoiceQuestion.PossibleAnswer( $key : label, $val : value, this not memberOf $possAnswers ) from $poss
then
Expand All @@ -229,7 +229,7 @@ rule "Clear obsolete multiple options"
when
$sga : SurveyGUIAdapter( $sid : itemId, $guiQuestions : surveyQuestions)
$qst : MultipleChoiceQuestion( $qid : id, $poss : possibleAnswers, $single : singleAnswer )
Questionnaire( id == $sid, itemList contains $qid )
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid, $possAnswers : possibleAnswers != null )
$pa : MultipleChoiceQuestion.PossibleAnswer( $key : label, $val : value, this not memberOf $poss ) from $possAnswers
then
Expand Down

0 comments on commit 0caefe3

Please sign in to comment.