⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datalayer.java

📁 Petri网分析工具PIPE is open-source
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
package pipe.dataLayer;

// Collections
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Observable;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import pipe.gui.CreateGui;

/**
 * <b>DataLayer</b> - Encapsulates entire Petri-Net, also contains functions to perform calculations
 *
 * @see <p><a href="..\PNMLSchema\index.html">PNML  -  Petri-Net XMLSchema (stNet.xsd)</a>
 * @see </p><p><a href="uml\DataLayer.png">DataLayer UML</a></p>
 * @version 1.0
 * @author James D Bloom
 */
public class DataLayer extends Observable implements pipe.gui.Constants{

  private static Random randomNumber = new Random(); // Random number generator

  /** PNML File Name */
  private String pnmlName = null;

  /** List containing all the Place objects in the Petri-Net */
  private ArrayList placesArray = null;
  /** ArrayList containing all the Transition objects in the Petri-Net */
  private ArrayList transitionsArray = null;
  /** ArrayList containing all the Arc objects in the Petri-Net */
  private ArrayList arcsArray = null;
  /** ArrayList containing all the Token objects in the Petri-Net */
  private ArrayList tokensArray = null;
  /** ArrayList containing all the Arrow objects in the Petri-Net */
  private ArrayList arrowsArray = null;

  /** ArrayList for net-level label objects (as opposed to element-level labels).*/
  private ArrayList labelsArray = null;

  /** An ArrayList used to point to either the Arc, Place or Transition ArrayLists when these ArrayLists are being update */
  private ArrayList changeArrayList = null;

  /** Initial Markup Matrix */
  private int[] initialMarkupMatrix = null;
  /** Initial Markup Matrix */
  private int[] currentMarkupMatrix = null;
  /** Markup Matrix Storage used during animation */
  private int[] markupMatrixAnimationStorage = null;

  /** Markup Matrix Storage */
  private Object[] markupStore = {null, null, null, null, null, null, null, null, null, null};
  /** Markup Matrix Storage Position */
  private int position = 0;

  /** Markup Storage Array */
  private PNMatrix markupHistoryMatrix = null;
  /** Foward Incidence Matrix */
  private PNMatrix fowardsIncidenceMatrix = null;
  /** Backward Incidence Matrix */
  private PNMatrix backwardsIncidenceMatrix = null;
  /** Incidence Matrix */
  private PNMatrix incidenceMatrix = null;

  /** X-Axis Scale Value */
  private final int DISPLAY_SCALE_FACTORX = 7; // Scale factors for loading other Petri-Nets (not yet implemented)
  /** Y-Axis Scale Value */
  private final int DISPLAY_SCALE_FACTORY = 7; // Scale factors for loading other Petri-Nets (not yet implemented)
  /** X-Axis Shift Value */
  private final int DISPLAY_SHIFT_FACTORX = 270; // Scale factors for loading other Petri-Nets (not yet implemented)
  /** Y-Axis Shift Value */
  private final int DISPLAY_SHIFT_FACTORY = 120; // Scale factors for loading other Petri-Nets (not yet implemented)

  /** Hashtable which maps PlaceTransitionObjects to their list of connected arcs */
  private Hashtable arcsMap = null;

