Package net.amin.mobateg.ampl

Source Code of net.amin.mobateg.ampl.ConvertToAMPL

/*
*
* @author Amin Rezaee
* amin_rezaee68@yahoo.com
* amin.rezaee68@gmail.com
*/

package net.amin.mobateg.ampl;

import java.math.BigInteger;
import java.security.SecureRandom;

import org.eclipse.ocl.expressions.PropertyCallExp;
import org.eclipse.ocl.uml.BooleanLiteralExp;
import org.eclipse.ocl.uml.IntegerLiteralExp;
import org.eclipse.ocl.uml.RealLiteralExp;
import org.eclipse.ocl.uml.Variable;
import org.eclipse.uml2.uml.Property;

import net.amin.mobateg.helper.variables.TCGBasicVariableType;
import net.amin.mobateg.helper.variables.TCGVariable;
import net.sf.parteg.base.testcasegraph.generated.TCGDisjunctiveNormalForm;
import net.sf.parteg.base.testcasegraph.generated.TCGOCLExpression;
import net.sf.parteg.base.testcasegraph.generated.TCGOCLOperation;
import net.sf.parteg.base.testcasegraph.generated.TCGRealNode;
import net.sf.parteg.base.testcasegraph.generated.TCGTransition;
import net.sf.parteg.base.testcasegraph.generated.impl.TCGOCLAtomImpl;
import net.sf.parteg.base.testcasegraph.generated.impl.TCGOCLOperationImpl;

public class ConvertToAMPL {
 
  private static SecureRandom random = new SecureRandom();

  protected static String randomString()
  {
    return new BigInteger(64, random).toString(32);
  }
  
  private static TCGTransition currentTransition;
  public static String TCGTransitionToAMPL(TCGTransition oTCGTransition) {
    currentTransition = oTCGTransition;
    StringBuilder result = new StringBuilder();
   
    if(!oTCGTransition.getPreconditions().isEmpty()){
      String activitionSetName = randomString();
      currentTransition.setPreSetName(activitionSetName);
      result.append("set " + activitionSetName
          + " within {0..Pathlength} default {};\n");
      int i=0;
      for (TCGDisjunctiveNormalForm oPre : oTCGTransition.getPreconditions()) {
         if(!oPre.getConjunctions().isEmpty())
           result.append(transitionConstraintToAMPL(
               oPre.getOriginalExpression(), "_pre",i));
         i++;
      }
    }
    if(!oTCGTransition.getPostconditions().isEmpty()){
      String activitionSetName = randomString();
      currentTransition.setPostSetName(activitionSetName);
      result.append("set " + activitionSetName
          + " within {0..Pathlength} default {};\n");
      int i=0;
      for (TCGDisjunctiveNormalForm oPost : oTCGTransition.getPostconditions()) {
        if(!oPost.getConjunctions().isEmpty())
          result.append(transitionConstraintToAMPL(
              oPost.getOriginalExpression(), "_post",i));
        i++;
      }
      //add continuity constraint to ampl file
      if(!currentTransition.getVariableThatWeNeedToAddContinutyConstraintForThem().isEmpty()){
        for(TCGVariable oVar : currentTransition.getVariableThatWeNeedToAddContinutyConstraintForThem()){
          result.append("s.t. ");
          result.append(activitionSetName + "_post" + i);
          result.append("{i in " + activitionSetName + "} : ");
          result.append("(" + oVar.getName()+"[i]) = (" + oVar.getName() + "[i-1])");
          result.append(";\n");   
          i++;
        }
      }
    }
    return result.toString();
  }
 
  public static String transitionConstraintToAMPL(
      TCGOCLExpression tcgoclExpression, String preOrPost, Integer i) {

    StringBuilder result = new StringBuilder();
    String activitionSetName;
    if (tcgoclExpression != null) {
      if(preOrPost == "_pre")
        activitionSetName=currentTransition.getPreSetName();
      else
        activitionSetName=currentTransition.getPostSetName();
      result.append("s.t. ");
      result.append(activitionSetName + preOrPost + i);
      result.append("{i in " + activitionSetName + "} : ");
      result.append(constraintToAMPL(tcgoclExpression));
      result.append(";\n");
    }
    return result.toString()
  }
 
