Beispiel #1
0
  @Test
  public void testAlphaNode() {
    final PropagationContext context =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);
    final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl(1L, kBase);

    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");

    final MvelConstraint constraint =
        new MvelConstraintTestUtil(
            "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor);

    final List list = new ArrayList();
    final Cheese cheese1 = new Cheese("cheddar", 20);
    final Cheese cheese2 = new Cheese("brie", 20);
    list.add(cheese1);
    list.add(cheese2);
    final MockDataProvider dataProvider = new MockDataProvider(list);

    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));

    From fromCe = new From(dataProvider);
    fromCe.setResultPattern(pattern);

    final ReteFromNode from =
        new ReteFromNode(
            3,
            dataProvider,
            new MockTupleSource(80),
            new AlphaNodeFieldConstraint[] {constraint},
            null,
            true,
            buildContext,
            fromCe);
    final MockLeftTupleSink sink = new MockLeftTupleSink(5);
    from.addTupleSink(sink);

    final Person person1 = new Person("xxx1", 30);
    final FactHandle person1Handle = workingMemory.insert(person1);
    final LeftTuple tuple1 = new LeftTupleImpl((DefaultFactHandle) person1Handle, from, true);
    from.assertLeftTuple(tuple1, context, workingMemory);

    // nothing should be asserted, as cheese1 is cheddar and we are filtering on stilton
    assertEquals(0, sink.getAsserted().size());

    // Set cheese1 to stilton and it should now propagate
    cheese1.setType("stilton");
    final Person person2 = new Person("xxx2", 30);
    final FactHandle person2Handle = workingMemory.insert(person2);
    final LeftTuple tuple2 = new LeftTupleImpl((DefaultFactHandle) person2Handle, from, true);
    from.assertLeftTuple(tuple2, context, workingMemory);

    final List asserted = sink.getAsserted();
    assertEquals(1, asserted.size());
    Tuple tuple = (Tuple) ((Object[]) asserted.get(0))[0];
    assertSame(person2, tuple.getObject(0));
    assertSame(cheese1, tuple.getObject(1));

    cheese2.setType("stilton");
    final Person person3 = new Person("xxx2", 30);
    final FactHandle person3Handle = workingMemory.insert(person3);
    final LeftTuple tuple3 = new LeftTupleImpl((DefaultFactHandle) person3Handle, from, true);
    from.assertLeftTuple(tuple3, context, workingMemory);

    assertEquals(3, asserted.size());
    tuple = (Tuple) ((Object[]) asserted.get(1))[0];
    assertSame(person3, tuple.getObject(0));
    assertSame(cheese1, tuple.getObject(1));
    tuple = (Tuple) ((Object[]) asserted.get(2))[0];
    assertSame(person3, tuple.getObject(0));
    assertSame(cheese2, tuple.getObject(1));

    assertNotSame(cheese1, cheese2);
  }
  @Test
  public void testTripleAlphaObjectCharacterConstraint() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor =
        store.getReader(Cheese.class, "charObjectType", this.getClass().getClassLoader());

    final MvelConstraint lit =
        new MvelConstraintTestUtil("charObjectType == 65", new LongFieldImpl(65), extractor);

    final AlphaNode al =
        new AlphaNode(
            buildContext.getNextId(),
            lit,
            new MockObjectSource(buildContext.getNextId()),
            buildContext);

    ad.addObjectSink(al);

    assertNull(ad.otherSinks);
    assertNotNull(ad.hashedFieldIndexes);
    assertEquals(1, ad.hashableSinks.size());
    assertEquals(al, ad.getSinks()[0]);

    final MvelConstraint lit2 =
        new MvelConstraintTestUtil("charObjectType == 66", new LongFieldImpl(66), extractor);

    final AlphaNode al2 =
        new AlphaNode(
            buildContext.getNextId(),
            lit2,
            new MockObjectSource(buildContext.getNextId()),
            buildContext);

    ad.addObjectSink(al2);

    assertNull(ad.hashedSinkMap);
    assertEquals(2, ad.hashableSinks.size());

    final MvelConstraint lit3 =
        new MvelConstraintTestUtil("charObjectType == 67", new LongFieldImpl(67), extractor);

    final AlphaNode al3 =
        new AlphaNode(
            buildContext.getNextId(),
            lit3,
            new MockObjectSource(buildContext.getNextId()),
            buildContext);
    ad.addObjectSink(al3);

    // this should now be nicely hashed.
    assertNotNull(ad.hashedSinkMap);
    assertNull(ad.hashableSinks);

    // test propagation
    Cheese cheese = new Cheese();
    cheese.setCharObjectType('B');
    CompositeObjectSinkAdapter.HashKey hashKey = new CompositeObjectSinkAdapter.HashKey();

    // should find this
    hashKey.setValue(extractor.getIndex(), cheese, extractor);
    ObjectSink sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
    assertSame(al2, sink);

    // should not find this one
    cheese.setCharObjectType('X');
    hashKey.setValue(extractor.getIndex(), cheese, extractor);
    sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
    assertNull(sink);

    // now remove one, check the hashing is undone
    ad.removeObjectSink(al2);
    assertNotNull(ad.hashableSinks);
    assertEquals(2, ad.hashableSinks.size());
    assertNull(ad.hashedSinkMap);
  }