  /**
   * Create Petri-Net object from PNML file with URI pnmlFileName
   *
   *
   * @param pnmlFileName Name of PNML File
   */
  public DataLayer(String pnmlFileName) {
    initializeMatrixes();
    try {
      loadPNML(pnmlFileName);
    } catch (IOException e) {
      System.err.println("IOException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
      e.printStackTrace(System.err);
    } catch (SAXException e) {
      System.err.println("SAXException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
      e.printStackTrace(System.err);
    } catch (TransformerException e) {
      System.err.println("TransformerException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
      e.printStackTrace(System.err);
    } catch (ParserConfigurationException e) {
      System.err.println("ParserConfigurationException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
      e.printStackTrace(System.err);
    }
  }

  /**
   * Create Petri-Net object from pnmlFile
   *
   * @param pnmlFile PNML File
   */
  public DataLayer(File pnmlFile) {
      this(pnmlFile.getAbsolutePath());
  }

  /**
   * Create empty Petri-Net object
   */
  public DataLayer() {
    initializeMatrixes();
  }

  /**
   * Initialize Arrays
   */
  private void initializeMatrixes() {
    placesArray = new ArrayList();
    transitionsArray = new ArrayList();
    arcsArray = new ArrayList();
    tokensArray = new ArrayList();
    arrowsArray = new ArrayList();
    labelsArray = new ArrayList();
    initialMarkupMatrix = null;
    fowardsIncidenceMatrix = null;
    backwardsIncidenceMatrix = null;
    incidenceMatrix = null;

    // may as well do the hashtable here as well
    arcsMap = new Hashtable();
  }

  /**
   * Function populates the arcsMap hashtable enabling easier cross referencing of places, transitions and the arcs connected to them.
   *
   */
  public void setArcConnectionMap() {
    // map (PTO, arcslist)


    // get all the PlaceTransitionObjects
    ArrayList allPTO = new ArrayList(placesArray);
    allPTO.addAll(transitionsArray);

    Iterator pto;
    Iterator arcs;
    PlaceTransitionObject current ;

    pto = allPTO.iterator();
    while (pto.hasNext()) {
      current = (PlaceTransitionObject)pto.next();
      // make an entry for each pto in our mapping structure
      arcsMap.put(current,new ArrayList());
    }

    arcs = arcsArray.iterator();
    Arc currentArc;
    PlaceTransitionObject source;
    PlaceTransitionObject target;
    ArrayList arcslist = null;


    // iterate over the arcs, getting the source and destination ptos and adding the arc to their lists
    while (arcs.hasNext()) {
      currentArc = (Arc)arcs.next();
      source = currentArc.getSource();
      target = currentArc.getTarget();


      // get the list of arcs attached to the source and target and add the arc to them
      try {
        ((ArrayList)arcsMap.get(source)).add(currentArc);
      } catch (NullPointerException ne1) {
//        System.out.println("Populating arcsMap: " + ne1.toString());
      }

      try {
        ((ArrayList)arcsMap.get(target)).add(currentArc);
      } catch (NullPointerException ne2) {
//        System.out.println("Populating arcsMap: " + ne2.toString());
      }



    }

  }


 /**
  * Remove first Place that has an id equal to idInput
  *
  * @param idInput id of Place object to remove from Petri-Net
  */
  public void removePlace(String idInput) {
    for(int i = 0 ; i < placesArray.size(); i++)
      if(idInput.equals(((Place)placesArray.get(i)).getId())) {
    placesArray.remove(i);
    setChanged();
      }
  }

  /**
   * Remove first Transition that has an id equal to idInput
   *
   * @param idInput id of Transition object to remove from Petri-Net
   */
  public void removeTransition(String idInput) {
    for(int i = 0 ; i < transitionsArray.size(); i++)
      if(idInput.equals(((Transition)transitionsArray.get(i)).getId())) {
    transitionsArray.remove(i);
    setChanged();
      }
  }

  /**
   * Remove first Arc that has an id equal to idInput
   *
   * @param idInput id of Arc object to remove from Petri-Net
   */
  public void removeArc(String idInput) {

    for(int i = 0 ; i < arcsArray.size(); i++)
      if(idInput.equals(((Arc)arcsArray.get(i)).getId())) {
    arcsArray.remove(i);
    setChanged();
      }
  }


  /**
   * Add placeInput to the back of the Place ArrayList
   * All observers are notified of this change (Model-View Architecture)
   *
   * @param placeInput Place Object to add
   */
  public void addPlace(Place placeInput) {
    boolean unique = true;
    if(placeInput != null) {
      if(placeInput.getId() != null && placeInput.getId().length() > 0) {
        for(int i = 0 ; i < placesArray.size() ; i++) {
          if(placeInput.getId().equals(((Place)placesArray.get(i)).getId())) {
            unique = false;
          }
        }
      } else {
        String id = null;
        if(placesArray != null && placesArray.size() > 0) {
          int no = placesArray.size();
          //          id = "P" + no;
          do {
            // System.out.println("in while loop");
            for(int i = 0 ; i < placesArray.size() ; i++) {
              id = "P" + no;
              if(placesArray.get(i) != null) {
                if(id.equals(((Place)placesArray.get(i)).getId())) {
                  // System.out.println("testing id: " + id);
                  unique = false;
                  no++;
                } else {
                  unique = true;
                }
              }
            }
          } while(!unique);
        } else {
          id = "P0";
        }

        if(id != null) {
          placeInput.setId(id);
        } else {
          placeInput.setId("error");
        }
      }
      placesArray.add(placeInput);
      setChanged();
      // notifyObservers(placeInput.getBounds());
      notifyObservers(placeInput);
    }
  }

  /**
   * Add placeInput to the back of the Place ArrayList
   * All observers are notified of this change (Model-View Architecture)
   *
   * @param placeInput Place Object to add
   */
  public void addAnnotation(AnnotationNote labelInput) {
  	boolean unique = true;
  	labelsArray.add(labelInput);
  	setChanged();
	notifyObservers(labelInput);
  	}
 
 
  /**
   * Add transitionInput to back of the Transition ArrayList
   * All observers are notified of this change (Model-View Architecture)
   *
   * @param transitionInput Transition Object to add
   */
  public void addTransition(Transition transitionInput) {
    boolean unique = true;
    if(transitionInput != null) {
      if(transitionInput.getId() != null && transitionInput.getId().length() > 0) {
        for(int i = 0 ; i < transitionsArray.size() ; i++) {
          if(transitionInput.getId().equals(((Transition)transitionsArray.get(i)).getId())) {
            unique = false;
          }
        }
      } else {
        String id = null;
        if(transitionsArray != null && transitionsArray.size() > 0) {
          int no = transitionsArray.size();
          do {
            // System.out.println("transition while loop");
            for(int i = 0 ; i < transitionsArray.size() ; i++) {
              id = "T" + no;
              if(transitionsArray.get(i) != null) {
                if(id.equals(((Transition)transitionsArray.get(i)).getId())) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -