{
"commands":[
{
"insert":{
"out-identifier":"jonny",
"object":{
"demo.firstattempt.Person":{
"name":"John",
"age":26,
"valid":"invalid"
}
}
}
},
{
"fire-all-rules":""
}
]
}{
"type":"SUCCESS",
"msg":"Container ruleContainer successfully called.",
"result":"{\r\n \"results\" : [ {\r\n \"key\" : \"\",\r\n \"value\" : 1\r\n }, {\r\n \"key\" : \"jonny\",\r\n \"value\" : {\"demo.firstattempt.Person\":{\"name\":\"John\",\"age\":26,\"valid\":\"valid\"}}\r\n } ],\r\n \"facts\" : [ {\r\n \"key\" : \"jonny\",\r\n \"value\" : {\r\n \"external-form\" : \"0:8:910483374:910483374:8:DEFAULT:NON_TRAIT:demo.firstattempt.Person\"\r\n }\r\n } ]\r\n}"
}{
"results":[
{
"key":"john",
"value":{
"type":"MAP",
"element":[
{
"key":"name",
"value":"John"
},
{
"key":"age",
"value":20
},
{
"key":"valid",
"value":"invalid"
}
]
}
}
],
"facts":[
{
"key":"john",
"value":{
"external-form":"0:7:1436937083:1920279190:7:DEFAULT:NON_TRAIT:java.util.LinkedHashMap"
}
}
]
}
My application code is like this: String serverUrl = "http://localhost:8080/kie-server/services/rest/server";
String user = "kieserver";
String password = "-------";
String containerId = "ruleContainer";
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
configuration.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
List<GenericCommand<?>> cmds = new ArrayList<GenericCommand<?>>();
BatchExecutionCommandImpl executionCommand = new BatchExecutionCommandImpl(cmds);
executionCommand.setLookup("defaultKieSession");
InsertObjectCommand insertObjectCommand1 = new InsertObjectCommand();
insertObjectCommand1.setOutIdentifier("john");
insertObjectCommand1.setObject(new Person("John",20,"invalid"));
FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand();
cmds.add(insertObjectCommand1);
cmds.add(fireAllRulesCommand);
RuleServicesClient ruleClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
ServiceResponse<String> response = ruleClient.executeCommands(containerId, executionCommand);
System.out.println("\n\n\n######### Rules executed");
System.out.println("\n\n " + response.getResult());--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/70937d61-c010-42fe-9d8e-2258b0047419%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
{
"commands":[
{
"insert":{
"out-identifier":"jony",
"return-object":"true",
"object":{
"demo.firstattempt.Person":{
"name":"john",
"age":25,
"valid":"invalid"
}
}
}
},
{
"fire-all-rules":""
}
]
}{
"type" : "SUCCESS",
"msg" : "Container rulesContainer successfully called.",
"result" : "{\r\n \"results\" : [ {\r\n \"key\" : \"\",\r\n \"value\" : 1\r\n }, {\r\n \"key\" : \"jony\",\r\n \"value\" : {\"demo.firstattempt.Person\":{\"name\":\"john\",\"age\":25,\"valid\":\"true\"}}\r\n } ],\r\n \"facts\" : [ {\r\n \"key\" : \"jony\",\r\n \"value\" : {\r\n \"external-form\" : \"0:1:1129868577:1129868577:1:DEFAULT:NON_TRAIT:demo.firstattempt.Person\"\r\n }\r\n } ]\r\n}"}
When I post from my app wrote in my first post, I got this:{
"results" : [ {
"key" : "marius",
"value" : {
"type" : "MAP",
"element" : [ {
"key" : "name",
"value" : "Marius"
}, {
"key" : "age",
"value" : 20
}, {
"key" : "valid",
"value" : "invalid"
} ]
}
} ],
"facts" : [ {
"key" : "marius",
"value" : {
"external-form" : "0:2:272566824:-81332424:2:DEFAULT:NON_TRAIT:java.util.LinkedHashMap"
}
} ]
}
My rule deployed on the server:package demo.firstattempt;
import java.lang.Number;
rule "Valid age"
dialect "mvel"
when
person : Person( age >= 18 )
then
person.setValid( "true" );
end
--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/496059de-e8d5-4691-a2ed-894b8cd82e89%40googlegroups.com.
package com.sample;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.drools.core.command.impl.GenericCommand;
import org.drools.core.command.runtime.BatchExecutionCommandImpl;
import org.drools.core.command.runtime.rule.FireAllRulesCommand;
import org.drools.core.command.runtime.rule.InsertObjectCommand;
import org.kie.server.api.marshalling.Marshaller;
import org.kie.server.api.marshalling.MarshallerFactory;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.model.KieContainerResource;
import org.kie.server.api.model.KieContainerResourceList;
import org.kie.server.api.model.ReleaseId;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.RuleServicesClient;
import demo.firstattempt.Person;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest {
public static final void main(String[] args) {
String serverUrl = "http://localhost:8080/kie-server/services/rest/server";
String user = "kieserver";
String password = "kieserver1!";
String containerId = "rulesContainer";
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
configuration.setMarshallingFormat(MarshallingFormat.JSON);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
KieContainerResource myContainer = null;
boolean deployContainer = true;
KieContainerResourceList containers = kieServicesClient.listContainers().getResult();
// check if the container is not yet deployed, if not deploy it
if (containers != null) {
for (KieContainerResource kieContainerResource : containers.getContainers()) {
if (kieContainerResource.getContainerId().equals(containerId)) {
System.out.println("\t######### Found container " + containerId + " skipping deployment...");
deployContainer = false;
myContainer = kieContainerResource;
break;
}
}
}
// deploy container if not there yet
if (deployContainer) {
System.out.println("\n\n\n######### Deploying container " + containerId);
KieContainerResource resource = new KieContainerResource(containerId, new ReleaseId("demo", "firstattempt", "1.0.8"));
kieServicesClient.createContainer(containerId, resource);
myContainer = resource;
}
List<GenericCommand<?>> cmds = new ArrayList<GenericCommand<?>>();
BatchExecutionCommandImpl executionCommand = new BatchExecutionCommandImpl(cmds);
executionCommand.setLookup("defaultKieSession");
InsertObjectCommand insertObjectCommand1 = new InsertObjectCommand();
insertObjectCommand1.setOutIdentifier("marius");
insertObjectCommand1.setReturnObject(true);
Person p = new Person("Marius",20,"invalid");
insertObjectCommand1.setObject(p);
FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand();
cmds.add(insertObjectCommand1);
cmds.add(fireAllRulesCommand);
RuleServicesClient ruleClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
ServiceResponse<String> response = ruleClient.executeCommands(containerId, executionCommand);
System.out.println("\n\n\n######### Rules executed");
System.out.println("\n\n " + response.getResult());
}
}
package demo.firstattempt;
/**
* This class was automatically generated by the data modeler tool.
*/
public class Person implements java.io.Serializable
{
static final long serialVersionUID = 1L;
@org.kie.api.definition.type.Label("Name")
private java.lang.String name;
@org.kie.api.definition.type.Label("Age")
private int age;
@org.kie.api.definition.type.Label(value = "Over 18")
private java.lang.String valid;
public Person()
{
}
public java.lang.String getName()
{
return this.name;
}
public void setName(java.lang.String name)
{
this.name = name;
}
public int getAge()
{
return this.age;
}
public void setAge(int age)
{
this.age = age;
}
public java.lang.String getValid()
{
return this.valid;
}
public void setValid(java.lang.String valid)
{
this.valid = valid;
}
public Person(java.lang.String name, int age, java.lang.String valid)
{
this.name = name;
this.age = age;
this.valid = valid;
}
}package demo.firstattempt;
import java.lang.Number;
rule "Valid age"
dialect "mvel"
when
person : Person( age >= 18 )
then
person.setValid( "true" );
end
Thank you!
--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/bda95751-62c5-4975-93f4-336a3bf38d90%40googlegroups.com.
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
configuration.setMarshallingFormat(MarshallingFormat.JSON);
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Person.class);
configuration.addJaxbClasses(classes);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
ServiceResponse<ExecutionResults> response = ruleClient.executeCommandsWithResults(containerId, executionCommand);- first you create marshaller:
https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-server-parent/kie-server-tests/kie-server-integ-tests-drools/src/test/java/org/kie/server/integrationtests/drools/StatefulSessionUsageIntegrationTest.java#L97
- next you take the data and unmarshal it:
https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-server-parent/kie-server-tests/kie-server-integ-tests-drools/src/test/java/org/kie/server/integrationtests/drools/StatefulSessionUsageIntegrationTest.java#L110-L111
Thank you. It worked after adding the class to the configuration!
KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration(serverUrl, user, password);
configuration.setMarshallingFormat(MarshallingFormat.JSON);
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Person.class);
configuration.addJaxbClasses(classes);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(configuration);
Is there an example on how to extract the result from the response? I tried withServiceResponse<ExecutionResults> response = ruleClient.executeCommandsWithResults(containerId, executionCommand);
but the "executeCommandsWithResults(string,commands)" it's not recognized (undefined for type RuleServicesClient)
I saw you posted this on another thread pointing to the links below, but that repository was updated one day after you provided the links:- first you create marshaller:
https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-server-parent/kie-server-tests/kie-server-integ-tests-drools/src/test/java/org/kie/server/integrationtests/drools/StatefulSessionUsageIntegrationTest.java#L97
- next you take the data and unmarshal it:
https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-server-parent/kie-server-tests/kie-server-integ-tests-drools/src/test/java/org/kie/server/integrationtests/drools/StatefulSessionUsageIntegrationTest.java#L110-L111
I have to update my kie.server.client jar? Or what should I do ?
--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/ec7986e2-bddd-43d5-bf61-a3913623dcc7%40googlegroups.com.
Marshaller marshaller = MarshallerFactory.getMarshaller(classes, configuration.getMarshallingFormat(), Person.class.getClassLoader());
ExecutionResults actualData = marshaller.unmarshall(response.getResult(), ExecutionResultImpl.class);
Person responsePerson = (Person) actualData.getValue("Marius");
System.out.println(responsePerson.getName()+" "+responsePerson.getAge()+" "+responsePerson.getValid());