Beispiel #3
0
  @Test
  public void testBetaNode() {
    final PropagationContext context =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);

    final StatefulKnowledgeSessionImpl workingMemory =
        new StatefulKnowledgeSessionImpl(
            1L, (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase());

    final ClassFieldReader priceExtractor = store.getReader(Cheese.class, "price");

    final ClassFieldReader ageExtractor = store.getReader(Person.class, "age");

    final Pattern pattern = new Pattern(0, new ClassObjectType(Person.class));

    final Declaration declaration = new Declaration("age", ageExtractor, pattern);

    MvelConstraint variableConstraint =
        new MvelConstraintTestUtil("price == age", declaration, priceExtractor);

    final RuleBaseConfiguration configuration = new RuleBaseConfiguration();
    configuration.setIndexRightBetaMemory(false);
    configuration.setIndexLeftBetaMemory(false);
    final BetaConstraints betaConstraints =
        new SingleBetaConstraints(variableConstraint, configuration);

    final List list = new ArrayList();
    final Cheese cheese1 = new Cheese("cheddar", 18);
    final Cheese cheese2 = new Cheese("brie", 12);
    list.add(cheese1);
    list.add(cheese2);
    final MockDataProvider dataProvider = new MockDataProvider(list);

    From fromCe = new From(dataProvider);
    fromCe.setResultPattern(new Pattern(0, new ClassObjectType(Cheese.class)));

    final ReteFromNode from =
        new ReteFromNode(
            3,
            dataProvider,
            new MockTupleSource(40),
            new AlphaNodeFieldConstraint[0],
            betaConstraints,
            true,
            buildContext,
            fromCe);
    final MockLeftTupleSink sink = new MockLeftTupleSink(5);
    from.addTupleSink(sink);

    final Person person1 = new Person("xxx1", 30);
    final FactHandle person1Handle = workingMemory.insert(person1);
    final LeftTuple tuple1 = new LeftTupleImpl((DefaultFactHandle) person1Handle, from, true);
    from.assertLeftTuple(tuple1, context, workingMemory);

    // nothing should be asserted, as cheese1 is cheddar and we are filtering on stilton
    assertEquals(0, sink.getAsserted().size());

    // Set cheese1 to stilton and it should now propagate
    cheese1.setPrice(30);
    final Person person2 = new Person("xxx2", 30);
    final FactHandle person2Handle = workingMemory.insert(person2);
    final LeftTuple tuple2 = new LeftTupleImpl((DefaultFactHandle) person2Handle, from, true);
    from.assertLeftTuple(tuple2, context, workingMemory);

    final List asserted = sink.getAsserted();
    assertEquals(1, asserted.size());
    Tuple tuple = (Tuple) ((Object[]) asserted.get(0))[0];
    assertSame(person2, tuple.getObject(0));
    assertSame(cheese1, tuple.getObject(1));

    cheese2.setPrice(30);
    final Person person3 = new Person("xxx2", 30);
    final FactHandle person3Handle = workingMemory.insert(person3);
    final LeftTuple tuple3 = new LeftTupleImpl((DefaultFactHandle) person3Handle, from, true);
    from.assertLeftTuple(tuple3, context, workingMemory);

    assertEquals(3, asserted.size());
    tuple = (Tuple) ((Object[]) asserted.get(1))[0];
    assertSame(person3, tuple.getObject(0));
    assertSame(cheese1, tuple.getObject(1));
    tuple = (Tuple) ((Object[]) asserted.get(2))[0];
    assertSame(person3, tuple.getObject(0));
    assertSame(cheese2, tuple.getObject(1));

    assertNotSame(cheese1, cheese2);
  }