Browse files

Informer : make Questions @propertyReactive

  • Loading branch information...
1 parent 0caefe3 commit 7ee6b86a5120e2969488ac0940d9a2808b8e7d0e @sotty sotty committed Aug 9, 2012
View
9 ...former/drools-informer-core/src/main/java/org/drools/informer/MultipleChoiceQuestion.java
@@ -16,6 +16,8 @@
package org.drools.informer;
+import org.drools.definition.type.Modifies;
+import org.drools.definition.type.PropertyReactive;
import org.mvel2.templates.util.io.StringBuilderStream;
import javax.swing.text.Position;
@@ -32,6 +34,7 @@
*
* @author Damon Horrell
*/
+@PropertyReactive
public class MultipleChoiceQuestion extends Question {
private static final long serialVersionUID = 1L;
@@ -189,6 +192,7 @@ public void setPossibleAnswers(Object[] possibleAnswers) {
}
+ @Modifies( "possibleAnswers" )
public void setPossibleAnswersByValue(Collection<String> possibleAnswers) {
if (possibleAnswers == null) {
this.possibleAnswers = null;
@@ -206,6 +210,7 @@ public void setPossibleAnswersByValue(Collection<String> possibleAnswers) {
}
+ @Modifies( "possibleAnswers" )
public void setPossibleAnswersByValue(String[] possibleAnswers) {
if (possibleAnswers == null) {
this.possibleAnswers = null;
@@ -217,6 +222,7 @@ public void setPossibleAnswersByValue(String[] possibleAnswers) {
}
}
+ @Modifies( "possibleAnswers" )
private void addPossibleAnswer(String value) {
StringTokenizer st = new StringTokenizer(value,"=");
String val = st.nextToken().trim().replace("\"","");
@@ -235,6 +241,7 @@ private void addPossibleAnswer(String value) {
*
* @param theValue of the possibleAnswer
*/
+ @Modifies( "possibleAnswers" )
public void removePossibleAnswer(String theValue) {
PossibleAnswer pos = null;
for (PossibleAnswer pa : possibleAnswers) {
@@ -277,6 +284,7 @@ public boolean hasPossibleAnswer(String theValue) {
* @param possibleAnswer
* @param atIndex If >= size of array then the answer is added to the end
*/
+ @Modifies( "possibleAnswers" )
public void insertPossibleAnswer(PossibleAnswer possibleAnswer, int atIndex) {
if (possibleAnswers == null) {
// Really should be discouraged from doing this! Least efficient way of building up the list.
@@ -366,6 +374,7 @@ String getInternalPossibleAnswersAsString() {
* @param possibleAnswers
* @deprecated
*/
+ @Modifies( "possibleAnswers" )
public void setPossibleAnswersAsString(String possibleAnswers) {
if (possibleAnswers == null || possibleAnswers.equals("")) {
this.possibleAnswers = null;
View
36 drools-informer/drools-informer-core/src/main/java/org/drools/informer/Question.java
@@ -15,6 +15,9 @@
*/
package org.drools.informer;
+import org.drools.definition.type.Modifies;
+import org.drools.definition.type.PropertyReactive;
+
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.math.BigDecimal;
@@ -68,6 +71,7 @@
*
* @author Damon Horrell
*/
+@PropertyReactive
public class Question extends Item {
private static final long serialVersionUID = 1L;
@@ -268,6 +272,7 @@ public String getLastAnswer() {
return lastAnswer;
}
+ @Modifies( "answered" )
public void setLastAnswer(String lastAnswer) {
this.lastAnswer = lastAnswer;
}
@@ -277,6 +282,7 @@ public String getTextAnswer() {
return textAnswer;
}
+ @Modifies( "answered" )
public void setTextAnswer(String textAnswer) {
checkType(QuestionType.TYPE_TEXT);
this.textAnswer = textAnswer;
@@ -287,6 +293,7 @@ public Long getNumberAnswer() {
return numberAnswer;
}
+ @Modifies( "answered" )
public void setNumberAnswer(Long numberAnswer) {
checkType(QuestionType.TYPE_NUMBER);
this.numberAnswer = numberAnswer;
@@ -297,6 +304,7 @@ public BigDecimal getDecimalAnswer() {
return decimalAnswer;
}
+ @Modifies( "answered" )
public void setDecimalAnswer(BigDecimal decimalAnswer) {
checkType(QuestionType.TYPE_DECIMAL);
this.decimalAnswer = decimalAnswer;
@@ -307,6 +315,7 @@ public Boolean getBooleanAnswer() {
return booleanAnswer;
}
+ @Modifies( "answered" )
public void setBooleanAnswer(Boolean booleanAnswer) {
checkType(QuestionType.TYPE_BOOLEAN);
this.booleanAnswer = booleanAnswer;
@@ -322,6 +331,7 @@ public Date getDateAnswer() {
}
}
+ @Modifies( "answered" )
public void setDateAnswer(Date dateAnswer) {
checkType(QuestionType.TYPE_DATE);
this.dateAnswer = dateAnswer == null ? null : getDateFormatter().format(dateAnswer);
@@ -333,6 +343,7 @@ public void setDateAnswer(Date dateAnswer) {
* @param dateAnswer
* @throws java.text.ParseException
*/
+ @Modifies( "answered" )
public void setDateAnswer(String dateAnswer) throws ParseException {
checkType(QuestionType.TYPE_DATE);
this.dateAnswer = dateAnswer == null ? null : getDateFormatter().format(getDateFormatter().parse(dateAnswer));
@@ -348,6 +359,7 @@ public String getListAnswer() {
return listAnswer;
}
+ @Modifies( "answered" )
public void setListAnswer(String listAnswer) {
checkType(QuestionType.TYPE_LIST);
this.listAnswer = listAnswer;
@@ -361,6 +373,7 @@ public void setListAnswer(String listAnswer) {
return Arrays.asList(split(this.listAnswer, ","));
}
+ @Modifies( { "lastAnswer", "decimalAnswer", "numberAnswer", "textAnswer", "booleanAnswer", "listAnswer", "dateAnswer", "answered" } )
public void setAnswer(Object answer) {
if (answerType == null) {
@@ -395,16 +408,19 @@ public void setAnswer(Object answer) {
}
}
+ @Modifies( { "numberAnswer", "answered" } )
public void setAnswer(long l) {
setNumberAnswer(l);
}
+ @Modifies( { "decimalAnswer", "answered" } )
public void setAnswer(double d) {
setDecimalAnswer(new BigDecimal(d));
}
+ @Modifies( { "booleanAnswer", "answered" } )
public void setAnswer(boolean b) {
- setBooleanAnswer(b);
+ setBooleanAnswer(b);
}
@@ -439,7 +455,7 @@ public boolean isAnswered() {
}
-
+ @Modifies( { "lastAnswer", "decimalAnswer", "numberAnswer", "textAnswer", "booleanAnswer", "listAnswer", "dateAnswer", "answered" } )
public void fit(String answerValue, QuestionType basicAnswerType) throws NumberFormatException, ParseException {
if (answerValue == null) {
setAnswer(null);
@@ -481,16 +497,30 @@ private void checkType(QuestionType answerType) {
}
}
- /**
+
+ @Modifies( { "presentationStyles", "stylesList" } )
+ public void removePresentationStyle(String presentationStyle) {
+ super.removePresentationStyle(presentationStyle);
+ }
+
+ @Modifies( { "presentationStyles", "stylesList" } )
+ public void addPresentationStyle(String presentationStyle) {
+ super.addPresentationStyle(presentationStyle);
+ }
+
+
+ /**
* Clears any previous answer (which may be of a different data type).
*/
+ @Modifies( { "lastAnswer", "decimalAnswer", "numberAnswer", "textAnswer", "booleanAnswer", "listAnswer", "dateAnswer", "answered" } )
private void clearAnswer() {
textAnswer = null;
numberAnswer = null;
decimalAnswer = null;
booleanAnswer = null;
dateAnswer = null;
listAnswer = null;
+ lastAnswer = null;
}
/**
View
2 ...informer-core/src/main/java/org/drools/informer/generator/annotations/AllowedAnswers.java
@@ -26,6 +26,6 @@
@Target( value = ElementType.FIELD )
public @interface AllowedAnswers {
- String[] values();
+ String[] values() default {};
}
View
4 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/Active.drl
@@ -147,7 +147,7 @@ rule "retract invalid answers for inactive questions"
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 )
+ $question : Question( $id : id == $invalidAnswer.questionId, active == false ) @watch(*)
$questionnaire : Questionnaire( enableActionValidation == true, itemList contains $id ) @watch( items )
then
retract( $invalidAnswer );
@@ -157,7 +157,7 @@ rule "deactivate orphan InvalidAnswers"
salience 2 // before: "propagate readonly from group to questions or subgroups"
when
$invalidAnswer : InvalidAnswer( active == true )
- not Question( id == $invalidAnswer.questionId )
+ not Question( id == $invalidAnswer.questionId ) @watch(*)
then
modify ( $invalidAnswer ) {
setActive( false );
View
21 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/Annotations.drl
@@ -141,37 +141,34 @@ end
rule "MultipleChoice_Question_Auto"
-dialect "mvel"
-no-loop
salience 1
when
$o : ISurveyable( $id : questionnaireId, surveyEnabled == true )
$k1 : Class() from $o.getClass()
Questionable( ) from $k1.getAnnotations()
- $qst : Questionnaire( $ctx : context == $id )
+ $qst : Questionnaire( $ctx : context == $id, $items : itemList )
$fld : Field( $fldName : name, $fldKlass : class ) from AnnotationHelper.getFields($k1)
$q : QuestionMark( $lab : label, $qType : type,
$req : required ) from $fld.getAnnotations()
AllowedAnswers( $ansValues : values ) from $fld.getAnnotations()
- not MultipleChoiceQuestion( context == $id, type == $fldName )
+ not MultipleChoiceQuestion( context == $id, type == $fldName ) @watch(*)
then
MultipleChoiceQuestion mcq = new MultipleChoiceQuestion( $fldName, $lab );
mcq.setContext( $id );
- mcq.setAnswerType($qType);
- mcq.setRequired($req);
- mcq.setPreLabel($lab);
+ mcq.setAnswerType( $qType );
+ mcq.setRequired( $req );
+ mcq.setPreLabel( $lab );
mcq.setPossibleAnswersByValue( $ansValues );
- modify ($qst) {
+ modify ( $qst ) {
addItem( mcq.getId() );
}
- insert(mcq);
DomainModelAssociation dma = new DomainModelAssociation( );
dma.setObject((Object) $o);
- dma.setQuestionId(mcq.id);
- dma.setContext($id);
- dma.setProperty($fldName);
+ dma.setQuestionId( mcq.getId() );
+ dma.setContext( $id );
+ dma.setProperty( $fldName );
insert(dma);
end
View
4 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/DomainModel.drl
@@ -36,7 +36,7 @@ rule "copy answers from the underlying domain model to the questions"
no-loop
salience 6
when
- $question : Question( questionId : id, answerType != null, $ans : answer, $last : lastAnswer )
+ $question : Question( questionId : id, answerType != null, $ans : answer, $last : lastAnswer ) @watch( * )
$association : DomainModelAssociation( questionId == questionId, lastAnswer == $last, object : object )
$obj : Object( this == object )
$answer : Object( this != $ans ) from DomainModelSupport.objectToAnswer( PropertyUtils.getProperty( $association.getObject(), $association.getProperty() ),
@@ -55,7 +55,7 @@ rule "copy question answers to the underlying domain model"
salience 7
no-loop
when
- $question : Question( questionId : id, answerType != null, $answer : answer != null )
+ $question : Question( questionId : id, answerType != null, $answer : answer != null ) @watch( lastAnswer )
$association : DomainModelAssociation( questionId == questionId, $object : object, $property : property, lastAnswer != $answer )
then
// Must retract here, in case we're changin a @key property and using equality mode
View
4 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/Queries.drl
@@ -61,9 +61,9 @@ end
query "answeredQuestions"
- question : Question( answered == true )
+ question : Question( answered == true ) @watch(*)
end
query "answeredQuestionsInContext" (String context)
- question : Question(answered == true, context == context)
+ question : Question(answered == true, context == context) @watch(*)
end
View
6 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/Question.drl
@@ -104,7 +104,7 @@ end
rule "discard invalid multiple choice answers"
salience 20 // before: "copy question answers to the underlying domain model"
when
- $question : MultipleChoiceQuestion( questionId : id, answered == true, $answers : answerItems != null, $possibleAnswers : possibleAnswers )
+ $question : MultipleChoiceQuestion( questionId : id, answered == true, $answers : answerItems != null, $possibleAnswers : possibleAnswers ) @watch(*)
$s : String() from $answers
not PossibleAnswer( value == $s ) from $possibleAnswers
then
@@ -132,7 +132,7 @@ end
rule "required questions answered"
salience -1 // after: any rules that create questions
when
- $question : Question( required == true, answered == false )
+ $question : Question( required == true, answered == false ) @watch(*)
then
InvalidAnswer invalidAnswer = new InvalidAnswer($question.getId(), "You must answer this question");
invalidAnswer.setType("invalid");
@@ -142,7 +142,7 @@ end
rule "questions missing answers"
salience -1 // after: any rules that create questions
when
- $question : Question( required == false, answered == false )
+ $question : Question( required == false, answered == false ) @watch(*)
then
InvalidAnswer invalidAnswer = new InvalidAnswer($question.getId(), "You must answer this question");
invalidAnswer.setType("missing");
View
19 drools-informer/drools-informer-core/src/main/resources/org/drools/informer/ReadOnly.drl
@@ -32,10 +32,10 @@ salience 5
when
$item : Group( itemStyles : stylesList,
stylesList not contains "readonly-inherited"
- && stylesList not contains "readonly" )
+ && stylesList not contains "readonly" )
exists (
Group( itemList contains $item.id, active == true,
- stylesList contains "readonly" || stylesList contains "readonly-inherited" )
+ stylesList contains "readonly" || stylesList contains "readonly-inherited" )
)
then
modify ( $item ) {
@@ -44,15 +44,14 @@ then
end
rule "propagate readonly from group to question"
-no-loop
salience 4
when
$item : Question( itemStyles : stylesList,
stylesList not contains "readonly-inherited"
- && stylesList not contains "readonly" )
+ && stylesList not contains "readonly" ) @watch( presentationStyles )
exists (
Group( itemList contains $item.id, active == true,
- stylesList contains "readonly" || stylesList contains "readonly-inherited" )
+ stylesList contains "readonly" || stylesList contains "readonly-inherited" )
)
then
modify ( $item ) {
@@ -71,7 +70,7 @@ when
|| stylesList contains "readonly" )
not (
Group( itemList contains $item.id, active == true,
- stylesList contains "readonly" || stylesList contains "readonly-inherited" )
+ stylesList contains "readonly" || stylesList contains "readonly-inherited" )
)
then
modify ( $item ) {
@@ -80,15 +79,15 @@ then
end
rule "remove readonly from group to question"
-no-loop
salience 1
when
$item : Question( itemStyles : stylesList,
stylesList contains "readonly-inherited"
- || stylesList contains "readonly" )
+ || stylesList contains "readonly"
+ ) @watch( presentationStyles )
not (
Group( itemList contains $item.id, active == true,
- stylesList contains "readonly" || stylesList contains "readonly-inherited" )
+ stylesList contains "readonly" || stylesList contains "readonly-inherited" )
)
then
modify ( $item ) {
@@ -97,5 +96,3 @@ then
end
-
-
View
5 ...nformer-core/src/main/resources/org/drools/informer/presentation/presentation_adapter.drl
@@ -205,8 +205,9 @@ when
Questionnaire( id == $sid, itemList contains $qid ) @watch( items )
$qga : QuestionGUIAdapter( itemId == $qid, $possAnswers : possibleAnswers == null )
then
- $qga.setPossibleAnswers(new java.util.LinkedList());
- update($qga);
+ modify ( $qga ) {
+ setPossibleAnswers( new java.util.LinkedList() );
+ }
end
View
2 ...s-informer/drools-informer-core/src/test/java/org/drools/informer/QuestionStatusTest.java
@@ -67,8 +67,6 @@ public void testValidityStatus() throws NoSuchFieldException {
Person p1 = new Person("0001",null,18);
-
-
kSession.insert(p1);
kSession.fireAllRules();
View
7 ...s-informer/drools-informer-core/src/test/java/org/drools/informer/rules/ReadOnlyTest.java
@@ -98,15 +98,20 @@ public void testReadOnly() {
assertEquals(true, isReadOnlyInherited(question1));
+ System.out.println( "-------------------------------------------" );
+
questionnaire.setActiveItem(group1.getId());
knowledgeSession.update(handleQuestionnaire, questionnaire);
knowledgeSession.fireAllRules();
+
assertEquals(true, isReadOnlyInherited(question1));
+ System.out.println( "-------------------------------------------" );
+
questionnaire.setActiveItem(group2.getId());
knowledgeSession.update(handleQuestionnaire, questionnaire);
knowledgeSession.fireAllRules();
- assertEquals(false, isReadOnlyInherited(question1));
+ assertEquals(false, isReadOnlyInherited(question1));
questionnaire.setActiveItem(group3.getId());
knowledgeSession.update(handleQuestionnaire, questionnaire);
View
2 ...-informer/drools-informer-core/src/test/resources/org/drools/informer/multiSurveyTest.drl
@@ -45,7 +45,7 @@ rule "onAnswer"
when
Question( type == "Question 1",
answer == "xyz",
- $ctx: context )
+ $ctx: context ) @watch( * )
then
list.add($ctx);
end

0 comments on commit 7ee6b86

Please sign in to comment.