首先介绍一下,这是一个ROBOCUP机器人比赛中的一个程序,所以有很多奇特的定义,我会解释。我只是想问个基础的问题。请大家耐心看下,谢谢
SOState.java
public class SOState <AgentType> implements java.io.Serializable{
protected SOSubState subState;
protected Stack<SOSubState> stack;
protected AgentType ownerAgent; public SOState(AgentType ownerAgent){
this.ownerAgent = ownerAgent;
stack = new Stack<SOSubState>();
} public void act() throws yab.agent.Agent.ActionCommandException {
while (subState != null) {
//这是我加的输出,表示如果这是个救火的机器人且它的标志是0的话,则输出
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState1 "+subState +" "+subState.runConditionConfirmed());
}
actSubState();
}
} private void actSubState() throws yab.agent.Agent.ActionCommandException { --------------(3)
if (subState.runConditionConfirmed()) { //这里就是条件是否符合,因为我只问第四秒时的情况,肯定符合
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState2 "+subState +" "+subState.runConditionConfirmed());
//第一行的输出由这里产生
}
try{
subState.act(); --------------(4)
}catch(Exception e){
if(!(e instanceof Agent.ActionCommandException))
e.printStackTrace();
} //System.err.println("Act has not been done!!!" + subState);
throw new yab.agent.Agent.ActionCommandException();
}
else {
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState3 "+subState +" "+subState.runConditionConfirmed());
}
subState = null;
if (!stack.isEmpty())
subState = stack.pop();
}
} protected void setSubState(SOSubState subState) throws yab.agent.Agent.ActionCommandException {
this.subState = subState;
actSubState(); --------------(2)
} protected void stackSubState(SOSubState subState) throws yab.agent.Agent.ActionCommandException {
if (this.subState != null)
stack.push(this.subState); this.subState = subState;
actSubState();
}
}
SOSubState.java
public abstract class SOSubState<AgentType> extends SOState<AgentType> implements java.io.Serializable{
private static final long serialVersionUID = -123456789123531L;
public SOSubState(AgentType ownerAgent){
super(ownerAgent);
} public abstract boolean runConditionConfirmed();}
FireInitialState.java 的片断
public void act() throws Agent.ActionCommandException{
setSubState(new InitialAttackState(ownerAgent)); --------------(1)首先执行
}public class InitialAttackState extends SOSubState <FireBrigadeAgent> 的 片断
public void act() throws Agent.ActionCommandException{ --------------(5) //ownerAgent.logger.log("In Initial Attack Act!"); if(ownerAgent.self().waterQuantity() == 0 && !(subState instanceof GotoRefuge))
stackSubState(new GotoRefuge(ownerAgent)); //因为不满足ownerAgent.self().waterQuantity() == 0被跳过。不要看
if(ownerAgent.self().fireIndex==0)
System.out.println(ownerAgent.self().time()+" in InitialAttackState1 "+subState); //问题就在这,为什么输出subState为NULL
super.act();
if(ownerAgent.self().fireIndex==0)
System.out.println(ownerAgent.self().time()+" in InitialAttackState2 "+subState);
。。
。
(省略)
public boolean runConditionConfirmed(){
return ownerAgent.self().time() <= 17;
}
}我把执行的流程标出来了.
这里我给出了第四秒时的输出 4为第四秒
4 in InSOState2 SOS.fire.substates.InitialAttackState@a32b true
4 in InitialAttackState1 null
4 in InitialAttackState2 null不知道大家能看懂我的意思没,我也不好怎么表述。
SOState.java
public class SOState <AgentType> implements java.io.Serializable{
protected SOSubState subState;
protected Stack<SOSubState> stack;
protected AgentType ownerAgent; public SOState(AgentType ownerAgent){
this.ownerAgent = ownerAgent;
stack = new Stack<SOSubState>();
} public void act() throws yab.agent.Agent.ActionCommandException {
while (subState != null) {
//这是我加的输出,表示如果这是个救火的机器人且它的标志是0的话,则输出
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState1 "+subState +" "+subState.runConditionConfirmed());
}
actSubState();
}
} private void actSubState() throws yab.agent.Agent.ActionCommandException { --------------(3)
if (subState.runConditionConfirmed()) { //这里就是条件是否符合,因为我只问第四秒时的情况,肯定符合
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState2 "+subState +" "+subState.runConditionConfirmed());
//第一行的输出由这里产生
}
try{
subState.act(); --------------(4)
}catch(Exception e){
if(!(e instanceof Agent.ActionCommandException))
e.printStackTrace();
} //System.err.println("Act has not been done!!!" + subState);
throw new yab.agent.Agent.ActionCommandException();
}
else {
if(ownerAgent instanceof FireBrigadeAgent) {
FireBrigadeAgent f =(FireBrigadeAgent)ownerAgent;
if(f.self().fireIndex==0)
System.out.println(f.self().time()+" in InSOState3 "+subState +" "+subState.runConditionConfirmed());
}
subState = null;
if (!stack.isEmpty())
subState = stack.pop();
}
} protected void setSubState(SOSubState subState) throws yab.agent.Agent.ActionCommandException {
this.subState = subState;
actSubState(); --------------(2)
} protected void stackSubState(SOSubState subState) throws yab.agent.Agent.ActionCommandException {
if (this.subState != null)
stack.push(this.subState); this.subState = subState;
actSubState();
}
}
SOSubState.java
public abstract class SOSubState<AgentType> extends SOState<AgentType> implements java.io.Serializable{
private static final long serialVersionUID = -123456789123531L;
public SOSubState(AgentType ownerAgent){
super(ownerAgent);
} public abstract boolean runConditionConfirmed();}
FireInitialState.java 的片断
public void act() throws Agent.ActionCommandException{
setSubState(new InitialAttackState(ownerAgent)); --------------(1)首先执行
}public class InitialAttackState extends SOSubState <FireBrigadeAgent> 的 片断
public void act() throws Agent.ActionCommandException{ --------------(5) //ownerAgent.logger.log("In Initial Attack Act!"); if(ownerAgent.self().waterQuantity() == 0 && !(subState instanceof GotoRefuge))
stackSubState(new GotoRefuge(ownerAgent)); //因为不满足ownerAgent.self().waterQuantity() == 0被跳过。不要看
if(ownerAgent.self().fireIndex==0)
System.out.println(ownerAgent.self().time()+" in InitialAttackState1 "+subState); //问题就在这,为什么输出subState为NULL
super.act();
if(ownerAgent.self().fireIndex==0)
System.out.println(ownerAgent.self().time()+" in InitialAttackState2 "+subState);
。。
。
(省略)
public boolean runConditionConfirmed(){
return ownerAgent.self().time() <= 17;
}
}我把执行的流程标出来了.
这里我给出了第四秒时的输出 4为第四秒
4 in InSOState2 SOS.fire.substates.InitialAttackState@a32b true
4 in InitialAttackState1 null
4 in InitialAttackState2 null不知道大家能看懂我的意思没,我也不好怎么表述。
请仔细注意这里的继承关系和属性关系就明白了.