自学内容网 自学内容网

activiti学习之模拟聊天机器人(含结束监听)

写在前面

本文来模拟实现一个聊天机器人。就是可以一直不厌其烦的跟你唠嗑!

1:需求

开发一个机器人聊天程序,如果是用户不说再见,则要一直跟用户聊下去。

2:开发

2.1:设计流程

在这里插入图片描述
当用户不说再见时就设置callEnd=2,此时会继续聊天:
在这里插入图片描述
当用户说再见时时就设置callEnd=1,此时就结束聊天:
在这里插入图片描述
xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="simpleChat" name="简单电话聊天" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="回答客户"></userTask>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow1" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow3" name="结束通话" sourceRef="exclusivegateway1" targetRef="endevent1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${callEnd == 1}]]></conditionExpression>
    </sequenceFlow>
    <manualTask id="manualtask1" name="跳回去"></manualTask>
    <sequenceFlow id="flow4" name="接续聊天" sourceRef="exclusivegateway1" targetRef="manualtask1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${callEnd == 2}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="manualtask1" targetRef="usertask1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_simpleChat">
    <bpmndi:BPMNPlane bpmnElement="simpleChat" id="BPMNPlane_simpleChat">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="210.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="320.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="490.0" y="137.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="620.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="manualtask1" id="BPMNShape_manualtask1">
        <omgdc:Bounds height="55.0" width="105.0" x="458.0" y="238.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="425.0" y="157.0"></omgdi:waypoint>
        <omgdi:waypoint x="490.0" y="157.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="245.0" y="157.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="157.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="530.0" y="157.0"></omgdi:waypoint>
        <omgdi:waypoint x="620.0" y="157.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="549.0" y="157.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="510.0" y="177.0"></omgdi:waypoint>
        <omgdi:waypoint x="510.0" y="238.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="521.0" y="199.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="510.0" y="238.0"></omgdi:waypoint>
        <omgdi:waypoint x="372.0" y="185.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

2.1:正文

2.1.1:部署流程

 @Test
public void deploy() {
    Deployment deployment = repositoryService.createDeployment() // 创建部署
            .addClasspathResource("bpmnfile/simple.bpmn20.xml") // 加载流程资源文件
            .name("测试电话机器人") // 流程名称
            .deploy(); // 部署
    System.out.println("流程部署ID:" + deployment.getId());
    System.out.println("流程部署Name:" + deployment.getName());
}

在这里插入图片描述

2.1.2:启动流程实例

/**
 * 启动流程实例
 */
@Test
public void start() {
    // 不会在act_ru_task表中生成数据,即不会生成任务,但是会在act_ru_execution表中生成数据,可以通过queryExecution查询
    // , 通过completeTask执行
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("simpleChat"); // 流程定义表的KEY字段值
    System.out.println("流程实例ID:" + pi.getId());
    System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}

在这里插入图片描述

2.1.3:完成任务

  • 用户不说再见的情况
@Test
public void complete() {
    // 直属领导审批
//        taskService.complete("677507");
    // 部门领导审批
//        taskService.complete("680007");
    String taskId = "4deda7ba-b2b5-11ef-a373-989096ca675d";
    // 董事长审批
//        final Task task = taskService.createTaskQuery().taskId("c4f42bf8-b2ac-11ef-af73-989096ca675d").list().get(0);

//        60861b7e-b21a-11ef-b642-989096ca675d
//        runtimeService.removeVariable(task.getExecutionId(), "callEnd");
    // 这里必须删除老的任务相同变量名称的变量,不然设置新的任务变量降不生效
    taskService.removeVariable(taskId, "callEnd");
    Map<String, Object> objectMap = new HashMap<>();
    objectMap.put("callEnd", 2); // 模拟用户不说再见的情况
//        taskService.complete("608c3601-b21a-11ef-b642-989096ca675d", objectMap);
//        objectMap.put("callEnd", 1); // 模拟用户说再见的情况,流程就结束啦
    taskService.complete(taskId, objectMap);
} 

此时流程不结束并生成新任务:
在这里插入图片描述

  • 用户说再见的情况
    在这里插入图片描述
    此时流程结束,并且所有的任务执行完毕!

3:升级,增加流程结束监听