  public static String constraintToAMPL(TCGOCLExpression tcgoclExpression){
    StringBuilder result = new StringBuilder();
   
    if(tcgoclExpression instanceof TCGOCLOperationImpl){
      if(((TCGOCLOperation) tcgoclExpression).getRight() != null){
        result.append("(" + constraintToAMPL(((TCGOCLOperation) tcgoclExpression).getLeft()) + ")");   
        result.append(((TCGOCLOperation) tcgoclExpression).getOperationName());
        result.append("(" + constraintToAMPL(((TCGOCLOperation) tcgoclExpression).getRight()) + ")");
      }
      else{
        result.append(((TCGOCLOperation) tcgoclExpression).getOperationName());
        result.append("(" + constraintToAMPL(((TCGOCLOperation) tcgoclExpression).getLeft()) + ")");
      }
       
    }
    if(tcgoclExpression instanceof TCGOCLAtomImpl){
      result.append(oclAtomConstraintToAMPL(((TCGOCLAtomImpl)tcgoclExpression)));
    }
    return result.toString();
  }
 
  @SuppressWarnings("rawtypes")
  public static String oclAtomConstraintToAMPL(TCGOCLAtomImpl tcgOCLAtomConstraint){
    StringBuilder result = new StringBuilder();
   
    Variable oVar = null;
    if (tcgOCLAtomConstraint.getElement() instanceof Variable) {  //input parameter
      oVar = (Variable)tcgOCLAtomConstraint.getElement();
      if (oVar != null) {
        result.append(oVar.getName());
      }   
    }
    else if (tcgOCLAtomConstraint.getElement() instanceof Property) {  //class property
      Property oProp = (Property) tcgOCLAtomConstraint.getElement();
      result.append(oProp.getName());
     
      boolean isPre = ((PropertyCallExp)(tcgOCLAtomConstraint.getOclReference())).isMarkedPre();     
      result.append("[i");
      if (isPre) {
        result.append("-1");
      }
      result.append("]");
    }
    /*else if (tcgOCLAtomConstraint.getElement() instanceof EReferenceImpl) {
      EReferenceImpl oRef = (EReferenceImpl) tcgOCLAtomConstraint.getElement();
      Integer.valueOf(oRef.getDefaultValueLiteral());
    }*/
    else if(tcgOCLAtomConstraint.getElement() instanceof RealLiteralExp){
      RealLiteralExp oRealExp = (RealLiteralExp) tcgOCLAtomConstraint.getElement();
      result.append(oRealExp.getRealSymbol());
    }
    else if(tcgOCLAtomConstraint.getElement() instanceof BooleanLiteralExp){
      BooleanLiteralExp oBoolExp = (BooleanLiteralExp) tcgOCLAtomConstraint.getElement();
      if(oBoolExp.getBooleanSymbol().booleanValue())
        result.append("1");
      else
        result.append("0");
    }
    else if (tcgOCLAtomConstraint.getElement() instanceof IntegerLiteralExp) {
      IntegerLiteralExp oIntExp = (IntegerLiteralExp) tcgOCLAtomConstraint.getElement();
      result.append(oIntExp.getIntegerSymbol());
    }     

    return result.toString();
  }
 
  public static String stateToAMPL(TCGRealNode oRealNode){   
    StringBuilder result = new StringBuilder();
    String nodeName = oRealNode.getName();
   
   
    if (nodeName == null || nodeName == ""){
      nodeName = randomString();
      oRealNode.setName(nodeName);
    }
    else {
      nodeName = nodeName.replaceAll("::", "_");
      nodeName = nodeName.replaceAll("\\s", "_")
    }
   
    result.append("set " + nodeName + " within {0..Pathlength} default {};\n");
    result.append("s.t. ");
    result.append(nodeName + "_invariant");
    result.append("{i in " + nodeName + "} : ");
    result.append(constraintToAMPL(oRealNode.getCondition().getOriginalExpression()));
    result.append(";\n");
   
    return result.toString();
  }

  public static String tcgBasicVariableToAMPL(TCGVariable var) {
    String typeSpec = null;
    if (var.getType().equals(TCGBasicVariableType.INTEGER))
      typeSpec = " : integer >=-10000, <= 10000";
    if (var.getType().equals(TCGBasicVariableType.BOOLEAN))
      typeSpec = " : integer >=0, <= 1";
    if (var.getType().equals(TCGBasicVariableType.REAL))
      typeSpec = ">=-10000, <= 10000";
   
    StringBuilder result = new StringBuilder();
    result.append("var " + var.getName()
        + (var.isIsParameter() ? "" : "{0..Pathlength}") + typeSpec);
   
    //set initial value for which model variables that have initial value
    if(var.getInitialValue() != null && var.getInitialValue() != ""){
      if(var.isIsParameter()){
        result.append(" := " + var.getInitialValue());
      }
      else {
        result.append(" := " + var.getInitialValue());
      }
    }
    else{
      result.append(" := 1");
    }
   
    result.append(";\n");
   
    return result.toString();
  }
 
}
TOP

Related Classes of net.amin.mobateg.ampl.ConvertToAMPL

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.