Java调用ILOG两种方式备忘

package com.test;

import ilog.rules.archive.IlrJarArchiveLoader;
import ilog.rules.engine.IlrContext;
import ilog.rules.engine.IlrParameterMap;
import ilog.rules.engine.IlrRuleset;
import ilog.rules.engine.IlrRulesetArchiveParser;
import ilog.rules.teamserver.brm.IlrBaseline;
import ilog.rules.teamserver.brm.IlrRuleProject;
import ilog.rules.teamserver.client.IlrRemoteSessionFactory;
import ilog.rules.teamserver.model.IlrArchiveOutput;
import ilog.rules.teamserver.model.IlrDefaultSearchCriteria;
import ilog.rules.teamserver.model.IlrSession;
import ilog.rules.teamserver.model.IlrSessionFactory;
import ilog.rules.teamserver.model.IlrSessionHelper;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.util.jar.JarInputStream;

import com.demo.dto.Driver;
import com.demo.dto.Result;

public class InvokeRuleSet {
	//A remote implementation of a session factory.
	private static IlrSessionFactory factory = new IlrRemoteSessionFactory();

	private static String userName = "rtsAdmin";

	private static String passWord = "rtsAdmin";

	private static String url = "http://localhost:8080/teamserver";

	private static String dataSource = "jdbc/ilogDataSource";

	private static String projectName = "RuleDemo2";

	private static ThreadLocal<IlrContext> localEngine = new ThreadLocal<IlrContext>();

	public static void invokeIlrJar() throws Exception{
		// This class is a parser of ruleset archives. The archive is given as a stream. The parsing provides: 
		//	a ruleset. 
		//	a business reflect used in the case of a business ruleset archive. 
		//	an execution reflect, used to create the provided ruleset. 
		IlrRulesetArchiveParser parser = new IlrRulesetArchiveParser();
		//Creates an archive loader which relies on a jar stream.
		IlrJarArchiveLoader ruleArchvieLoader = new IlrJarArchiveLoader(
				new JarInputStream(new FileInputStream("/demo2.jar")));
		//Parses the ruleset archive.
		parser.parseArchive(ruleArchvieLoader);
		//Get a ruleset issued from a ruleset archive parsing.
		IlrRuleset rtsRuleSet = parser.getRuleset();
		//IlrContext is the base class of all the execution contexts. Rules can be executed only within an execution context. 
		//In ILOG JRules, the rule engine is an instance of IlrContext, the rule engine is simply a Java object.
		//An IlrContext instance is always attached to an IlrRuleset. If the context is created without a ruleset passed as an argument, it creates its own ruleset. 
		//An IlrContext instance contains all the methods required to control the rule engine. IlrRuleset is responsible for rule management, IlrContext is responsible for rule execution.
		IlrContext context = new IlrContext();
		context.setRuleset(rtsRuleSet);
		//Implements a structure for storing parameter values to set or get from ruleset variables. Each parameter is stored with its name and its value. 
		IlrParameterMap paramMap = new IlrParameterMap();
		Driver vhl = new Driver();
		vhl.setAge(6);
		vhl.setSex('1');
		//vhl.setNme("zs");
		//Store for the parameter "name" and its value "value".
		paramMap.setParameter("drv", vhl);
		//Sets the values of the declared ruleset variables contained in the passed IlrParameterMap (defined either with the "in" or "inout" modifier).
		context.setParameters(paramMap);
		//Executes the ruleflow defined in the context's ruleset.
		//Executes the task passed as the argument.
		//context.execute(taskName);
		context.execute();
		//Gets the value of the ruleset parameter.
		//Returns the values of the "out" ruleset variables (those defined either with the "inout" or "out" modifier).
		//IlrParameterMap rpm = context.getReturnValues();
		Result r = (Result) context.getParameterValue("res");
		System.err.println(r.getResult());
		//Disconnects all connected IlrTool.
		context.disconnectTools();
		//Called by Rule Studio to prepare a context for another execution.
		context.reset();
	}
	
	public static void ilrRemote() throws Exception {
		//Connects the given user to Rule Team Server.
		factory.connect(userName, passWord, url, dataSource);
		IlrSession session = factory.getSession();
		// get the project by name
		IlrRuleProject ruleProject = (IlrRuleProject) IlrSessionHelper
				.getProjectNamed(session, projectName);
		// open current baseline
		IlrBaseline currentBaseline = IlrSessionHelper.getCurrentBaseline(
				session, ruleProject);
		session.setWorkingBaseline(currentBaseline);
		//IlrDefaultSearchCriteria is the default implementation of IlrSearchCriteria. It is used to specify the search criteria passed to the findElements search methods of an IlrSession object. 
		IlrSearchCriteria criteria = new IlrDefaultSearchCriteria("查找 所有 规则");
		//Generates a ruleset archive for the given query and extractor validator symbol. 
		//If the preference ilog.rules.teamserver.buildCheckArchive is set to true, then the archive is checked just after being generated. 
		//When errors are found during the archive generation or the check: 
		//If the severity of the error is greater than or equal to the preference ilog.rules.teamserver.rulesetGenerationAbortLevel, an exception is thrown and the returned IlrArchiveOutput is null. 
		//Otherwise, the error is put in the error list of the archive output. 
		IlrArchiveOutput jarOutArray =  session.generateRulesetArchive(criteria,null,projectName);
		session.close();
		IlrRulesetArchiveParser parser = new IlrRulesetArchiveParser();
		IlrJarArchiveLoader ruleArchvieLoader = new IlrJarArchiveLoader(
				new JarInputStream(new ByteArrayInputStream(jarOutArray.getBytes())));
		parser.parseArchive(ruleArchvieLoader);
		IlrRuleset rtsRuleSet = parser.getRuleset();
		IlrContext context = localEngine.get();
		if (context == null) {
			context = new IlrContext();
			context.setRuleset(rtsRuleSet);
			localEngine.set(context);
		}
		IlrParameterMap paramMap = new IlrParameterMap();
		Driver vhl = new Driver();
		vhl.setAge(6);
		vhl.setSex('1');
		paramMap.setParameter("drv", vhl);
		context.setParameters(paramMap);
		context.execute();
		Result r = (Result) context.getParameterValue("res");
		System.err.println(r.getResult());
		context.disconnectTools();
		context.reset();
	}	

	public static void main(String[] args) throws Exception {
		ilrRemote();
	}
}
jrules-engine.jar
jrules-language.jar
jrules-res-execution.jar
jrules-res-session-ejb3-WAS7_stub.jar
jrules-res-session-ejb3-WASCE21.jar
jrules-ruleartifacts.jar
jrules-teamserver.jar

猜你喜欢

转载自ninnd.iteye.com/blog/1331253