Ejemplo n.º 1
0
  public void addAll(PetriNet net) {
    HashMap<String, String> signMap = new HashMap<String, String>();

    Iterator<Place> placeIt = net.places.values().iterator();

    while (placeIt.hasNext()) {
      Place p = placeIt.next();
      signMap.put(p.sign, addPlace(p).sign);
    }

    Iterator<Transition> transIt = net.transitions.values().iterator();

    while (transIt.hasNext()) {
      Transition t = transIt.next();
      signMap.put(t.sign, addTransition(t).sign);
    }

    Iterator<Edge> edgeIt = net.edges.iterator();

    while (edgeIt.hasNext()) {
      Edge e = edgeIt.next();

      if (e.from instanceof Place) {
        if (signMap.containsKey(e.from.sign)) {
          e.from = places.get(signMap.get(e.from.sign));
        }
        if (signMap.containsKey(e.to.sign)) {
          e.to = transitions.get(signMap.get(e.to.sign));
        }
      } else {
        if (signMap.containsKey(e.from.sign)) {
          e.from = transitions.get(signMap.get(e.from.sign));
        }
        if (signMap.containsKey(e.to.sign)) {
          e.to = places.get(signMap.get(e.to.sign));
        }
      }

      edges.add(e);
    }
  }
  public Graph(Graph<T> g) {
    // Deep copies

    // Copy the vertices (which copies the edges)
    for (Vertex<T> v : g.getVerticies()) {
      this.verticies.add(new Vertex<T>(v));
    }

    // Update the object references
    for (Vertex<T> v : this.verticies) {
      for (Edge<T> e : v.getEdges()) {
        Vertex<T> fromVertex = e.getFromVertex();
        Vertex<T> toVertex = e.getToVertex();
        int indexOfFrom = this.verticies.indexOf(fromVertex);
        e.from = this.verticies.get(indexOfFrom);
        int indexOfTo = this.verticies.indexOf(toVertex);
        e.to = this.verticies.get(indexOfTo);
        this.edges.add(e);
      }
    }

    type = g.getType();
  }
  /** Specified in Node */
  protected Node makeBinary(Collection newedges, Edge[] edgemapping, Node caller) {
    // O(n) each edge is visited 1 time in the first part
    // In the second part an upper bound of the total number
    // of runs through the while-loop is O(n) (number of edges).
    Iterator it = edges.iterator();
    Edge e1, e2, newedge1, newedge2, next;
    InnerNode thiscopy = new InnerNode();
    Node tmp;

    // first part (recursion)
    while (it.hasNext()) { // Invoke recursively    //Like the copy-method
      next = (Edge) it.next();
      if (next.to != caller) {
        tmp = next.to.makeBinary(newedges, edgemapping, this);
        e1 = thiscopy.addNeighbour(tmp);
        e2 = tmp.addNeighbour(thiscopy);
        e1.backedge = e2;
        e2.backedge = e1;
        edgemapping[next.getId()] = e1;
        edgemapping[next.getBackEdge().getId()] = e2;
      }
    }

    // second part (fixing degree with a while-loop)
    while ((thiscopy.edges.size() > 2 && caller != null)
        || thiscopy.edges.size() > 3) { // not binary
      // System.out.println(thiscopy+" has "+thiscopy.edges.size()+" edges");
      it = thiscopy.edges.iterator();
      e1 =
          (Edge)
              it.next(); // 1               //Save two outgoing edges (e1 and e2) for binarification
      if (e1.to == caller) e1 = (Edge) it.next(); // 1 or 2
      it.remove(); // Remove this edge from the edge list, see why below **
      e2 = (Edge) it.next(); // 2 or 3
      if (e2.to == caller)
        e2 =
            (Edge)
                it
                    .next(); // 3  //since edges.size() >=4 I can get three elements out without
                             // problems
      it.remove(); // Remove this edge from the edge list, see why below **

      // e1 and e2 are two outgoing edges that does not lead back to the caller

      InnerNode newnode = new InnerNode(); // Make a new node to attach e1 and e2 to

      newedge1 = thiscopy.addNeighbour(newnode); // attach this node to the new node
      newedge2 = newnode.addNeighbour(thiscopy); // and vice versa
      newedge1.backedge = newedge2;
      newedge2.backedge = newedge1;
      newedges.add(newedge1); // Since these edges are new edges, add them to the collection
      newedges.add(newedge2);

      // Now instead of using removeNeighbour and that stuff, we use a 'hack'. We change the
      // endpoint of the
      // edges between 'e1' and 'thiscopy' to 'e1' and 'newnode', and 'e2' and 'thiscopy' to 'e2'
      // and 'newnode' respectively.
      // This way any new edge already added to the newedges-collection is still valid.

      e1.from = newnode; // change the endpoints
      e2.from = newnode;
      e1.getBackEdge().to = newnode; //
      e2.getBackEdge().to = newnode;

      // now we need to remove e1 and e2 from this nodes edgelist and add them to newnode's edge
      // list, but we
      // already did half of that above **
      newnode.edges.add(e1);
      newnode.edges.add(e2); // the other half well done.

      // For each run of this while-loop one neighbour is added and two are removed, hence it must
      // terminate.
    }

    return thiscopy;
  }
Ejemplo n.º 4
0
 public boolean hasEdge(Edge _e) {
   return hasEdge(_e.from(), _e.to());
 }
Ejemplo n.º 5
0
 public boolean remove(Edge _e) {
   return remove(_e.from(), _e.to());
 }
Ejemplo n.º 6
0
 public boolean insert(Edge _e) {
   return insert(_e.from(), _e.to());
 }
Ejemplo n.º 7
0
  public PetriNet getSelection(Boolean delete) {
    PetriNet retVal = new PetriNet();

    Iterator<Place> placeIt = places.values().iterator();

    while (placeIt.hasNext()) {
      Place next = placeIt.next();

      if (next.selected) {
        retVal.addPlace((Place) next.clone());
      }
    }

    Iterator<Transition> transIt = transitions.values().iterator();

    while (transIt.hasNext()) {
      Transition next = transIt.next();

      if (next.selected) {
        retVal.addTransition((Transition) next.clone());
      }
    }

    Iterator<Edge> edgeIt = edges.iterator();

    while (edgeIt.hasNext()) {
      Edge next = edgeIt.next();

      if (next.isSelected()) {
        Edge e = new Edge(retVal);

        if (next.from instanceof Transition) {
          e.from = retVal.transitions.get(next.from.sign);
          e.to = retVal.places.get(next.to.sign);
        } else if (next.from instanceof Place) {
          e.from = retVal.places.get(next.from.sign);
          e.to = retVal.transitions.get(next.to.sign);
        }

        if (e.from != null && e.to != null) retVal.edges.add(e);
      }
    }

    if (delete) {
      for (int i = 0; i < edges.size(); ++i) {
        if (edges.get(i).isSelected()) {
          edges.remove(i);
          --i;
        }
      }

      transIt = retVal.transitions.values().iterator();

      while (transIt.hasNext()) {
        Transition next = transIt.next();
        transitions.remove(next.sign);
      }

      placeIt = retVal.places.values().iterator();

      while (placeIt.hasNext()) {
        Place next = placeIt.next();
        places.remove(next.sign);
      }
    }

    return retVal;
  }