Exemplo n.º 1
0
  /**
   * Construct.
   *
   * @param kBase The <code>InternalKnowledgeBase</code> of this agenda.
   * @param initMain Flag to initialize the MAIN agenda group
   */
  public ReteAgenda(InternalKnowledgeBase kBase, boolean initMain) {

    this.agendaGroups = new HashMap<String, InternalAgendaGroup>();
    this.activationGroups = new HashMap<String, InternalActivationGroup>();
    this.focusStack = new LinkedList<AgendaGroup>();
    this.scheduledActivations = new org.drools.core.util.LinkedList<ScheduledAgendaItem>();
    this.agendaGroupFactory = kBase.getConfiguration().getAgendaGroupFactory();

    if (initMain) {
      // MAIN should always be the first AgendaGroup and can never be
      // removed
      this.main = agendaGroupFactory.createAgendaGroup(AgendaGroup.MAIN, kBase);

      this.agendaGroups.put(AgendaGroup.MAIN, this.main);

      this.focusStack.add(this.main);
    }
    eager = new LinkedList<RuleAgendaItem>();

    Object object =
        ClassUtils.instantiateObject(
            kBase.getConfiguration().getConsequenceExceptionHandler(),
            kBase.getConfiguration().getClassLoader());
    if (object instanceof ConsequenceExceptionHandler) {
      this.legacyConsequenceExceptionHandler = (ConsequenceExceptionHandler) object;
    } else {
      this.consequenceExceptionHandler =
          (org.kie.api.runtime.rule.ConsequenceExceptionHandler) object;
    }

    this.declarativeAgenda = kBase.getConfiguration().isDeclarativeAgenda();
  }
  /**
   * Tests the assertion of objects into LeftInputAdapterNode
   *
   * @throws Exception
   */
  @Test
  public void testAssertObjectWithoutMemory() throws Exception {
    PropagationContextFactory pctxFactory =
        kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext pcontext =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);

    BuildContext context = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());
    final EntryPointNode entryPoint = new EntryPointNode(-1, kBase.getRete(), context);
    entryPoint.attach(context);

    final ObjectTypeNode objectTypeNode =
        new ObjectTypeNode(0, entryPoint, new ClassObjectType(Object.class), context);

    objectTypeNode.attach(context);

    final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(23, objectTypeNode, buildContext);
    liaNode.attach(context);

    final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl(1L, kBase);

    final MockLeftTupleSink sink = new MockLeftTupleSink();
    liaNode.addTupleSink(sink);

    final Object string1 = "cheese";

    // assert object
    final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert(string1);
    liaNode.assertObject(f0, pcontext, workingMemory);

    final List asserted = sink.getAsserted();
    assertLength(1, asserted);
    final LeftTuple tuple0 = (LeftTuple) ((Object[]) asserted.get(0))[0];
    assertSame(string1, workingMemory.getObject(tuple0.get(0)));
  }
Exemplo n.º 3
0
  @Before
  public void setUp() throws Exception {
    store.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);

    this.kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    buildContext = new BuildContext(kBase, new ReteooBuilder.IdGenerator());
    pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
  }
Exemplo n.º 4
0
  public Rete(InternalKnowledgeBase kBase) {
    super(
        0,
        RuleBasePartitionId.MAIN_PARTITION,
        kBase != null && kBase.getConfiguration().isMultithreadEvaluation());
    this.entryPoints = Collections.synchronizedMap(new HashMap<EntryPointId, EntryPointNode>());
    this.kBase = kBase;

    hashcode = calculateHashCode();
  }
  private InternalFactHandle createEventFactHandle(
      StatefulKnowledgeSessionImpl wm, InternalKnowledgeBase kBase) {
    // EntryPointNode
    Rete rete = kBase.getRete();

    NodeFactory nFacotry = kBase.getConfiguration().getComponentFactory().getNodeFactoryService();

    RuleBasePartitionId partionId = RuleBasePartitionId.MAIN_PARTITION;
    EntryPointNode entryPointNode =
        nFacotry.buildEntryPointNode(
            1, partionId, false, (ObjectSource) rete, EntryPointId.DEFAULT);
    InternalWorkingMemoryEntryPoint wmEntryPoint =
        new NamedEntryPoint(EntryPointId.DEFAULT, entryPointNode, wm);

    EventFactHandle factHandle =
        new EventFactHandle(1, (Object) new Person(), 0, (new Date()).getTime(), 0, wmEntryPoint);

    return factHandle;
  }
  @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 InstrumentedBuildContent context =
        new InstrumentedBuildContent(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 = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();

    PropagationContextFactory pctxFactory =
        kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    kBase.addPackage(pkg);

    StatefulKnowledgeSessionImpl ksession =
        (StatefulKnowledgeSessionImpl) kBase.newStatefulKnowledgeSession();

    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, 1, null, tuple, null),
            new RuleTerminalNode(
                0,
                new CompositeObjectSinkAdapterTest.MockBetaNode(),
                context.getRule(),
                subrule,
                0,
                new BuildContext(kBase, null)),
            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());
  }