Search in sources :

Example 21 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method createReturnValueRule.

private void createReturnValueRule(final PackageDescr packageDescr, final String expression) {
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    pattern.addConstraint(new ExprConstraintDescr("price == (" + expression + ")"));
    ruleDescr.setConsequence("update(stilton);");
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 22 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class ModelBuilderImplTest method getGlobalDescr.

private GlobalDescr getGlobalDescr() {
    GlobalDescr toReturn = new GlobalDescr();
    toReturn.setType(Double.class.getName());
    toReturn.setIdentifier("DOUBLE_GLOBAL");
    return toReturn;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr)

Example 23 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class ChangeSetBuilder method diffResource.

private static ResourceChangeSet diffResource(String file, byte[] originalBytes, byte[] currentBytes, List<TypeDeclarationDescr> typeDeclarations) {
    ResourceChangeSet pkgcs = new ResourceChangeSet(file, ChangeType.UPDATED);
    ResourceType type = ResourceType.determineResourceType(file);
    if (ResourceType.DRL.equals(type) || ResourceType.GDRL.equals(type) || ResourceType.RDRL.equals(type) || ResourceType.TDRL.equals(type)) {
        try {
            PackageDescr originalPkg = new DrlParser().parse(new ByteArrayResource(originalBytes));
            PackageDescr currentPkg = new DrlParser().parse(new ByteArrayResource(currentBytes));
            String pkgName = isEmpty(currentPkg.getName()) ? getDefaultPackageName() : currentPkg.getName();
            String oldPkgName = isEmpty(originalPkg.getName()) ? getDefaultPackageName() : originalPkg.getName();
            if (!oldPkgName.equals(pkgName)) {
                // so it is useless to further investigate other changes
                return pkgcs;
            }
            pkgcs.setPackageName(pkgName);
            for (RuleDescr crd : currentPkg.getRules()) {
                pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
            }
            // needs to be cloned
            List<RuleDescr> orules = new ArrayList<>(originalPkg.getRules());
            diffDescrs(originalBytes, currentBytes, pkgcs, orules, currentPkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
            // needs to be cloned
            List<FunctionDescr> ofuncs = new ArrayList<>(originalPkg.getFunctions());
            diffDescrs(originalBytes, currentBytes, pkgcs, ofuncs, currentPkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
            // needs to be cloned
            List<GlobalDescr> oglobals = new ArrayList<>(originalPkg.getGlobals());
            diffDescrs(originalBytes, currentBytes, pkgcs, oglobals, currentPkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
            for (TypeDeclarationDescr typeDeclaration : currentPkg.getTypeDeclarations()) {
                if (isEmpty(typeDeclaration.getNamespace())) {
                    typeDeclaration.setNamespace(isEmpty(currentPkg.getNamespace()) ? DEFAULT_PACKAGE : currentPkg.getNamespace());
                }
                typeDeclarations.add(typeDeclaration);
            }
        } catch (Exception e) {
            logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
        }
    }
    pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
    return pkgcs;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Arrays(java.util.Arrays) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) ResourceChange(org.kie.internal.builder.ResourceChange) RuleDescr(org.drools.drl.ast.descr.RuleDescr) TypeResolver(org.drools.core.addon.TypeResolver) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DrlParser(org.drools.drl.parser.DrlParser) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Logger(org.slf4j.Logger) ClassUtils.convertClassToResourcePath(org.drools.core.util.ClassUtils.convertClassToResourcePath) Iterator(java.util.Iterator) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Collection(java.util.Collection) Set(java.util.Set) BaseDescr(org.drools.drl.ast.descr.BaseDescr) List(java.util.List) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DEFAULT_PACKAGE(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl.DEFAULT_PACKAGE) Comparator(java.util.Comparator) ClassUtils.convertResourceToClassName(org.drools.core.util.ClassUtils.convertResourceToClassName) StringUtils(org.drools.core.util.StringUtils) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) ResourceType(org.kie.api.io.ResourceType) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 24 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class CompositePackageDescr method internalAdd.

private void internalAdd(Resource resource, PackageDescr packageDescr) {
    for (ImportDescr descr : packageDescr.getImports()) {
        addImport(descr);
        descr.setResource(resource);
    }
    for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
        addFunctionImport(descr);
        descr.setResource(resource);
    }
    for (AccumulateImportDescr descr : packageDescr.getAccumulateImports()) {
        addAccumulateImport(descr);
        descr.setResource(resource);
    }
    for (AttributeDescr descr : packageDescr.getAttributes()) {
        addAttribute(descr);
        descr.setResource(resource);
    }
    for (GlobalDescr descr : packageDescr.getGlobals()) {
        addGlobal(descr);
        descr.setResource(resource);
    }
    for (FunctionDescr descr : packageDescr.getFunctions()) {
        addFunction(descr);
        descr.setResource(resource);
    }
    for (RuleDescr descr : packageDescr.getRules()) {
        addRule(descr);
        descr.setResource(resource);
    }
    // Avoid adding the same type declaration twice, see
    // TypeDeclarationTest.testDuplicatedTypeDeclarationInDifferentResources
    // IncrementalCompilationTest.testIncrementalCompilationWithAmbiguousRedeclares
    // RHDM-1738
    Set<TypeDeclarationDescr> typeDeclarationDescrs = new HashSet<>(getTypeDeclarations());
    for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
        if (!typeDeclarationDescrs.contains(descr)) {
            addTypeDeclaration(descr);
            descr.setResource(resource);
        }
    }
    for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
        addEnumDeclaration(enumDescr);
        enumDescr.setResource(resource);
    }
    for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
        addEntryPointDeclaration(descr);
        descr.setResource(resource);
    }
    for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
        addWindowDeclaration(descr);
        descr.setResource(resource);
    }
    packageDescr.getPreferredPkgUUID().ifPresent(pkgUUID -> {
        if (getPreferredPkgUUID().isPresent() && !pkgUUID.equals(getPreferredPkgUUID().get())) {
            throw new RuntimeException(String.format("Trying to overwrite preferredPkgUUID %s with a different value %s", getPreferredPkgUUID().get(), pkgUUID));
        }
        setPreferredPkgUUID(pkgUUID);
    });
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) EntryPointDeclarationDescr(org.drools.drl.ast.descr.EntryPointDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) HashSet(java.util.HashSet)

Aggregations

GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)24 PackageDescr (org.drools.drl.ast.descr.PackageDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)13 Test (org.junit.Test)12 AndDescr (org.drools.drl.ast.descr.AndDescr)10 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Cheese (org.drools.mvel.compiler.Cheese)6 List (java.util.List)5 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)5 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 ImportDescr (org.drools.drl.ast.descr.ImportDescr)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Iterator (java.util.Iterator)2