view src/main/gov/nasa/jpf/util/event/CheckEvent.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children caa0924e093d
line wrap: on
line source

/*
 * Copyright (C) 2014, United States Government, as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All rights reserved.
 *
 * The Java Pathfinder core (jpf-core) platform is licensed under the
 * Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0. 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package gov.nasa.jpf.util.event;

import gov.nasa.jpf.vm.MJIEnv;

/**
 * a pseudo event that encapsulates a (possibly composed) check
 * 
 * This event type uses 'alt' for disjunction and 'next' for conjunction if
 * they point to CheckEvents
 */
public abstract class CheckEvent extends Event {
  
  protected CheckEvent (String name, Object... arguments){
    super(name, arguments);
  }
  
  public abstract boolean evaluate(MJIEnv env);
  
  public boolean check (MJIEnv env){
    if (!evaluate(env)){
      if (alt != null && alt instanceof CheckEvent){
        return ((CheckEvent)alt).check(env);
      } else {
        return false;
      }
      
    } else {
      if (next != null && next instanceof CheckEvent){
        return ((CheckEvent)next).check(env);
      } else {
        return true;
      }
    }
  }
  
  public CheckEvent or (CheckEvent orCheck){
    addAlternative(orCheck);
    
    return this;
  }
  
  public CheckEvent and (CheckEvent andCheck){
    addNext( andCheck);
    
    return this;
  }
  
  public String getExpression(){
    if (alt == null && !(next instanceof CheckEvent)){
      return toString();
      
    } else {
      StringBuilder sb = new StringBuilder();
      
      sb.append('(');
      sb.append(name);
      
      for (Event e = alt; e != null; e = e.alt){
        sb.append( " || ");
        sb.append(e.name);
      }
      
      for (Event e = next; e instanceof CheckEvent; e = e.next){
        sb.append( " && ");
        sb.append(e.name);        
      }
      
      sb.append(')');
      
      return sb.toString();
    }
  }
}