/*
* 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;
global java.lang.Integer max;
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);
if (v > max.intValue()) {
jbi.getLogger().debug("Value too high");
jbi.fault( "" );
} else {
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 = "" + $v + "";
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