示例#1
0
  @Test
  public void testAction() {
    MockClass mock = new MockClass();
    List<Integer> list = new ArrayList<Integer>();

    Action action = PipelineFactory.newMvelAction("this.setValues( [0, 1, 2, 3, 4] ) ");

    assertNull(mock.getValues());
    action.receive(mock, new BasePipelineContext(Thread.currentThread().getContextClassLoader()));

    System.out.println(mock.getValues().get(0));
    assertEquals(5, mock.getValues().size());
    assertEquals(0, mock.getValues().get(0).intValue());
    assertEquals(4, mock.getValues().get(4).intValue());
  }
  @Test
  public void testSplitter() {
    MockClass mock = new MockClass();
    List<Integer> list = new ArrayList<Integer>();

    Action action = PipelineFactory.newMvelAction("this.setValues( [0, 1, 2, 3, 4] ) ");
    Expression expr = PipelineFactory.newMvelExpression("this.values");
    action.setReceiver(expr);

    Splitter splitter = PipelineFactory.newIterateSplitter();
    expr.setReceiver(splitter);

    ListAdapter listAdapter = new ListAdapterImpl(list, true);
    splitter.setReceiver(listAdapter);

    assertNull(mock.getValues());
    action.receive(mock, new BasePipelineContext(Thread.currentThread().getContextClassLoader()));

    System.out.println(list.get(0));
    assertEquals(5, list.size());
    assertEquals(0, list.get(0).intValue());
    assertEquals(4, list.get(4).intValue());
  }
  @Test
  public void testGlobal() throws Exception {
    Options xjcOpts = new Options();
    xjcOpts.setSchemaLanguage(Language.XMLSCHEMA);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    JaxbConfiguration jaxbConf = KnowledgeBuilderFactory.newJaxbConfiguration(xjcOpts, "xsd");

    kbuilder.add(
        ResourceFactory.newClassPathResource("order.xsd", getClass()), ResourceType.XSD, jaxbConf);

    assertFalse(kbuilder.hasErrors());

    kbuilder.add(
        ResourceFactory.newClassPathResource("test_Jaxb.drl", getClass()), ResourceType.DRL);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    assertFalse(kbuilder.hasErrors());

    assertFalse(kbuilder.hasErrors());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    KnowledgeRuntimeCommand setGlobalStage =
        PipelineFactory.newStatefulKnowledgeSessionSetGlobal("order");

    JAXBContext jaxbCtx =
        KnowledgeBuilderHelper.newJAXBContext(
            jaxbConf.getClasses().toArray(new String[jaxbConf.getClasses().size()]), kbase);

    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    Transformer transformer = PipelineFactory.newJaxbFromXmlTransformer(unmarshaller);
    transformer.setReceiver(setGlobalStage);

    Pipeline pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession);
    pipeline.setReceiver(transformer);

    String xml =
        StringUtils.readFileAsString(
            new InputStreamReader(getClass().getResourceAsStream("order.xml")));

    ResultHandlerImpl resultHandler = new ResultHandlerImpl();
    pipeline.insert(xml, resultHandler);

    // now round trip that global
    Action executeResult = PipelineFactory.newExecuteResultHandler();

    Action assignAsResult = PipelineFactory.newAssignObjectAsResult();
    assignAsResult.setReceiver(executeResult);

    // transformer = PipelineFactory.newXStreamToXmlTransformer( xstream );
    Marshaller marshaller = jaxbCtx.createMarshaller();
    transformer = PipelineFactory.newJaxbToXmlTransformer(marshaller);
    transformer.setReceiver(assignAsResult);

    KnowledgeRuntimeCommand getGlobalStage = PipelineFactory.newStatefulKnowledgeSessionGetGlobal();
    getGlobalStage.setReceiver(transformer);

    pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession);
    pipeline.setReceiver(getGlobalStage);

    resultHandler = new ResultHandlerImpl();
    pipeline.insert("order", resultHandler);

    assertEqualsIgnoreWhitespace(xml, (String) resultHandler.getObject());
  }