How to call a Jboss BRMS Rule from Rest Client - web-services

I have created a Rule using a decision table in Jboss BRMS and deployed as a REST service using the kie-server. But can't find how to pass parameters to the service.
When I create a Test Scenario and execute the Rule service, It runs fine.
kie-container shows me the End point as
http://host:port/kie-server/services/rest/server/containers/container1
But I can't find how to pass parameters to the service using a Rest Client (Advanced Rest Client).
I send this Parameters with the Request and it gives me the following response.
REQUEST
{
"commands":[
{
"insert":{
"out-identifier":"Customer",
"return-object":"true",
"object": {
"example.si.Customer":{
"time":3,
"age":70,
"gender":"MALE"
}
}
}
},
{
"fire-all-rules":""
}
]
}
HEADERS
X-KIE-ContentType: JSON
Accept: application/json
Content-Type: application/json
authorization: Basic a2llc2VydmVyOmtpZXNlcnZlcjEh
RESPONSE
{
"type": "SUCCESS"
"msg": "Container sinterest5 successfully called."
"result": "{ "results" : [ { "key" : "", "value" : 0 }, { "key" : "Customer", "value" : {"example.si.Customer":{ "time" : 3, "age" : 70, "interest" : null, "gender" : "MALE", "rate" : null, "amount" : null }} } ], "facts" : [ { "key" : "Customer", "value" : {"org.drools.core.common.DefaultFactHandle":{ "external-form" : "0:5:695977235:695977235:5:DEFAULT:NON_TRAIT:example.si.Customer" }} } ] }"
}
And my Rule is..
package Deciding_Rates;
//generated from Decision Table
import example.si.Customer;
function void interest(int time, int rate, int amount)
{System.out.println ( (time * rate * amount )/100 );System.out.println(System.currentTimeMillis());}
lock-on-active true
// rule values at C15, header at C10
rule "DecidingRate_15"
ruleflow-group "Rate"
when
$customer : Customer(Time< 5, Age>=65, Gender == "MALE")
then
modify($customer){setRate(10)};
System.out.println("Time < 5, Senior, Male");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C16, header at C10
rule "DecidingRate_16"
ruleflow-group "Rate"
when
$customer : Customer(Time< 5, Age>=65, Gender == "FEMALE")
then
modify($customer){setRate(11)};
System.out.println("Time < 5, Senior, Female");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C17, header at C10
rule "DecidingRate_17"
ruleflow-group "Rate"
when
$customer : Customer(Time< 5, Age<65, Gender == "MALE")
then
modify($customer){setRate(8)};
System.out.println("Time < 5, young, Male");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C18, header at C10
rule "DecidingRate_18"
ruleflow-group "Rate"
when
$customer : Customer(Time< 5, Age<65, Gender == "FEMALE")
then
modify($customer){setRate(9)};
System.out.println("Time < 5, young, Female");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C19, header at C10
rule "DecidingRate_19"
ruleflow-group "Rate"
when
$customer : Customer(Time >= 5, Age>=65, Gender == "MALE")
then
modify($customer){setRate(12)};
System.out.println("Time >= 5, Senior, Male");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C20, header at C10
rule "DecidingRate_20"
ruleflow-group "Rate"
when
$customer : Customer(Time >= 5, Age>=65, Gender == "FEMALE")
then
modify($customer){setRate(13)};
System.out.println("Time >= 5, Senior, Female");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C21, header at C10
rule "DecidingRate_21"
ruleflow-group "Rate"
when
$customer : Customer(Time >= 5, Age<65, Gender == "MALE")
then
modify($customer){setRate(10)};
System.out.println("Time >= 5, young, Male");
interest($customer.getTime(), $customer.getRate(), 100);
end
// rule values at C22, header at C10
rule "DecidingRate_22"
ruleflow-group "Rate"
when
$customer : Customer(Time >= 5, Age<65, Gender == "FEMALE")
then
modify($customer){setRate(11)};
System.out.println("Time >= 5, young, Female");
interest($customer.getTime(), $customer.getRate(), 100);
end
Am I doing anything wrong Here? Sysouts are not visible in server logs. I am not sure is my rules are getting fired or not.

Related

Radio button with radar plot shiny

I'm trying to make a shiny dashboard with attributes of superheros graphed in a radar plot. I'd also like to be able to use radio buttons to select which of the superheros I'd like to see in the graph. However, when I run this code, I get an error that says: Error in polygon: invalid value specified for graphical parameter "lwd". There is no lwd command that I'm aware of for radar charts so I'm not sure how to correct this. Does anyone have a suggestion on how to handle this error?
library(fmsb)
data<-data.frame(Strength = c(7, 0, 6, 7, 4, 3),
Speed = c(7, 0 , 5, 7, 3, 2),
Intelligence = c(7, 0, 6, 2, 4, 3),
Fighting_Skills = c(7, 0, 4, 4, 4, 6),
Energy = c(7, 0, 6, 6, 1, 1),
Durability = c(7, 0, 6, 6, 3, 3),
row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America"))
head(data)
colors_fill<-c(scales::alpha("gray", 0.1))
#scales::alpha("gold", 0.1),
#scales::alpha("tomato", 0.2),
#scales::alpha("skyblue", 0.2))
colors_line<-c(scales::alpha("darkgray", 0.9))
#scales::alpha("gold", 0.9),
#scales::alpha("tomato", 0.9),
#scales::alpha("royalblue", 0.9))
#radarchart(data,
#seg =7,
#title = "Radar Chart",
#pcol = colors_line,
#pfcol = colors_fill,
#plwd = 1)
#legend(x = 0.6,
#y=1.35,
#legend = rownames(data[-c(1,2),]),
# bty = "n", pch = 20, col = colors_line, cex = 1.2, pt.cex = 3)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Radar chart"),
# Sidebar with a radio buttons for person
sidebarLayout(
sidebarPanel(
radioButtons("variablechoice", "People Choice",
choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
selected = "Thor")
),
# Show a plot
mainPanel(
plotOutput("radar")
)
)
)
# Define server logic required to draw a radar plot
server <- function(input, output) {
output$radar <- renderPlot({
if( input$variablechoice=="Iron Man"){new<-data[c(3),] }
if( input$variablechoice=="Thor"){new<-data[c(4),] }
if( input$variablechoice=="Spiderman"){new<-data[c(5),] }
if( input$variablechoice=="Captain America"){new<-data[c(6),] }
radarchart(new,
seg = 7,
#title = "Radar Chart",
pcol = colors_line,
pfcol = colors_fill,
plwd = 0.5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
The issue is that you have missed to include the first two rows of your data which contain the min and max values for the categories in your new dataframe. That's why radarchart throws an error:
library(fmsb)
library(shiny)
data <- data.frame(
Strength = c(7, 0, 6, 7, 4, 3),
Speed = c(7, 0, 5, 7, 3, 2),
Intelligence = c(7, 0, 6, 2, 4, 3),
Fighting_Skills = c(7, 0, 4, 4, 4, 6),
Energy = c(7, 0, 6, 6, 1, 1),
Durability = c(7, 0, 6, 6, 3, 3),
row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America")
)
colors_fill <- c(scales::alpha("gray", 0.1))
colors_line <- c(scales::alpha("darkgray", 0.9))
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Radar chart"),
# Sidebar with a radio buttons for person
sidebarLayout(
sidebarPanel(
radioButtons("variablechoice", "People Choice",
choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
selected = "Thor"
)
),
# Show a plot
mainPanel(
plotOutput("radar")
)
)
)
# Define server logic required to draw a radar plot
server <- function(input, output) {
output$radar <- renderPlot({
if (input$variablechoice == "Iron Man") {
new <- data[c(1:2, 3), ]
}
if (input$variablechoice == "Thor") {
new <- data[c(1:2, 4), ]
}
if (input$variablechoice == "Spiderman") {
new <- data[c(1:2, 5), ]
}
if (input$variablechoice == "Captain America") {
new <- data[c(1:2, 6), ]
}
radarchart(new,
seg = 7,
# title = "Radar Chart",
pcol = colors_line,
pfcol = colors_fill,
plwd = 0.5
)
})
}
# Run the application
shinyApp(ui = ui, server = server)

How to display index based on value?

This is my code example:
budi = {"Name" : "Budi", "Gender" : "Male", "Age" : 18}
ahmad = {"Name" : "Ahmad", "Gender" : "Male", "Age" : 7}
ika = {"Name" : "Ika", "Gender" : "Female", "Age" : 18}
marged = [budi, ahmad, ika]
I want the results like this, for example based on the 18 year old:
The oldest participants are: Budi and Ika
Here is the code according to what have you asked in the question. Please go through this very simple implementation.
from operator import itemgetter
budi = {"Name" : "Budi", "Gender" : "Male", "Age" : 1}
ahmad = {"Name" : "Ahmad", "Gender" : "Male", "Age" : 7}
ika = {"Name" : "Ika", "Gender" : "Female", "Age" : 18}
marged = [budi, ahmad, ika]
newlist = sorted(marged, key=itemgetter('Age'), reverse=True)
maxAge = newlist[0]["Age"]
finalList = [newlist[0]["Name"]]
for person in newlist[1:]:
if person["Age"] == maxAge:
finalList.append(person["Name"])
if len(finalList) == 1:
print "The oldest participant is: " + finalList[0]
else:
print "The oldest participants are: ",
for name in finalList:
print name+" ",

Django query sum values from related table

I have two tables:
Ticket Table
id paid_with_tax
1 5
2 6
3 7
TicketAdjustment Table
id ticket_id value_with_tax
1 1 2
2 1 1
3 1 2
4 1 3
5 2 5
The query I use:
use = 0
Ticket.objects.all().annotate(
paid_amount=Sum(
F('paid_with_tax') +
Coalesce(F('ticketadjustment__value_with_tax'), 0) * use
)
)
the query would return the following:
[
{id: 1, paid_amount: 7},
{id: 1, paid_amount: 6},
{id: 1, paid_amount: 7},
{id: 1, paid_amount: 8},
{id: 2, paid_amount: 11},
{id: 3, paid_amount: 7},
]
but the above is incorrect since the Ticket Table id=1 values are duplicated by the TicketAdjustment Table values.
how can i get the query to sum the TicketAdjustment Table values and return the following:
[
{id: 1, paid_amount: 13},
{id: 2, paid_amount: 11},
{id: 3, paid_amount: 7},
]
Here the solution for your problem :
Ticket.objects.all().annotate(
paid_amount=(F('paid_with_tax') +
Sum(Coalesce(F('ticketadjustment__value_with_tax'), 0))
)
).values_list('id', 'paid_amount')
values_list select the field you want in your result.
In your primary request there is a big problem.
Sum(F('paid_with_tax') + Coalesce(F('ticketadjustment__value_with_tax'), 0) * use)
This ligne miltiply value_with_tax with zero. So give you zero. It's like :
Sum(F('paid_with_tax'))
You want the sum of value_with_tax for each ticket, this is why I move Sum on it :
Sum(Coalesce(F('ticketadjustment__value_with_tax'), 0))
And after add the value of paid_with_tax
NB : I remove your variable use, because don't know is goal.

Regex to find State and Zip from Address

Trying to make regex that can get state from address
1- 1234 Bellaire Blvd, Suite 123, Houston, TX 77036
2- 1234 BELLAIRE BL #123, HOUSTON, TX 77036
I have this for state
\w{2}(?=\s\d{1,5})
And this for Zip
(?<=\w{2}\s)\d{5}
FOR STATE
In 1st case above regex is returning "te" from "Suite" and TX for state which is correct
However, in 2nd case it is returning nothing
FOR ZIP
77036 is returned in 1st case and null is returned in 2nd case
I don't think regular expressions are the best way to do this. Rather I'd use an API to parse the address into it's components. You will need state_abbreviation and you're sorted. Example response:
[
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "1 Santa Claus Ln",
"last_line": "North Pole AK 99705-9901",
"delivery_point_barcode": "997059901010",
"components": {
"primary_number": "1",
"street_name": "Santa Claus",
"street_suffix": "Ln",
"city_name": "North Pole",
"state_abbreviation": "AK",
"zipcode": "99705",
"plus4_code": "9901",
"delivery_point": "01",
"delivery_point_check_digit": "0"
},
"metadata": {
"record_type": "S",
"zip_type": "Standard",
"county_fips": "02090",
"county_name": "Fairbanks North Star",
"carrier_route": "C004",
"congressional_district": "AL",
"rdi": "Commercial",
"elot_sequence": "0001",
"elot_sort": "A",
"latitude": 64.75233,
"longitude": -147.35297,
"precision": "Zip8",
"time_zone": "Alaska",
"utc_offset": -9,
"dst": true
},
"analysis": {
"dpv_match_code": "Y",
"dpv_footnotes": "AABB",
"dpv_cmra": "N",
"dpv_vacant": "N",
"active": "Y",
"footnotes": "L#"
}
},
{
"input_index": 1,
"candidate_index": 0,
"addressee": "Apple Inc",
"delivery_line_1": "1 Infinite Loop",
// truncated for brevity
}
]
Hope that helped.
You can match against ', ([A-Z]{2}) ' the state will then be the subpattern matched by the parentheses. In python it would look like this.
import re
s1 = "1- 1234 Bellaire Blvd, Suite 123, Houston, TX 77036"
s2 = "2- 1234 BELLAIRE BL #123, HOUSTON, TX 77036"
m = re.search(', ([A-Z]{2}) ', s1)
print(m.group(1))

MongoDB writing a query for search engine

I am trying to write a search script in MongoDB but can't figure out how to do it....The thing I wan't to do is as follows....
Lets I have a string array XD = {"the","new","world"}
Now i want to search string array XD in MongoDB document (using regex) and get the result document. For example..
{ _id: 1, _content: "there was a boy" }
{ _id: 2, _content: "there was a boy in a new world" }
{ _id: 3, _content: "a boy" }
{ _id: 4, _content: "there was a boy in world" }
now I want to get result in accordance to _content contains the string in string array XD
{ _id: 2, _content: "there was a boy in a new world", _times: 3 }
{ _id: 4, _content: "there was a boy in world", times: 2 }
{ _id: 1, _content: "there was a boy", times: 1 }
as first document (_id : 2 ) contains all three { "the" in there, "new" as new, "world" as world } so it got 3
second document (_id: 4) only two { "world" as world } so it got 2
Here is what you can do.
Create a Regex to be matched with _content
XD = ["the","new","world"];
regex = new RegExp(XD.join("|"), "g");
Store a JS function on the server, which matches the _content with XD and returns the counts matched
db.system.js.save(
{
_id: "findMatchCount",
value : function(str, regexStr) {
XD = ["the","new","world"];
var matches = str.match(regexStr);
return (matches !== null) ? matches.length : 0;
}
}
)
Use the function with mapReduce
db.test.mapReduce(
function(regex) {
emit(this._id, findMatchCount(this._content, regex));
},
function(key,values) {
return values;
},
{ "out": { "inline": 0 } }
);
This will produce the output as below:
{
"results" : [
{
"_id" : 1,
"value" : 1
},
{
"_id" : 2,
"value" : 1
},
{
"_id" : 3,
"value" : 1
},
{
"_id" : 4,
"value" : 1
}
],
"timeMillis" : 1,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 0,
"output" : 4
},
"ok" : 1
}
I am not sure how efficient this solution is but it works.
Hope this helps.