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

📄 transition.java

📁 petrinets小程序
💻 JAVA
字号:
import java.awt.Point;
import java.util.Random;
import java.util.Vector;


public class transition implements Cloneable{
	
	public static final int ORIENTATION_VERTICAL = 1;
    public static final int ORIENTATION_DIAGONAL1 = 2;
    public static final int ORIENTATION_HORIZONTAL = 3;
    public static final int ORIENTATION_DIAGONAL2 = 4;
    public static final int ORIENTATION_ALL = 5;
    private int orientation;
    
    public static final int height = 30;
    public static final int width = 3;
    
    int priority;
    String name;
    
    int x;
    int y;
    int xName;
    int yName;

    public boolean highlight;
    public double ranval;
    
    public transition(){
    	name = "";
    	x = 0;
    	y = 0;
    	xName = 0;
    	yName = 0;
    	priority = 0;
    	orientation = ORIENTATION_VERTICAL;
    	highlight = false;
    	ranval = 0;
    }
    public transition(String string){
    	name = string;
    	x = 0;
    	y = 0;
    	xName = 0;
    	yName = 0;
    	priority = 0;
    	orientation = ORIENTATION_VERTICAL;
    	highlight = false;
    	ranval = 0;
    }
    
    public void setName(String name) {
    	this.name = name;
     }
    public String getName() {
    	return name;
     }

     public void setPriority(int priority){
    	 this.priority = priority;
     }
     public int getPriority(){
         return priority;
     }

     public void setRanval(double ranval){
    	 this.ranval = ranval;
     }
     public double getRanval(){   
    	 return ranval;
     }

     public void setX(int x) {
         this.x = x;
     }
     public int getX() {
         return x;
     }
     
     public void setY(int y) {
         this.y = y;
     }
     public int getY() {
         return y;
     }
     
     public static int getHeight() {
         return height;
     }
     public static int getWidth() {
         return width;
     }

     public void setXName(int xName) {
         this.xName = xName;
     }
     public int getXName() {
         return xName;
     }
     
     public void setYName(int yName) {
         this.yName = yName;
     }
     public int getYName() {
         return yName;
     }
     public Point getNamePosition() {
    	 return new Point(xName, yName);
         
     }
     
     public void setOrientation(int orientation) {
         this.orientation = orientation;
     }
     public int getOrientation() {
         return orientation;
     }
     public void cycleOrientation() {
         switch (orientation) {
         case ORIENTATION_VERTICAL:
             orientation = ORIENTATION_DIAGONAL1;
             break;
         case ORIENTATION_DIAGONAL1:
             orientation = ORIENTATION_HORIZONTAL;
             break;
         case ORIENTATION_HORIZONTAL:
             orientation = ORIENTATION_DIAGONAL2;
             break;
         case ORIENTATION_DIAGONAL2:
             orientation = ORIENTATION_ALL;
             break;
         case ORIENTATION_ALL:
             orientation = ORIENTATION_VERTICAL;
             break;
         }
     }
     
     public boolean canFire(pn p, boolean priorEnabled){        //NEW 23.5.97 AH
         int i;
         boolean canfire = true;
         boolean nonode = true;
         boolean higherprio = false;
         boolean negatedEdge = false;
         int nodeindex;

         int index = p.transitions.indexOf(this);

        for (i=0; i<p.edges.size(); i++){
          edge e = ((edge) p.edges.elementAt(i));
          if (e.isNegated()) negatedEdge = true;


          if (((e.getTFrom() == edge.TRANSITION) && (e.getIFrom() == index))
               || ((e.getTTo() == edge.TRANSITION) && (e.getITo() == index))) {
            nonode = false;
            // System.out.println("4");
            if (e.getTFrom() == edge.PLACE) {
                if (negatedEdge)  {
                    int ind = e.getIFrom();
                    place z = ((place) p.places.elementAt(ind));

                    if (z.getTokens() > 0) { canfire = false;
                                            break;
                                           }          //leave **-for-loop
                    Vector testTransition = new Vector();                   //
                    testTransition.addElement(this);                        //
                    if (p.getConnectedTokens(testTransition) < 1) {          // neu
                         canfire = false;                                   // 11.5.97
                         break;                                             // JW
                    }                                                       //
                }

                if (priorEnabled) {
                    for (int k = 0; k < p.edges.size(); k++){
                        edge f = (edge) p.edges.elementAt(k);
                        if ((f.getTTo() == edge.TRANSITION) && (index != f.getITo())
                            && (p.getTransition(f.getITo()).priority > this.priority)
                            && (p.getPlace(f.getIFrom()).isTokensSuperWeight(f.getWeight())))
                                higherprio = true;
                    }
                }


                if (negatedEdge == false){
                    nodeindex = e.getIFrom();
                    int w = e.getWeight();
                    canfire = (((place) p.places.elementAt(nodeindex)).isTokensSuperWeight(w));
                    if (canfire == false) break; //leave **-for-loop
                }
            }
          }
          negatedEdge = false;
        }
        if (nonode == true) canfire = false;
        if (priorEnabled == true) {
          if (higherprio == true) canfire = false;}


        return canfire;
     }
     public boolean canfire(marking m, pn p, boolean priorEnabled){
    	 for(int i = 0; i < m.mar.length; i++)
    		 p.getPlace(i).setTokens(m.mar[i]);
    	 if(this.canFire(p, true)) return true;
    	 else
    		 return false;
     }
     
