📄 rulesetcompiler.java
字号:
Rule[] rules = this.ruleSet.getRules();
//Use the regex ruleset name + timestamp to hopefully create a unique namespace
//This is further backed by the derived class name from the rulename
String rulePackageName = this.packageName + "." + this.ruleSetValidFileName + "_" + System.currentTimeMillis();
// use a HashMap to map the rules to their new class names, used for wiring
Map ruleNameMap = new HashMap();
for ( int i = 0; i < rules.length; i++ )
{
compileRule( rules[i],
rulePackageName,
parents,
ruleNameMap,
this.knowledgeHelper,
src,
dst,
classLoader );
}
// File conf = new File( this.temp,
// "rule-set.conf" );
//
// Properties prop = new Properties();
// prop.setProperty( "name",
// this.ruleSetName );
// FileOutputStream fos = new FileOutputStream( conf );
// prop.store( fos ,
// null );
// fos.close();
setInvokers( this.ruleSet,
rulePackageName,
ruleNameMap,
classLoader );
// createBinJar();
// createSrcJar();
// conf.delete();
}
private static void compileRule(Rule rule,
String packageName,
Map parents,
Map ruleMap,
String knowledgeHelper,
ResourceReader src,
ResourceStore dst,
ClassLoader classLoader) throws IOException
{
RuleCompiler compiler = RuleCompiler.getInstance();
compiler.compile( rule,
packageName,
parents,
ruleMap,
knowledgeHelper,
src,
dst,
classLoader );
}
private void setInvokers(RuleSet ruleSet,
String packageName,
Map ruleMap,
ClassLoader classLoader) throws IntegrationException
{
Rule[] rules = ruleSet.getRules();
Rule rule = null;
SemanticInvokeable component = null;
String name = null;
String semanticPackageName = null;
try
{
for ( int i = 0; i < rules.length; i++ )
{
rule = rules[i];
Condition[] conditions = (Condition[]) rule.getConditions().toArray( new Condition[rule.getConditions().size()] );
for ( int j = 0; j < conditions.length; j++ )
{
//only wire up this condition if it implements SemanticInvokeble
if ( !(conditions[j] instanceof SemanticInvokeable) )
{
continue;
}
component = (SemanticInvokeable) conditions[j];
name = component.getName();
semanticPackageName = packageName + "." + component.getSemanticType();
component.setInvoker( (Invoker) classLoader.loadClass( semanticPackageName + "." + ruleMap.get( rule ) + "Invoker$" + name.toUpperCase().charAt( 0 ) + name.substring( 1 ) + "Invoker" ).newInstance() );
}
//only wire up this consequenceif it implements SemanticInvokeble
if ( rule.getConsequence() instanceof SemanticInvokeable )
{
component = (SemanticInvokeable) rule.getConsequence();
name = component.getName();
semanticPackageName = packageName + "." + component.getSemanticType();
component.setInvoker( (Invoker) classLoader.loadClass( semanticPackageName + "." + ruleMap.get( rule ) + "Invoker$" + name.toUpperCase().charAt( 0 ) + name.substring( 1 ) + "Invoker" ).newInstance() );
}
}
}
catch ( InstantiationException e )
{
throw new IntegrationException( "Unable to bind RuleSet '" + ruleSet.getName() + "' component to Class Method: " + e.getMessage(),
e );
}
catch ( IllegalAccessException e )
{
throw new IntegrationException( "Unable to bind RuleSet '" + ruleSet.getName() + "' component to Class Method: " + e.getMessage(),
e );
}
catch ( ClassNotFoundException e )
{
throw new IntegrationException( "Unable to bind RuleSet '" + ruleSet.getName() + "' component to Class Method: " + e.getMessage(),
e );
}
}
// private void createBinJar() throws FileNotFoundException,
// IOException
// {
// String jarName = this.ruleSetName + ".jar";
// this.binJar = new File( this.temp,
// jarName );
// Jarer jarer = new Jarer( this.binJar );
//
// jarer.addDirectory( this.dst );
// jarer.addObject( this.ruleSetName,
// this.ruleSet );
//
// File conf = new File( this.temp,
// "rule-set.conf" );
//
// jarer.addFile( conf,
// "rule-set.conf" );
//
// jarer.close();
// conf.delete();
// this.binJar.deleteOnExit();
//
// }
//
// /**
// * Only create a src jar is the src directory has entries
// *
// * @throws FileNotFoundException
// * @throws IOException
// */
// private void createSrcJar() throws FileNotFoundException,
// IOException
// {
// // Only create a src jar if the src directory has entries
// if ( this.src.list().length != 0 )
// {
// String jarName = this.ruleSet.getName().replaceAll( "(^[0-9]|[^\\w$])",
// "_" ) + "-src.jar";
// this.srcJar = new File( this.temp,
// jarName );
// Jarer jarer = new Jarer( this.srcJar );
// jarer.addDirectory( this.src );
// jarer.close();
// this.srcJar.deleteOnExit();
// }
// }
/**
* Takes a given name and makes sure that its legal and doesn't already exist. If the file exists it increases counter appender untill it is unique.
*
* @param packageName
* @param name
* @param ext
* @return
*/
private String generateUniqueLegalName(String packageName,
ResourceReader src,
String name,
String ext)
{
// replaces the first char if its a number and after that all non
// alphanumeric or $ chars with _
String newName = name.replaceAll( "(^[0-9]|[^\\w$])",
"_" );
// make sure the class name does not exist, if it does increase the counter
int counter = -1;
boolean exists = true;
while ( exists )
{
counter++;
String fileName = packageName.replaceAll( "\\.",
"/" ) + newName + "_" + counter + ext;
exists = src.isAvailable( fileName );
}
// we have duplicate file names so append counter
if ( counter >= 0 )
{
newName = newName + "_" + counter;
}
return newName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -