Calling Drools Agenda after completion of iteration of Arraylist in DRL file - iteration

I am iterating a list in the DRL file. I need to call the new Agenda after completion of the loop. But the following code calls the Agenda "B to C" for all iterations
rule "Difference in offsets"
dialect "java"
lock-on-active
when
$notification : NotificationVO()
wtOffset:Integer() from $notification.getWeightOffset();
then
System.out.println("Hello loop1");
$notification.getOffsetChngesInterval().
add((wtOffset-$notification.getInitialOffset()));
update($notification);
drools.setFocus("B to C");
rule "Last activity"
dialect "java"
salience 2
no-loop true
auto-focus false
agenda-group "B to C"
when
$notification:NotificationVO
($notification.getOffsetChngesInterval()!=null)
then
System.out.println("Rule2---"+
$notification.getOffsetChngesInterval().size());
end
In the above code i want to focus the agenda-group "B to C" only after completing the iteration of $notification.getWeightOffset();.

Two comments. (1) The abundant usage of rule attributes is something to be avoided. (2) Rule "Difference in offsets" implements a procedural paradigm: map elements of one list to create another list.
That said, I'd do the calculation of the "changes intervals" on the right hand side of a rule that triggers when there are more "weight offsets" that "changes intervals". (I'm guessing that this is a good condition.)
rule "Difference in offsets"
dialect "java"
when
$ntf: NotificationVO( weightOffset.size() > offsetChngesInterval.size(),
$initOff: initialOffset )
then
for( Integer ioff: $ntf.getWeightOffset ){
$ntf.getOffsetChngesInterval().add(ioff - $initOff );
}
update( $ntf );
System.out.println( $ntf.getOffsetChngesInterval().size());
end

Related

while loop end everything after it's complete

