Package eas.plugins.standard.liveInteraction

Source Code of eas.plugins.standard.liveInteraction.AllroundAgentFinderPlugin

/*
* File name:        AgentFinder.java (package eas.plugins.standard)
* Author(s):        Lukas König
* Java version:     6.0
* Generation date:  03.06.2011 (21:01:51)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.plugins.standard.liveInteraction;

import java.awt.Color;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import eas.plugins.AbstractDefaultPlugin;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;

/**
* @author Lukas König
*/
public class AllroundAgentFinderPlugin
                        extends AbstractDefaultPlugin<AbstractEnvironment<?>> implements ListFrameListener {

    private static final long serialVersionUID = 1L;
    private ListFrame frame;
    private AbstractEnvironment<?> environment;

    @Override
    public List<SingleParameter> getParameters() {
        return null;
    }

    @Override
    public String id() {
        return AbstractDefaultPlugin.ALLROUND_PLUGIN_PREFIX + "-agentJuggler";
    }
   
    @Override
    public void runBeforeSimulation(AbstractEnvironment<?> env, ParCollection params) {
        this.environment = env;
        List<Class<?>> classList = env.getAllMatchingAgentTypes();
        Class<?>[] classListObj = new Class<?>[classList.size()];
        int i = 0;
       
        for (Class<?> c : classList) {
            classListObj[i] = c;
            i++;
        }
       
        JList<Class<?>> list = new JList<Class<?>>(classListObj);
       
        ListCellRenderer<Object> localeRenderer = new DefaultListCellRenderer() {
            private static final long serialVersionUID = 1L;
           
            @Override
            public JComponent getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {     
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                Class<?> realValue = (Class<?>) value;

                HashMap<Type, Object> knownObjects = new HashMap<>();
                knownObjects.put(ParCollection.class, GlobalVariables.getPrematureParameters());
                String after = "";
               
                int acceptable = 0;
                for (Constructor<?> c : realValue.getConstructors()) {
                    if (ConstructorFactory.isAcceptable(c, knownObjects, AllroundAgentFinderPlugin.this.environment)) {
                        acceptable++;
                    }
                }
               
                if (acceptable > 0 && !Modifier.isAbstract(realValue.getModifiers())) {
                    after = "    [" + acceptable + " constructor(s)]";
                    setForeground(Color.blue);
                } else {
                    after = "    <unconstructable>";
                    setForeground(Color.red);
                }
               
                setText(realValue.getSimpleName() + after);    
                return this;  
            }
        };
        list.setCellRenderer(localeRenderer);
       
        frame = new ListFrame("Potential agents for environment '"
                + env.getEnvironmentName() + "'.", list, this);
        frame.setVisible(true);
    }

    @Override
    public void runAfterSimulation(AbstractEnvironment<?> env, ParCollection params) {
        frame.dispose();
    }

    @Override
    public void runDuringSimulation(AbstractEnvironment<?> env, Wink simZyk, ParCollection params) {
       
    }
   
    @Override
    public void onSimulationResumed(AbstractEnvironment<?> env, Wink resumeTime,
            ParCollection params) {
        this.frame.setVisible(true);
    }

    private void insertAgentIntoEnvironment(Constructor<?> constructor) {
        HashMap<Type, Object> knownObjects = new HashMap<>();
        knownObjects.put(ParCollection.class, GlobalVariables.getPrematureParameters());
        AbstractAgent<?> agent = (AbstractAgent<?>) ConstructorFactory.constructViaUserInput(constructor, knownObjects, this.environment);
        this.environment.addAbstractAgent(agent);
    }
   
    @Override
    public void selectedItemConfirmed(Object selected, ListFrame caller) {
        if (Class.class.isAssignableFrom(selected.getClass())) {
            Class<?> c = (Class<?>) selected;
           
            JList<Constructor<?>> list = new JList<>(c.getConstructors());
           
            ListCellRenderer<Object> localeRenderer = new DefaultListCellRenderer() {
                private static final long serialVersionUID = 1L;
               
                @Override
                public JComponent getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {     
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    Constructor<?> realValue = (Constructor<?>) value;
                   
                    String parameters = "";
                    for (Parameter par : realValue.getParameters()) {
                        parameters += par.getType().getSimpleName() + " " + par.getName() + ", ";
                    }
                   
                    setText((realValue.getDeclaringClass().getSimpleName() + " (" + parameters + ")").replace(", )", ")"));    
                    return this;  
                }
            };
            list.setCellRenderer(localeRenderer);

            ListFrame frame = new ListFrame(
                    "Please choose how to construct the agent '" + c.getSimpleName() + "'.",
                    list,
                    this);
           
            if (c.getConstructors().length == 1) {
                frame.getListFrameListener().selectedItemConfirmed(c.getConstructors()[0], frame);
            } else {
                frame.setVisible(true);
            }
        } else {
            caller.dispose();
            Constructor<?> c = (Constructor<?>) selected;
            this.insertAgentIntoEnvironment(c);
        }
    }
}
TOP

Related Classes of eas.plugins.standard.liveInteraction.AllroundAgentFinderPlugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.