Search in sources :

Example 26 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class KiePackagesBuilder method createKiePackage.

private KnowledgePackageImpl createKiePackage(String name) {
    KnowledgePackageImpl kpkg = new KnowledgePackageImpl(name);
    kpkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(configuration.getClassLoader()));
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<>(kpkg.getImports().keySet()), configuration.getClassLoader(), name);
    typeResolver.addImport(name + ".*");
    kpkg.setTypeResolver(typeResolver);
    return kpkg;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Example 27 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class KiePackagesBuilder method build.

public CanonicalKiePackages build() {
    for (Model model : models) {
        for (TypeMetaData metaType : model.getTypeMetaDatas()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(metaType.getPackage(), this::createKiePackage);
            pkg.addTypeDeclaration(createTypeDeclaration(pkg, metaType));
        }
        for (Global global : model.getGlobals()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(global.getPackage(), this::createKiePackage);
            pkg.addGlobal(global.getName(), global.getType());
        }
        for (Query query : model.getQueries()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(query.getPackage(), this::createKiePackage);
            pkg.addRule(compileQuery(pkg, query));
        }
        int ruleCounter = 0;
        for (Rule rule : model.getRules()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(rule.getPackage(), this::createKiePackage);
            RuleImpl ruleImpl = compileRule(pkg, rule);
            ruleImpl.setLoadOrder(ruleCounter++);
            pkg.addRule(ruleImpl);
        }
    }
    return new CanonicalKiePackages(packages);
}
Also used : TypeMetaData(org.drools.model.TypeMetaData) Query(org.drools.model.Query) DroolsQuery(org.drools.core.base.DroolsQuery) Model(org.drools.model.Model) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Rule(org.drools.model.Rule) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Global(org.drools.model.Global) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint)

Example 28 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl 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 29 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class FactTemplateTest method testFacts.

@Test
public void testFacts() {
    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[] fields1 = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese1 = new FactTemplateImpl(pkg, "Cheese", fields1);
    final Fact stilton1 = cheese1.createFact(10);
    stilton1.setFieldValue("name", "stilton");
    stilton1.setFieldValue("price", new Integer(200));
    final Fact stilton2 = cheese1.createFact(11);
    stilton2.setFieldValue(0, "stilton");
    stilton2.setFieldValue(1, new Integer(200));
    assertEquals(stilton1, stilton2);
    assertEquals(stilton1.hashCode(), stilton2.hashCode());
    final Fact brie = cheese1.createFact(12);
    brie.setFieldValue("name", "brie");
    brie.setFieldValue("price", new Integer(55));
    assertFalse(stilton1.equals(brie));
    assertFalse(stilton1.hashCode() == brie.hashCode());
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 30 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class FactTemplateTest method testFieldsAndGetters.

@Test
public void testFieldsAndGetters() {
    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);
    assertEquals("org.store", cheese.getPackage().getName());
    assertEquals("Cheese", cheese.getName());
    assertEquals(2, cheese.getNumberOfFields());
    assertSame(fields, cheese.getAllFieldTemplates());
    assertSame(cheeseName, cheese.getFieldTemplate(0));
    assertSame(cheesePrice, cheese.getFieldTemplate(1));
    assertSame(cheeseName, cheese.getFieldTemplate("name"));
    assertSame(cheesePrice, cheese.getFieldTemplate("price"));
    assertEquals(0, cheese.getFieldTemplateIndex("name"));
    assertEquals(1, cheese.getFieldTemplateIndex("price"));
}
Also used : KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)41 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)26 Test (org.junit.Test)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)18 ClassObjectType (org.drools.core.base.ClassObjectType)13 Pattern (org.drools.core.rule.Pattern)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)9 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)8 WorkingMemory (org.drools.core.WorkingMemory)7 Consequence (org.drools.core.spi.Consequence)7 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 Declaration (org.drools.core.rule.Declaration)6 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)6 IOException (java.io.IOException)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)5 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5