Lazy loading and execution of drools

Source: Internet
Author: User
Tags stringbuffer

Drools a lot of information on the Internet, here for their contact understanding and practical application of the following summary,

Engineering introduced.
Engineering Pom.xml, at least join

<dependency>
	<groupId>org.kie</groupId>
	<artifactId>kie-api</artifactId>
	<version>6.5.0.Final</version>
</dependency>
<dependency>
	<groupid >org.drools</groupId>
	<artifactId>drools-compiler</artifactId>
	<version> 6.5.0.final</version>
</dependency>

From the official website of the package, for example: Drools-distribution-6.5.0.final, from the binaries directory, the general need to manually introduce Third-party packages:
Ant-1.8.3.jar
Core-3.1.1.jar
Ecj-4.4.2.jar
......
Forget what else, these are used in the runtime, so in the actual use, can be based on the error information (java.lang.ClassNotFoundException), back to push the need for the jar, anyway, what is needed in the binaries directory. Simple encapsulation.

Package com.pomelo.drools.util;
Import java.util.ArrayList;

Import java.util.List;
Import org.kie.api.KieServices;
Import Org.kie.api.builder.KieBuilder;
Import Org.kie.api.builder.KieFileSystem;
Import Org.kie.api.builder.KieRepository;
Import Org.kie.api.builder.Message;
Import Org.kie.api.runtime.KieContainer;
Import org.kie.api.runtime.KieSession;
Import Org.kie.api.runtime.rule.AgendaFilter;

Import Org.kie.api.runtime.rule.Match;

Import COM.POMELO.DROOLS.MODEL.RULECFG;
	 /** * @author ZXZ * */public class Mydrools {/** * for the GKS, GKR, gkfs the concept of these three ambiguous, probably an application to build a set of OK.
	* Private static final kieservices GKS = KieServices.Factory.get ();
	private static final Kierepository GKR = Gks.getrepository ();
	private static final Kiefilesystem Gkfs = Gks.newkiefilesystem ();
	 /** * for local application tag which rule file is loaded.
	* private static list<string> Gkfsfacade = new arraylist<string> ();
	 /** * is used to hold containers for the loaded rules, and each load rule needs to be regenerated.
	* private static Kiecontainer KC; /** * Rule configuration, generally exist in the database, here with static statement blockClass
	
	* * private static rulecfg rulecfg;
		static {StringBuffer SB = new StringBuffer ("Package com.pomelo.drools.drl;\n");
		Sb.append ("Import com.pomelo.drools.model.users\n");
		Sb.append ("Rule \" userstatuschangerule\ "\ n");
		Sb.append ("when\n");
		Sb.append ("users:users (status = = 0) \ n");
		Sb.append ("then\n");
		Sb.append ("users.status = 1;\n");
		Sb.append ("System.out.println" ("done\"); \ n ");

		Sb.append ("end;\n");
		Rulecfg = new Rulecfg ();
		Rulecfg.setruleid (2000001);
		Rulecfg.setclassify ("User_status_change");
		Rulecfg.setrulename ("Userstatuschangerule");
		Rulecfg.setrule (Sb.tostring ());
	Rulecfg.setstatus (0);
	 /** * RuleId: Rule encoding, for example: 2000001.
	 * Params: Parameters required for rule judgment. /public static void Invokeins (final int ruleid, Object ... params) {/** * loadcfg: The query Rule object is encoded according to the rule, the actual application is query database, here simplifies not doing
		 Description
		* * Rulecfg rulecfg = loadcfg (RuleId);
		 /** * Generate rules file storage path, here can roughly speculate drool part of the core content, is to write the rules in DRL file, drools build a file system inside themselves.
		
		*/String Drlpath = Gendrlpath (rulecfg); if (ruleCfg.getstatus () = = Rulecfg.status_init) gkfsfacade.remove (Drlpath); if (!gkfsfacade.contains (Drlpath)) {synchronized (Gkfsfacade) {if (!gkfsfacade.contains (Drlpath)) {Gkfs.write (
					Drlpath, Rulecfg.getrule ());
					Kiebuilder KB = Gks.newkiebuilder (GKFS);
					 /** * Similar to Java Engineering, the file system is compiled.
					* * Kb.buildall ();
						if (Kb.getresults (). Hasmessages (Message.Level.ERROR)) {System.out.println (Kb.getresults (). toString ());
						 /** * If the compilation fails, the current rule is removed from the file system and recompiled.
						* * Gkfs.delete (rulecfg.getclassify ());
						KB = Gks.newkiebuilder (GKFS);
						Kb.buildall ();
						KC = Gks.newkiecontainer (Gkr.getdefaultreleaseid ());
					throw new RuntimeException ("Build errors:\n" + kb.getresults (). toString ());
					KC = Gks.newkiecontainer (Gkr.getdefaultreleaseid ());
					Rulecfg.setstatus (rulecfg.status_loaded);
				Gkfsfacade.add (Drlpath);
	}} firetherule (Rulecfg.getrulename (), params); private static rulecfg loadcfg (final intRuleId) {return rulecfg; private static String Gendrlpath (Rulecfg rulecfg) {return "src/main/resources/drl/" + rulecfg.getclassify () + "/"
	+ rulecfg.getrulename () + ". DrL";
		} private static void Firetherule (String rulename, Object ... params) {kiesession KS = Kc.newkiesession ();
			try {for (int i = 0;i < params.length;i++) {Ks.insert (params[i]);
		} ks.fireallrules (Rulenameequalsagendafilter (rulename));
		finally {ks.dispose (); } private static Agendafilter Rulenameequalsagendafilter (final String rulename) {return new Agendafilter () {p
			Ublic Boolean Accept (Match parammatch) {return Parammatch.getrule (). GetName (). Equals (RuleName);
	}
		}; }
}
Complete engineering, see Https://github.com/Monicazxz/MyDrools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.