package trackerModule.sim.tracker;
import java.util.ArrayList;
import trackerModule.core.datastructure.TDB;
import trackerModule.core.datastructure.Unit;
import trackerModule.core.rulestructure.Action;
import trackerModule.core.rulestructure.IMessageObject;
import trackerModule.core.rulestructure.RDB;
import trackerModule.core.rulestructure.Rule_Unit;
/**
* The Class Ship.
*/
public class Ship extends Tracker implements IMessageObject {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -717521612739142208L;
/** The count of fishing ships. */
private static int countOfFishingShips = 0;
/** The count of merchant ships. */
private static int countOfMerchantShips = 0;
/** The time_ fishing. */
int TIME_FISHING = 300;//60;
/** The time_ meeting. */
int TIME_MEETING = 400;//100;
/** The time_ starting. */
int TIME_STARTING = 100;//40;
/** The time_ patrolling. */
int TIME_PATROLING = 300;//70;
/**
* Instantiates a new ship.
*
* @param str the name
*/
public Ship(String str) {
super(str);
}
/**
* ship process <br/>
* 1. Initialize mission<br/>
* 1.1. position <br/>
* 2. run mission<br/>
* 3. exit mission <br/>
* Initialize
*
* @param shipType the ship type
* @param shipLocation the ship location
* @param illicitCargoPlan_shipName the illicit cargo plan_ship name
*/
public void init(String shipType, String shipLocation, String illicitCargoPlan_shipName){
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize ship's TDB
Unit ships = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS");
Unit ship = ships.create(getName());
Unit mission = ship.create("Mission");
Unit departure = ship.create("Departure");
Unit location = ship.create("Location");
Unit destination = ship.create("Destination");
Unit type = ship.create("Type");
Unit InRoute = ship.create("Route");
Unit meeting = ship.create("Meeting");
Unit appearance = ship.create("Appearance");
Unit size = ship.create("Size");
Unit crew = ship.create("Crew");
Unit Latitude = ship.create("Latitude");
Unit Longitude = ship.create("Longitude");
Unit Speed = ship.create("Speed");
Unit soi = ship.create("SOI");
Unit state = ship.create("State");
Unit stateTime = ship.create("StateTime");
Unit stateTimeCount = ship.create("StateTimeCount");
Unit abnormalAction = ship.create("AbnormalAction");
Unit x = ship.create("X");
Unit y = ship.create("Y");
Unit IllicitCargoPlanShip = ship.create("IllicitCargoPlan_Ship");
Unit IllicitCargoPlanArea = ship.create("IllicitCargoPlan_Area");
// new continuous variables
// Unit speedChange = ship.create("speedChange");
// Unit turnRate = ship.create("turnRate");
// Unit propellerTurnCount = ship.create("propellerTurnCount");
// Unit cavitation = ship.create("cavitation");
// Unit shipRCSChange = ship.create("shipRCSChange");
// init the new continuous variables
// speedChange.setData(0);
// turnRate.setData(0);
// propellerTurnCount.setData(0);
// cavitation.setData(0);
// shipRCSChange.setData(0);
soi.setData(0.0);
//set state
state.setData("none");
stateTime.setData(0);
stateTimeCount.setData(0);
Speed.setData(10.0);
//set ship type
if( shipType != null )
type.setData(0, shipType);
else
type.setData(0, getShipType());
//set mission
mission.setData(0, getMission());
//set information
InRoute.setData(0, "Usual");
meeting.setData(0, "none");
IllicitCargoPlanArea.setData(0, "none");
appearance.setData(0, "APP1");
size.setData(0, getSize());
//set initial location
if( shipLocation!= null )
location.setData(shipLocation);
else
location.setData(getLocation());
departure.setData(0,location.getData());
destination.setData(0, getDestination());
//set shipIllicitCargoPlan
if( illicitCargoPlan_shipName != null )
IllicitCargoPlanShip.setData(0, illicitCargoPlan_shipName);
//set location
Double pos[] = getRandomPosition( location.getData() );
x.setData(pos[0]);
y.setData(pos[1]);
}
/**
* Initialize the rule.
*/
public void initRule(){
Unit type = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName()+".Type");
//set rule set
if(type.getData().equalsIgnoreCase("FishingShip")) {
createRulesForNormalFishingShip();
}else
if(type.getData().equalsIgnoreCase("MerchantShip")) {
createRulesForNormalMerchantShip();
}else
if(type.getData().equalsIgnoreCase("BombingFishingShip")) {
createRulesForBombingFishingShip();
}else
if(type.getData().equalsIgnoreCase("BombingMerchantShip")) {
createRulesForBombingMerchantShip();
}else
if(type.getData().equalsIgnoreCase("IllicitCargoMerchantShip")) {
createRulesForIllicitCargoShip();
}else
if(type.getData().equalsIgnoreCase("IllicitCargoFishingShip")) {
createRulesForIllicitCargoShip();
}else
if(type.getData().equalsIgnoreCase("NavyShip")) {
createRulesForNavyShip();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rule instance functions
/**
* New rules moving.
*
* @param sRule the rule unit
* @param strPrevState the previous state
* @param strFirst the first
* @param strSecond the second
* @param Action the action
* @param time the time
* @return the string
*/
String newRulesMoving( Rule_Unit sRule, String strPrevState, String strFirst, String strSecond, String Action, int time ){
String strState1 = sRule.createUniqueStateName(Action + "At" + strFirst);
String strState2 = sRule.createUniqueStateName("movingTo" + strSecond);
Rule_Unit ru;
//rule move to first location ////////////////////////////////////////////////////////
Double posFirst[] = getRandomPosition(strFirst);
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("InArea", posFirst[0], posFirst[1]);
ru.setAction("SetState", strState1, time);
//not
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("NotInArea", posFirst[0], posFirst[1]);
ru.setAction("MoveTo", posFirst[0], posFirst[1]);
//rule fishing at first location ////////////////////////////////////////////////////////
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("TimeFinished");
ru.setAction("SetState", strState2 );
//not
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("NotTimeFinished");
ru.setAction(Action);
return strState2;
}
/**
* New rules.
*
* @return the rule_ unit
*/
Rule_Unit newRules(){
Rule_Unit shipsRule = (Rule_Unit) RDB.This().get("RDBROOT.SHIP_RULE");
return shipsRule.create(getName());
}
/**
* New rules starting.
*
* @param sRule the rule unit
* @param strFirstLocation the first location
* @param time the time
* @return the string
*/
String newRulesStarting(Rule_Unit sRule, String strFirstLocation, int time){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Rule_Unit ru = null;
//rule start
ru = sRule.create();
ru.setCondition("IsGlobalTime", 1 );
ru.setAction( "SetState", "waitingAt"+location.getData(0), time);
//rule wait at a port ////////////////////////////////////////////////////////
ru = sRule.create();
ru.setCondition("IsState", "waitingAt"+location.getData(0));
ru.setCondition("TimeFinished");
ru.setAction("SetState", "movingTo" + strFirstLocation );
//not
ru = sRule.create();
ru.setCondition("IsState", "waitingAt"+location.getData(0));
ru.setCondition("NotTimeFinished");
ru.setAction("Waiting" );
return "movingTo" + strFirstLocation;
}
/**
* New rules ending.
*
* @param sRule the rule unit
* @param strPrevState the previous state
* @param strLocation the location
* @param Action the action
*/
void newRulesEnding( Rule_Unit sRule, String strPrevState, String strLocation, String Action ){
String strState1 = sRule.createUniqueStateName(Action + "At" + strLocation);
Rule_Unit ru;
Double pos[] = getRandomPosition(strLocation);
//rule move to third location ////////////////////////////////////////////////////////
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("InArea", pos[0], pos[1] );
ru.setAction( "SetState", strState1, (int)(Math.random()*10)+30);
//not
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("NotInArea", pos[0], pos[1]);
ru.setAction("MoveTo", pos[0], pos[1]);
//rule fishing at second location ////////////////////////////////////////////////////////
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("TimeFinished");
ru.setAction("SetState", "end");
//not
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("NotTimeFinished");
ru.setAction("Wandering" );
}
/**
* New rules meeting.
*
* @param sRule the rule unit
* @param strPrevState the previous state
* @param strFirst the first
* @param strSecond the second
* @param ship the ship
* @param time the time
* @return the string
*/
String newRulesMeeting( Rule_Unit sRule, String strPrevState, String strFirst, String strSecond, String ship, int time ){
String strState1 = sRule.createUniqueStateName("MeetingAt" + strFirst);
String strState2 = sRule.createUniqueStateName("ContactingAt" + ship);
String strState3 = sRule.createUniqueStateName("WanderingTo" + strFirst);
String strState4 = sRule.createUniqueStateName("movingTo" + strSecond);
Rule_Unit ru;
//move to meeting area
Double posFirst[] = getRandomPosition(strFirst);
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("InArea", posFirst[0], posFirst[1]);
ru.setAction("SetState", strState1, 1);
//not
ru = sRule.create();
ru.setCondition("IsState", strPrevState);
ru.setCondition("NotInArea", posFirst[0], posFirst[1]);
ru.setAction("MoveTo", posFirst[0], posFirst[1]);
//in meeting area, another ship arrived
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("Met", ship);
ru.setAction("SetState", strState2, 1 );
//not
ru = sRule.create();
ru.setCondition("IsState", strState1);
ru.setCondition("Unmet", ship);
ru.setAction("Wandering" );
//to meet each other, move to each ship
ru = sRule.create();
ru.setCondition("IsState", strState2);
ru.setCondition("Contact", ship);
ru.setAction("SetState", strState3, time );
//not
ru = sRule.create();
ru.setCondition("IsState", strState2);
ru.setCondition("Uncontact", ship);
ru.setAction("Contacting", ship );
//after meeting, wandering for a while
ru = sRule.create();
ru.setCondition("IsState", strState3);
ru.setCondition("TimeFinished");
ru.setAction("SetState", strState4 );
//not
ru = sRule.create();
ru.setCondition("IsState", strState3);
ru.setCondition("NotTimeFinished");
ru.setAction("Wandering");
return strState4;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for Normal Fishing Ship
/**
* Creates the rules for normal fishing ship.
*/
void createRulesForNormalFishingShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
//Determine tree ports scenario
String strPrevState = "";
int randSceario = getRandomOnThree();
Rule_Unit sRule = null;
if( randSceario == 0 ){//one port scenario
int randPort = getRandomOnThree();
String strFirst = World.This().mapNearestFishingArea.get(location.getData()).get(randPort);
sRule = newRules();
strPrevState = newRulesStarting( sRule, strFirst, getStartTimeForFishingShip() );
strPrevState = newRulesMoving( sRule, strPrevState, strFirst, location.getData(), "Fishing" , (int)(Math.random()*60)+100);
}else
if( randSceario == 1 ){//two port scenario
int randPort = getRandomOnThree();
String strFirst = World.This().mapNearestFishingArea.get(location.getData()).get(randPort);
int randPort2 = getRandomOnThree(randPort);
String strSecond = World.This().mapNearestFishingArea.get(location.getData()).get(randPort2);
sRule = newRules();
strPrevState = newRulesStarting(sRule, strFirst, getStartTimeForFishingShip() );
strPrevState = newRulesMoving( sRule, strPrevState, strFirst, strSecond, "Fishing", (int)(Math.random()*60)+100 );
strPrevState = newRulesMoving( sRule, strPrevState, strSecond, location.getData(), "Fishing", (int)(Math.random()*60)+100 );
}else{//tree or more port scenario
int randPort = getRandomOnThree();
String strFirst = World.This().mapNearestFishingArea.get(location.getData()).get(randPort);
int randPort2 = getRandomOnThree(randPort);
String strSecond = World.This().mapNearestFishingArea.get(location.getData()).get(randPort2);
int randPort3 = getRandomOnThree(randPort, randPort2);
String strThird = World.This().mapNearestFishingArea.get(location.getData()).get(randPort3);
sRule = newRules();
strPrevState = newRulesStarting(sRule, strFirst, getStartTimeForFishingShip() );
strPrevState = newRulesMoving( sRule, strPrevState, strFirst, strSecond, "Fishing", (int)(Math.random()*60)+100 );
strPrevState = newRulesMoving( sRule, strPrevState, strSecond, strThird, "Fishing", (int)(Math.random()*60)+100 );
strPrevState = newRulesMoving( sRule, strPrevState, strThird, location.getData(), "Fishing", (int)(Math.random()*60)+100 );
}
newRulesEnding( sRule, strPrevState, location.getData(), "Anchoring" );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for Normal Merchant Ship
/**
* Creates the rules for normal merchant ship.
*/
void createRulesForNormalMerchantShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Unit destination = ship.get("Destination");
ArrayList<String> arrayWayPoints;
if( isMerchantArea(location.getData()) || isMerchantArea(destination.getData()) ){
arrayWayPoints = World.This().spaMerchant.getShortestPath(location.getData(), destination.getData());
}else {
arrayWayPoints = World.This().spaArea.getShortestPath(location.getData(), destination.getData());
}
//System.out.println(location.getData() + " to " + destination.getData());
//System.out.println(arrayWayPoints.toString());
Rule_Unit sRule = null;
String strPrevState = "";
String strPrev = "";
int i = 0;
for( String str : arrayWayPoints ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForMerchantShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Moving", 1 );
strPrev = str;
i++;
}
newRulesEnding( sRule, strPrevState, destination.getData(), "Anchoring" );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for Bombing Fishing Ship
/**
* Creates the rules for bombing fishing ship.
*/
void createRulesForBombingFishingShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Unit destination = ship.get("Destination");
ArrayList<String> arrayWayPoints;
arrayWayPoints = World.This().spaUnusualArea.getShortestPath(location.getData(), destination.getData());
//System.out.println(location.getData() + " to " + destination.getData());
//System.out.println(arrayWayPoints.toString());
Rule_Unit sRule = null;
String strPrevState = "";
String strPrev = "";
int i = 0;
for( String str : arrayWayPoints ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForFishingShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Moving", 0 );
strPrev = str;
i++;
}
newRulesEnding( sRule, strPrevState, destination.getData(), "Attacking" );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for Bombing Merchant Ship
/**
* Creates the rules for bombing merchant ship.
*/
void createRulesForBombingMerchantShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Unit destination= ship.get("Destination");
ArrayList<String> arrayWayPoints;
arrayWayPoints = World.This().spaUnusualArea.getShortestPath(location.getData(), destination.getData());
//System.out.println(location.getData() + " to " + destination.getData());
//System.out.println(arrayWayPoints.toString());
Rule_Unit sRule = null;
String strPrevState = "";
String strPrev = "";
int i = 0;
for( String str : arrayWayPoints ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForMerchantShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Moving", 0 );
strPrev = str;
i++;
}
newRulesEnding( sRule, strPrevState, destination.getData(), "Attacking" );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for navy Ship
/**
* Creates the rules for navy ship.
*/
void createRulesForNavyShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Unit destination = ship.get("Destination");
ArrayList<String> arrayWayPoints1;
ArrayList<String> arrayWayPoints2;
arrayWayPoints1 = World.This().spaArea.getShortestPath(location.getData(), destination.getData());
arrayWayPoints2 = World.This().spaArea.getShortestPath(destination.getData(), location.getData());
//System.out.println(location.getData() + " to " + destination.getData());
//System.out.println(arrayWayPoints1.toString());
//System.out.println(arrayWayPoints2.toString());
Rule_Unit sRule = null;
String strPrevState = "";
String strPrev = "";
int i = 0;
int turning = 10; // how many patrol
for( int k = 0; k < turning; k++ ){
for( String str : arrayWayPoints1 ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForMerchantShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Moving", 1 );
strPrev = str;
i++;
}
for( String str : arrayWayPoints2 ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForMerchantShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Moving", 1 );
strPrev = str;
i++;
}
}
newRulesEnding( sRule, strPrevState, destination.getData(), "Anchoring" );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// create rule for Illicit Cargo Ship
/**
* Creates the rules for illicit cargo ship.
*/
void createRulesForIllicitCargoShip(){
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+getName());
Unit location = ship.get("Location");
Unit destination = ship.get("Destination");
Unit IllicitCargoPlanShip = ship.get("IllicitCargoPlan_Ship");
Unit IllicitCargoPlanArea = ship.get("IllicitCargoPlan_Area");
Unit ship2 = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+IllicitCargoPlanShip.getData());
Unit IllicitCargoPlanArea2 = ship2.get("IllicitCargoPlan_Area");
Unit location2 = ship2.get("Location");
String strMeeting = IllicitCargoPlanArea2.getData();
if( IllicitCargoPlanArea.getData().equalsIgnoreCase("none") &&
IllicitCargoPlanArea2.getData().equalsIgnoreCase("none") ){
strMeeting = World.This().getUnusualAreaBetween( location.getData(), location2.getData() );
IllicitCargoPlanArea.setData(strMeeting);
IllicitCargoPlanArea2.setData(strMeeting);
}
ArrayList<String> arrayWayPoints1;
arrayWayPoints1 = World.This().spaUnusualArea.getShortestPath(location.getData(), strMeeting);
ArrayList<String> arrayWayPoints2;
arrayWayPoints2 = World.This().spaUnusualArea.getShortestPath(strMeeting, destination.getData());
//System.out.println(location.getData() + " to " + strMeeting);
//System.out.println(arrayWayPoints1.toString());
//System.out.println(strMeeting + " to " + destination.getData());
//System.out.println(arrayWayPoints2.toString());
Rule_Unit sRule = null;
String strPrevState = "";
String strPrev = "";
int i = 0;
for( String str : arrayWayPoints1 ){
if( i == 0 ){
sRule = newRules();
strPrevState = newRulesStarting( sRule, str, getStartTimeForFishingShip() );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Wandering", 1 );
strPrev = str;
i++;
}
i = 0;
for( String str : arrayWayPoints2 ){
if( i == 0 ){
strPrevState = newRulesMeeting( sRule, strPrevState, strPrev, str, IllicitCargoPlanShip.getData(), (int)(Math.random()*200)+50 );
}
else
strPrevState = newRulesMoving( sRule, strPrevState, strPrev, str, "Wandering", 1 );
strPrev = str;
i++;
}
newRulesEnding( sRule, strPrevState, destination.getData(), "Wandering" );
//sRule.print("");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// condition
/**
* Check condition.
*
* @param ru the rule unit
*/
public void checkCondition(Rule_Unit ru) {
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+ru.getOwnerName());
Unit state = ship.get("State");
Unit stateTime = ship.get("StateTime");
Unit stateTimeCount = ship.get("StateTimeCount");
for( String str : ru.getCondition().keySet() ){
Object[] ob = ru.getCondition().get(str);
if( str.equalsIgnoreCase("IsGlobalTime")){
if( RDB.This().getTime() != (Integer)ob[0] )
return;
}else
if( str.equalsIgnoreCase("IsState") ){
if( !ob[0].equals(state.getData()) )
return;
}else
if( str.equalsIgnoreCase("TimeFinished") ){
//System.out.println(stateTime.getData() + ": " + stateTimeCount.getData());
if( !stateTime.getData().equals(stateTimeCount.getData()) )
return;
}else
if( str.equalsIgnoreCase("NotTimeFinished") ){
if( stateTime.getData().equals(stateTimeCount.getData()) )
return;
}else
if( str.equalsIgnoreCase("InArea") ){
if( !isIn(ru.getOwnerName(), (Double)ob[0], (Double)ob[1] ) )
return;
}else
if( str.equalsIgnoreCase("NotInArea") ){
if( isIn(ru.getOwnerName(), (Double)ob[0], (Double)ob[1] ) )
return;
}else
if( str.equalsIgnoreCase("Met") ){
if( !isMet(ru.getOwnerName(), (String)ob[0] ) )
return;
}else
if( str.equalsIgnoreCase("Unmet") ){
if( isMet(ru.getOwnerName(), (String)ob[0] ) )
return;
}else
if( str.equalsIgnoreCase("Contact") ){
if( !isContact(ru.getOwnerName(), (String)ob[0] ) )
return;
}else
if( str.equalsIgnoreCase("Uncontact") ){
if( isContact(ru.getOwnerName(), (String)ob[0] ) )
return;
}
}
checkAction(ru);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// action
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Check action.
*
* @param ru the rule unit
*/
public void checkAction(Rule_Unit ru) {
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+ru.getOwnerName());
Unit state = ship.get("State");
Unit stateTime = ship.get("StateTime");
Unit stateTimeCount = ship.get("StateTimeCount");
Double rand = Math.random();
Action ac = ru.inProbability(rand);
for( String str : ac.getActionMap().keySet() ){
Object[] ob = ac.getActionMap().get(str);
if( str.equalsIgnoreCase("SetState") ){
// System.out.println(ru.getOwnerName() + ": " + str + ": " + ob[0]);
state.setData(ob[0]);
if( ob.length >= 2 ){
stateTime.setData(ob[1]);
stateTimeCount.setData(0);
}
}else
if( str.equalsIgnoreCase("MoveTo") ){
moveToDestination( ru.getOwnerName(), (Double)ob[0], (Double)ob[1] );
}else
if( str.equalsIgnoreCase("Wandering") ){
wandering(ru.getOwnerName());
}else
if( str.equalsIgnoreCase("Waiting") ){
}else
if( str.equalsIgnoreCase("Contacting") ){
Unit anotherShip = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+(String)ob[0]);
moveToDestination( ru.getOwnerName(), anotherShip.get("X").getDataByDouble(), anotherShip.get("Y").getDataByDouble() );
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// rule is updated
/* (non-Javadoc)
* @see trackerModule.core.rulestructure.IMessageObject#onUpdated(java.lang.Object)
*/
public void onUpdated(Object u) {
Rule_Unit ru = (Rule_Unit)u;
if( !ru.getName().equalsIgnoreCase("RDBROOT") &&
!ru.getName().equalsIgnoreCase("SHIP_RULE") &&
!ru.getParent().getName().equalsIgnoreCase("SHIP_RULE"))
checkCondition(ru);
else
if( !ru.getName().equalsIgnoreCase("RDBROOT") &&
!ru.getName().equalsIgnoreCase("SHIP_RULE") ){
//count state time
Unit ship = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+ru.getName());
Unit stateTimeCount = ship.get("StateTimeCount");
stateTimeCount.setData(stateTimeCount.getDataByInt()+1);
// stateTimeCount.print("");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Get Functions
/**
* Gets the location.
*
* @return the location
*/
public String getLocation(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS." + strName + ".Location").getData();
if( str == null ){
if( getShipType().equalsIgnoreCase("MerchantShip") ){
return World.This().getRandomBigPortWithMerchantArea();
}else
if( getShipType().equalsIgnoreCase("BombingMerchantShip") ||
getShipType().equalsIgnoreCase("IllicitCargoMerchantShip") ){
return World.This().getRandomBigPort();
}else{
Double r = Math.random();
if( r > 0.1 )
return World.This().getRandomSmallPort();
else
return World.This().getRandomBigPort();
}
}
return str;
}
/**
* Gets the ship type.
*
* @return the ship type
*/
public String getShipType(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Type").getData();
if( str == null ){
double r = Math.random();
if( r < 0.85 )
str = "FishingShip";
else
if( 0.85 < r && r <= 0.95 )
str = "MerchantShip";
else
if( 0.95 < r && r <= 0.98 )
str = "BombingFishingShip";
else
if( 0.98 < r && r <= 0.988 )
str = "BombingMerchantShip";
else
if( 0.988 < r && r <= 0.99 )
str = "IllicitCargoMerchantShip";
else
str = "IllicitCargoFishingShip";
}
return str;
}
/**
* Gets the start time.
*
* @return the start time
*/
public String getStartTime(){
String str;
Integer actionTime = (int)(Math.random()*TIME_STARTING)+TIME_STARTING;
return actionTime.toString();
}
/**
* Gets the mission.
*
* @return the mission
*/
public String getMission(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Mission").getData();
if( str == null ){
String strType = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Type").getData();
if( strType.equals("IllicitCargoMerchantShip") ||
strType.equals("MerchantShip") ||
strType.equals("BombingMerchantShip") ){
str = "Shipping";
}else
if( strType.equals("IllicitCargoFishingShip") ||
strType.equals("FishingShip") ||
strType.equals("BombingFishingShip")){
str = "Fishing";
}else
str = "Searching";
}
return str;
}
/**
* Gets the size.
*
* @return the size
*/
public String getSize(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Size").getData();
if( str == null ){
String strType = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Type").getData();
if( strType.equals("IllicitCargoMerchantShip") ||
strType.equals("MerchantShip") ||
strType.equals("BombingMerchantShip") ){
Double r = Math.random();
if( r < 0.8)
str = "Small";
else
str = "Big";
}else
if( strType.equals("IllicitCargoFishingShip") ||
strType.equals("FishingShip") ||
strType.equals("BombingFishingShip")){
Double r = Math.random();
if( r < 0.8)
str = "Big";
else
str = "Small";
}else
str = "Big";
}
return str;
}
/**
* Gets the destination.
*
* @return the destination
*/
public String getDestination(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Destination").getData();
String strLoc = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Location").getData();
Unit uSize = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Size");
if( str == null ){
String strType = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Type").getData();
if( strType.equals("MerchantShip") ){
str = World.This().getRandomBigPortWithMerchantArea(strLoc);
Double r = Math.random();
String newSize = "";
if(isMerchantArea(str)){
if( r < 0.6)
newSize = "Big";
else
newSize = "Small";
}else{
if( r < 0.3)
newSize = "Big";
else
newSize = "Small";
}
uSize.setData(0, newSize);
}else
if( strType.equals("IllicitCargoMerchantShip") ||
strType.equals("BombingMerchantShip") ){
str = World.This().getRandomBigPort(strLoc);
Double r = Math.random();
String newSize = "";
if(isMerchantArea(str)){
if( r < 0.6)
newSize = "Big";
else
newSize = "Small";
}else{
if( r < 0.3)
newSize = "Big";
else
newSize = "Small";
}
uSize.setData(0, newSize);
}else
if( strType.equals("IllicitCargoFishingShip") ||
strType.equals("FishingShip") ||
strType.equals("BombingFishingShip")){
str = World.This().getRandomSmallPort(strLoc);
}else
str = World.This().getRandomSmallPort(strLoc);
}
return str;
}
/**
* Gets the x.
*
* @return the x
*/
public String getX(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".X").getData();
if( str == null ){
double r = Math.random();
if( r < 0.5 )
str = "10";
else
str = "20";
}
return str;
}
/**
* Gets the y.
*
* @return the y
*/
public String getY(){
String str = TDB.This().get("TDBROOT.MODEL.WORLD.SHIPS."+strName + ".Y").getData();
if( str == null ){
double r = Math.random();
if( r < 0.5 )
str = "10";
else
str = "20";
}
return str;
}
//by 3 ships per one hour, this gives the random time
/**
* Gets the start time for fishing ship.
*
* @return the start time for fishing ship
*/
public int getStartTimeForFishingShip(){
countOfFishingShips++;
int time = countOfFishingShips/4;
time = (int)(Math.random()*60)+time*60;
return time;
}
/**
* Gets the start time for merchant ship.
*
* @return the start time for merchant ship
*/
public int getStartTimeForMerchantShip(){
countOfMerchantShips++;
int time = countOfMerchantShips/3;
time = (int)(Math.random()*60)+time*60;
return time;
}
}