/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package inport; import inport.ConversionUtils.Utils; import java.util.*; /** * * @author topazh_ag */ public class Operation { private OperationTemplate template; private boolean fixation = false; private double start; private double duration; private MovingObject executor; private Optional bunker = Optional.empty(); private Optional intensity = Optional.empty(); private List resources; public void setIntensity(Optional intensity) { this.intensity = intensity; } public Optional getIntensity() { return intensity; } public Optional getBunker() { return bunker; } public void setBunker(Optional bunker) { this.bunker = bunker; } public boolean getFixation() { return fixation; } public void setFixation(boolean fixation) { this.fixation = fixation; } public List getResources() { return resources; } public void setResources(List resources) { this.resources = resources; } public MovingObject getExecutor() { return executor; } public void setExecutor(MovingObject executor) { this.executor = executor; } /** * 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() { } private static String getTransportName(MovingObject obj) { if (obj instanceof Bunker) { return "бункеровщик " + obj.getId(); } if (obj instanceof TransportShip) { return "судно " + obj.getId(); } if (obj instanceof Tow) { return "буксир " + obj.getId(); } return (obj.getName().isEmpty()) ? "оборудование " + obj.getId() : obj.getName(); } private static String getShortTransportName(MovingObject obj) { if ((obj instanceof TransportShip) || (obj instanceof Tow)) { return Integer.toString(obj.getId()); } return (obj.getName().isEmpty()) ? Integer.toString(obj.getId()) : obj.getName(); } private static String firstCharToUppercase(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); } private static String resourcesToString(List resources) { return "{" + String.join(", ", Utils.map(resources, Operation::getShortTransportName)) + "}"; } @Override public String toString() { return toString(false, null); } public String toString(boolean includeAnnotations, ArrayList maxSizes) { ArrayList components = getComponentsOfStringValue(includeAnnotations); StringBuilder sb = new StringBuilder(); for (int compNo = 0; compNo < components.size(); compNo++) { sb.append(components.get(compNo)); if (maxSizes != null) { for (int delta = maxSizes.get(compNo) - components.get(compNo).length(); delta > 0; delta--) { sb.append(" "); } } } return sb.toString(); } public ArrayList getComponentsOfStringValue(boolean includeAnnotations) { ArrayList res = new ArrayList<>(); if (executor == null) { executor = Utils.getExecutor(template); } if (resources == null) { resources = Utils.getResources(template); } StringBuilder sb = new StringBuilder(); res.add(Integer.toString(template.getId())); res.add("; " + (fixation ? "F" : "R")); res.add("; " + start); res.add("; " + duration); sb.append(" (").append(executor.getId()).append(" "); sb.append(bunker.map(b -> b.getId() + " ").orElse("")).append("["); boolean isFirst = true; for (MovingObject obj : resources) { if (isFirst) { isFirst = false; } else { sb.append(", "); } sb.append(obj.getId()); } sb.append("]"); sb.append(intensity.map(i -> " " + i).orElse("")); sb.append(")"); res.add(sb.toString()); sb = new StringBuilder(); if (includeAnnotations) { sb.append(" \\* "); if (template instanceof MovingTemplate) { MovingTemplate o = (MovingTemplate) template; sb.append(firstCharToUppercase(getTransportName(executor)) + " идёт к причалу " + o.getDestination().getId() + " от причала " + o.getStartLocation().getId()); if (! resources.isEmpty()) { sb.append(" используя буксиры " + resourcesToString(resources)); } sb.append("."); } else if (template instanceof LoadingTemplate) { LoadingTemplate o = (LoadingTemplate) template; sb.append(firstCharToUppercase(getTransportName(executor))); sb.append(" принимает груз " + o.getCargo().getId()); if (bunker.isPresent()) { sb.append(" из бункеровщика " + bunker.get().getId()); } else { sb.append(" из хранилища " + o.getStorage().getId()); } sb.append(" с интенсивностью " + intensity.get()); sb.append(" у причала " + o.getStartLocation().getId()); if (! resources.isEmpty()) { sb.append(" используя оборудование " + resourcesToString(resources)); } sb.append("."); } else if (template instanceof MooringTemplate) { MooringTemplate o = (MooringTemplate) template; sb.append(firstCharToUppercase(getTransportName(executor))); if (o.isDirect()) { sb.append(" швартуется к причалу "); } else { sb.append(" отшвартовывается от причала "); } sb.append(o.getStartLocation().getId()); if (! resources.isEmpty()) { sb.append(" используя буксиры " + resourcesToString(resources)); } sb.append("."); } } res.add(sb.toString()); return res; } public static ArrayList getMaxSizesOfComponents(Collection operations, boolean includeAnnotations) { ArrayList res = new ArrayList<>(); for (Operation operation : operations) { ArrayList components = operation.getComponentsOfStringValue(includeAnnotations); for (int i = 0; i < components.size(); i++) { if (i >= res.size()) { res.add(0); } res.set(i, Math.max(res.get(i), components.get(i).length())); } } return res; } public Operation(String str, Map m_vessel, Map m_bunker, Map m_equipment, Map m_template) { String lStr = str.substring(0, str.indexOf('(')).trim(); String rStr = str.substring(str.indexOf('(') + 1, str.indexOf(')')).trim(); { String[] items = lStr.split(";"); setStart(Double.parseDouble(items[2].trim())); setDuration(Double.parseDouble(items[3].trim())); setTemplate(m_template.get(Integer.parseInt(items[0].trim()))); setFixation(items[1].trim().equals("F")); } { String[] items = rStr.substring(rStr.indexOf('[') + 1, rStr.indexOf(']')).split(","); ArrayList resources = new ArrayList<>(); for (String item : items) { if (item.trim().isEmpty()) { continue; } resources.add(m_equipment.get(Integer.valueOf(item.trim()))); } setResources(resources); } { String[] items = rStr.substring(0, rStr.indexOf('[')).split(" "); int id = Integer.valueOf(items[0].trim()); MovingObject ex; if (m_bunker.containsKey(id)) { ex = m_bunker.get(id); } else { ex = m_vessel.get(id); } setExecutor(ex); if (items.length > 1) { setBunker(Optional.of(m_bunker.get(Integer.valueOf(items[1].trim())))); } } String intensity = rStr.substring(rStr.indexOf(']') + 1).trim(); if (! intensity.isEmpty()) { setIntensity(Optional.of(Integer.valueOf(intensity))); } } }