001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.contract.lang.op.logical;
028:
029: import org.cougaar.lib.contract.lang.*;
030: import org.cougaar.lib.contract.lang.op.OpBuilder;
031: import org.cougaar.lib.contract.lang.op.OpCodes;
032: import org.cougaar.lib.contract.lang.op.reflect.InstanceOfOp;
033:
034: /**
035: * "not" <code>Op</code> -- returns logical "!" of given <code>Op</code>.
036: **/
037: public final class NotOp extends OpImpl {
038:
039: public Op u1;
040:
041: public NotOp() {
042: }
043:
044: public final int getID() {
045: return OpCodes.NOT_ID;
046: }
047:
048: public final Op parse(final OpParser p) throws ParseException {
049: Op u1 = p.nextOp();
050: if (u1 == null) {
051: throw new ParseException("\"" + OpCodes.NOT_NAME
052: + "\" expecting single argument, not zero");
053: } else if (!(u1.isReturnBoolean())) {
054: throw new ParseException("\"" + OpCodes.NOT_NAME
055: + "\" given invalid argument " + u1
056: + " with non-boolean return type of "
057: + u1.getReturnClass());
058: }
059:
060: Op u2 = p.nextOp();
061: if (u2 != null) {
062: throw new ParseException("\"" + OpCodes.NOT_NAME
063: + "\" expecting single argument, "
064: + "but given additional "
065: + u2.getClass().toString());
066: }
067:
068: int u1ID = u1.getID();
069: if (u1ID == OpCodes.NOT_ID) {
070: return ((NotOp) u1).u1;
071: } else if (u1ID == OpCodes.TRUE_ID) {
072: return FalseOp.singleInstance;
073: } else if (u1ID == OpCodes.FALSE_ID) {
074: return TrueOp.singleInstance;
075: } else if (u1ID == OpCodes.INSTANCEOF_ID) {
076: // (not isX) is (isNotX) makes life _so_ much easier...
077: Type type = (Type) u1;
078: return OpBuilder.createInstanceOf((!(type.isNot())), type
079: .getClazz());
080: } else {
081: this .u1 = u1;
082: return this ;
083: }
084: }
085:
086: public final boolean execute(final Object o) {
087: return (!(u1.execute(o)));
088: }
089:
090: public final void setConst(final String key, final Object val) {
091: u1.setConst(key, val);
092: }
093:
094: public final void accept(TreeVisitor visitor) {
095: // (not op)
096: visitor.visitWord(OpCodes.NOT_NAME);
097: if (u1 != null) {
098: u1.accept(visitor);
099: } else {
100: visitor.visitConstant(null, "?");
101: }
102: visitor.visitEnd();
103: }
104: }
|