All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.planner.examples.traindesign.domain.RailNode Maven / Gradle / Ivy

/*
 * Copyright 2011 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.planner.examples.traindesign.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.drools.planner.examples.common.domain.AbstractPersistable;
import org.drools.planner.examples.traindesign.domain.solver.RailNodeShortestPath;
import org.drools.planner.examples.traindesign.domain.solver.RailPath;

@XStreamAlias("RailNode")
public class RailNode extends AbstractPersistable implements Comparable {

    private String code;
    private int blockSwapCost;

    private List originatingRailArcList;
    @XStreamOmitField
    private Map shortestPathMap;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getBlockSwapCost() {
        return blockSwapCost;
    }

    public void setBlockSwapCost(int blockSwapCost) {
        this.blockSwapCost = blockSwapCost;
    }

    public List getOriginatingRailArcList() {
        return originatingRailArcList;
    }

    public void setOriginatingRailArcList(List originatingRailArcList) {
        this.originatingRailArcList = originatingRailArcList;
    }

    public Map getShortestPathMap() {
        return shortestPathMap;
    }

    public RailNodeShortestPath getShortestPathTo(RailNode other) {
        return shortestPathMap.get(other);
    }

    public void setShortestPathMap(Map shortestPathMap) {
        this.shortestPathMap = shortestPathMap;
    }

    public int compareTo(RailNode other) {
        return new CompareToBuilder()
                .append(id, other.id)
                .toComparison();
    }

    @Override
    public String toString() {
        return code;
    }

    public void initializeShortestPathMap(List railNodeList) {
        shortestPathMap = new HashMap(
                railNodeList.size());
        // Dijkstra algorithm
        List unvisitedShortestPathList = new ArrayList(
                railNodeList.size());

        RailNodeShortestPath originShortestPath = new RailNodeShortestPath();
        originShortestPath.setOrigin(this);
        originShortestPath.setDestination(this);
        originShortestPath.setDistance(0);
        originShortestPath.resetRailPathList();
        RailPath originRailPath = new RailPath(new ArrayList(0));
        originShortestPath.addRailPath(originRailPath);
        shortestPathMap.put(this, originShortestPath);
        unvisitedShortestPathList.add(originShortestPath);

        while (!unvisitedShortestPathList.isEmpty()) {
            RailNodeShortestPath campingShortestPath = unvisitedShortestPathList.remove(0);
            for (RailArc nextRailArc : campingShortestPath.getDestination().getOriginatingRailArcList()) {
                RailNode nextNode = nextRailArc.getDestination();
                int nextDistance = campingShortestPath.getDistance() + nextRailArc.getDistance();

                RailNodeShortestPath nextShortestPath = shortestPathMap.get(nextNode);
                if (nextShortestPath == null) {
                    nextShortestPath = new RailNodeShortestPath();
                    nextShortestPath.setOrigin(this);
                    nextShortestPath.setDestination(nextNode);
                    nextShortestPath.setDistance(Integer.MAX_VALUE);
                    shortestPathMap.put(nextNode, nextShortestPath);
                    unvisitedShortestPathList.add(nextShortestPath);
                }
                if (nextDistance <= nextShortestPath.getDistance()) {
                    if (nextDistance < nextShortestPath.getDistance()) {
                        nextShortestPath.setDistance(nextDistance);
                        nextShortestPath.resetRailPathList();
                    }
                    for (RailPath campingRailPath : campingShortestPath.getRailPathList()) {
                        List railArcList = new ArrayList(campingRailPath.getRailArcList());
                        railArcList.add(nextRailArc);
                        RailPath nextRailPath = new RailPath(railArcList);
                        nextShortestPath.addRailPath(nextRailPath);
                    }
                }
            }
            Collections.sort(unvisitedShortestPathList);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy