⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astoperationexpression.java

📁 UML设计测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * USE - UML based specification environment * Copyright (C) 1999-2004 Mark Richters, University of Bremen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* $ProjectHeader: use 2-3-0-release.1 Mon, 12 Sep 2005 20:18:33 +0200 green $ */package org.tzi.use.parser.ocl;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.tzi.use.config.Options;import org.tzi.use.parser.Context;import org.tzi.use.parser.ExprContext;import org.tzi.use.parser.MyToken;import org.tzi.use.parser.SemanticException;import org.tzi.use.uml.mm.MAttribute;import org.tzi.use.uml.mm.MClass;import org.tzi.use.uml.mm.MNavigableElement;import org.tzi.use.uml.mm.MOperation;import org.tzi.use.uml.ocl.expr.ExpAttrOp;import org.tzi.use.uml.ocl.expr.ExpInvalidException;import org.tzi.use.uml.ocl.expr.ExpNavigation;import org.tzi.use.uml.ocl.expr.ExpObjAsSet;import org.tzi.use.uml.ocl.expr.ExpObjOp;import org.tzi.use.uml.ocl.expr.ExpStdOp;import org.tzi.use.uml.ocl.expr.ExpVariable;import org.tzi.use.uml.ocl.expr.Expression;import org.tzi.use.uml.ocl.type.CollectionType;import org.tzi.use.uml.ocl.type.ObjectType;import org.tzi.use.uml.ocl.type.Type;import org.tzi.use.util.StringUtil;/** * Node of the abstract syntax tree constructed by the parser. * * @version     $ProjectVersion: 2-3-0-release.1 $ * @author  Mark Richters */public class ASTOperationExpression extends ASTExpression {    private MyToken fOp;    private ASTExpression fSrcExpr;    private List fArgs;     // (ASTExpression)     private boolean fHasParentheses;    private boolean fFollowsArrow;    private Expression[] fArgExprs;    private boolean fIsPre;    private MyToken fExplicitRolename;    public ASTOperationExpression(MyToken op,                                   ASTExpression source,                                   boolean followsArrow) {        fOp = op;        fSrcExpr = source;        fArgs = new ArrayList();        fHasParentheses = false;        fFollowsArrow = followsArrow;    }    public void addArg(ASTExpression arg) {        fArgs.add(arg);    }    public void setIsPre() {        fIsPre = true;    }    public void hasParentheses() {        fHasParentheses = true;    }    public void setExplicitRolename( MyToken rolename ) {        fExplicitRolename = rolename;    }    /*      operation may be one of:       (1) predefined OCL operation      (2) attribute operation on object type (no arguments)      (3) "isQuery" operation on object type (possibly with arguments)      (4) navigation operation on object type      (5) shorthand for collect      (6) set operation on single object resulting from      navigation over associations with multiplicity zero or one      (p. 7-13 of OMG UML 1.3)      (7) variable      possible combinations of syntax:      source expression:      none or      s: simple value      o: object      c: collection      s.op    110 (1)      s.op()  111 (1)      s->op   120 (1) with warning      s->op() 121 (1) with warning              o.op    210 (2,4,1)      o.op[nav]  1210 (4)      o.op()  211 (3,1)      o->op   220 (6) if s has object type and results from navigating 0..1 end      o->op() 221 (6) to allow uniform syntax of operation calls              c.op    410 (5) with implicit (2,4,1)      c.op()  411 (5) with implicit (3,1)      c->op   420 (1)      c->op() 421 (1)      op      000 (2,4,7)      op()    001 (1,3)    */    public Expression gen(Context ctx) throws SemanticException {        Expression res = null;        Expression srcExpr = null;        String opname = fOp.getText();        if (fSrcExpr != null ) {            srcExpr = fSrcExpr.gen(ctx);            res = gen1(ctx, srcExpr);        } else {            // if no source expression is given, it is either a            // variable or a reference to expression determined by            // context. In the latter case we use the context as            // source expression and proceed as if it has been given            // explicitly.            if (! fHasParentheses ) {                // variable?                // (7) check for variable                Type type = ctx.varTable().lookup(opname);                if (type != null )                    res = new ExpVariable(opname, type);            }            // do we have a context expression that is implicitly            // assumed to be the source expression?            if (res == null ) {                ExprContext ec = ctx.exprContext();                if (! ec.isEmpty() ) {                    // construct source expression                    ExprContext.Entry e = ec.peek();                    srcExpr = new ExpVariable(e.fName, e.fType);                    if (e.fType.isCollection() )                        fFollowsArrow = true;                    res = gen1(ctx, srcExpr);                } else                    throw new SemanticException(fOp, "Undefined " +                                                 ( fHasParentheses ? "operation" : "variable" ) +                                                 " `" + opname + "'.");            }        }        if (fIsPre ) {            if (! ctx.insidePostCondition() )                 throw new SemanticException(fOp,                                             "Modifier @pre is only allowed in postconditions.");            res.setIsPre();        }        if (opname.equals("oclIsNew") ) {            if (! ctx.insidePostCondition() )                 throw new SemanticException(fOp,                                             "Operation oclIsNew is only allowed in postconditions.");        }        return res;    }        private Expression gen1(Context ctx, Expression srcExpr)         throws SemanticException     {        Expression res = null;        String opname = fOp.getText();        Type srcType = srcExpr.type();                // generate argument expressions        fArgExprs = new Expression[fArgs.size() + 1];        fArgExprs[0] = srcExpr;        Iterator it = fArgs.iterator();        int i = 1;        while (it.hasNext() ) {            ASTExpression astExpr = (ASTExpression) it.next();            fArgExprs[i++] = astExpr.gen(ctx);        }            // flags for various cases        final int SRC_SIMPLE_TYPE     = 0x0100;        final int SRC_OBJECT_TYPE     = 0x0200;        final int SRC_COLLECTION_TYPE = 0x0400;        final int DOT                 = 0x0010;        final int ARROW               = 0x0020;        final int NO_EXPLICIT_ROLENAME = 0x0000;        final int EXPLICIT_ROLENAME = 0x1000;        final int NO_PARENTHESES      = 0x0000;        final int PARENTHESES         = 0x0001;        int opcase;        if (srcType.isObjectType() )            opcase = SRC_OBJECT_TYPE;        else if (srcType.isCollection() )            opcase = SRC_COLLECTION_TYPE;        else            opcase = SRC_SIMPLE_TYPE;        opcase += fFollowsArrow ? ARROW : DOT;        opcase += fHasParentheses ? PARENTHESES : NO_PARENTHESES;        opcase += fExplicitRolename != null ? EXPLICIT_ROLENAME : NO_EXPLICIT_ROLENAME;        switch ( opcase ) {        case SRC_SIMPLE_TYPE + DOT + NO_PARENTHESES:         case SRC_SIMPLE_TYPE + DOT + PARENTHESES:        case SRC_COLLECTION_TYPE + ARROW + PARENTHESES:        case SRC_COLLECTION_TYPE + ARROW + NO_PARENTHESES:            // (1) predefined OCL operation            res = genStdOperation(ctx, fOp, opname, fArgExprs);            break;        case SRC_SIMPLE_TYPE + ARROW + NO_PARENTHESES:        case SRC_SIMPLE_TYPE + ARROW + PARENTHESES:            ctx.reportWarning(fOp, "application of `" + opname +                               "' to a single value should be done with `.' " +                              "instead of `->'.");            // (1) predefined OCL operation            res = genStdOperation(ctx, fOp, opname, fArgExprs);            break;        case SRC_OBJECT_TYPE + DOT + NO_PARENTHESES:            MClass srcClass = ((ObjectType) srcType).cls();            MAttribute attr = srcClass.attribute(opname, true);            if (attr != null ) {                // (2) attribute operation on object type (no arguments)                res = new ExpAttrOp(attr, srcExpr);            } else {                // (4) navigation operation on object type

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -