code: /****************************************************************************
  
      ePMC - an extensible probabilistic model checker
      Copyright (C) 2017
  
      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
   *****************************************************************************/
  
  package epmc.jani.model.property;
  
  import java.util.Map;
  
  import javax.json.Json;
  import javax.json.JsonObject;
  import javax.json.JsonObjectBuilder;
  import javax.json.JsonString;
  import javax.json.JsonValue;
  
  import epmc.error.Positional;
  import epmc.expression.Expression;
  import epmc.jani.model.JANIIdentifier;
  import epmc.jani.model.JANINode;
  import epmc.jani.model.ModelJANI;
  import epmc.jani.model.UtilModelParser;
  import epmc.jani.model.expression.JANIExpression;
  
  /**
   * JANI expression for deadlock operators.
   * 
   * @author Ernst Moritz Hahn
   */
  public final class JANIPropertyDeadlock implements JANIExpression {
      /** Identifier of this JANI expression type. */
      public final static String IDENTIFIER = "deadlock";
      private final static String OP = "op";
      private final static String DEADLOCK = "deadlock";
  
      private ModelJANI model;
      private boolean initialized;
      /** Positional information. */
      private Positional positional;
  
      private void resetFields() {
          initialized = false;
          positional = null;
      }
  
      public JANIPropertyDeadlock() {
          resetFields();
      }
  
      @Override
      public JANINode parse(JsonValue value) {
          return parseAsJANIExpression(value);
      }
  
      @Override 
      public JANIExpression parseAsJANIExpression(JsonValue value) {
          assert model != null;
          assert value != null;
          resetFields();
          if (!(value instanceof JsonObject)) {
              return null;
          }
          JsonObject object = (JsonObject) value;
          if (!object.containsKey(OP)) {
              return null;
          }
          if (!(object.get(OP) instanceof JsonString)) {
              return null;
          }
          if (!object.getString(OP).equals(DEADLOCK)) {
              return null;
          }
          initialized = true;
          positional = UtilModelParser.getPositional(value);
          return this;
      }
  
      @Override
      public JsonValue generate() {
          assert initialized;
          assert model != null;
          JsonObjectBuilder builder = Json.createObjectBuilder();
          builder.add(OP, DEADLOCK);
          UtilModelParser.addPositional(builder, positional);
          return builder.build();
      }
  
      @Override
      public JANIExpression matchExpression(ModelJANI model, Expression expression) {
          assert expression != null;
          assert model != null;
          resetFields();
          if (!(expression instanceof ExpressionDeadlock)) {
              return null;
          }
          initialized = true;
          positional = expression.getPositional();
          return this;
      }
  
      @Override
      public Expression getExpression() {
          assert initialized;
          assert model != null;
          return new ExpressionDeadlock(positional);
      }
  
      @Override
      public void setIdentifiers(Map<String, ? extends JANIIdentifier> identifiers) {
      }	
  
      @Override
      public void setModel(ModelJANI model) {
          this.model = model;
      }
  
      @Override
      public ModelJANI getModel() {
          return model;
      }
  
      @Override
      public String toString() {
          return UtilModelParser.toString(this);
      }
      
      @Override
      public void setPositional(Positional positional) {
          this.positional = positional;
      }
      
      @Override
      public Positional getPositional() {
          return positional;
      }
  }
  