📄 rulesetcompiler.java
字号:
package org.drools.smf;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.jci.readers.MemoryResourceReader;
import org.apache.commons.jci.readers.ResourceReader;
import org.apache.commons.jci.stores.MemoryResourceStore;
import org.apache.commons.jci.stores.ResourceStore;
import org.apache.commons.jci.stores.ResourceStoreClassLoader;
import org.drools.IntegrationException;
import org.drools.rule.Rule;
import org.drools.rule.RuleSet;
import org.drools.spi.Condition;
import org.drools.spi.Functions;
public class RuleSetCompiler
{
private final RuleSet ruleSet;
private final String ruleSetValidFileName;
private final String packageName;
private final String knowledgeHelper;
private final MemoryResourceReader src;
private final MemoryResourceStore dst;
// private File srcJar;
// private File binJar;
public RuleSetCompiler(RuleSet ruleSet,
String packageName,
String knowledgeHelper) throws IntegrationException,
IOException
{
// srcJar = null;
// binJar = null;
this.ruleSet = ruleSet;
this.ruleSetValidFileName = this.ruleSet.getName().replaceAll( "(^[0-9]|[^\\w$])",
"_" ).toLowerCase();
this.packageName = packageName;
this.knowledgeHelper = knowledgeHelper;
this.src = new MemoryResourceReader();
this.dst = new MemoryResourceStore();
compile();
// //srcJar may be null so check before we do toURL
// URL srcJarUrl = null;
// if ( ( this.srcJar != null ) )
// {
// srcJarUrl = this.srcJar.toURL();
// }
//
//
// return new RuleSetPackage( ruleSet, this.binJar.toURL(), srcJarUrl );
}
public RuleSet getRuleSet()
{
return ruleSet;
}
public byte[] getSourceDeploymentJar() throws IOException
{
String[] files = this.src.list();
ByteArrayOutputStream output = new ByteArrayOutputStream();
Jarer jarer = new Jarer( output );
for ( int i = 0; i < files.length; i++ )
{
jarer.addCharArray( this.src.getContent( files[i] ),
files[i] );
}
jarer.close();
return output.toByteArray();
}
public byte[] getBinaryDeploymentJar() throws IOException
{
String[] files = this.dst.list();
ByteArrayOutputStream output = new ByteArrayOutputStream();
Jarer jarer = new Jarer( output );
for ( int i = 0; i < files.length; i++ )
{
jarer.addByteArray( this.dst.read( files[i] ),
files[i].replace('.', '/') + ".class" );
}
jarer.addObject( this.ruleSetValidFileName,
this.ruleSet );
Properties prop = new Properties();
prop.setProperty( "name",
this.ruleSetValidFileName );
ByteArrayOutputStream propStream = new ByteArrayOutputStream();
prop.store(propStream, null);
jarer.addByteArray(propStream.toByteArray(), "rule-set.conf");
jarer.close();
return output.toByteArray();
}
private void compile() throws IOException,
IntegrationException
{
Map functionMap = this.ruleSet.getFunctions();
Iterator it = functionMap.values().iterator();
SemanticFunctions functions = null;
SemanticFunctionsCompiler compiler = null;
String functionClassName = null;
String name = null;
String semanticPackageName = null;
ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
if ( parentClassLoader == null )
{
parentClassLoader = this.getClass().getClassLoader();
}
ResourceStoreClassLoader classLoader = new ResourceStoreClassLoader( parentClassLoader,
new ResourceStore[]{dst} );
Map parents = new HashMap();
Map files = new HashMap();
Object object = null;
while ( it.hasNext() )
{
object = it.next();
if ( !(object instanceof SemanticFunctions) )
{
continue;
}
functions = (SemanticFunctions) object;
name = functions.getName();
// Make a copy of the imports
// Cannot use the original as it will be updated by the compiler
Set imports = new HashSet();
imports.addAll( this.ruleSet.getImporter().getImports() );
compiler = functions.getSemanticFunctionsCompiler();
//Use the regex ruleset name + timestamp to hopefully create a unique namespace
//This is further backed by the derived class name from the rulename
semanticPackageName = this.packageName + "." + this.ruleSetValidFileName + "_" + System.currentTimeMillis() + "." + compiler.getSemanticType();
functionClassName = generateUniqueLegalName( semanticPackageName,
src,
name.toUpperCase().charAt( 0 ) + name.substring( 1 ),
"." + compiler.getSemanticType() );
compiler.generate( (Functions) functions,
imports,
semanticPackageName,
functionClassName,
null,
src,
files );
parents.put( compiler.getSemanticType(),
semanticPackageName + "." + functionClassName );
}
it = files.keySet().iterator();
object = null;
List list = null;
while ( it.hasNext() )
{
object = it.next();
if ( object instanceof SemanticFunctionsCompiler )
{
compiler = (SemanticFunctionsCompiler) object;
list = (List) files.get( compiler );
compiler.compile( (String[]) list.toArray( new String[list.size()] ),
src,
dst,
classLoader );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -