how to get inserted fact in java client

741 views
Skip to first unread message

caicongyang

unread,
Oct 25, 2016, 4:35:05 AM10/25/16
to Drools Usage
Hi maciej:
           My rule has two fact; how to get inserted fact in java client? follow is my rules files:
 
package caicongyang.test.student;

import caicongyang.test.student.Student;
import caicongyang.test.student.Location;


rule
"one"
no-loop
   
when
        s
:Student(name =="tom")
   
then
        insert
(new Location("newyork"));
end

I add GetObjectsCommand in  List<Command<?>> commands ,but how i get this insert fact in java client and get insert  fact list ;
  anyone can give me some demo ?

Maciej Swiderski

unread,
Oct 25, 2016, 5:32:22 AM10/25/16
to drools...@googlegroups.com
you need to provde more context information - how to you use it? Do you run in workbench or in kie server etc.

Maciej
--
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/4042c6ba-b170-430a-88ae-1b2c7cbe300e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

caicongyang

unread,
Oct 26, 2016, 12:46:04 AM10/26/16
to Drools Usage
Hi Maciej:
            I  hava two face , one of face from java input ,anthor one in rules new insert ,both of them need to retuan in java code ;and i use kie-server java client like below code : i want  get the location object from "out" OutIdentifier but its return all object contains last session fact;
 so I need clear old fact from last session in work memory;
           about
clear old fact from last session in work memory, I need to close  manually? but i not find related API
               
 private static String url = "http://192.168.0.100:8080/kie-server/services/rest/server";


    private static String username = "kieserver";
   
private static String password = "kieserver1!";
   
private static String container = "mary";
   
private static String kieSession = "kiesession";

   
public static void main(String[] args) {
       
Student student = new Student();
        student
.setName("tom");
       
KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(url, username, password);
        config
.setMarshallingFormat(MarshallingFormat.JSON);
        config
.setTimeout(30000L);

       
KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
       
RuleServicesClient rules = client.getServicesClient(RuleServicesClient.class);

//        boolean deployContainer = true;
//        KieContainerResourceList containers = client.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(container)) {
//                    System.out.println("\t######### Found container " + container + " skipping deployment...");
//                    deployContainer = false;
//                    break;
//                }
//            }
//        }
//
//
//        // deploy container if not there yet
//        if (deployContainer) {
//            System.out.println("\t######### Deploying container " + container);
//            KieContainerResource resource = new KieContainerResource(container,
//                    new ReleaseId("u51", "test", "0.0.1-SNAPSHOT"));
//            client.createContainer(container, resource);
//        }

        KieCommands cmdFactory = KieServices.Factory.get().getCommands();

       
List<Command<?>> commands = new LinkedList<Command<?>>();
       
GetObjectsCommand getObjectsCommand = new GetObjectsCommand();
        getObjectsCommand
.setOutIdentifier("out");

        commands
.add(cmdFactory.newInsert(student, "student"));
        commands
.add(getObjectsCommand);

        commands
.add(cmdFactory.newFireAllRules());

       
ServiceResponse<org.kie.api.runtime.ExecutionResults> response = rules.executeCommandsWithResults(container,
                cmdFactory
.newBatchExecution(commands, kieSession));
       
System.out.println(response.getMsg());
       
ExecutionResults result = response.getResult();
       
System.out.println(result.getFactHandle("out"));
       
Collection<String> identifiers = result.getIdentifiers();
       
for (String string : identifiers) {
           
//System.out.println(string);
        }
       
List<Location> results = new ArrayList<Location>();

       
ArrayList objects = (ArrayList) result.getValue("out");
       
System.out.println(objects.size()); // its will get all object Contains the last sessions object ;
        for (Object ob : objects) {
           
// System.out.println(ob);
            // Location lo = (Location)ob;
            // System.out.println(lo.getName());
        }
       
//System.out.println(objects);


        student = (Student) result.getValue("student"); // its get right
       
System.out.println(student.getAge());
       
System.out.println(student.getVersion());
   
}


        

在 2016年10月25日星期二 UTC+8下午5:32:22,Maciej Swiderski写道:

Maciej Swiderski

unread,
Oct 26, 2016, 2:53:01 AM10/26/16
to Drools Usage
seems like you use stateful session while you should most likely use stateless. Make sure that ksession is stateless in kmodule.xml of your project then it will dispose ksession after request so all facts will be disposed as well.

Maciej

To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

caicongyang

unread,
Oct 26, 2016, 4:40:25 AM10/26/16
to Drools Usage
Thanks Maciej:
           related rules  I insert Location in rule file :

insert(new Location("newyork"));

I get this fact from  "out" identifier below code;
but i get  Student not Location fact;

getObjectsCommand.setOutIdentifier("out");


在 2016年10月26日星期三 UTC+8下午2:53:01,Maciej Swiderski写道:

Maciej Swiderski

unread,
Oct 26, 2016, 4:49:12 AM10/26/16
to Drools Usage
please provide complete usage instead of single lines as that don't give complete view at what you've done

Maciej

To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

caicongyang

unread,
Oct 26, 2016, 6:04:12 AM10/26/16
to Drools Usage
Thank you very much Maciej:
      i want get Location fact and Student fact from "out" indentify use java client ;but i am fail

drl:

package caicongyang.test.student;

import caicongyang.test.student.Student;
import caicongyang.test.student.Location;


rule
"one"
no-loop
   
when
        s
:Student(name =="tom")
   
then
        insert
(new Location("newyork"));
end

java client:

       
ArrayList objects = (ArrayList) result.getValue("out");
       
System.out.println(objects.size());  
        for (Object ob : objects) {
           
 System.out.println(ob);

        }
        student = (Student) result.getValue("student"); // its get right
       
System.out.println(student.getAge());
       
System.out.println(student.getVersion());
   
}









在 2016年10月26日星期三 UTC+8下午4:49:12,Maciej Swiderski写道:

Ged Byrne

unread,
Oct 26, 2016, 1:45:29 PM10/26/16
to Drools Usage
Hi caicongyang,

You can consider using a query, which I wrote about in a response to the other thread.

Have you tried using the QueryService?


Regards, 


Ged

caicongyang

unread,
Oct 26, 2016, 10:25:30 PM10/26/16
to Drools Usage
Hi  Ged:
           Is there no other better way? In addition to query

在 2016年10月27日星期四 UTC+8上午1:45:29,Ged Byrne写道:

Ged Byrne

unread,
Oct 27, 2016, 3:11:47 PM10/27/16
to Drools Usage
Sorry, not that I know of.

Ged Byrne

unread,
Oct 27, 2016, 3:11:48 PM10/27/16
to Drools Usage
Reply all
Reply to author
Forward
0 new messages