Search in sources :

Example 6 with LengthUnit

use of net.parostroj.timetable.model.units.LengthUnit in project grafikon by jub77.

the class LoadFilter4d21 method checkDiagram.

@Override
public void checkDiagram(TrainDiagram diagram, ModelVersion version) {
    if (version.compareTo(new ModelVersion(4, 21, 0)) <= 0) {
        // convert route unit...
        Attributes attributes = diagram.getAttributes();
        if (attributes.containsKey(TrainDiagram.ATTR_ROUTE_LENGTH_UNIT)) {
            // try to convert to length unit
            String lUnitStr = attributes.get(TrainDiagram.ATTR_ROUTE_LENGTH_UNIT, String.class);
            LengthUnit lUnit = LengthUnit.getByKey(lUnitStr);
            attributes.setRemove(TrainDiagram.ATTR_ROUTE_LENGTH_UNIT, lUnit);
        }
    }
    if (version.compareTo(new ModelVersion(4, 21, 1)) <= 0) {
        // convert train name templates (common)
        diagram.getTrainsData().setTrainNameTemplate(convertForAbbreviation(diagram.getTrainsData().getTrainNameTemplate()));
        diagram.getTrainsData().setTrainCompleteNameTemplate(convertForAbbreviation(diagram.getTrainsData().getTrainCompleteNameTemplate()));
        // in train types
        for (TrainType type : diagram.getTrainTypes()) {
            type.setTrainNameTemplate(convertForAbbreviation(type.getTrainNameTemplate()));
            type.setTrainCompleteNameTemplate(convertForAbbreviation(type.getTrainCompleteNameTemplate()));
        }
    }
}
Also used : Attributes(net.parostroj.timetable.model.Attributes) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) TrainType(net.parostroj.timetable.model.TrainType) LengthUnit(net.parostroj.timetable.model.units.LengthUnit)

Example 7 with LengthUnit

use of net.parostroj.timetable.model.units.LengthUnit in project grafikon by jub77.

the class TrainsHelper method convertLength.

/**
 * converts length in mm to unit specified by train diagram.
 *
 * @param diagram train diagram
 * @param length length
 * @return converted length
 */
public static Integer convertLength(TrainDiagram diagram, Integer length) {
    Integer convertedLength = length;
    if (convertedLength == null)
        return null;
    LengthUnit lengthUnit = diagram.getAttribute(TrainDiagram.ATTR_LENGTH_UNIT, LengthUnit.class);
    if (lengthUnit == LengthUnit.AXLE) {
        Integer lpa = diagram.getAttribute(TrainDiagram.ATTR_LENGTH_PER_AXLE, Integer.class);
        Scale scale = diagram.getAttribute(TrainDiagram.ATTR_SCALE, Scale.class);
        convertedLength = (convertedLength * scale.getRatio()) / lpa;
    } else if (lengthUnit != null) {
        BigDecimal converted = lengthUnit.convertFrom(new BigDecimal(convertedLength), LengthUnit.MM);
        try {
            convertedLength = UnitUtil.convert(converted);
        } catch (ArithmeticException e) {
            log.warn("Couldn't convert value {} to {}: {}", convertedLength, lengthUnit.getKey(), e.getMessage());
        }
    }
    return convertedLength;
}
Also used : LengthUnit(net.parostroj.timetable.model.units.LengthUnit) BigDecimal(java.math.BigDecimal)

Example 8 with LengthUnit

use of net.parostroj.timetable.model.units.LengthUnit in project grafikon by jub77.

the class TrainsHelper method convertWeightToLength.

/**
 * converts weight to length based on conversion ratio and state of the train empty/loaded.
 *
 * @param train train
 * @param weight weight
 * @return length
 */
public static Integer convertWeightToLength(Train train, Integer weight) {
    if (weight == null)
        return null;
    // weight in kg
    TrainDiagram diagram = train.getDiagram();
    Integer wpa = train.getAttributes().getBool(Train.ATTR_EMPTY) ? diagram.getAttribute(TrainDiagram.ATTR_WEIGHT_PER_AXLE_EMPTY, Integer.class) : diagram.getAttribute(TrainDiagram.ATTR_WEIGHT_PER_AXLE, Integer.class);
    LengthUnit lu = diagram.getAttribute(TrainDiagram.ATTR_LENGTH_UNIT, LengthUnit.class);
    // length in mm
    double axles = (double) (weight * 1000) / wpa;
    Integer result = null;
    if (lu == LengthUnit.AXLE) {
        // number of axles should be an even number
        result = (int) axles;
    } else {
        Integer lpa = diagram.getAttribute(TrainDiagram.ATTR_LENGTH_PER_AXLE, Integer.class);
        Scale scale = diagram.getAttribute(TrainDiagram.ATTR_SCALE, Scale.class);
        // length in mm
        result = (int) (axles * lpa);
        // adjust by scale
        result = result / scale.getRatio();
        // convert to unit
        BigDecimal converted = lu.convertFrom(new BigDecimal(result), LengthUnit.MM);
        try {
            result = UnitUtil.convert(converted);
        } catch (ArithmeticException e) {
            log.warn("Couldn't convert value {} to {}: {}", result, lu.getKey(), e.getMessage());
            result = null;
        }
    }
    return result;
}
Also used : LengthUnit(net.parostroj.timetable.model.units.LengthUnit) BigDecimal(java.math.BigDecimal)

Example 9 with LengthUnit

use of net.parostroj.timetable.model.units.LengthUnit in project grafikon by jub77.

the class TrainTimetablesExtractor method getTrainTimetables.

public TrainTimetables getTrainTimetables() {
    List<TrainTimetable> result = new LinkedList<>();
    List<Text> texts = null;
    // trains
    for (Train train : trains) {
        result.add(this.createTimetable(train));
    }
    // texts
    for (TextItem item : diagram.getTextItems()) {
        Text text = this.createText(item);
        if (text != null) {
            if (texts == null) {
                texts = new LinkedList<>();
            }
            texts.add(text);
        }
    }
    TrainTimetables timetables = new TrainTimetables(result);
    timetables.setTexts(texts);
    // routes
    timetables.setRoutes(RoutesExtractor.convert(routes, diagram));
    // route length unit
    LengthUnit unit = diagram.getAttribute(TrainDiagram.ATTR_ROUTE_LENGTH_UNIT, LengthUnit.class);
    if (unit != null) {
        timetables.setRouteLengthUnit(unit);
    }
    // route info (lower priority)
    timetables.setRouteNumbers(diagram.getAttribute(TrainDiagram.ATTR_ROUTE_NUMBERS, String.class));
    timetables.setRouteStations(diagram.getAttribute(TrainDiagram.ATTR_ROUTE_NODES, String.class));
    // validity
    timetables.setValidity(diagram.getAttribute(TrainDiagram.ATTR_ROUTE_VALIDITY, String.class));
    // cycle
    if (cycle != null) {
        DriverCyclesExtractor ex = new DriverCyclesExtractor(diagram, null, false);
        timetables.setCycle(ex.createCycle(cycle));
    }
    return timetables;
}
Also used : TextItem(net.parostroj.timetable.model.TextItem) TranslatedString(net.parostroj.timetable.model.TranslatedString) Train(net.parostroj.timetable.model.Train) LinkedList(java.util.LinkedList) LengthUnit(net.parostroj.timetable.model.units.LengthUnit)

Example 10 with LengthUnit

use of net.parostroj.timetable.model.units.LengthUnit in project grafikon by jub77.

the class StationTimetablesExtractor method getLength.

private LengthInfo getLength(TimeInterval interval) {
    LengthInfo lengthInfo = null;
    Train train = interval.getTrain();
    TrainType trainType = train.getType();
    if (!interval.isLast() && interval.isStop() && trainType != null && trainType.getAttributes().getBool(TrainType.ATTR_SHOW_WEIGHT_INFO)) {
        Pair<Node, Integer> length = getNextLength(interval.getOwnerAsNode(), train, NextType.LAST_STATION);
        // if length was calculated
        if (length != null && length.second != null) {
            // update length with station lengths
            lengthInfo = new LengthInfo();
            lengthInfo.setLength(length.second);
            LengthUnit lengthUnitObj = diagram.getAttribute(TrainDiagram.ATTR_LENGTH_UNIT, LengthUnit.class);
            lengthInfo.setLengthInAxles(LengthUnit.AXLE == lengthUnitObj);
            lengthInfo.setLengthUnit(lengthUnitObj);
            lengthInfo.setStationAbbr(length.first.getAbbr());
        }
    }
    return lengthInfo;
}
Also used : Node(net.parostroj.timetable.model.Node) TrainType(net.parostroj.timetable.model.TrainType) Train(net.parostroj.timetable.model.Train) LengthUnit(net.parostroj.timetable.model.units.LengthUnit)

Aggregations

LengthUnit (net.parostroj.timetable.model.units.LengthUnit)10 BigDecimal (java.math.BigDecimal)4 Node (net.parostroj.timetable.model.Node)2 Train (net.parostroj.timetable.model.Train)2 TrainType (net.parostroj.timetable.model.TrainType)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 IniConfigSection (net.parostroj.timetable.gui.ini.IniConfigSection)1 Attributes (net.parostroj.timetable.model.Attributes)1 StringWithLocale (net.parostroj.timetable.model.LocalizedString.StringWithLocale)1 TextItem (net.parostroj.timetable.model.TextItem)1 TranslatedString (net.parostroj.timetable.model.TranslatedString)1 ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)1 SpeedUnit (net.parostroj.timetable.model.units.SpeedUnit)1 WeightUnit (net.parostroj.timetable.model.units.WeightUnit)1 Pair (net.parostroj.timetable.utils.Pair)1