/*
* $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;
}
}