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

org.jamesii.ml3.model.state.SimpleState Maven / Gradle / Ivy

Go to download

The Modeling Language for Linked Lives, a domain specific modeling language for agent-based computational demography.

There is a newer version: 0.2.8
Show newest version
/*
 * Copyright 2017 University of Rostock
 *
 * 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.jamesii.ml3.model.state;

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

import org.jamesii.ml3.model.agents.IAgent;

/**
 * 
 * A basic implementation of {@link IState}.
 * 
 * @author Oliver Reinhardt
 *
 */
public class SimpleState implements IState {
	/**
	 * All living agents by id.
	 */
	private Map agentsAlive;
	/**
	 * All dead agents by id.
	 */
	private Map agentsDead;
	
	/**
	 * Creates an empty state with no agents and timestamp 0.
	 */
	public SimpleState() {
		agentsAlive = new HashMap();
		agentsDead = new HashMap();
	}
	
	@Override
	public Collection getAgents() {
		List all = new ArrayList();
		all.addAll(agentsAlive.values());
		all.addAll(agentsDead.values());
		
		return all;
	}
	
	@Override
	public Collection getAgentsByType(String type) {
		Collection all = getAgentsAliveByType(type);
		
		for (IAgent agent: agentsDead.values()) {
			if (agent.getType().equals(type)) all.add(agent);
		}
		
		return all;
	}
	
	@Override
	public Collection getAgentsAlive() {
		return agentsAlive.values();
	}
	
	@Override
	public Collection getAgentsAliveByType(String type) {
		List all = new ArrayList();
		
		for (IAgent agent: agentsAlive.values()) {
			if (agent.getType().equals(type)) all.add(agent);
		}
		
		return all;
	}

	@Override
	public void addAgent(IAgent agent) {
		if (agent.isAlive()) {
			agentsAlive.put(agent.getID(), agent);
		}
		else {
			agentsDead.put(agent.getID(), agent);
		}
	}

	@Override
	public void addAgents(Collection agents) {
		for (IAgent agent: agents) {
			addAgent(agent);
		}
	}

	@Override
	public IAgent getAgent(int id) {
		if (agentsAlive.containsKey(id))
			return agentsAlive.get(id);
		else return agentsDead.get(id);
	}

	@Override
	public void killAgent(int id, double time) {
		if (agentsAlive.containsKey(id)) {
			IAgent agent = agentsAlive.get(id);
			agent.die(time);
			agentsAlive.remove(id);
			agentsDead.put(id, agent);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy