WAP聊天室机器人设计(转)

WAP聊天室机器人开发
本文详细介绍了一款WAP平台上聊天室机器人的开发过程,包括数据模型设计、核心类RobotBeat.java的实现,以及机器人如何在不同房间中随机发言的策略。文章提供了完整的代码示例,展示了机器人初始化、清理及登录的全过程。

1。前段时间开发了个WAP上聊天室的机器人。流程图如下
200688162022336.jpg

2.数据模型
200688162022932.jpg

3.Main Source:

RobotBeat.java

package pubchat;

import java.io.*;
import java.net.*;
import java.util.*;
import pubchat.dao.*;
import pubchat.client.*;
import org.apache.log4j.Logger;
import pubchat.wap.ClientTest;

public class RobotBeat extends Thread {
Logger log = Logger.getLogger(RobotBeat.class);
int timediff; //组件重画的间隔时间
volatile boolean shouldrun; //设为false为停止线程
ChatDAO chatDao = null;
RobotDAO robotDao = null;
Vector roomVec = null;
int roomSize = 0;
Vector robotVec = null;
boolean clearFlg = false;
private static int wordsCount = 0;

public RobotBeat() {
this.timediff = 1 * 60 * 1000;
this.shouldrun = true;
System.out.println("机器人线程启动。。。。。");
this.chatDao = new ChatDAO(true);
this.robotDao = new RobotDAO(true);
this.roomVec = chatDao.selectChatRoom(); //大类,小类,房间id, 房间名,房间类
this.roomSize = this.roomVec.size();
this.robotInit();
}

public void run() {
while (shouldrun) {
try {
//robotInit();

//robotClear();
//robotInit();
wordsCount++;
long start = System.currentTimeMillis();
System.out.println("Sleep start:"+start);
sleep(timediff);
long end = System.currentTimeMillis();
System.out.println("Sleep End:"+end);
//随机说话
// if (this.getProperty()==Driver.FLT_ROBOT) {
//机器人线程
if (wordsCount%20==0) {
System.gc();
robotClear();
robotInit();
} else {
ClientTest content = new ClientTest();
for (int i = 0; i < robotVec.size(); i++) {
String sayId = (String) robotVec.elementAt(i);
String sayWords = this.robotDao.selectRandomWord();
System.out.println("机器人 " + sayId + ":" + sayWords);
content.say(sayId, sayWords);
}
content = null;
}

//robotClear();
//robotInit();

//robotInit();

} catch (Exception e) {
e.printStackTrace();
}

}

}


/**
* Clear the robot user
*/
private void robotClear() {
//Clear robot from userHash
//Vector beats = Driver.query_heart_beats();
//System.out.println("Driver.query_heart_beats:"+beats.size());
for (int i = 0; i Client c = (Client) Driver.userHash.get(robotVec.elementAt(i));
if (c != null) {
System.out.println("c.Property:"+c.getProperty());
if (c.getProperty() == Driver.FLT_ROBOT) {
System.out.println("移除机器人 ID:"+c.getId());
Driver.removeUser(c.getId());
c.move_player("");
}
}
}
this.robotVec = null;

}

/**
* Init the robot user
*/
private void robotInit() {
this.robotVec = new Vector();
Vector robotVec = robotDao.selectRandomList(roomSize);
System.out.println("RoomSize:"+roomSize+"============"+roomVec.size());

for (int i = 0; i < roomSize; i++) {
//login the robot
String[] strRoom = (String[]) roomVec.elementAt(i);
System.out.println("Roomid:---&gt"+strRoom[2]);
String[] strRobot = (String[]) robotVec.elementAt(i);
//Roomid strs[2]
//Robot login
robotLogin(strRobot, strRoom[2]);
}
}

/**
* Login the robot user and move to the specify room
* @param argUserid String[]
* @param argRoomid String
* @return int
*/
private int robotLogin(String[] argUserid, String argRoomid) {
System.out.println("robotLogin:User->" + argUserid[1]+argUserid[0] + "到房间->" +
argRoomid);
Client client = (Client) Driver.userHash.get(argUserid);
if (client != null) {
log.info(argUserid + "a已经登录过了a");
return 0;
} else {
client = new RobotClient(argUserid[0]);
client.setShortname(argUserid[1]);
client.create();
Driver.add_User(client);
client.move_player(argRoomid);
this.robotVec.add(argUserid[0]);
return 1;
}
}
}
RobotDao.java
package pubchat.dao;
import java.util.Vector;

public class RobotDAO extends AbstractDAO {
public RobotDAO(boolean isPool) {
super(isPool);
}

/* The default answer of question */
static final String DEFAULT_ANSWER = "我们换个话题吧:)";
/* SQL - Get the robot list */
static final String SELECT_RANDOM_LIST = "Select id,nickname,status,last_change,reg_time,birthday,sex,flag "+
" From (Select id,nickname,status,last_change,reg_time,birthday,Decode(sex,'0','女','男') sex,flag "+
" From t_robot_list "+
" Order by dbms_random.random) "+
" Where rownum<=? ";
/* SQL - Get the random word */
static final String SELECT_RANDOM_WORD = "Select content "+
" From (Select content "+
" From t_robot_default "+
" Order by dbms_random.random) "+
" Where rownum<=1 ";
/* SQL - Get the answer by question */
static final String SELECT_A_BY_Q = "Select ANSWER "+
" From (Select ANSWER "+
" From t_robot_answer a, t_robot_question q, t_robot_exc e "+
" Where a.msg_id = e.a_id "+
" And e.q_id = q.msg_id "+
" And q.content like ? "+
" Order by dbms_random.random) "+
" Where rownum <= 1 ";
/* SQL - Add the new question to the table */
static final String ADD_QUESTION = "Insert into t_robot_question "+
" (msg_id, content) " +
"Values "+
" (seq_t_robot_question_id.nextval, ?) ";

/**
* Get the random list of robot
* @param argCount int
* @return Vector
*/
public Vector selectRandomList(int argCount) {
String[] arrPara = {String.valueOf(argCount)};
return this.selectVecData(SELECT_RANDOM_LIST,arrPara,8);
}

/**
* Get the random word
* @return String
*/
public String selectRandomWord() {
String retStr = "";
Vector v = this.selectVecData(SELECT_RANDOM_WORD,new String[]{},1);
if (v.size()>0) {
String[] arrTemp = (String[])v.get(0);
retStr = arrTemp[0];
}
return retStr;
}

/**
* Get the answer by question
* @param argContent String
* @return String
*/
public String selectQuestionByAnswer(String argContent) {
String retStr = DEFAULT_ANSWER;
String[] arrPara = {String.valueOf(argContent)};
Vector v = this.selectVecData(SELECT_A_BY_Q,arrPara,1);
if (v.size()>0) {
String[] arrTemp = (String[]) v.get(0);
retStr = arrTemp[0];
}
// } else {
// this.addQuestion(argContent);
// }
return retStr;
}

public void addQuestion(String argContent) {
String[] arrPara = {String.valueOf(argContent)};
this.updateData(ADD_QUESTION,arrPara);
}

}



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-125242/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10294527/viewspace-125242/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值