[rules-users] Drools Guvnor API information?

Luciano A. Andrade andrade.luciano at gmail.com
Tue Feb 15 14:02:54 EST 2011


I did it! If any one find this used fool. To connect the drools server
with php, you need a php code like the following:
====================
<?php
$url = "http://192.168.56.101:8080/drools-server/kservice/kservice/rest/execute"
;
$post_data = file_get_contents("/home/luciano/drools-request2.xml");
$headers = array("Content-Type" => 'text/plain; charset=utf-8',
"Accept" => "text/plain; charset=utf-8");
$rta = http_post_data($url, $post_data,array('headers' => $headers ));
====================
The file "/home/luciano/drools-request2.xml" looks like this
====================
<?xml version="1.0" encoding="UTF-8"?>
<batch-execution lookup="ksession1">
  <insert out-identifier="Message1" return-object="true">
    <mortgages.Applicant>
      <name>john</name>
      <age>50</age>
      <approved>False</approved>
      <creditRating>OK</creditRating>
    </mortgages.Applicant>
  </insert>
  <insert out-identifier="Message2" return-object="true">
    <mortgages.LoanApplication>
      <amount>10000</amount>
    </mortgages.LoanApplication>
  </insert>
  <insert out-identifier="Message3" return-object="true">
    <mortgages.IncomeSource>
      <amount>1000</amount>
    </mortgages.IncomeSource>
  </insert>
  <fire-all-rules/>
</batch-execution>
===================
this will return something like
==========================
string(992) "HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build:
SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
Date: Tue, 15 Feb 2011 18:46:53 GMT
Content-Type: text/plain
Content-Length: 757

<?xml version='1.0' encoding='UTF-8'?><execution-results><result
identifier="Message2"><mortgages.LoanApplication><amount>10000</amount><approved>false</approved><explanation>Only
AA</explanation></mortgages.LoanApplication></result><result
identifier="Message1"><mortgages.Applicant><creditRating>OK</creditRating><approved>true</approved><name>john</name><age>50</age></mortgages.Applicant></result><result
identifier="Message3"><mortgages.IncomeSource><amount>1000</amount></mortgages.IncomeSource></result><fact-handle
identifier="Message2"
external-form="0:-1:23979927:1:3:null"/><fact-handle
identifier="Message1"
external-form="0:2:8614136:1:2:DEFAULT"/><fact-handle
identifier="Message3"
external-form="0:4:8366806:1:4:DEFAULT"/></execution-results>"
=========================
In the drools-server, i unzip the drools-5.1.1-server.war inside the
jboss-4.2.3.GA/server/default/deploy/drools-server.war/ directory. The
jboos server is from the drools-5.1.1-guvnor-standalone.zip, I
download the pkg file from the guvnor / KnowlegeBase / mortgages / URL
for package binary, download the file and placed in the
jboss-4.2.3.GA/server/default/deploy/drools-server.war/WEB-INF/classes
directory.
Modify the file
jboss-4.2.3.GA/server/default/deploy/drools-server.war/WEB-INF/classes/knowledge-services.xml
and change the line
<drools:resource  type="DRL" source="classpath:test.drl>
for
<drools:resource type="PKG" source="classpath:mortgages.pkg" />



