Commit 7a9c7444 authored by Vladislav Kiselev's avatar Vladislav Kiselev

init

parents
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Berth {
private int id;
private String name;
private boolean isRaid;
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public boolean getIsRaid() {
return isRaid;
}
public void setIsRaid(boolean isRaid) {
this.isRaid = isRaid;
}
public Berth(int id, String name, boolean isRaid) {
this.id = id;
this.name = name;
this.isRaid = isRaid;
}
public Berth() {
}
@Override
public String toString() {
return id + ";" + name;
}
public Berth(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Bunker extends MovingObject {
public Bunker(int id, String name) {
super(id, name);
}
public Bunker() {
super( );
}
@Override
public String toString() {
return getId() + ";" + getName();
}
public Bunker(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Cargo {
private double cost;
private int id;
private String name;
/**
* Get the value of cost
*
* @return the value of cost
*/
public double getCost() {
return cost;
}
/**
* Set the value of cost
*
* @param cost new value of cost
*/
public void setCost(double cost) {
this.cost = cost;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Cargo(double cost, int id, String name) {
this.cost = cost;
this.id = id;
this.name = name;
}
public Cargo() {
}
@Override
public String toString() {
return id + ";" + name + ";" + cost;
}
public Cargo(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
cost = 0; //Double.parseDouble(tokens[2]);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
*
* @author topazh_ag
*/
public class CargoFlow {
private Storage storage;
private Cargo cargo;
Map<Double, Double> flow;
public Map<Double, Double> getFlow() {
return flow;
}
public void setFlow(Map<Double, Double> flow) {
this.flow = flow;
}
/**
* Get the value of storage
*
* @return the value of storage
*/
public Storage getStorage() {
return storage;
}
/**
* Set the value of storage
*
* @param storage new value of storage
*/
public void setStorage(Storage storage) {
this.storage = storage;
}
public Cargo getCargo() {
return cargo;
}
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
public CargoFlow() {
this.flow = new HashMap<>();
}
public CargoFlow(Storage storage, Cargo cargo) {
this.storage = storage;
this.cargo = cargo;
this.flow = new HashMap<>();
}
public double getCurrentValue(double forTime)
{
double res = 0.0;
boolean isFound = false;
Set<Double> keyTimes = flow.keySet();
double prevKey = -1.0;
for (Double keyTime : keyTimes)
{
if (forTime>prevKey && forTime<keyTime)
{
res = flow.get(prevKey);
isFound = true;
break;
} else
prevKey = keyTime;
}
if (!isFound && forTime>prevKey)
res = flow.get(prevKey);
return res;
}
public double getTotalValue(double forTime)
{
double res = 0.0;
boolean isFound = false;
Set<Double> keyTimes = flow.keySet();
double prevKey = -1.0;
for (Double keyTime : keyTimes)
{
if (forTime>prevKey && forTime<keyTime)
{
res += flow.get(prevKey)*(forTime-prevKey);
isFound = true;
break;
} else
{
res += flow.get(prevKey)*(keyTime-prevKey);
prevKey = keyTime;
}
}
if (!isFound && forTime>prevKey)
res += flow.get(prevKey)*(forTime-prevKey);
return res;
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Double s : flow.keySet())
{
if (!first)
res += "," + s+ ":" + flow.get(s);
else
res += s + ":" + flow.get(s);
first = false;
}
return storage.getId() + ";[" + res +"]";
}
public CargoFlow(String s, Map<Integer, Storage> mp, Map<Integer, Cargo> cp) {
this.flow = new HashMap<>();
String[] tokens = s.split(";");
int key = Integer.parseInt(tokens[0].trim());
storage = mp.get(key);
key = Integer.parseInt(tokens[1].trim());
cargo = cp.get(key);
String[] rs = tokens[2].trim().replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
String[] kv = crs.split(":");
Double key1 = Double.parseDouble(kv[0].trim());
Double value1 = Double.parseDouble(kv[1].trim());
flow.put(key1, value1);
}
}
}
This diff is collapsed.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class LoadingEquipment extends MovingObject{
public LoadingEquipment() {
super();
}
public LoadingEquipment(int id, String name) {
super(id, name);
}
@Override
public String toString() {
return getId() + ";" + getName();
}
public LoadingEquipment(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author topazh_ag
*/
public class LoadingTemplate extends OperationTemplate {
private TransportShip loader;
private Storage storage;
private List<LoadingEquipment> resources;
private double intensity;
/**
* Get the value of resources
*
* @return the value of resources
*/
public List<LoadingEquipment> getResources() {
return resources;
}
/**
* Set the value of resources
*
* @param resources new value of resources
*/
public void setResources(List<LoadingEquipment> resources) {
this.resources = resources;
}
public TransportShip getLoader() {
return loader;
}
public void setLoader(TransportShip loader) {
this.loader = loader;
}
public double getIntensity() {
return intensity;
}
public void setIntensity(double intensity) {
this.intensity = intensity;
}
public Storage getStorage() {
return storage;
}
public void setStorage(Storage storage) {
this.storage = storage;
}
public LoadingTemplate(TransportShip loader, Berth berth, Storage storage, double intensity, int id) {
super(id, berth);
this.loader = loader;
this.storage = storage;
this.resources = new ArrayList<>();
this.intensity = intensity;
}
public LoadingTemplate() {
this.resources = new ArrayList<>();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(LoadingEquipment eq : resources)
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
int source = loader.getId();
if (intensity>0)
source = storage.getId();
int target = loader.getId();
if (intensity<=0)
target = storage.getId();
int cargos = storage.getCargo().getId();
return getId() + ";" + "loa;" + twtoString() + ";" + source + ";" + cargos + ";" + target + ";" + getStartLocation().getId() + ";[" + res +"];"+Math.abs(intensity);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MooringTemplate extends TowUsingTemplate {
private TransportShip moorer;
private boolean direct;
/**
* Get the value of direction
*
* @return the value of direction
*/
public boolean isDirect() {
return direct;
}
/**
* Set the value of direction
*
* @param direction new value of direction
*/
public void setDirect(boolean direct) {
this.direct = direct;
}
/**
* Get the value of moorer
*
* @return the value of moorer
*/
public MovingObject getMoorer() {
return moorer;
}
/**
* Set the value of moorer
*
* @param mover new value of moorer
*/
public void setMoorer(TransportShip moorer) {
this.moorer = moorer;
}
public MooringTemplate(TransportShip moorer, Berth berth, double duration, boolean direct, int id) {
super(duration, id, berth);
this.moorer = moorer;
this.direct = direct;
}
public MooringTemplate() {
super();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : getResources())
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
String code = "mrn";
if (!direct)
code = "unm";
return getId() + ";" + code + ";" + twtoString() + ";" + moorer.getId() + ";" + getStartLocation().getId() + ";[" + res +"];"+getDuration();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingObject {
private int id;
private String name;
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public MovingObject(int id, String name) {
this.id = id;
this.name = name;
}
public MovingObject() {
}
public MovingObject(String s) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingObjectState {
private MovingObject vessel;
private Berth location;
/**
* Get the value of location
*
* @return the value of location
*/
public Berth getLocation() {
return location;
}
/**
* Set the value of location
*
* @param location new value of location
*/
public void setLocation(Berth location) {
this.location = location;
}
/**
* Get the value of vessel
*
* @return the value of vessel
*/
public MovingObject getVessel() {
return vessel;
}
/**
* Set the value of vessel
*
* @param vessel new value of vessel
*/
public void setVessel(MovingObject vessel) {
this.vessel = vessel;
}
/**
*
* @param vessel
* @param location
*/
public MovingObjectState(MovingObject vessel, Berth location) {
this.vessel = vessel;
this.location = location;
}
public MovingObjectState() {
}
@Override
public String toString() {
return getVessel().getId() + ";" + getLocation().getId();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class MovingTemplate extends TowUsingTemplate {
private MovingObject mover;
private Berth destination;
/**
* Get the value of destination
*
* @return the value of destination
*/
public Berth getDestination() {
return destination;
}
/**
* Set the value of destination
*
* @param destination new value of destination
*/
public void setDestination(Berth destination) {
this.destination = destination;
}
/**
* Get the value of mover
*
* @return the value of mover
*/
public MovingObject getMover() {
return mover;
}
/**
* Set the value of mover
*
* @param mover new value of mover
*/
public void setMover(MovingObject mover) {
this.mover = mover;
}
public MovingTemplate(MovingObject mover, Berth source, Berth destination, double duration, int id) {
super(duration, id, source);
this.mover = mover;
this.destination = destination;
}
public MovingTemplate() {
super();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : getResources())
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
return getId() + ";" + "mov;" + twtoString() + ";" + mover.getId() + ";" + getStartLocation().getId() + ";" + destination.getId() + ";[" + res +"];"+getDuration();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Operation {
private OperationTemplate template;
private double start;
private double duration;
/**
* Get the value of duration
*
* @return the value of duration
*/
public double getDuration() {
return duration;
}
/**
* Set the value of duration
*
* @param duration new value of duration
*/
public void setDuration(double duration) {
this.duration = duration;
}
/**
* Get the value of start
*
* @return the value of start
*/
public double getStart() {
return start;
}
/**
* Set the value of start
*
* @param start new value of start
*/
public void setStart(double start) {
this.start = start;
}
/**
* Get the value of template
*
* @return the value of template
*/
public OperationTemplate getTemplate() {
return template;
}
/**
* Set the value of template
*
* @param template new value of template
*/
public void setTemplate(OperationTemplate template) {
this.template = template;
}
public Operation() {
}
@Override
public String toString() {
return template.getId() + "; R; " + start + "; " + duration;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public abstract class OperationTemplate {
private int id;
private Berth startLocation;
Map<Double, Double> timeWindows;
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Berth getStartLocation() {
return startLocation;
}
public void setStartLocation(Berth startLocation) {
this.startLocation = startLocation;
}
public Map<Double, Double> getTimeWindows() {
return timeWindows;
}
public void setBanWindows(Map<Double, Double> timeWindows) {
this.timeWindows = timeWindows;
}
protected String twtoString() {
String res = "";
boolean first = true;
for(Double s : timeWindows.keySet())
{
if (!first)
res += "," + s + ":" + timeWindows.get(s);
else
res += s + ":" + timeWindows.get(s);
first = false;
}
return "[" + res +"]";
}
public OperationTemplate(int id, Berth startLocation) {
this.id = id;
this.startLocation = startLocation;
this.timeWindows = new HashMap<>();
}
public OperationTemplate() {
this.timeWindows = new HashMap<>();
}
public void setTimeWindow(String s) {
this.timeWindows = new HashMap<>();
String[] rs = s.replace("[", "").replace("]", "").split(",");
for (String crs : rs)
if (crs.length()>0)
{
String[] kv = crs.split(":");
Double key1 = Double.parseDouble(kv[0]);
Double value1 = Double.parseDouble(kv[1]);
timeWindows.put(key1, value1);
}
}
}
package inport;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamResult;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author topazh_ag
*/
public class SerializationHelper {
public String serialize(Object object) throws Exception{
StringWriter resultWriter = new StringWriter();
StreamResult result = new StreamResult( resultWriter );
XMLStreamWriter xmlStreamWriter =
XMLOutputFactory.newInstance().createXMLStreamWriter(result);
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(new JAXBElement(new QName(object.getClass().getSimpleName()), object.getClass(), object), xmlStreamWriter);
String res = resultWriter.toString();
return res;
}
public Object deserialize(String str, Class klass) throws Exception{
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is);
reader = new CamelCaseTransfomingReaderDelegate(reader, klass);
JAXBContext context = JAXBContext.newInstance(klass);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement elem = unmarshaller.unmarshal(reader, klass);
Object object = elem.getValue();
return object;
}
//adapts to Java property naming style
private static class CamelCaseTransfomingReaderDelegate extends StreamReaderDelegate {
Class klass = null;
public CamelCaseTransfomingReaderDelegate(XMLStreamReader xsr, Class klass) {
super(xsr);
this.klass = klass;
}
@Override
public String getLocalName() {
String nodeName = super.getLocalName();
if (!nodeName.equals(klass.getSimpleName()))
{
nodeName = nodeName.substring(0, 1).toLowerCase() +
nodeName.substring(1, nodeName.length());
}
return nodeName.intern(); //NOTE: intern very important!..
}
}
}
\ No newline at end of file
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public class Storage {
private int id;
private String name;
private double volume;
private Cargo cargo;
/**
* Get the value of cargo
*
* @return the value of cargo
*/
public Cargo getCargo() {
return cargo;
}
/**
* Set the value of cargo
*
* @param cargo new value of cargo
*/
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
/**
* Get the value of volume
*
* @return the value of volume
*/
public double getVolume() {
return volume;
}
/**
* Set the value of volume
*
* @param volume new value of volume
*/
public void setVolume(double volume) {
this.volume = volume;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName() {
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the value of id
*
* @return the value of id
*/
public int getId() {
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id) {
this.id = id;
}
public Storage(int id, String name, double volume, Cargo cargo) {
this.id = id;
this.name = name;
this.volume = volume;
this.cargo = cargo;
}
public Storage() {
}
@Override
public String toString() {
return id + ";" + name + ";" + cargo.getId() + ";" +volume;
}
public Storage(String s, Map<Integer, Cargo> cargoes) {
String[] tokens = s.split(";");
id = Integer.parseInt(tokens[0].trim());
name = tokens[1].trim();
volume = Double.parseDouble(tokens[3].trim());
int key = Integer.parseInt(tokens[2].trim());
cargo = cargoes.get(key);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.Map;
/**
*
* @author topazh_ag
*/
public class StorageState {
private Object storage;
private Cargo cargo;
private double cargoState;
/**
* Get the value of storage
*
* @return the value of storage
*/
public Object getStorage() {
return storage;
}
/**
* Set the value of storage
*
* @param storage new value of storage
*/
public void setStorage(Object storage) {
this.storage = storage;
}
public Cargo getCargo() {
return cargo;
}
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
/**
* Get the value of cargoState
*
* @return the value of cargoState
*/
public double getCargoState() {
return cargoState;
}
/**
* Set the value of cargoState
*
* @param cargoState new value of cargoState
*/
public void setCargoState(double cargoState) {
this.cargoState = cargoState;
}
/**
*
* @param storage
*/
public StorageState(Object storage, Cargo cargo, double cargoState) {
this.storage = storage;
this.cargo = cargo;
this.cargoState = cargoState;
}
public StorageState() {
}
@Override
public String toString() {
if (storage instanceof Storage)
return cargo.getId() + ";" + ((Storage)storage).getId() + ";" + cargoState;
if (storage instanceof TransportShip)
return cargo.getId() + ";" + ((TransportShip)storage).getId() + ";" + cargoState;
return "";
}
public StorageState(String s, Map<Integer, Storage> mp, Map<Integer, MovingObject> vp, Map<Integer, Cargo> cp) {
String[] tokens = s.split(";");
int key = Integer.parseInt(tokens[0].trim());
cargo = cp.get(key);
key = Integer.parseInt(tokens[1].trim());
if (mp.containsKey(key))
storage = mp.get(key);
if (vp.containsKey(key))
storage = vp.get(key);
cargoState = Double.parseDouble(tokens[2].trim());
}
}
This diff is collapsed.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class Tow extends MovingObject {
public Tow(int id, String name) {
super(id, name);
}
public Tow() {
}
@Override
public String toString() {
return getId() + ";" + getName() + ";1000000";
}
public Tow(String s) {
super(s);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author topazh_ag
*/
public abstract class TowUsingTemplate extends OperationTemplate {
private List<Tow> resources;
private double duration;
/**
* Get the value of resources
*
* @return the value of resources
*/
public List<Tow> getResources() {
return resources;
}
/**
* Set the value of resources
*
* @param resources new value of resources
*/
public void setResources(List<Tow> resources) {
this.resources = resources;
}
/**
* Get the value of duration
*
* @return the value of duration
*/
public double getDuration() {
return duration;
}
/**
* Set the value of duration
*
* @param duration new value of duration
*/
public void setDuration(double duration) {
this.duration = duration;
}
public TowUsingTemplate(double duration, int id, Berth startLocation) {
super(id, startLocation);
this.resources = new ArrayList<>();
this.duration = duration;
}
public TowUsingTemplate() {
this.resources = new ArrayList<>();
}
@Override
public String toString() {
String res = "";
boolean first = true;
for(Tow eq : resources)
{
if (!first)
res += "," + eq.getId();
else
res += eq.getId();
first = false;
}
return getId() + ";" + "tut" + ";" + ";[" + res +"];"+duration;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package inport;
/**
*
* @author topazh_ag
*/
public class TransportShip extends MovingObject {
private double cargoMax;
public double getCargoMax() {
return cargoMax;
}
public void setCargoMax(double cargoMax) {
this.cargoMax = cargoMax;
}
public TransportShip(int id, String name, double cargoMax) {
super(id, name);
this.cargoMax = cargoMax;
}
public TransportShip() {
}
@Override
public String toString() {
return getId() + ";" + getName() + ";" + cargoMax;
}
public TransportShip(String s) {
super(s);
String[] tokens = s.split(";");
cargoMax = Double.parseDouble(tokens[2].trim());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment