How to compile .drl file through ant build script construct

I am new to Drools. I want to know if a .drl file can be compiled with any command that can be entered at the windows command prompt (shell / cmd). I looked at the binaries that come with the drools distribution, but I cannot find a way to compile the .drl file. The reason I am interested in such a command is because I want to write an ant build file that will compile my java classes and rules and create a jar. This jar should be self-sufficient, i.e. Run the jar from the command line to start the main program, which transfers the facts in the session, as a result of which the rules that work with these facts are automatically executed.

+4
source share
3 answers

DroolsCompilerAntTask used for this. This will take all your rule files and compile them into a serialized file. There seem to be some bugs in 5.3, although I'm currently trying to do this. In the meantime, here is an illustrative build file that you can use to create an executable JAR based on Drools. The string will fail if the rules cannot be compiled.

 <project name="DroolsProto" default="dist" basedir="."> <property name="build.src" location="src"/> <property name="build.target" location="target"/> <property name="build.dist" location="dist"/> <property name="build.artifact" value="droolsproto"/> <property name="one-jar.dist.dir" value="~/Work/Data/Misc/OneJar"/> <property name="one-jar.version" value="0.97"/> <property name="one-jar.ant.jar" value="${one-jar.dist.dir}/one-jar-ant-task-${one-jar.version}.jar"/> <path id="build.lib.path"> <fileset dir="${build.src}/lib"> <include name="**/*.jar"/> </fileset> </path> <taskdef name="one-jar" classname="com.simontuffs.onejar.ant.OneJarTask" classpath="${one-jar.ant.jar}" onerror="report"/> <taskdef name="droolscompiler" classname="org.drools.contrib.DroolsCompilerAntTask"> <classpath refid="build.lib.path"/> </taskdef> <target name="clean"> <delete dir="${build.target}"/> <delete dir="${build.dist}"/> </target> <target name="init"> <tstamp/> <mkdir dir="${build.target}"/> <mkdir dir="${build.dist}"/> </target> <target name="compile" depends="init"> <mkdir dir="${build.target}/classes"/> <javac srcdir="${build.src}/main/java" destdir="${build.target}/classes"> <classpath refid="build.lib.path"/> <include name="**/*.java"/> <exclude name="**/*Test.java"/> </javac> </target> <target name="verify-rules"> <droolscompiler srcDir="${build.src}/main/resources" toFile="${build.target}/classes/foo.foo"> <classpath refid="build.lib.path"/> </droolscompiler> </target> <target name="verify-resources" depends="verify-rules"/> <target name="bundle-resources" depends="verify-resources"> <copy todir="${build.target}/classes"> <fileset dir="${build.src}/main/resources"/> </copy> </target> <target name="dist" depends="compile, bundle-resources"> <one-jar destfile="${build.dist}/${build.artifact}.jar"> <manifest> <attribute name="One-Jar-Main-Class" value="org.drools.examples.HelloWorldExample"/> </manifest> <main> <fileset dir="${build.target}/classes"/> </main> <lib> <fileset dir="${build.src}/lib"> <include name="**/*.jar"/> </fileset> </lib> </one-jar> </target> </project> 

Note that the assembly uses One-Jar to create a stand-alone executable, you can replace it with a Super Jar ™ '. There is also a DroolsVerifierAntTask that supposedly can check for logical errors in your rules (as opposed to syntax ones), but I have no experience with this.

+2
source

You can use something like this:

 private static void compile(final String srcFile, final String destFile) throws IOException { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); URL src = FormChecker.class.getResource(srcFile); Resource r = ResourceFactory.newInputStreamResource(src.openStream()); kbuilder.add(r, ResourceType.DRL); if (kbuilder.hasErrors()) { throw new IllegalStateException("Can not initialize Drools: " + kbuilder.getErrors().toString()); } Collection<KnowledgePackage> kpackages = kbuilder.getKnowledgePackages(); File dest = new File(destFile); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest)); out.writeObject(kpackages); out.close(); } 
+2
source

The droolsjbpm-tools zip file contains drools- ant .

+1
source

Source: https://habr.com/ru/post/1379445/


All Articles