Search in sources :

Example 11 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class TypeCoercionTest method testCoercionOfStringValueWithoutQuotes.

@Test
public void testCoercionOfStringValueWithoutQuotes() throws Exception {
    // JBRULES-3080
    final String str = "package org.drools.compiler.test; \n" + "declare A\n" + "   field : String\n" + "end\n" + "rule R when\n" + "   A( field == 12 )\n" + "then\n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();
    final FactType typeA = kbase.getFactType("org.drools.compiler.test", "A");
    final Object a = typeA.newInstance();
    typeA.set(a, "field", "12");
    ksession.insert(a);
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 12 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class PatternTest method testUppercaseField2.

@Test
public void testUppercaseField2() throws Exception {
    final String rule = "package org.drools.compiler\n" + "declare SomeFact\n" + "    Field : String\n" + "    aField : String\n" + "end\n" + "rule X\n" + "when\n" + "    SomeFact( Field == \"foo\", aField == \"bar\" )\n" + "then\n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(rule);
    final KieSession ksession = createKnowledgeSession(kbase);
    final FactType factType = kbase.getFactType("org.drools.compiler", "SomeFact");
    final Object fact = factType.newInstance();
    factType.set(fact, "Field", "foo");
    factType.set(fact, "aField", "bar");
    ksession.insert(fact);
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 13 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class PatternTest method testUppercaseField.

@Test
public void testUppercaseField() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "global java.util.List list\n";
    rule += "declare Address\n";
    rule += "    Street: String\n";
    rule += "end\n";
    rule += "rule \"r1\"\n";
    rule += "when\n";
    rule += "    Address($street: Street)\n";
    rule += "then\n";
    rule += "    list.add($street);\n";
    rule += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(rule);
    final KieSession ksession = createKnowledgeSession(kbase);
    ksession.setGlobal("list", new ArrayList<String>());
    final FactType addressType = kbase.getFactType("org.drools.compiler.test", "Address");
    final Object address = addressType.newInstance();
    addressType.set(address, "Street", "5th Avenue");
    ksession.insert(address);
    ksession.fireAllRules();
    final List list = (List) ksession.getGlobal("list");
    assertEquals(1, list.size());
    assertEquals("5th Avenue", list.get(0));
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 14 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KieMetaInfoBuilder method generateKieModuleMetaInfo.

public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
    // TODO: I think this method is wrong because it is only inspecting packages that are included
    // in at least one kbase, but I believe it should inspect all packages, even if not included in
    // any kbase, as they could be included in the future
    Map<String, TypeMetaInfo> typeInfos = new HashMap<String, TypeMetaInfo>();
    Map<String, Set<String>> rulesPerPackage = new HashMap<String, Set<String>>();
    KieModuleModel kieModuleModel = kModule.getKieModuleModel();
    for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
        KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl) kModule.getKnowledgeBuilderForKieBase(kieBaseName);
        Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageRegistry();
        KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
        KieModuleCache.CompilationData.Builder _compData = createCompilationData();
        for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
            PackageRegistry pkgRegistry = pkgRegistryMap.get(kPkg.getName());
            JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
            List<String> types = new ArrayList<String>();
            for (FactType factType : kPkg.getFactTypes()) {
                Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
                if (typeDeclaration != null) {
                    typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
                }
                String className = factType.getName();
                String internalName = className.replace('.', '/') + ".class";
                if (trgMfs != null) {
                    byte[] bytes = runtimeData.getBytecode(internalName);
                    if (bytes != null) {
                        trgMfs.write(internalName, bytes, true);
                    }
                }
                types.add(internalName);
            }
            Set<String> rules = rulesPerPackage.get(kPkg.getName());
            if (rules == null) {
                rules = new HashSet<String>();
            }
            for (Rule rule : kPkg.getRules()) {
                if (!rules.contains(rule.getName())) {
                    rules.add(rule.getName());
                }
            }
            if (!rules.isEmpty()) {
                rulesPerPackage.put(kPkg.getName(), rules);
            }
            addToCompilationData(_compData, runtimeData, types);
        }
        _kmoduleCacheBuilder.addCompilationData(_compData.build());
        if (trgMfs != null) {
            writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
        }
    }
    return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ClassDefinition(org.drools.core.factmodel.ClassDefinition) KiePackage(org.kie.api.definition.KiePackage) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) FactType(org.kie.api.definition.type.FactType) TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieModuleMetaInfo(org.drools.core.rule.KieModuleMetaInfo) Rule(org.kie.api.definition.rule.Rule) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 15 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class DescrBuilderTest method testDeclare.

@Test
public void testDeclare() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.beans").newDeclare().type().name("StockTick").newAnnotation("role").value("event").end().newAnnotation("author").value("bob").end().newField("symbol").type("String").end().newField("price").type("double").end().end().newDeclare().enumerative().name("Planets").newAnnotation("kind").value("enum").end().newEnumLiteral("earth").constructorArg("6.0").constructorArg("10.0").end().newEnumLiteral("jupiter").constructorArg("44.0").constructorArg("50.0").end().newField("mass").type("double").end().newField("radius").type("double").end().end().getDescr();
    assertEquals(1, pkg.getTypeDeclarations().size());
    assertEquals(1, pkg.getEnumDeclarations().size());
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.beans", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    FactType stType = kbase.getFactType("org.beans", "StockTick");
    assertNotNull(stType);
    Object st = stType.newInstance();
    stType.set(st, "symbol", "RHT");
    stType.set(st, "price", 10);
    assertEquals("RHT", stType.get(st, "symbol"));
// stType.getAnnotation("author"); TODO: implement support for this
}
Also used : KiePackage(org.kie.api.definition.KiePackage) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9