use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class BayesRuntimeImpl method createInstance.
// @Override
// public BayesInstance createBayesFact(Class cls) {
// // using the two-tone pattern, to ensure only one is created
// BayesInstance instance = instances.get( cls.getName() );
// if ( instance == null ) {
// instance = createInstance(cls);
// }
//
// return instance;
// }
public BayesInstance createInstance(Class cls) {
// synchronised using the two-tone pattern, to ensure only one is created
// BayesInstance instance = instances.get( cls.getName() );
// if ( instance != null ) {
// return instance;
// }
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) runtime.getKieBase().getKiePackage(cls.getPackage().getName());
Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
BayesPackage bayesPkg = (BayesPackage) map.get(ResourceType.BAYES);
JunctionTree jtree = bayesPkg.getJunctionTree(cls.getSimpleName());
BayesInstance instance = new BayesInstance(jtree, cls);
return instance;
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class BayesWeaverService method merge.
@Override
public void merge(KieBase kieBase, KiePackage kiePkg, BayesPackage bayesPkg) {
Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
if (existing == null) {
existing = new BayesPackage();
map.put(ResourceType.BAYES, existing);
}
for (String name : bayesPkg.listJunctionTrees()) {
existing.addJunctionTree(name, bayesPkg.getJunctionTree(name));
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class AssemblerTest method testBayesPackageAssembly.
@Test
public void testBayesPackageAssembly() throws Exception {
KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
InternalKnowledgePackage kpkg = kbuilder.getPackageRegistry("org.drools.beliefs.bayes.integration").getPackage();
BayesPackage bkpg = (BayesPackage) kpkg.getResourceTypePackages().get(ResourceType.BAYES);
assertNotNull(bkpg.getJunctionTree("Garden"));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class TypeDeclarationConfigurator method wireTimestampAccessor.
private static void wireTimestampAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
Timestamp timestamp = annotated.getTypedAnnotation(Timestamp.class);
if (timestamp != null) {
BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
String timestampField;
try {
timestampField = timestamp.value();
} catch (Exception e) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
return;
}
type.setTimestampAttribute(timestampField);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, timestampField, pkg);
if (results != null) {
type.setTimestampExtractor(getFieldExtractor(type, timestampField, pkg, results));
} else {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error creating field accessors for timestamp field '" + timestamp + "' for type '" + type.getTypeName() + "'"));
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class RuleBuilderTest method testBuild.
@Test
public void testBuild() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
pkg.addGlobal("results", List.class);
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final String ruleClassName = "RuleClassName.java";
ruleDescr.setClassName(ruleClassName);
ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
// second GE should be a not
final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
assertTrue(not.isNot());
// not has no outer declarations
assertTrue(not.getOuterDeclarations().isEmpty());
assertEquals(1, not.getInnerDeclarations().size());
assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
// second not
final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
assertTrue(not2.isNot());
// not has no outer declarations
assertTrue(not2.getOuterDeclarations().isEmpty());
assertEquals(1, not2.getInnerDeclarations().size());
assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
Aggregations