Exemple #1
0
  /**
   * Sets the number of tokens to the specified PlaceNode (Place or ReferencePlace). If specified
   * PlaceNode is ReferencePlace, it will set number of tokens to its connected Place. If the
   * specified ReferencePlace is not connected to any Place, it will throw RuntimeException. If the
   * specified number of tokens is negative, it will throw RuntimeException. If the resulting Place
   * is static, number of tokens will be set to initial marking instead.
   */
  public void setTokens(PlaceNode placeNode, int tokens) {
    if (tokens < 0) {
      // throw new RuntimeException("Number of tokens must be non-negative");
      throw new IllegalStateException("Number of tokens must be non-negative");
    }

    Place place = placeNode.getPlace();

    if (place == null) {
      // throw new RuntimeException("setTokens() to disconnected ReferencePlace");
      throw new IllegalStateException("setTokens() to disconnected ReferencePlace");
    }

    if (place.isStatic()) {
      petriNet.getInitialMarking().map.put(place, tokens);
    } else {
      this.map.put(place, tokens);
    }
  }
Exemple #2
0
  /**
   * Returns the number of tokens based on the specified PlaceNode (Place or ReferencePlace). If
   * specified PlaceNode is ReferencePlace, it will return number of tokens of its connected Place.
   * If the specified ReferencePlace is not connected to any Place, it will return zero. If the
   * resulting Place is static, number of tokens will be given from initial marking instead.
   */
  public int getTokens(PlaceNode placeNode) {
    Place place = placeNode.getPlace();
    if (place
        == null) { // In case of disconnected ReferencePlace, we want it to appear with zero tokens.
      // Disconnected ReferencePlaces can be found in stored subnets.
      return 0;
    }

    Marking marking;
    if (place.isStatic()) {
      marking = petriNet.getInitialMarking();
    } else {
      marking = this;
    }

    if (marking.map.get(place)
        == null) { // Place has zero tokens in the beginning. Not every place is in map. Only those
      // previously edited.
      return 0;
    }

    return marking.map.get(place);
  }