/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed 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.drools.examples.broker.rules

import org.drools.examples.broker.model.SuddenDropEvent
import org.drools.examples.broker.model.PortfolioAction
import org.drools.examples.broker.model.Action
import java.util.Map
import java.util.HashMap

import function org.drools.examples.broker.misc.BrokerUtils.selectAction

dialect "mvel"

// an event declaration to represent situations
// where a sudden drop happens. In this case we are not
// defining any explicit expiration policy.
declare SuddenDropEvent
    @role( event )
end

// here we have an example of a rule that controls a process
rule "Start adjust position process"
when
    $sde : SuddenDropEvent( ) from entry-point "Analysis Events"
then
    variables = [ "symbol" : $sde.symbol ];
    drools.getKnowledgeRuntime().startProcess( "adjust position", variables );
end


// below we have rules controlled by the process,
// i.e., the process will fire these rules when necessary
// to re-evaluate the position
rule "If the drop is between 6% and 8%, buy more shares"
    ruleflow-group "evaluate position"
when
    $sde : SuddenDropEvent( percent >= -0.08 && < -0.06 ) from entry-point "Analysis Events"
then
    with( pa = new PortfolioAction() ) {
        action = Action.BUY,
        symbol = $sde.symbol,
        quant = 100
    }
    insert( pa );
end    

rule "If the drop is on more than 8%, sell shares"
    ruleflow-group "evaluate position"
when
    $sde : SuddenDropEvent( percent < -0.08 ) from entry-point "Analysis Events"
then
    with( pa = new PortfolioAction() ) {
        action = Action.SELL,
        symbol = $sde.symbol,
        quant = 100
    }
    insert( pa );
end    


rule "If the drop is between 5% and 6%, do nothing"
    ruleflow-group "evaluate position"
when
    $sde : SuddenDropEvent( percent >= -0.06 ) from entry-point "Analysis Events"
then
    with( pa = new PortfolioAction() ) {
        action = Action.NOACTION,
        symbol = $sde.symbol
    }
    insert( pa );
end    

