Package easysm.stores

Source Code of easysm.stores.Events

package easysm.stores;

import java.util.List;
import java.util.Set;

import easysm.datatypes.Action;
import easysm.datatypes.Expr;
import easysm.datatypes.information.CondReactInfo;
import easysm.datatypes.information.EventInfo;
import easysm.datatypes.information.EventsInfo;
import easysm.datatypes.information.SplitInfo;
import easysm.datatypes.visualization.EventsVis;
import easysm.factories.ListFactory;

/**
* @author FourCheeses Software Solutions
*/
public class Events extends Element
{
  /**
   * Required field to make serialization more robust. See documentation for
   * technical details.
   */
  private static final long serialVersionUID = -4737854882522699854L;

  private List<Event> eventList = ListFactory.createList(Event.class);
  private EventsVis vis;

  public Events(EventsInfo eventsInfo)
  {
    super(eventsInfo);
    vis = new EventsVis(this);
    initEventList();
  }

  /*
   *
   * Properties
   *
   */

  public List<Event> eventList()
  {
    return eventList;
  }

  public EventsVis vis()
  {
    return vis;
  }

  /*
   *
   * Operations called by Main
   *
   */
 
  public void split(List<SplitInfo> splitsInfo)
  {
    for (SplitInfo split : splitsInfo) {
      CondReact cr = split.condReact();
      cr.changeUsedCond(split.usedCond());
      cr.changeUsedReact(split.usedReact());
      cr.changeRemCond(split.remCond());
      cr.changeRemReact(split.remReact());
    }
  }

  public void addEvent(Event event)
  {
    eventList.add(event);
  }
 
  public void addEvents(Set<Event> events)
  {
    eventList.addAll(events);
  }

  public void deleteEvent(Event event)
  {
    eventList.remove(event);
  }

  public void updateEvent(Event event,
                          EventInfo newEventInfo)
  {
    event.changeName(newEventInfo.name());
    event.changeParameters(newEventInfo.paramsInfo());
  }

  public void addCR(Event event,
                    CondReactInfo newCrInfo)
  {
    CondReact newCR = new CondReact(newCrInfo);
    event.condsReacts().add(newCR);
  }

  public void deleteCR(CondReact cr)
  {
    for (Event ev : eventList) {
      for (CondReact currCR : ev.condsReacts()) {
        if (cr == currCR) {
          ev.condsReacts().remove(cr);
          return;
        }
      }
    }
  }

  public void updateCR(CondReact cr,
                       CondReactInfo newCrInfo)
  {
    for (Event ev : eventList) {
      for (CondReact currCR : ev.condsReacts()) {
        if (cr == currCR) {
          cr.changeCond(newCrInfo.cond());
          cr.changeReact(newCrInfo.react());
          return;
        }
      }
    }
  }
 
  /*
   *
   * Public methods
   *
   */
 
  public void clear()
  {
    initEventList();
  }
 
  @Override
  public String toString()
  {
    StringBuilder builder = new StringBuilder();
    for (Event event : eventList) {
      builder.append(eventToString(event));
        }
    return builder.toString();
  }
 
  /*
   *
   * Private methods
   *
   */
 
  private void initEventList()
  {
    eventList.clear();
    // Adds default "created" event
    addEvent(Event.CREATED);
    // Clears "created" event condition reaction
    CondReact createdCR = Event.CREATED.condsReacts().get(0);
    createdCR.changeCond(null);
    createdCR.changeReact(null);
    createdCR.changeUsedCond(null);
    createdCR.changeUsedReact(null);
    createdCR.changeRemCond(null);
    createdCR.changeRemReact(null);
  }
 
  private String eventToString(Event event)
  {
    StringBuilder builder = new StringBuilder();
    String fsp = "    "; // 4 spaces, tabs seem too large
    // Event begin
    builder.append("event " + event + " {\n");
    // Conditions reactions
    boolean first = true;
    for (CondReact cr : event.condsReacts()) {
      if (first) {
        first = false;
      } else {
        builder.append('\n');
      }
      builder.append(fsp + "Condition:\t" + condWithOffset(cr.cond(), "\t") + "\n");
      builder.append(fsp + "Reaction:\t" + reactWithOffset(cr.react(), "\t") + "\n");
    }
    // Event end
    builder.append("}\n\n");
    return builder.toString();
  }
 
  private String condWithOffset(Expr cond, String offset)
  {
    StringBuilder builder = new StringBuilder();
    for (char ch : cond.toString().toCharArray()) {
      builder.append(ch);
      if (ch == '\n') {
        builder.append(offset);
      }
    }
    return builder.toString();
  }
 
  private String reactWithOffset(Action react, String offset)
  {
    StringBuilder builder = new StringBuilder();
    for (char ch : react.toString().toCharArray()) {
      builder.append(ch);
      if (ch == '\n') {
        builder.append(offset);
      }
    }
    return builder.toString();
  }
}
TOP

Related Classes of easysm.stores.Events

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.