ExtinguishRequest.java 3.91 KB
Newer Older
Juon Kawakami's avatar
init  
Juon Kawakami committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package firesimulator.simulator;

import firesimulator.util.Configuration;
import firesimulator.world.Building;
import firesimulator.world.FireBrigade;

import org.apache.log4j.Logger;

/**
 * @author tn
 *
 */
public class ExtinguishRequest {
    private static final Logger LOG = Logger.getLogger(ExtinguishRequest.class);
		
    public static final int REASON_OK=1;
    public static final int REASON_OK_VIRTUAL=2;
    public static final int REASON_FB_WAS_NULL=-1;
    public static final int REASON_TO_MUCH_WATER=-2;
    public static final int REASON_TANK_EMPTY=-3;
    public static final int REASON_OUT_OF_RANGE=-4;
    public static final int REASON_NEGATIVE_WATER=-5;//Add by Ali Modaresi To fix using kernel bug
    
    public static final String OK="passed all tests";
    public static final String OK_VIRT="is virtual mode";
    public static final String ER_FB_NULL="firebrigade is null";
    public static final String ER_TO_MUCH="firebrigade is exceeding limt";
    public static final String ER_EMPTY="tank is empty";
    public static final String ER_RANGE="target is out of range";
    public static final String ER_NEGATIVE_WATER="negative water request";//Add by Ali Modaresi To fix using kernel bug
    public static final String UNKNOWN="unknown code ";
    
	public static int MAX_WATER_PER_CYCLE;
	public static int MAX_DISTANCE;
	private FireBrigade source;
	private Building target;
	private int quantity;
	private static boolean DEBUG_VERBOSE=true;
	
	public ExtinguishRequest(FireBrigade source, Building target,int quantity){
		this.target=target;
		this.source=source;
		this.quantity=quantity;
	}
	
	public void verbose(String msg){
	    if(DEBUG_VERBOSE)
                LOG.debug(msg);
	}
	
	public int validate(){
	    if(source==null && Configuration.isActive("virtual"))return REASON_OK_VIRTUAL;
		if(source==null)return REASON_FB_WAS_NULL;	
		if(source.getWaterUsed()+quantity>MAX_WATER_PER_CYCLE){						
			return REASON_TO_MUCH_WATER;
		}
		if(source.getWaterQuantity()<quantity){			
			return REASON_TANK_EMPTY;
		}
		if(distance(source,target)>MAX_DISTANCE)
			return REASON_OUT_OF_RANGE;
		
		if(quantity<0){//Added by Ali Modaresi to fix using kernel bug
			LOG.warn("Using kernel bug.... Extinguish with negative water");//Added by Ali Modaresi to fix using kernel bug
			return REASON_NEGATIVE_WATER;//Added by Ali Modaresi to fix using kernel bug
		}
		return REASON_OK;		
	}
	
	public String getReason(int code){
	    switch (code) {
        case REASON_OK:
            return OK;
        case REASON_OK_VIRTUAL:
        		return OK_VIRT;
        case REASON_FB_WAS_NULL:
        		return ER_FB_NULL;
        case REASON_OUT_OF_RANGE:
        		return ER_RANGE;
        	case REASON_TANK_EMPTY:
        		return ER_EMPTY;
        	case REASON_TO_MUCH_WATER:
        		return ER_TO_MUCH;
        	case REASON_NEGATIVE_WATER://Added by Ali Modaresi to fix using kernel bug
        		return ER_NEGATIVE_WATER;//Added by Ali Modaresi to fix using kernel bug
        default:
            return UNKNOWN+code;
        }
	}
	

	private double distance(FireBrigade source2, Building target2) {
		double x=source2.getX()-target2.getX();
		double y=source2.getY()-target2.getY();
		return Math.sqrt((x*x)+(y*y));
	}

	public boolean execute(){
	    verbose(toString());
	    int result;
		if((result=validate())<0){
		    verbose("ERROR reason = "+getReason(result)+"\n");
		    return false;
		}
		if(source!=null){
			source.addWaterUsed(quantity);
			source.setWaterQuantity(source.getWaterQuantity()-quantity);		
		}
		target.setWaterQuantity(target.getWaterQuantity()+quantity);
		verbose("OK reason = "+getReason(result)+"\n");
		return true;
	}

	public FireBrigade getSource() {
		return source;
	}
	
	public String toString(){
	    String fb;
	    try{
	        fb="fb="+source.getID();
	    }catch (Exception e) {
            fb="fb=null";
        }
		return "extinguish request; "+fb+", target="+target.getID()+", quantity="+quantity+" -> ";
	}

}