如下设置一个监听器:
在这里插入图片描述
xml:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="simpleChat" name="简单电话聊天" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="回答客户"></userTask>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow1" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End">
      <extensionElements>
        <activiti:executionListener event="end" class="activity.study.initmysql.test.simple.CsdnUserTaskTaskListner"></activiti:executionListener>
      </extensionElements>
    </endEvent>
    <sequenceFlow id="flow3" name="结束通话" sourceRef="exclusivegateway1" targetRef="endevent1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${callEnd == 1}]]></conditionExpression>
    </sequenceFlow>
    <manualTask id="manualtask1" name="跳回去"></manualTask>
    <sequenceFlow id="flow4" name="接续聊天" sourceRef="exclusivegateway1" targetRef="manualtask1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${callEnd == 2}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="manualtask1" targetRef="usertask1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_simpleChat">
    <bpmndi:BPMNPlane bpmnElement="simpleChat" id="BPMNPlane_simpleChat">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="180.0" y="330.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="290.0" y="320.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="460.0" y="327.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="590.0" y="330.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="manualtask1" id="BPMNShape_manualtask1">
        <omgdc:Bounds height="55.0" width="105.0" x="428.0" y="428.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="395.0" y="347.0"></omgdi:waypoint>
        <omgdi:waypoint x="460.0" y="347.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="215.0" y="347.0"></omgdi:waypoint>
        <omgdi:waypoint x="290.0" y="347.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="500.0" y="347.0"></omgdi:waypoint>
        <omgdi:waypoint x="590.0" y="347.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="490.0" y="354.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="480.0" y="367.0"></omgdi:waypoint>
        <omgdi:waypoint x="480.0" y="428.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="491.0" y="389.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="480.0" y="428.0"></omgdi:waypoint>
        <omgdi:waypoint x="342.0" y="375.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

监听器:

package activity.study.initmysql.test.simple;


import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;

public class CsdnUserTaskTaskListner implements /*TaskListener,*/ ExecutionListener {
    /*@Override
    public void notify(DelegateTask delegateTask) {

        System.out.println("聊天结束啦111");
        *//*
          String EVENTNAME_CREATE = "create";
          String EVENTNAME_ASSIGNMENT = "assignment";
          String EVENTNAME_COMPLETE = "complete";
          String EVENTNAME_DELETE = "delete";
         *//*
        String eventName = delegateTask.getEventName();
        if (EVENTNAME_CREATE.equals(eventName)) System.out.println("create");
        if (EVENTNAME_ASSIGNMENT.equals(eventName)) System.out.println("assignment");
        if (EVENTNAME_COMPLETE.equals(eventName)) System.out.println("complete");
        if (EVENTNAME_DELETE.equals(eventName)) System.out.println("delete");
    }*/

    @Override
    public void notify(DelegateExecution execution) {
        System.out.println("聊天结束啦222");
    }
}

因为相比之前的流程设计只有在结束节点多了一个监听器的调用,所以这里只演示结束时:

@Test
public void complete() {
    // 直属领导审批
//        taskService.complete("677507");
    // 部门领导审批
//        taskService.complete("680007");
    String taskId = "d9e21163-b2b9-11ef-8928-989096ca675d";
    // 董事长审批
//        final Task task = taskService.createTaskQuery().taskId("c4f42bf8-b2ac-11ef-af73-989096ca675d").list().get(0);

//        60861b7e-b21a-11ef-b642-989096ca675d
//        runtimeService.removeVariable(task.getExecutionId(), "callEnd");
    // 这里必须删除老的任务相同变量名称的变量,不然设置新的任务变量降不生效
    taskService.removeVariable(taskId, "callEnd");
    Map<String, Object> objectMap = new HashMap<>();
//        objectMap.put("callEnd", 2); // 模拟用户不说再见的情况
//        taskService.complete("608c3601-b21a-11ef-b642-989096ca675d", objectMap);
    objectMap.put("callEnd", 1); // 模拟用户说再见的情况,流程就结束啦
    taskService.complete(taskId, objectMap);
}

输出:

2024-12-05 11:37:36.081  INFO 52092 --- [           main] a.s.initmysql.test.simple.SimpleTest     : Started SimpleTest in 16.563 seconds (JVM running for 18.428)
聊天结束啦222
...

写在后面

参考文章列表

activiti学习之在任务节点增加监听器

activiti学习之变量


原文地址:https://blog.csdn.net/wang0907/article/details/144259016

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!