2011/2/14 Luciano A. Andrade <andrade.luciano at gmail.com>:
> Great, but i like (need really) to triger the rules from a web
> service, since i am outside the JVM, i will use the web interface to
> manage the rules, at least at the beginning.
>
> 2011/2/14 Vikas Hazrati <vhazrati at inphina.com>:
>> Hey Luciano, Meanwhile i tries pumping in the rule with a webdav api
>> (Sardine in my case) and that seems to work and trigger ok for me. this is
>> how i did that
>> public class SardineTest {
>> public static void main(String[] args) throws SardineException {
>> Sardine sardine = SardineFactory.begin("admin", "admin");
>> List<DavResource> resources = sardine
>> .getResources("http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/webdav/packages");
>> for (DavResource res : resources) {
>> System.out.println(res); // calls the .toString() method.
>> }
>> addARule();
>> }
>> private static void addARule() throws SardineException {
>> Sardine sardine = SardineFactory.begin("username", "password");
>> String ruleDescription = "when Entity(entityName==\"Car1s\") then
>> System.out.println( \"*************rule executed\")";
>> byte[] data = ruleDescription.getBytes();
>> sardine.put("http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/webdav/packages/Analytics/SimpleDRLRule.drl",
>> data);
>> System.out.println("Rule added ...");
>> }
>> }
>>
>> once i did this, I could see the rule added as a technical rule on the
>> guvnor web app ... the and then triggered to rule with something like this
>>
>> public class GuvnorTest {
>> @Test
>> public void testDroolsWithGuvnor() throws Exception {
>> KnowledgeBase knowledgeBase = createKnowledgeBase();
>> StatefulKnowledgeSession session =
>> knowledgeBase.newStatefulKnowledgeSession();
>> try {
>> Entity entity = new Entity();
>> entity.setEntityName("Car1s");
>> session.insert(entity);
>> Assert.assertTrue(session.getFactCount() == 1);
>> System.out.println("And the count is  " + session.getFactCount());
>> session.fireAllRules();
>> System.out.println("And the count is  " + session.getFactCount());
>> Assert.assertEquals(1, session.getFactCount());
>> }
>> finally {
>> session.dispose();
>> }
>> }
>> private static KnowledgeBase createKnowledgeBase() {
>> KnowledgeAgentConfiguration kaconf =
>> KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
>> kaconf.setProperty( "drools.agent.scanDirectories", "false" );
>> KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test
>> agent", kaconf );
>> kagent.applyChangeSet(
>> ResourceFactory.newClassPathResource("opal-guvnor.xml"));
>> return kagent.getKnowledgeBase();
>> }
>> }
>> where opal-guvnor.xml is my change set file and goes like this
>> <change-set xmlns='http://drools.org/drools-5.0/change-set'
>>     xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
>>     xs:schemaLocation='http://drools.org/drools-5.0/change-set
>> http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd'
>>>
>>     <add>
>>          <resource
>> source='http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/Analytics/LATEST.drl'
>> type='DRL' basicAuthentication="enabled" username="admin" password="admin"/>
>>     </add>
>> </change-set>
>>
>> not sure if this is the right / best way but at least could add a rule to
>> the repository with this. Ideally,
>> 1) I would like to run a validate / verify to see that the rule has been
>> added correctly, I dont know the api to do that
>> 2) I would like to rebuild my package and/or create a new snapshot, dont
>> know how to do that either though the rule seems to execute without doing
>> that.
>> 3) Have not given any thoughts about security etc right now ...
>>
>> @Drools experts, your thoughts?
>> Regards | Vikas
>>
>> On Sat, Feb 12, 2011 at 12:20 AM, Luciano A. Andrade
>> <andrade.luciano at gmail.com> wrote:
>>>
>>> I think i am in the same situation, i following
>>>
>>> http://www.reverttoconsole.com/blog/java/accessing-drools-5-rules-via-a-restful-api-using-drools-server-and-groovy-restclient/
>>> it recomends
>>>
>>> http://www.amazon.com/JBoss-Drools-Business-Rules-Browne/dp/1847196063/ref=sr_1_1?ie=UTF8&s=books&qid=1259786026&sr=8-1
>>> but i found a similar example on the chapter 11 of
>>>
>>> http://www.amazon.com/Drools-JBoss-Rules-Developers-Guide/dp/1847195644/ref=pd_bxgy_b_text_b#reader_1847195644
>>>
>>> i didn't manage to run properly first and foremost since i didn't have
>>> the rules in place as the example requires, and since i have to run it
>>> from PHP, i am working on it, so if you make any progress please i am
>>> really interested.
>>>
>>> Also i don't really understand how it sopuse to run the rules from the
>>> webdav interface.
>>>
>>> 2011/2/11 Vikas Hazrati <vhazrati at inphina.com>:
>>> > Hi Luciano,
>>> > Believe me ;) I already did that before posting to the group but the
>>> > information that I found is skeletal or anywhere near it. Do we have the
>>> > api
>>> > documented somewhere?
>>> > Also i see that people on this group talk about the KnowledgeAgent and
>>> > KBuilder etc. Where is all that documented? I would like to use the api
>>> > to
>>> > pump rules into the govrnor from my flex UI. Any pointers to the api
>>> > which I
>>> > can explore to do that are appreciated.
>>> > On the IRC channel i met Rikkola who suggested to use the webdav, I
>>> > would
>>> > like to explore that but at the same time I would like to see if REST or
>>> > any
>>> > alternate api suits my need. Again, help appreciated :)
>>> > Regards | Vikas
>>> >
>>> > On Fri, Feb 11, 2011 at 8:01 PM, Luciano A. Andrade
>>> > <andrade.luciano at gmail.com> wrote:
>>> >>
>>> >> I think you are looking for the "Drools REST API", google it and you
>>> >> find some documentation.
>>> >>
>>> >> 2011/2/11 Vikas Hazrati <vhazrati at inphina.com>:
>>> >> >
>>> >> > Hi,
>>> >> >
>>> >> > I am looking at using Guvnor for our project where users would be
>>> >> > creating
>>> >> > rules using our UI. For this i need to add / modify rule or any asset
>>> >> > for
>>> >> > that matter using an api.
>>> >> >
>>> >> > Unfortunately, i could not find enough documentation to suggest the
>>> >> > best
>>> >> > way
>>> >> > to use the REST api, which I guess is provided by Guvnor. Could
>>> >> > someone
>>> >> > let
>>> >> > me know the location of where i can get some information for this
>>> >> > api. I
>>> >> > also see that the issue GUVNOR-1080
>>> >> > (https://issues.jboss.org/browse/GUVNOR-1080) is marked resolved so
>>> >> > the
>>> >> > api
>>> >> > should exist right? or that we cannot access it remotely until we
>>> >> > have
>>> >> > the
>>> >> > Atom Pub Interface done?
>>> >> >
>>> >> > Help appreciated .
>>> >> > --
>>> >> > View this message in context:
>>> >> >
>>> >> > http://drools-java-rules-engine.46999.n3.nabble.com/Drools-Guvnor-API-information-tp2471798p2471798.html
>>> >> > Sent from the Drools - User mailing list archive at Nabble.com.
>>> >> > _______________________________________________
>>> >> > rules-users mailing list
>>> >> > rules-users at lists.jboss.org
>>> >> > https://lists.jboss.org/mailman/listinfo/rules-users
>>> >> >
>>> >> _______________________________________________
>>> >> rules-users mailing list
>>> >> rules-users at lists.jboss.org
>>> >> https://lists.jboss.org/mailman/listinfo/rules-users
>>> >
>>> >
>>> >
>>> > --
>>> > Best Regards | Vikas
>>> >
>>> > Inphina Technologies
>>> > Co-Founder and Technical Architect
>>> > Mobile: +91-9899174194
>>> > Landline: +91-11-25126026
>>> > http://inphina.com
>>> > http://thoughts.inphina.com
>>> > http://twitter.com/inphina
>>> >
>>> >
>>> > _______________________________________________
>>> > rules-users mailing list
>>> > rules-users at lists.jboss.org
>>> > https://lists.jboss.org/mailman/listinfo/rules-users
>>> >
>>> >
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>
>> --
>> Best Regards | Vikas
>>
>> Inphina Technologies
>> Co-Founder and Technical Architect
>> Mobile: +91-9899174194
>> Landline: +91-11-25126026
>> http://inphina.com
>> http://thoughts.inphina.com
>> http://twitter.com/inphina
>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>




More information about the rules-users mailing list