Search in sources :

Example 1 with CompiledNetwork

use of org.drools.core.reteoo.compiled.CompiledNetwork in project drools by kiegroup.

the class ObjectTypeNodeCompiler method compile.

/**
 * Creates a {@link CompiledNetwork} for the specified {@link ObjectTypeNode}. The {@link PackageBuilder} is used
 * to compile the generated source and load the class.
 *
 * @param kBuilder     builder used to compile and load class
 * @param objectTypeNode OTN we are generating a compiled network for
 * @return CompiledNetwork
 */
public static CompiledNetwork compile(KnowledgeBuilderImpl kBuilder, ObjectTypeNode objectTypeNode) {
    if (objectTypeNode == null) {
        throw new IllegalArgumentException("ObjectTypeNode cannot be null!");
    }
    if (kBuilder == null) {
        throw new IllegalArgumentException("PackageBuilder cannot be null!");
    }
    ObjectTypeNodeCompiler compiler = new ObjectTypeNodeCompiler(objectTypeNode);
    String packageName = compiler.getPackageName();
    PackageRegistry pkgReg = kBuilder.getPackageRegistry(packageName);
    if (pkgReg == null) {
        kBuilder.addPackage(new PackageDescr(packageName));
        pkgReg = kBuilder.getPackageRegistry(packageName);
    }
    String source = compiler.generateSource();
    String generatedSourceName = compiler.getName();
    JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
    dialect.addSrc(compiler.getBinaryName(), source.getBytes(IoUtils.UTF8_CHARSET));
    kBuilder.compileAll();
    kBuilder.updateResults();
    CompiledNetwork network;
    try {
        network = (CompiledNetwork) Class.forName(generatedSourceName, true, kBuilder.getRootClassLoader()).newInstance();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    } catch (InstantiationException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    }
    return network;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompiledNetwork(org.drools.core.reteoo.compiled.CompiledNetwork) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect)

Aggregations

PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)1 CompiledNetwork (org.drools.core.reteoo.compiled.CompiledNetwork)1