001: /*
002: * $Id: SimpleNegatedFormVisitor.java 576 2005-11-07 07:32:35Z hengels $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.sf.net).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.model;
015:
016: import org.concern.model.parser.ExpressionParser;
017: import org.concern.model.parser.ParseException;
018: import org.concern.model.parser.ast.*;
019:
020: import java.io.StringReader;
021: import java.util.Iterator;
022:
023: /**
024: * @version $Revision: 576 $
025: */
026: public class SimpleNegatedFormVisitor extends NodeVisitor {
027: boolean negation;
028:
029: public String transformCondition(String postcondition)
030: throws ParseException {
031: if (postcondition == null || postcondition.length() == 0)
032: return null;
033: ExpressionParser p = new ExpressionParser(new StringReader(
034: postcondition));
035: StringBuffer expr = new StringBuffer();
036: //negation.push(Boolean.TRUE);
037: negation = true;
038: expr.append(visit(p.or()));
039: return expr.toString();
040: }
041:
042: public Object visit(AndOperator a) {
043: StringBuffer expr = new StringBuffer();
044: expr.append('(');
045: for (Iterator iterator = a.getChildren().iterator(); iterator
046: .hasNext();) {
047: AbstractNode node = (AbstractNode) iterator.next();
048:
049: if (expr.length() > 1) {
050: expr.append(' ').append(and()).append(' ');
051: }
052: expr.append(node.accept(this ));
053: }
054: expr.append(')');
055: return expr;
056: }
057:
058: public Object visit(OrOperator o) {
059: StringBuffer expr = new StringBuffer();
060: expr.append('(');
061: for (Iterator iterator = o.getChildren().iterator(); iterator
062: .hasNext();) {
063: AbstractNode node = (AbstractNode) iterator.next();
064:
065: if (expr.length() > 1) {
066: expr.append(' ').append(or()).append(' ');
067: }
068: expr.append(node.accept(this ));
069: }
070: expr.append(')');
071: return expr;
072: }
073:
074: public Object visit(NotOperator n) {
075: negation = !negation;
076: Object o = ((AbstractNode) n.getChildren().iterator().next())
077: .accept(this );
078: negation = !negation;
079: return o;
080: }
081:
082: public Object visit(Name n) {
083: String conditionName = n.getContent();
084: if (!negation)
085: conditionName = "!" + conditionName;
086: return conditionName;
087: }
088:
089: private String and() {
090: //return Boolean.TRUE.equals(negation.peek()) ? "||" : "&&";
091: return negation ? "&&" : "||";
092: }
093:
094: private String or() {
095: //return Boolean.TRUE.equals(negation.peek()) ? "||" : "&&";
096: return negation ? "||" : "&&";
097: }
098:
099: public static void main(String[] args) throws ParseException {
100: String expression = args[0];
101: System.out.println("expression = " + expression);
102: expression = new SimpleNegatedFormVisitor()
103: .transformCondition(args[0]);
104: System.out.println("expression = " + expression);
105: }
106: }
|