com.github.rinde.rinsim.examples.experiment.Taxi.java Source code

Java tutorial

Introduction

Here is the source code for com.github.rinde.rinsim.examples.experiment.Taxi.java

Source

package com.github.rinde.rinsim.examples.experiment;

/*
 * Copyright (C) 2011-2016 Rinde van Lon, iMinds-DistriNet, KU Leuven
 *
 * 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.
 */

import com.github.rinde.rinsim.core.model.pdp.PDPModel;
import com.github.rinde.rinsim.core.model.pdp.Parcel;
import com.github.rinde.rinsim.core.model.pdp.Vehicle;
import com.github.rinde.rinsim.core.model.pdp.VehicleDTO;
import com.github.rinde.rinsim.core.model.road.RoadModel;
import com.github.rinde.rinsim.core.model.road.RoadModels;
import com.github.rinde.rinsim.core.model.time.TimeLapse;
import com.github.rinde.rinsim.geom.Point;
import com.google.common.base.Optional;

/**
 * Implementation of a very simple taxi agent. It moves to the closest customer,
 * picks it up, then delivers it, repeat.
 *
 * @author Rinde van Lon
 */
class Taxi extends Vehicle {
    private static final double SPEED = 50d;
    private Optional<Parcel> curr;

    Taxi(Point startPosition, int capacity) {
        super(VehicleDTO.builder().capacity(capacity).startPosition(startPosition).speed(SPEED).build());
        curr = Optional.absent();
    }

    Taxi(Point startPosition, int capacity, double speed) {
        super(VehicleDTO.builder().capacity(capacity).startPosition(startPosition).speed(speed).build());
        curr = Optional.absent();
    }

    @Override
    public void afterTick(TimeLapse timeLapse) {
    }

    @Override
    protected void tickImpl(TimeLapse time) {
        final RoadModel rm = getRoadModel();
        final PDPModel pm = getPDPModel();

        if (!time.hasTimeLeft()) {
            return;
        }
        if (!curr.isPresent()) {
            curr = Optional.fromNullable(RoadModels.findClosestObject(rm.getPosition(this), rm, Parcel.class));
        }

        if (curr.isPresent()) {
            final boolean inCargo = pm.containerContains(this, curr.get());
            // sanity check: if it is not in our cargo AND it is also not on the
            // RoadModel, we cannot go to curr anymore.
            if (!inCargo && !rm.containsObject(curr.get())) {
                curr = Optional.absent();
            } else if (inCargo) {
                // if it is in cargo, go to its destination
                rm.moveTo(this, curr.get().getDeliveryLocation(), time);
                if (rm.getPosition(this).equals(curr.get().getDeliveryLocation())) {
                    // deliver when we arrive
                    pm.deliver(this, curr.get(), time);
                }
            } else {
                // it is still available, go there as fast as possible
                rm.moveTo(this, curr.get(), time);
                if (rm.equalPosition(this, curr.get())) {
                    // pickup customer
                    pm.pickup(this, curr.get(), time);
                }
            }
        }
    }
}