Exemple #1
0
  @Test
  public void testEqualsObject() {
    final LeftTupleSource ts = new MockTupleSource(1);
    final ObjectSource os = new MockObjectSource(2);

    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());

    final BetaNode j1 = new JoinNode(1, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode j2 = new JoinNode(2, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode n1 = new NotNode(3, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode n2 = new NotNode(4, ts, os, EmptyBetaConstraints.getInstance(), buildContext);

    assertEquals(j1, j1);
    assertEquals(j2, j2);
    assertEquals(j1, j2);
    assertEquals(n1, n1);
    assertEquals(n2, n2);
    assertEquals(n1, n2);

    assertFalse(j1.equals(n1));
    assertFalse(j1.equals(n2));
    assertFalse(n1.equals(j1));
    assertFalse(n1.equals(j2));
  }
  /**
   * 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)));
  }
  @Before
  public void setUp() throws Exception {
    store.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    this.kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();

    this.buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());
  }
  @Test
  public void testLeftInputAdapterNode() {
    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);

    assertEquals(23, liaNode.getId());

    assertEquals(0, objectTypeNode.getSinkPropagator().getSinks().length);
    liaNode.attach(context);
    assertEquals(1, objectTypeNode.getSinkPropagator().getSinks().length);
  }
  public ClassObjectTypeConf(
      final EntryPointId entryPoint, final Class<?> clazz, final InternalKnowledgeBase kBase) {
    this.cls =
        (Activation.class.isAssignableFrom(clazz))
            ? ClassObjectType.Match_ObjectType.getClassType()
            : clazz;
    this.kBase = kBase;
    this.entryPoint = entryPoint;
    this.typeDecl = kBase.getTypeDeclaration(clazz);
    isEvent = typeDecl != null && typeDecl.getRole() == TypeDeclaration.Role.EVENT;
    isTrait = determineTraitStatus();

    ObjectType objectType =
        kBase.getClassFieldAccessorCache().getClassObjectType(new ClassObjectType(clazz, isEvent));

    this.concreteObjectTypeNode = kBase.getRete().getObjectTypeNodes(entryPoint).get(objectType);
    if (this.concreteObjectTypeNode == null) {
      BuildContext context = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());
      context.setCurrentEntryPoint(entryPoint);
      if (DroolsQuery.class == clazz) {
        context.setTupleMemoryEnabled(false);
        context.setObjectTypeNodeMemoryEnabled(false);
      } else if (context.getKnowledgeBase().getConfiguration().isSequential()) {
        // We are in sequential mode, so no nodes should have memory
        //                context.setTupleMemoryEnabled( false );
        //                context.setObjectTypeNodeMemoryEnabled( false );
        context.setTupleMemoryEnabled(true);
        context.setObjectTypeNodeMemoryEnabled(true);
      } else {
        context.setTupleMemoryEnabled(true);
        context.setObjectTypeNodeMemoryEnabled(true);
      }
      // there must exist an ObjectTypeNode for this concrete class
      this.concreteObjectTypeNode = PatternBuilder.attachObjectTypeNode(context, objectType);
    }

    this.supportsPropertyListeners = checkPropertyListenerSupport(clazz);

    Traitable ttbl = cls.getAnnotation(Traitable.class);
    this.traitTmsEnabled = ttbl != null && ttbl.logical();
  }
  @Test
  public void testQueryTerminalNode() {
    final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class);
    final ObjectTypeNode queryObjectTypeNode =
        new ObjectTypeNode(
            this.buildContext.getNextId(), this.entryPoint, queryObjectType, buildContext);
    queryObjectTypeNode.attach(buildContext);

    ClassFieldReader extractor = store.getReader(DroolsQuery.class, "name");

    MvelConstraint constraint =
        new MvelConstraintTestUtil(
            "name == \"query-1\"", FieldFactory.getInstance().getFieldValue("query-1"), extractor);

    final QueryImpl query = new QueryImpl("query-1");
    buildContext.setRule(query);
    AlphaNode alphaNode =
        new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext);
    alphaNode.attach(buildContext);

    final LeftInputAdapterNode liaNode =
        new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext);
    liaNode.attach(buildContext);

    final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
    final ObjectTypeNode cheeseObjectTypeNode =
        new ObjectTypeNode(
            this.buildContext.getNextId(), this.entryPoint, cheeseObjectType, buildContext);
    cheeseObjectTypeNode.attach(buildContext);

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

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

    alphaNode =
        new AlphaNode(
            this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext);
    alphaNode.attach(buildContext);

    BuildContext buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());
    buildContext.setTupleMemoryEnabled(false);

    final JoinNode joinNode =
        new JoinNode(
            this.buildContext.getNextId(),
            liaNode,
            alphaNode,
            EmptyBetaConstraints.getInstance(),
            buildContext);
    joinNode.attach(buildContext);

    final QueryTerminalNode queryNode =
        new QueryTerminalNode(
            this.buildContext.getNextId(),
            joinNode,
            query,
            ((QueryImpl) query).getLhs(),
            0,
            buildContext);

    queryNode.attach(buildContext);

    final KnowledgePackageImpl pkg = new KnowledgePackageImpl("com.drools.test");
    pkg.addRule(query);
    ((KnowledgeBaseImpl) kBase).addPackages(Arrays.asList(new InternalKnowledgePackage[] {pkg}));

    KieSession kSession = kBase.newKieSession();
    QueryResults results = kSession.getQueryResults("query-1");

    assertEquals(0, results.size());

    final Cheese stilton1 = new Cheese("stilton", 100);
    final FactHandle handle1 = kSession.insert(stilton1);

    results = kSession.getQueryResults("query-1");

    assertEquals(1, results.size());

    final Cheese cheddar = new Cheese("cheddar", 55);
    kSession.insert(cheddar);

    results = kSession.getQueryResults("query-1");

    assertEquals(1, results.size());

    final Cheese stilton2 = new Cheese("stilton", 5);

    final FactHandle handle2 = kSession.insert(stilton2);

    results = kSession.getQueryResults("query-1");

    assertEquals(2, results.size());

    /**
     * QueryResultsRow result = results.get( 0 ); assertEquals( 1, result.size() ); assertEquals(
     * stilton1, result.get( 0 ) );
     *
     * <p>result = results.get( 1 ); assertEquals( 1, result.size() ); assertEquals( stilton2,
     * result.get( 0 ) );
     */
    int i = 0;
    for (final Iterator<QueryResultsRow> it = results.iterator(); it.hasNext(); ) {
      QueryResultsRow resultRow = it.next();
      if (i == 1) {
        //                assertSame( stilton2, result.g( 0 ) );
      } else {
        //                assertSame( stilton1, result.get( 0 ) );
      }
      i++;
    }

    kSession.delete(handle1);
    results = kSession.getQueryResults("query-1");

    assertEquals(1, results.size());

    kSession.delete(handle2);
    results = kSession.getQueryResults("query-1");

    assertEquals(0, results.size());
  }
 @Before
 public void setUp() throws Exception {
   this.kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
   this.buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator());
 }