You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/11/02 23:50:34 UTC

svn commit: r470579 - in /incubator/servicemix/trunk/servicemix-drools/src: main/java/org/apache/servicemix/drools/model/ test/java/org/apache/servicemix/drools/ test/java/org/apache/servicemix/drools/fibonacci/ test/resources/

Author: gnodet
Date: Thu Nov  2 14:50:33 2006
New Revision: 470579

URL: http://svn.apache.org/viewvc?view=rev&rev=470579
Log:
Show servicemix-drools used as a service (calculate fibonacci suite)

Added:
    incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/
    incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Fibonacci.java
    incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Request.java
    incubator/servicemix/trunk/servicemix-drools/src/test/resources/fibonacci.drl
    incubator/servicemix/trunk/servicemix-drools/src/test/resources/log4j.properties
Modified:
    incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/JbiHelper.java
    incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/Message.java
    incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/DroolsComponentTest.java

Modified: incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/JbiHelper.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/JbiHelper.java?view=diff&rev=470579&r1=470578&r2=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/JbiHelper.java (original)
+++ incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/JbiHelper.java Thu Nov  2 14:50:33 2006
@@ -25,6 +25,8 @@
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.servicemix.client.ServiceMixClient;
 import org.apache.servicemix.client.ServiceMixClientFacade;
 import org.apache.servicemix.drools.DroolsEndpoint;
@@ -40,7 +42,7 @@
  * @version $Revision: 426415 $
  */
 public class JbiHelper {
-    
+
     private DroolsEndpoint endpoint;
     private Exchange exchange;
     private WorkingMemory memory;
@@ -58,15 +60,15 @@
     public DroolsEndpoint getEndpoint() {
         return endpoint;
     }
-    
+
     public ComponentContext getContext() {
         return endpoint.getContext();
     }
-    
+
     public DeliveryChannel getChannel() throws MessagingException {
         return getContext().getDeliveryChannel();
     }
-    
+
     public ServiceMixClient getClient() {
         return new ServiceMixClientFacade(getContext());
     }
@@ -75,6 +77,10 @@
         return exchange;
     }
 
+    public Log getLogger() {
+        return LogFactory.getLog(memory.getRuleBase().getPackages()[0].getName());
+    }
+
     /**
      * Forwards the inbound message to the given
      *
@@ -118,7 +124,7 @@
         }
         update();
     }
-    
+
     public void fault(String content) throws Exception {
         MessageExchange exchange = this.exchange.getInternalExchange();
         if (exchange instanceof InOnly) {
@@ -132,8 +138,18 @@
         }
         update();
     }
-    
+
+    public void answer(String content) throws Exception {
+        MessageExchange exchange = this.exchange.getInternalExchange();
+        NormalizedMessage out = exchange.createMessage();
+        out.setContent(new StringSource(content));
+        exchange.setMessage(out, "out");
+        getChannel().sendSync(exchange);
+        update();
+    }
+
     protected void update() {
         this.memory.modifyObject(this.exchangeFactHandle, this.exchange);
     }
+
 }

Modified: incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/Message.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/Message.java?view=diff&rev=470579&r1=470578&r2=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/Message.java (original)
+++ incubator/servicemix/trunk/servicemix-drools/src/main/java/org/apache/servicemix/drools/model/Message.java Thu Nov  2 14:50:33 2006
@@ -22,6 +22,7 @@
 
 import org.apache.servicemix.expression.JAXPBooleanXPathExpression;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.w3c.dom.Element;
 
 public class Message {
 
@@ -60,6 +61,10 @@
     
     public void setProperty(String name, Object value) {
         message.setProperty(name, value);
+    }
+    
+    public Element getContent() {
+        return (Element) ((DOMSource) message.getContent()).getNode();
     }
     
 }

Modified: incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/DroolsComponentTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/DroolsComponentTest.java?view=diff&rev=470579&r1=470578&r2=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/DroolsComponentTest.java (original)
+++ incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/DroolsComponentTest.java Thu Nov  2 14:50:33 2006
@@ -99,6 +99,8 @@
         r1.getMessageList().assertMessagesReceived(1);
         r2.getMessageList().assertMessagesReceived(1);
         r3.getMessageList().assertMessagesReceived(1);
+        
+        Thread.sleep(50);
     }
     
     public void testRouteInOut() throws Exception {
@@ -159,6 +161,29 @@
         client.sendSync(me);
         assertEquals(ExchangeStatus.ERROR, me.getStatus());
         
+        Thread.sleep(50);
+    }
+
+    public void testFibonacci() throws Exception {
+        drools = new DroolsComponent();
+        DroolsEndpoint endpoint = new DroolsEndpoint(drools.getServiceUnit(),
+                                                     new QName("drools"), "endpoint");
+        endpoint.setRuleBaseResource(new ClassPathResource("fibonacci.drl"));
+        drools.setEndpoints(new DroolsEndpoint[] { endpoint });
+        jbi.activateComponent(drools, "servicemix-drools");
+        
+        jbi.start();
+        
+        InOut me = client.createInOutExchange();
+        me.setService(new QName("drools"));
+        me.getInMessage().setContent(new StringSource("<fibonacci>50</fibonacci>"));
+        me.getInMessage().setProperty("prop", Boolean.TRUE);
+        client.sendSync(me);
+        Element e = new SourceTransformer().toDOMElement(me.getOutMessage());
+        assertEquals("result", e.getLocalName());
+        assertEquals("12586269025", e.getTextContent());
+        client.done(me);
+        
+        Thread.sleep(50);
     }
-    
 }

Added: incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Fibonacci.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Fibonacci.java?view=auto&rev=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Fibonacci.java (added)
+++ incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Fibonacci.java Thu Nov  2 14:50:33 2006
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.drools.fibonacci;
+
+public class Fibonacci {
+
+    private int sequence;
+
+    private long value;
+
+    public Fibonacci(final int sequence) {
+        this.sequence = sequence;
+        this.value = -1;
+    }
+
+    public int getSequence() {
+        return this.sequence;
+    }
+
+    public void setValue(final long value) {
+        this.value = value;
+    }
+
+    public long getValue() {
+        return this.value;
+    }
+
+    public String toString() {
+        return "Fibonacci(" + this.sequence + "/" + this.value + ")";
+    }
+
+}

Added: incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Request.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Request.java?view=auto&rev=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Request.java (added)
+++ incubator/servicemix/trunk/servicemix-drools/src/test/java/org/apache/servicemix/drools/fibonacci/Request.java Thu Nov  2 14:50:33 2006
@@ -0,0 +1,18 @@
+package org.apache.servicemix.drools.fibonacci;
+
+public class Request {
+
+    private final int value;
+    
+    public Request(int value) {
+        this.value = value;
+    }
+
+    /**
+     * @return the value
+     */
+    public int getValue() {
+        return value;
+    }
+    
+}

