Customizing the ruleset cache

Using the XU ruleset API, you can customize the ruleset cache by writing your own implementation class and modifying the XU deployment descriptor accordingly.

Procedure

To customize a ruleset cache named mypackage.MyRulesetCache:

  1. Implement the interface IlrRulesetCache.
    package ilog.rules.res.xu.ruleset.cache.sample;
    
    import java.util.*;
    import java.util.logging.*;
    
    import ilog.rules.res.xu.ruleset.*;
    
    public class RulesetCacheImpl implements IlrRulesetCache {
    	public static final int DEFAULT_MAXIMUM_SIZE = 2;
    
    	protected IlrRulesetUsageInformationMonitor monitor;
    	protected Logger logger;
    	protected ArrayList<IlrXURuleset> rulesets = new ArrayList<IlrXURuleset>();
    
    	/** Maximum number of rulesets that can be stored in the cache */
    	protected int maxSize = DEFAULT_MAXIMUM_SIZE;
    
    	public synchronized void addRuleset(IlrXURuleset ruleset) {
    		if (rulesets.size() == maxSize) {
    			logger.info("Max size reached, removing less used ruleset");
    			removeLessUsedRuleset();
    		}
    		rulesets.add(ruleset);
    	}
    
    
    	/**
    	 * Removes the least executed ruleset.
    	 */
    	protected void removeLessUsedRuleset() {
    		long minExecCount = Long.MAX_VALUE;
    		IlrXURuleset minExecRuleset = null;
    
    		for(IlrXURuleset ruleset: rulesets) {
    			IlrRulesetUsageInformation info
    				= monitor.getRulesetUsageInformation(ruleset.getCanonicalRulesetPath());
    			long execCounter = info.getExecutionCount();
    
    			if (execCounter <= minExecCount) {
    				minExecRuleset = ruleset;
    				minExecCount = execCounter;
    			}
    		}
    
    		if (minExecRuleset != null) {
    			logger.info("Remove ruleset: "+minExecRuleset.getCanonicalRulesetPath());
    			rulesets.remove(minExecRuleset);
    		}
    	}
    
    	public synchronized void clear() {
    		rulesets.clear();
    	}
    
    	public synchronized IlrXURuleset getDeprecatedRuleset(String canonicalRulesetPath,
    			ClassLoader xomClassLoader) {
    		return null;
    	}
    
    	public synchronized IlrXURuleset getRuleset(String canonicalRulesetPath,
    			ClassLoader xomClassLoader) {
    		for(IlrXURuleset ruleset: rulesets) {
    			if (ruleset.getCanonicalRulesetPath().equals(canonicalRulesetPath) &&
    					ruleset.getXOMClassLoader() == xomClassLoader) {
    				return ruleset;
    			}
    		}
    
    		return null;
    	}
    
    	public void initialize(Logger logger, Map<String, String> properties,
    			IlrRulesetUsageInformationMonitor monitor)
    			throws IlrRulesetCacheException {
    		this.monitor = monitor;
    		this.logger = logger;
    	}
    
    	public synchronized void rulesetChanged(String canonicalRulesetPath) {
    		Iterator<IlrXURuleset> iterator = rulesets.iterator();
    		while(iterator.hasNext()) {
    			IlrXURuleset ruleset = iterator.next();
    			if (ruleset.getCanonicalRulesetPath().equals(canonicalRulesetPath)) {
    				iterator.remove();
    			}
    		}
    	}
    
    }
     
  2. Add the implementation class to the XU deployment descriptor.
    <config-property>
    <config-property-name>rulesetCacheProperties<config-property-name> 
    <config-property-type>java.lang.String</config-property-type> 
    <config-property-value>ruleset.cache.class=mypackage.MyRulesetCache</config-property-value> 
    </config-property><config-property>
    
    <config-property-name>rulesetCacheProperties</config-property-name> 
    <config-property-type>java.lang.String<config-property-type> 
    <config-propertyvalue>ruleset.cache.class=mypackage.MyRulesetCache
    <mycustomprop=mycustomvalue/config-property-value>
    </config-property>
    
    <config-property>
    <config-property-name>rulesetUsageMonitorEnabled</config-property-name>
    <config-property-type>java.lang.Boolean</config-property-type> 
    <config-property-value>true/config-property-value>
    </config-property>
  3. Package the class.

    For a Java™ SE deployment, you must add the class to the same class path as the XU or its parent. In Java EE, you must package the class in the XU .rar file.