Search in sources :

Example 11 with PackageDescr

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

the class KnowledgeBuilderImpl method decisionTableToPackageDescr.

PackageDescr decisionTableToPackageDescr(Resource resource, ResourceConfiguration configuration) throws DroolsParserException, IOException {
    DecisionTableConfiguration dtableConfiguration = configuration instanceof DecisionTableConfiguration ? (DecisionTableConfiguration) configuration : null;
    if (dtableConfiguration != null && !dtableConfiguration.getRuleTemplateConfigurations().isEmpty()) {
        List<String> generatedDrls = DecisionTableFactory.loadFromInputStreamWithTemplates(resource, dtableConfiguration);
        if (generatedDrls.size() == 1) {
            return generatedDrlToPackageDescr(resource, generatedDrls.get(0));
        }
        CompositePackageDescr compositePackageDescr = null;
        for (String generatedDrl : generatedDrls) {
            PackageDescr packageDescr = generatedDrlToPackageDescr(resource, generatedDrl);
            if (packageDescr != null) {
                if (compositePackageDescr == null) {
                    compositePackageDescr = new CompositePackageDescr(resource, packageDescr);
                } else {
                    compositePackageDescr.addPackageDescr(resource, packageDescr);
                }
            }
        }
        return compositePackageDescr;
    }
    String generatedDrl = DecisionTableFactory.loadFromResource(resource, dtableConfiguration);
    return generatedDrlToPackageDescr(resource, generatedDrl);
}
Also used : CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 12 with PackageDescr

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

the class TypeDeclarationBuilder method normalizeForeignPackages.

protected void normalizeForeignPackages(PackageDescr packageDescr) {
    Map<String, PackageDescr> foreignPackages = null;
    for (AbstractClassTypeDeclarationDescr typeDescr : packageDescr.getClassAndEnumDeclarationDescrs()) {
        if (kbuilder.filterAccepts(ResourceChange.Type.DECLARATION, typeDescr.getNamespace(), typeDescr.getTypeName())) {
            if (!typeDescr.getNamespace().equals(packageDescr.getNamespace())) {
                // If the type declaration is for a different namespace, process that separately.
                PackageDescr altDescr;
                if (foreignPackages == null) {
                    foreignPackages = new HashMap<String, PackageDescr>();
                }
                if (foreignPackages.containsKey(typeDescr.getNamespace())) {
                    altDescr = foreignPackages.get(typeDescr.getNamespace());
                } else {
                    altDescr = new PackageDescr(typeDescr.getNamespace());
                    altDescr.setResource(packageDescr.getResource());
                    foreignPackages.put(typeDescr.getNamespace(), altDescr);
                }
                if (typeDescr instanceof TypeDeclarationDescr) {
                    altDescr.addTypeDeclaration((TypeDeclarationDescr) typeDescr);
                } else if (typeDescr instanceof EnumDeclarationDescr) {
                    altDescr.addEnumDeclaration((EnumDeclarationDescr) typeDescr);
                }
                for (ImportDescr imp : packageDescr.getImports()) {
                    altDescr.addImport(imp);
                }
                kbuilder.getOrCreatePackageRegistry(altDescr);
            }
        }
    }
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) EnumDeclarationDescr(org.drools.compiler.lang.descr.EnumDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 13 with PackageDescr

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

the class TypeDeclarationCache method registerTypeDeclaration.

private void registerTypeDeclaration(String packageName, TypeDeclaration typeDeclaration) {
    if (typeDeclaration.getNature() == TypeDeclaration.Nature.DECLARATION || packageName.equals(typeDeclaration.getTypeClass().getPackage().getName())) {
        PackageRegistry packageRegistry = kbuilder.getOrCreatePackageRegistry(new PackageDescr(packageName, ""));
        packageRegistry.getPackage().addTypeDeclaration(typeDeclaration);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 14 with PackageDescr

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

the class DrlParser method compile.

private PackageDescr compile(boolean isEditor, final DRLParser parser) throws DroolsParserException {
    PackageDescr pkgDescr = null;
    try {
        if (isEditor) {
            parser.enableEditorInterface();
        }
        pkgDescr = parser.compilationUnit(resource);
        editorSentences = parser.getEditorInterface();
        makeErrorList(parser);
        if (isEditor || !this.hasErrors()) {
            return pkgDescr;
        } else {
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        final ParserError err = new ParserError(resource, GENERIC_ERROR_MESSAGE + e.toString() + "\n" + Arrays.toString(e.getStackTrace()), -1, 0);
        this.results.add(err);
        if (isEditor) {
            return pkgDescr;
        } else {
            throw new DroolsParserException(GENERIC_ERROR_MESSAGE + e.getMessage(), e);
        }
    }
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) IOException(java.io.IOException) ExpanderException(org.drools.compiler.lang.ExpanderException)

Example 15 with PackageDescr

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

the class FunctionHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    FunctionDescr functionDescr = (FunctionDescr) parser.getCurrent();
    final NodeList parameters = element.getElementsByTagName("parameter");
    for (int i = 0, length = parameters.getLength(); i < length; i++) {
        final String identifier = ((Element) parameters.item(i)).getAttribute("identifier");
        final String type = ((Element) parameters.item(i)).getAttribute("type");
        emptyAttributeCheck("parameter", "identifier", identifier, parser);
        emptyAttributeCheck("parameter", "type", type, parser);
        functionDescr.addParameter(type, identifier);
    }
    // we allow empty, "", bodies - but make sure that we atleast have a body element
    NodeList list = element.getElementsByTagName("body");
    if (list.getLength() == 0) {
        throw new SAXParseException("function must have a <body>", parser.getLocator());
    }
    functionDescr.setText(((org.w3c.dom.Text) list.item(0).getChildNodes().item(0)).getWholeText());
    final PackageDescr packageDescr = (PackageDescr) parser.getData();
    packageDescr.addFunction(functionDescr);
    return functionDescr;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr)

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