Search in sources :

Example 71 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class ModelBuilderImpl method compileKnowledgePackages.

@Override
protected void compileKnowledgePackages(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    validateUniqueRuleNames(packageDescr);
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    String pkgName = pkg.getName();
    PackageModel model = packageModels.computeIfAbsent(pkgName, s -> {
        final DialectCompiletimeRegistry dialectCompiletimeRegistry = pkgRegistry.getDialectCompiletimeRegistry();
        return new PackageModel(pkgName, this.getBuilderConfiguration(), isPattern, dialectCompiletimeRegistry);
    });
    generateModel(this, pkg, packageDescr, model, isPattern);
}
Also used : DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 72 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class ModelBuilderImpl method buildRules.

protected void buildRules(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
        generatePOJOs(packageDescr, pkgRegistry);
    }
    List<GeneratedClassWithPackage> allGeneratedPojos = packageModels.values().stream().flatMap(p -> p.getGeneratedPOJOsSource().stream().map(c -> new GeneratedClassWithPackage(c, p.getName(), p.getImports()))).collect(Collectors.toList());
    // Every class gets compiled in each classloader, maybe they can be compiled only one time?
    final Map<String, Class<?>> allCompiledClasses = new HashMap<>();
    for (CompositePackageDescr packageDescr : packages) {
        InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
        allCompiledClasses.putAll(compileType(this, pkg.getPackageClassLoader(), allGeneratedPojos));
    }
    for (CompositePackageDescr packageDescr : packages) {
        InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
        allGeneratedPojos.forEach(c -> registerType(pkg.getTypeResolver(), allCompiledClasses));
    }
    if (hasErrors()) {
        // if Error while generating pojo do not try compile rule as they very likely depends hence fail too.
        return;
    }
    for (CompositePackageDescr packageDescr : packages) {
        setAssetFilter(packageDescr.getFilter());
        PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
        compileKnowledgePackages(packageDescr, pkgRegistry);
        setAssetFilter(null);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) POJOGenerator.registerType(org.drools.modelcompiler.builder.generator.POJOGenerator.registerType) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Collection(java.util.Collection) ModelGenerator.generateModel(org.drools.modelcompiler.builder.generator.ModelGenerator.generateModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclarationFactory(org.drools.compiler.builder.impl.TypeDeclarationFactory) POJOGenerator.generatePOJO(org.drools.modelcompiler.builder.generator.POJOGenerator.generatePOJO) Collectors(java.util.stream.Collectors) POJOGenerator.compileType(org.drools.modelcompiler.builder.generator.POJOGenerator.compileType) ArrayList(java.util.ArrayList) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) List(java.util.List) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) HashMap(java.util.HashMap) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 73 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class PatternTest method testDeclarationsFactTemplate.

@Test
public void testDeclarationsFactTemplate() throws Exception {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
    final ObjectType type = new FactTemplateObjectType(cheese);
    final Pattern col = new Pattern(0, type, "foo");
    final Declaration dec = col.getDeclaration();
    final InternalReadAccessor ext = dec.getExtractor();
    assertEquals(Fact.class, ext.getExtractToClass());
    final Fact stilton = cheese.createFact(10);
    stilton.setFieldValue("name", "stilton");
    stilton.setFieldValue("price", new Integer(200));
    assertEquals(stilton, dec.getValue(null, stilton));
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) Fact(org.drools.core.facttemplates.Fact) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 74 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class TypeDeclarationConfigurator method wireDurationAccessor.

private static void wireDurationAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
    Duration duration = annotated.getTypedAnnotation(Duration.class);
    if (duration != null) {
        BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
        String durationField;
        try {
            durationField = duration.value();
        } catch (Exception e) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
            return;
        }
        type.setDurationAttribute(durationField);
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, durationField, pkg);
        if (results != null) {
            type.setDurationExtractor(getFieldExtractor(type, durationField, pkg, results));
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error processing @duration for TypeDeclaration '" + type.getFullName() + "': cannot access the field '" + durationField + "'"));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) Duration(org.kie.api.definition.type.Duration) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 75 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class BayesAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    BayesNetwork network;
    JunctionTreeBuilder builder;
    KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
    Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
    if (bif == null) {
        return;
    }
    try {
        network = XmlBifParser.buildBayesNetwork(bif);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return;
    }
    try {
        builder = new JunctionTreeBuilder(network);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
        return;
    }
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
    InternalKnowledgePackage kpkgs = pkgReg.getPackage();
    Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
    BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
    if (bpkg == null) {
        bpkg = new BayesPackage();
        rpkg.put(ResourceType.BAYES, bpkg);
    }
    bpkg.addJunctionTree(network.getName(), builder.build());
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) ResourceType(org.kie.api.io.ResourceType) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Bif(org.drools.beliefs.bayes.model.Bif) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8