Search in sources :

Example 6 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method dslrReaderToPackageDescr.

private PackageDescr dslrReaderToPackageDescr(Resource resource, Reader dslrReader) throws DroolsParserException {
    boolean hasErrors;
    PackageDescr pkg;
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    DefaultExpander expander = getDslExpander();
    try {
        if (expander == null) {
            expander = new DefaultExpander();
        }
        String str = expander.expand(dslrReader);
        if (expander.hasErrors()) {
            for (ExpanderException error : expander.getErrors()) {
                error.setResource(resource);
                addBuilderResult(error);
            }
        }
        pkg = parser.parse(resource, str);
        this.results.addAll(parser.getErrors());
        hasErrors = parser.hasErrors();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (dslrReader != null) {
            try {
                dslrReader.close();
            } catch (IOException e) {
            }
        }
    }
    return hasErrors ? null : pkg;
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) ExpanderException(org.drools.compiler.lang.ExpanderException) IOException(java.io.IOException) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander)

Example 7 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method removeObjectsGeneratedFromResource.

public ResourceRemovalResult removeObjectsGeneratedFromResource(Resource resource) {
    boolean modified = false;
    if (pkgRegistryMap != null) {
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            modified = packageRegistry.removeObjectsGeneratedFromResource(resource) || modified;
        }
    }
    if (results != null) {
        Iterator<KnowledgeBuilderResult> i = results.iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (processBuilder != null && processBuilder.getErrors() != null) {
        Iterator<? extends KnowledgeBuilderResult> i = processBuilder.getErrors().iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (results.size() == 0) {
        // TODO Error attribution might be bugged
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            packageRegistry.getPackage().resetErrors();
        }
    }
    Collection<String> removedTypes = typeBuilder.removeTypesGeneratedFromResource(resource);
    for (List<PackageDescr> pkgDescrs : packages.values()) {
        for (PackageDescr pkgDescr : pkgDescrs) {
            pkgDescr.removeObjectsGeneratedFromResource(resource);
        }
    }
    if (kBase != null) {
        modified = kBase.removeObjectsGeneratedFromResource(resource) || modified;
    }
    return new ResourceRemovalResult(modified, removedTypes);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 8 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.

private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
    List<PackageDescr> pkgDescrs = new ArrayList<>();
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    for (PMMLResource res : resources) {
        for (String key : res.getRules().keySet()) {
            String src = res.getRules().get(key);
            PackageDescr descr = null;
            descr = parser.parse(false, src);
            if (descr != null) {
                descr.setResource(resource);
                pkgDescrs.add(descr);
                dumpGeneratedRule(descr, key, src);
            } else {
                addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
            }
        }
    }
    return pkgDescrs;
}
Also used : PMMLResource(org.drools.compiler.compiler.PMMLResource) ParserError(org.drools.compiler.compiler.ParserError) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 9 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackageFromDrl.

/**
 * Load a rule package from DRL source using the supplied DSL configuration.
 *
 * @param source The source of the rules.
 * @param dsl    The source of the domain specific language configuration.
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromDrl(final Reader source, final Reader dsl) throws DroolsParserException, IOException {
    this.resource = new ReaderResource(source, ResourceType.DSLR);
    final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    final PackageDescr pkg = parser.parse(source, dsl);
    this.results.addAll(parser.getErrors());
    if (!parser.hasErrors()) {
        addPackage(pkg);
    }
    this.resource = null;
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) ReaderResource(org.drools.core.io.impl.ReaderResource) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 10 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method addPackage.

public synchronized void addPackage(InternalKnowledgePackage newPkg) {
    PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
    InternalKnowledgePackage pkg = null;
    if (pkgRegistry != null) {
        pkg = pkgRegistry.getPackage();
    }
    if (pkg == null) {
        PackageDescr packageDescr = new PackageDescr(newPkg.getName());
        pkgRegistry = getOrCreatePackageRegistry(packageDescr);
        mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
        pkg = pkgRegistry.getPackage();
    }
    // first merge anything related to classloader re-wiring
    pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
    if (newPkg.getFunctions() != null) {
        for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
            if (pkg.getFunctions().containsKey(entry.getKey())) {
                addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
            }
            pkg.addFunction(entry.getValue());
        }
    }
    pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
    pkg.getDialectRuntimeRegistry().onBeforeExecute();
    // we have to do this before the merging, as it does some classloader resolving
    TypeDeclaration lastType = null;
    try {
        // Resolve the class for the type declaation
        if (newPkg.getTypeDeclarations() != null) {
            // add type declarations
            for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
                lastType = type;
                type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
    }
    // now merge the new package into the existing one
    mergePackage(pkg, newPkg);
}
Also used : AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) Function(org.drools.core.rule.Function) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)243 Test (org.junit.Test)194 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)46 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 InputStreamReader (java.io.InputStreamReader)27 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)24 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14