Esempio n. 1
0
  @Override
  public ASTNode transform(Rule node) throws TransformerException {

    final boolean heating = node.containsAttribute(MetaK.Constants.heatingTag);
    final boolean cooling = node.containsAttribute(MetaK.Constants.coolingTag);
    if (!(heating || cooling)) return node;
    if (!(node.getBody() instanceof Rewrite)) {
      GlobalSettings.kem.register(
          new KException(
              KException.ExceptionType.ERROR,
              KException.KExceptionGroup.CRITICAL,
              "Heating/Cooling rules should have rewrite at the top.",
              getName(),
              node.getFilename(),
              node.getLocation()));
    }
    KSequence kSequence;
    Rewrite rewrite = (Rewrite) node.getBody();
    if (heating) {
      if (!(rewrite.getRight() instanceof KSequence)) {
        GlobalSettings.kem.register(
            new KException(
                KException.ExceptionType.ERROR,
                KException.KExceptionGroup.CRITICAL,
                "Heating rules should have a K sequence in the rhs.",
                getName(),
                node.getFilename(),
                node.getLocation()));
      }
      kSequence = (KSequence) rewrite.getRight();
    } else {
      if (!(rewrite.getLeft() instanceof KSequence)) {
        GlobalSettings.kem.register(
            new KException(
                KException.ExceptionType.ERROR,
                KException.KExceptionGroup.CRITICAL,
                "Cooling rules should have a K sequence in the lhs.",
                getName(),
                node.getFilename(),
                node.getLocation()));
      }
      kSequence = (KSequence) rewrite.getLeft();
    }
    List<Term> kSequenceContents = kSequence.getContents();
    if (kSequenceContents.size() != 2) {
      GlobalSettings.kem.register(
          new KException(
              KException.ExceptionType.ERROR,
              KException.KExceptionGroup.CRITICAL,
              "Heating/Cooling rules should have exactly 2 items in their K Sequence.",
              getName(),
              node.getFilename(),
              node.getLocation()));
    }
    final Term freezer = kSequenceContents.get(1);
    if (!(freezer instanceof Freezer)) {
      kSequenceContents = new ArrayList<Term>(kSequenceContents);
      kSequenceContents.set(1, new ContextsToHeating(context).freeze(freezer));
      kSequence = kSequence.shallowCopy();
      kSequence.setContents(kSequenceContents);
      rewrite = rewrite.shallowCopy();
      if (heating) {
        rewrite.replaceChildren(rewrite.getLeft(), kSequence, context);
      } else {
        rewrite.replaceChildren(kSequence, rewrite.getRight(), context);
      }
      node = node.shallowCopy();
      node.setBody(rewrite);
    }
    return node;
  }