     public boolean fire(pn p, boolean priorEnabled){          //NEW 23.4.97 AH
    	    int i;
    	    int nodeindexFrom;
    	    int nodeindexTo;
    	    int index;
    	    boolean ret = false;
    	    int w;


    	    index = p.transitions.indexOf(this);

    	    if (canFire(p, priorEnabled) == true)
    	    {
    	     for (i=0; i<p.edges.size(); i++){
    	       edge e = (edge) p.edges.elementAt(i);
    	     if (((e.getTFrom() == edge.TRANSITION) && (e.getIFrom() == index))
    	         || ((e.getTTo() == edge.TRANSITION) && (e.getITo() == index)))
    	      {

    	        if (e.getTFrom() == edge.PLACE)
    	        {  w = e.getWeight();
    	           nodeindexFrom = ((edge) p.edges.elementAt(i)).getIFrom();
    	          ((place) p.places.elementAt(nodeindexFrom)).decTokens(w);
    	        }
    	       if (e.getTTo() == edge.PLACE)
    	         {
    	           w = e.getWeight();
    	           nodeindexTo = ((edge) p.edges.elementAt(i)).getITo();
    	           if (e.isNegated()) ((place) p.places.elementAt(nodeindexTo)).decTokens(1);
    	            else            ((place) p.places.elementAt(nodeindexTo)).incTokens(w);
    	         }
    	      }
    	     }
    	     ret = true;
    	    }
    	    else {}

    	    return ret;

     }
     
/*     public boolean fire(pn p, boolean priorEnabled){
    	 return this.fire((pn)p, priorEnabled);
     }
     */
     public boolean checkTrans(pn p){/////is this transition in the pn
    	 int i;
    	 boolean ret = false;
    	 int index = p.transitions.indexOf(this);
    	     	    
    	 for (i=0; i< p.edges.size(); i++){
    		 edge e = (edge) p.edges.elementAt(i);
    		 if ((e.getTTo() == edge.PLACE) && (e.getIFrom() == index) && (e.getTFrom() == edge.TRANSITION))
    			 ret = true;
    	 }
    	 return ret;
     }
     
     public double distance(double x, double y){/////// the distance from (x,y) to the transition
    	 double dist;
    	 double dx;
    	 double dy;
    	 dx = (this.x - x);
    	 dy = (this.y - y);
    	 dist = Math.sqrt(dx*dx + dy*dy);
    	 return dist;
     }
     
     public void randomize(){
    	 Random ran = new Random();
    	 this.ranval = Math.random();
     }
     
     public boolean equals(transition t){
    	 boolean isequal = false;

         if ( (x == t.x) && (y == t.y) && (priority == t.priority) && (name == t.name)
              && (xName == t.xName) && (yName == t.yName) && (orientation == t.orientation)
              && (highlight == t.highlight) && (ranval == t.ranval))
        	 isequal = true;

         return isequal;
       }
     
     public synchronized Object clone(){
    	 transition t = new transition();
    	 t.setName(name);
    	 t.setPriority(priority);
    	 t.setX(x);
    	 t.setY(y);
    	 t.setOrientation(orientation);
    	 t.setXName(xName);
    	 t.setYName(yName);
    	 t.setRanval(ranval);
    	 return t;
     }
     
}

⌨️ 快捷键说明

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