Package org.mule.module.rules

Source Code of org.mule.module.rules.RulesComponent

/*
* $Id: RulesComponent.java 17326 2010-05-19 20:08:47Z tcarlson $
* --------------------------------------------------------------------------------------
* Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/

package org.mule.module.rules;

import org.mule.SimpleMuleSession;
import org.mule.api.MuleEvent;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.component.AbstractComponent;

import org.drools.runtime.rule.FactHandle;

/**
* A service backed by a rules engine such as Drools.
*/
public class RulesComponent extends AbstractComponent
{
    private Rules rules;

    @Override
    protected void doInitialise() throws InitialisationException
    {
        rules.setMuleContext(muleContext);
        super.doInitialise();
    }

    @Override
    protected Object doInvoke(MuleEvent event) throws Exception
    {
        // Add this message to the CEP event stream.
        Object payload = event.getMessage().getPayload();
        logger.debug("Adding message to the knowledge base: " + payload);
       
        FactHandle handle = rules.getSession().getFactHandle(payload);
        if (handle != null)
        {
            rules.getEntryPoint().update(handle, payload);
        }
        else
        {
            rules.getEntryPoint().insert(payload);
        }

        // Update the MuleSession handle so that the rules can fire events.
        try
        {
            rules.getSession().setGlobal("mule", new SimpleMuleSession(event.getSession(), muleContext));
        }
        catch (Exception e)
        {
            logger.warn(e.getMessage());
        }
       
        rules.updateAgendaGroup();
        return rules.getSession().fireAllRules();
    }

    public Rules getRules()
    {
        return rules;
    }

    public void setRules(Rules rules)
    {
        this.rules = rules;
    }
}
TOP

Related Classes of org.mule.module.rules.RulesComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.