use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class RuleContext method getDeclaration.
Declaration getDeclaration(Variable variable) {
if (variable.isFact()) {
Declaration declaration = innerDeclaration.get(variable);
if (declaration == null) {
declaration = queryDeclaration.get(variable);
}
if (declaration == null) {
Pattern pattern = patterns.get(variable);
declaration = pattern != null ? pattern.getDeclaration() : null;
}
return declaration;
} else {
Global global = ((Global) variable);
ObjectType objectType = builder.getObjectType(global);
InternalReadAccessor globalExtractor = new GlobalExtractor(global.getName(), objectType);
return new Declaration(global.getName(), globalExtractor, new Pattern(0, objectType));
}
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.
private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
BitMask leftMask = getLeftInferredMask();
ObjectType leftObjectType = leftInput.getObjectType();
if (leftObjectType instanceof ClassObjectType) {
TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
for (Declaration decl : accumulate.getRequiredDeclarations()) {
if (leftObjectType.equals(decl.getPattern().getObjectType())) {
leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
}
}
}
}
setLeftInferredMask(leftMask);
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class LeftTupleSource method initDeclaredMask.
protected void initDeclaredMask(BuildContext context, LeftTupleSource leftInput) {
if (context == null || context.getLastBuiltPatterns() == null) {
// only happens during unit tests
leftDeclaredMask = AllSetBitMask.get();
return;
}
if (leftInput.getType() != NodeTypeEnums.LeftInputAdapterNode) {
// BetaNode's not after LIANode are not relevant for left mask property specific, so don't block anything.
leftDeclaredMask = AllSetBitMask.get();
return;
}
// left input pattern
Pattern pattern = getLeftInputPattern(context);
ObjectType objectType = getObjectTypeForPropertyReactivity((LeftInputAdapterNode) leftInput, pattern);
if (!(objectType instanceof ClassObjectType)) {
// Only ClassObjectType can use property specific
leftDeclaredMask = AllSetBitMask.get();
return;
}
Class objectClass = ((ClassWireable) objectType).getClassType();
// if pattern is null (e.g. for eval or query nodes) we cannot calculate the mask, so we set it all
if (pattern != null && isPropertyReactive(context, objectClass)) {
Collection<String> leftListenedProperties = pattern.getListenedProperties();
List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
leftDeclaredMask = calculatePositiveMask(objectClass, leftListenedProperties, settableProperties);
leftNegativeMask = calculateNegativeMask(objectClass, leftListenedProperties, settableProperties);
setLeftListenedProperties(leftListenedProperties);
} else {
// if property specific is not on, then accept all modification propagations
leftDeclaredMask = AllSetBitMask.get();
}
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class StreamsTest method testEventExpirationValue.
@Test(timeout = 10000)
public void testEventExpirationValue() throws Exception {
String drl1 = "package org.drools.pkg1\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
String drl2 = "package org.drools.pkg2\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
kbuilder.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
kbase.addPackages(kbuilder.getKnowledgePackages());
List<ObjectTypeNode> otns = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectType stot = new ClassObjectType(StockTick.class);
for (ObjectTypeNode otn : otns) {
if (otn.getObjectType().isAssignableFrom(stot)) {
assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
}
}
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
Aggregations