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());
}
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();
}
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();
}
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);
}
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
}
Aggregations