So I'm trying to make a lottery program where you pick a animal, letter, and number, those are put into a array and compared to another that has the parts chosen by random.
Testing with what I call the animal round.
I have a while loop for invalid entries, that it will not move on till one of the four animals is accepted. But when it does that, the variable invalid goes to false and the coding after it doesn't get used at all. I had this problem last night, and when I finally gave up and went to bed, I decided I'll write it out on flowgorithm (If you haven't heard it makes a flow chart and you can go through programming with it step by step).
I made it, and it worked like expected, I copy and paste it over, and I get the exact same problem as last night.
Here is the code.
#import library
import random
#get variables
game = True
invalid = True
animalarray = [""]
animalarray.append("tiger")
animalarray.append("cow")
animalarray.append("turtle")
animalarray.append("bird")
lotteryarray = [""]
#game loop
#animal round
print("Pick a animal: ")
print("tiger")
print("cow")
print("turtle")
print("bird")
print(" ")
lotteryarray[0] = input()
#while loop for invalid entry
while invalid == True:
if lotteryarray[0] == "tiger" or lotteryarray[0] == "cow" or lotteryarray[0] == "turtle" or lotteryarray[0] == "bird":
invalid == False
else:
print("Invalid entry!")
lotteryarray[0] = input()
print(" ")
print("You chose " + lotteryarray[0])
game == False
And this is all I get in the shell:
Pick a animal:
tiger
cow
turtle
bird
tiger
the tiger there is what I put in, it isn't being printed.
And here is the flowgorithm, like I said, in flowgorithm this works.
flowgorithm of lottery game
I figured it out.
I printed what invalid was after it was meant to change to False and it didn't change, I changed the two equal signs to one and it worked and changed the value.

Why this list has only void items in Racket

I tried to have user input function using code on this page: A simple Racket terminal interaction
(define entry_list (for/list ([line (in-lines)]
#:break (string=? line "done"))
(println line)))
(println entry_list)
Output is:
this
"this "
is
"is "
a
"a "
test
"test"
for testing only
"for testing only"
done
'(#<void> #<void> #<void> #<void> #<void>)
Why is the list consisting of only "void" items?
That is because the println function returns #<void>. If instead of println you put something that returned a different value for each line you would end up with a more interesting list.
For example, the following code should return a list with the lines that you typed in:
(define entry_list
(for/list ([line (in-lines)]
#:break (string=? line "done"))
line))
If you just want to print the lines then you could have used for instead of for/list, to avoid creating an useless list of voids at the end:

REBOL layout: How to create layout words automatically - word has no context?

Using the REBOL/View 2.7.8 Core, I would like to prepare a view layout beforehand by automatically assigning words to various layout items, as in the following example.
Instead of
prepared-view: [across
cb1: check
label "Checkbox 1"
cb2: check
label "Checkbox 2"
cb3: check
label "Checkbox 3"
cb4: check
label "Checkbox 4"
]
view layout prepared-view
I would thus like the words cb1 thru cb5 to be created automatically, e.g.:
prepared-view2: [ across ]
for i 1 4 1 [
cbi: join "cb" i
cbi: join cbi ":"
cbi: join cbi " check"
append prepared-view2 to-block cbi
append prepared-view2 [
label ]
append prepared-view2 to-string join "Checkbox " i
]
view layout prepared-view2
However, while difference prepared-view prepared-view2 shows no differences in the block being parsed (== []), the second script leads to an error:
** Script Error: cb1 word has no context
** Where: forever
** Near: new/var: bind to-word :var :var
I've spent hours trying to understand why, and I think somehow the new words need to be bound to the specific context, but I have not yet found any solution to the problem.
What do I need to do?
bind prepared-view2 'view
view layout prepared-view2
creates the correct bindings.
And here's another way to dynamically create layouts
>> l: [ across ]
== [across]
>> append l to-set-word 'check
== [across check:]
>> append l 'check
== [across check: check]
>> append l "test"
== [across check: check "test"]
>> view layout l
And then you can use loops to create different variables to add to your layout.
When you use TO-BLOCK to convert a string to a block, that's a low-level operation that doesn't go through the "ordinary" binding to "default" contexts. All words will be unbound:
>> x: 10
== 10
>> code: to-block "print [x]"
== [print [x]]
>> do code
** Script Error: print word has no context
** Where: halt-view
** Near: print [x]
So when you want to build code from raw strings at runtime whose lookups will work, one option is to use LOAD and it will do something default-ish, and that might work for some code (the loader is how the bindings were made for the code you're running that came from source):
>> x: 10
== 10
>> code: load "print [x]"
== [print [x]]
>> do code
10
Or you can name the contexts/objects explicitly (or by way of an exemplar word bound into that context) and use BIND.

Fixing the Rebol3 example calculator to use normal math operator precedence

In the example calculator:
REBOL [title: "Calculator"]
do %r3-gui.r3
stylize [
btn: button [
facets: [init-size: 50x50]
actors: [on-action:[set-face f join get-face f get-face face]]
]
]
view [
hgroup [
f: field return
btn "1" btn "2" btn "3" btn " + " return
btn "4" btn "5" btn "6" btn " - " return
btn "7" btn "8" btn "9" btn " * " return
btn "0" btn "." btn " / " btn "=" on-action [
attempt [set-face f form do get-face f]
]
]
]
...the resulting program doesn't (as Rebol traditionally doesn't) evaluate mathematical expressions with * having a higher precedence than +. E.g. 2 + 3 * 4 gives 20 instead of 14.
I thought I had read somewhere that Rebol3 contained a new function that would evaluate math expressions more the way folks are used from nearly every other context. Is that true? If so, can the above code be made to use it without a significant amount of change?
I'm not sure there's a specific function in Rebol 3 that evaluates operators according to formal precedence (I'd be pleased to be corrected), though there are attempts in the wild to implement such a function. If you were to locate such a function, you can just change the evaluator from do to do-expression (where do-expression is said function) in the on-action block of your "=" button.
Who said that "the normal order" to evaluate should be the best?
Some 'wiseguy' once came up with that because he could not handle being wrong unfortunately he was stronger than the others and threatened to hammer their heads in if they wouldnot do things like he had done, so we are taught to this day that multiplication precedes over addition despite of the order they were put in. Rebol rebels!
;)

Defining Variables Prior to use in TCL

I'm brand new to TCL, and am trying to wrap my brain around all the usages of "", {}, and [] that it uses. Something I'm used to doing in other languages is defining my variables prior to use, at the beginning of the application. The below code works:
puts "Please enter an integer of choice to be added: "
flush stdout
gets stdin intAnswer
puts "Please enter a second integer of choice to be added: "
flush stdout
gets stdin intAnswerTwo
puts "Please enter a third integer of choice to be added: "
flush stdout
gets stdin intAnswerThree
puts "The total of the three integers is: [expr $intAnswer + $intAnswerTwo + $intAnswerThree]"
What I'm wanting to do is define the variables prior to use. As such:
set intAnswer 0
set intAnswerTwo 0
set intAnswerThree 0
set intTotal 0
This code, placed at the beginning, doesn't work with the rest of the code. What am I missing?
The code looks absolutely fine to me, though [expr {$intAnswer + $intAnswerTwo + $intAnswerThree}] would be better (as it stops potential reinterpretation of the variables' contents, which would be both a safety and performance issue).
However, if you really want to have integers from the user, you need to validate their input. This is most easily done by writing a procedure to do the job so you can reuse it (i.e., you refactor the code to get a value so you can use a more sophisticated version and get it right once):
proc getIntFromUser {message} {
# Loop forever (until we [exit] or [return $response])
while true {
puts $message
flush stdout
set response [gets stdin]
# Important to check for EOF...
if {[eof stdin]} {
exit
}
# The validator (-strict is needed for ugly historical reasons)
if {[string is integer -strict $response]} {
return $response
}
# Not an integer, so moan about it
puts "\"$response\" is not an integer!"
}
}
Now you have that procedure, the rest of your code can become:
set intAnswer [getIntFromUser "Please enter an integer of choice to be added: "]
set intAnswerTwo [getIntFromUser "Please enter a second integer of choice to be added: "]
set intAnswerThree [getIntFromUser "Please enter a third integer of choice to be added: "]
puts "The total of the three integers is: [expr {$intAnswer + $intAnswerTwo + $intAnswerThree}]"
The art of writing good Tcl code (or good code in pretty much any other language) is knowing what are the good points to refactor. A good starting point is “if you do it twice or more, do it once and share”. It's doubly good if you can give the procedure a good name and clear interface, a clear indication that you've got it right. Indeed, you could also go for:
set total [expr {
[getIntFromUser "Please enter an integer of choice to be added: "] +
[getIntFromUser "Please enter a second integer of choice to be added: "] +
[getIntFromUser "Please enter a third integer of choice to be added: "]
}]
puts "The total of the three integers is: $total"
The results observed by the user will be identical.