Added: incubator/servicemix/trunk/servicemix-drools/src/test/resources/fibonacci.drl
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/test/resources/fibonacci.drl?view=auto&rev=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/test/resources/fibonacci.drl (added)
+++ incubator/servicemix/trunk/servicemix-drools/src/test/resources/fibonacci.drl Thu Nov  2 14:50:33 2006
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.drools
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.drools.model.Exchange;
+import org.apache.servicemix.drools.fibonacci.Fibonacci;
+import org.apache.servicemix.drools.fibonacci.Request;
+
+global org.apache.servicemix.drools.model.JbiHelper jbi;
+
+rule Init
+	when
+		$me : Exchange()
+		not Fibonacci()
+	then
+		String t = $me.getIn().getContent().getTextContent();
+		int v = Integer.parseInt( t );
+		jbi.getLogger().debug("request value: " + v);
+		assert( new Request( v ) );
+		assert( new Fibonacci( v ) );
+end
+
+rule Answer
+	when
+		Request( $r : value )
+		Fibonacci( $s: sequence == $r, $v: value != -1  )
+	then
+		String t = "<result>" + $v + "</result>";
+		jbi.getLogger().debug( "answering: " + t );
+		jbi.answer( t );
+end
+
+rule Recurse
+	salience 10
+	when
+		f : Fibonacci ( value == -1 )
+	then
+		assert( new Fibonacci( f.getSequence() - 1 ) );
+		jbi.getLogger().debug( "recurse for " + f.getSequence() );
+end
+
+rule Bootstrap1
+	salience 20
+	when
+		f : Fibonacci( sequence == 1, value == -1 )
+	then 
+		f.setValue( 1 );		
+		modify( f );
+		jbi.getLogger().debug( f.getSequence() + " == " + f.getValue() );
+end
+
+rule Bootstrap2
+	when
+		f : Fibonacci( sequence == 2, value == -1 )
+	then 
+		f.setValue( 1 );		
+		modify( f );
+		jbi.getLogger().debug( f.getSequence() + " == " + f.getValue() );		
+end
+
+rule Calculate
+	when
+		f1 : Fibonacci( s1 : sequence, value != -1 )
+		f2 : Fibonacci( s2 : sequence == (new Integer( s1.intValue() + 1  ) ), value != -1 )
+ 		f3 : Fibonacci( sequence == (new Integer( s2.intValue() + 1 ) ), value == -1 )				
+	then	
+		f3.setValue( f1.getValue() + f2.getValue() );
+		modify( f3 );
+		retract( f1 );
+		jbi.getLogger().debug( f3.getSequence() + " == " + f3.getValue() );
+end	

Added: incubator/servicemix/trunk/servicemix-drools/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-drools/src/test/resources/log4j.properties?view=auto&rev=470579
==============================================================================
--- incubator/servicemix/trunk/servicemix-drools/src/test/resources/log4j.properties (added)
+++ incubator/servicemix/trunk/servicemix-drools/src/test/resources/log4j.properties Thu Nov  2 14:50:33 2006
@@ -0,0 +1,41 @@
+# 
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed  under the  License is distributed on an "AS IS" BASIS,
+# WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+# implied.
+#  
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+#
+# The logging properties used during tests..
+#
+log4j.rootLogger=INFO, stdout
+
+log4j.logger.org.springframework=INFO
+log4j.logger.org.apache.activemq=INFO
+log4j.logger.org.apache.activemq.spring=WARN
+log4j.logger.org.apache.servicemix=DEBUG
+
+# CONSOLE appender 
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+# File appender
+log4j.appender.out=org.apache.log4j.FileAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.out.file=target/servicemix-test.log
+log4j.appender.out.append=true