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

com.github.rinde.rinsim.experiment.MASConfiguration Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/*
 * 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.
 */
package com.github.rinde.rinsim.experiment;

import static com.google.common.base.Preconditions.checkArgument;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

import com.github.rinde.rinsim.core.model.ModelBuilder;
import com.github.rinde.rinsim.pdptw.common.AddDepotEvent;
import com.github.rinde.rinsim.pdptw.common.AddParcelEvent;
import com.github.rinde.rinsim.scenario.TimeOutEvent;
import com.github.rinde.rinsim.scenario.TimedEvent;
import com.github.rinde.rinsim.scenario.TimedEventHandler;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/**
 * This represents a configuration of a multi-agent system in a simulation. The
 * multi-agent system can be controlled in a centralized or a decentralized
 * manner. The configuration consists of a set of {@link ModelBuilder}s that
 * configure the models necessary for the multi-agent system and it consists of
 * {@link TimedEventHandler}s, there should be a handler for each type of event
 * in the scenario for which the configuration is used.
 * 

* Constructing instances can be done via {@link #builder()}. * @author Rinde van Lon */ @AutoValue public abstract class MASConfiguration implements Serializable { private static final long serialVersionUID = -4538293931753741399L; MASConfiguration() {} /** * @return The name of the configuration. */ public abstract String getName(); /** * @return A possibly empty list of model builders. */ public abstract ImmutableSet> getModels(); /** * @return The event handlers of the configuration. */ public abstract ImmutableMap, TimedEventHandler> getEventHandlers(); /** * @return A new {@link Builder}. */ public static Builder builder() { return new Builder(); } /** * Creates a new copying {@link Builder}. * @param config The configuration to copy all settings from into the created * builder. * @return A new {@link Builder} instance initialized with the same settings * as the specified configuration. */ public static Builder builder(MASConfiguration config) { return new Builder(config); } /** * Creates a new {@link Builder} with default handlers for PDPTW problems. The * default handlers are: *

    *
  • {@link TimeOutEvent} is handled by {@link TimeOutEvent#ignoreHandler()} *
  • *
  • {@link AddParcelEvent} is handled by * {@link AddParcelEvent#defaultHandler()}
  • *
  • {@link AddDepotEvent} is handled by * {@link AddDepotEvent#defaultHandler()}
  • *
* Each of these default handlers may be overridden by calls to * {@link Builder#addEventHandler(Class, TimedEventHandler)}. * @return A new {@link Builder}. */ public static Builder pdptwBuilder() { return new Builder().addDefaultPDPTWHandlers(); } /** * Builder for constructing {@link MASConfiguration} instances. * @author Rinde van Lon */ public static final class Builder { String name; ImmutableSet.Builder> modelsBuilder; Map, TimedEventHandler> eventHandlers; Builder() { name = MASConfiguration.class.getSimpleName(); modelsBuilder = ImmutableSet.builder(); eventHandlers = new LinkedHashMap<>(); } Builder(MASConfiguration config) { this(); name = config.getName(); modelsBuilder.addAll(config.getModels()); eventHandlers.putAll(config.getEventHandlers()); } /** * Sets the name of the configuration. * @param string The new name. * @return This, as per the builder pattern. */ public Builder setName(String string) { name = string; return this; } /** * Adds the specified model builder. * @param model The model builder to add. * @return This, as per the builder pattern. */ public Builder addModel(ModelBuilder model) { checkSerializable(model); modelsBuilder.add(model); return this; } /** * Adds the specified model builders. * @param models The model builders to add. * @return This, as per the builder pattern. */ public Builder addModels(Iterable> models) { for (final ModelBuilder mb : models) { addModel(mb); } return this; } /** * Adds the specified {@link TimedEventHandler}s. * @param type The type of event to handle. * @param handler The handler to add. * @return This, as per the builder pattern. */ public Builder addEventHandler(Class type, TimedEventHandler handler) { checkSerializable(handler); eventHandlers.put(type, handler); return this; } Builder addDefaultPDPTWHandlers() { return addEventHandler(TimeOutEvent.class, TimeOutEvent.ignoreHandler()) .addEventHandler(AddDepotEvent.class, AddDepotEvent.defaultHandler()) .addEventHandler(AddParcelEvent.class, AddParcelEvent.defaultHandler()); } /** * @return A new {@link MASConfiguration} instance. */ public MASConfiguration build() { return new AutoValue_MASConfiguration( name, modelsBuilder.build(), ImmutableMap.copyOf(eventHandlers)); } static void checkSerializable(Object o) { checkArgument(o instanceof Serializable, "%s is not Serializable.", o); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy