00001
00002
00003
00004
00005
00006
00007
00008 package com.declarativa.interprolog.util;
00009 import com.declarativa.interprolog.*;
00010
00012 public class GoalToExecute{
00013 private GoalFromJava goal;
00014 private ResultFromProlog result;
00015 private boolean executing;
00016 private boolean ended;
00017 private boolean firstGoalStatus = false;
00018
00019 public GoalToExecute(GoalFromJava goal){
00020 this.goal = goal;
00021 result=null; ended=false; executing=false;
00022 }
00023
00025 public synchronized ResultFromProlog waitForResult(){
00026 if (ended) return result;
00027 System.out.println("---> Now it will hang here ....");
00028 try { wait();}
00029 catch(InterruptedException e){throw new IPException("Unexpected:"+e);}
00030 if (result==null) throw new IPException("Inconsistency in GoalToExecute");
00031 return result;
00032 }
00033
00034 public synchronized void setResult(ResultFromProlog result){
00035 if (this.result!=null || hasEnded()) {
00036 throw new IPException("Inconsistency in GoalToExecute");
00037 }
00038 this.result=result;
00039 ended=true;
00040 notify();
00041 }
00042
00043 public boolean wasInterrupted(){
00044 return hasEnded() && "interrupted".equals(result.error);
00045 }
00046
00047 public boolean wasAborted(){
00048 return hasEnded() && "aborted".equals(result.error);
00049 }
00050
00051 public synchronized void interrupt(){
00052 raiseError("interrupted");
00053 }
00054
00055 public synchronized void abort(){
00056 raiseError("aborted");
00057 }
00058
00059 private void raiseError(String s){
00060 if (result==null) result = new ResultFromProlog(-1,false,0,null);
00061 result.error=s;
00062 ended=true;
00063 notify();
00064 }
00065
00066 public GoalFromJava getGoal(){ return goal;}
00067
00068 public void prologWasCalled(){
00069 if (executing) throw new IPException("Bad use of prologWasCalled");
00070 executing=true;
00071 }
00072
00073 public boolean hasStarted(){
00074 return executing;
00075 }
00076
00077 public boolean hasEnded(){
00078 return ended;
00079 }
00080
00081 public int getTimestamp(){return goal.timestamp;}
00082
00083 public void setFirstGoalStatus(){
00084 firstGoalStatus = true;
00085 }
00086
00087 public boolean isFirstGoal(){
00088 return firstGoalStatus;
00089 }
00090
00091 public String toString(){
00092 return "ResultFromProlog: timestamp=="+getTimestamp();
00093 }
00094 }