bpel2remarkpi.java

来自「这是我们参加06年全国开源软件的竞赛作品」· Java 代码 · 共 1,272 行 · 第 1/4 页

JAVA
1,272
字号
/*
Copyright (C) 1989, 1991 Free Software Foundation, Inc.  
51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

author: Yuan yongfu  lijin   liyong   lib 511,the College of Mathematics and Computer Science,HuNan Normal University,China

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

*/
package edu.hunnu.webjetchecker.convert;

import java.io.File;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import edu.hunnu.webjetchecker.*;
import edu.hunnu.webjetchecker.convert_interface.Bpel2Pi_inter;

/**
 * @author Ly
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

// 这是在原来的基础上,针对宋燕提出的具体情况做了以下更改:
// 1. 把linkSource变成了二元组输出和接受名字
// 2. 将所有的匹配结构去除,影响的结构主要是while , switch ;
// 3.
public class Bpel2RemarkPi extends Bpel2PiTop implements Bpel2Pi_inter {
    // idea: 将bpel文件结构看作一棵树,深度遍历所有节点,递归调用所有分支!
    // 在一字符串传递祖先节点的信息,父与子之间的信息用 ;隔开,某个节点的前置喝后置条件用 & 隔开 。
    public int sequenceTimes = 0; // 专门用于parseSequence中记录它已经被调用的次数

    public int switchTimes = 0;

    public int flowTimes = 0;

    public int pickTimes = 0;

    public int whileTimes = 0;

    public int linkSourceTimes = 0;

    public int linkTargetTimes = 0;

    public int invokeTimes = 0;

    public int receiveTimes = 0;

    public int replyTimes = 0;

    public int assignTimes = 0;

    public int waitTimes = 0;

    public int terminateTimes = 0;

    public int emptyTimes = 0;

    public int throwTimes = 0;

    public int onAlarmTimes = 0;

    public int onMessageTimes = 0;

    public int caseTimes = 0;

    public int otherwiseTimes = 0;

    private File file;

    // 建立两个字符串数组!一个存储全部的后置名字的通道名(不含"'"),第二个字符串数组全部置为空!如果后面的子节点(flow)对某个后置名进行了分裂,则
    // 在对应的第二个数组中将分裂对应的输入动作置入。
    // 在活动之前加前置条件时(sequence),先将前置条件的每个名字的通道名在第一数组中查找,得到index,
    // 再对第二个数组的对应位置查找,如果不为空,则表示该名字曾经分裂,则用该内容替换前置条件!
    ArrayList array1 = new ArrayList();

    ArrayList array2 = new ArrayList();

    int listIndex = 0;

    private String result = "";

    private String agentHead = "";

    // 本程序的link的处理也是尽量在某个结点处理,不能把它传递到其子节点。对于flow结构,如<flow>P1,P2,P3</flow>则对每个进程
    // 添加一个后置条件'ai<xi>,然后再增加一个新的并发进程P4!P4 = a1(y1).a2(y2).a3(y3).'link<value>;

    // public int controlRearCondition = 0 ; //这个条件用于对生成的字符串进行替换操作
    /*
     * 如<sequence> <flow1> <flow2> P1 <flow> </flow> P2 </sequence> 显然
     * sequence对flow1有个后置输出,同时对P2有个前置输入。经过flow2时,后置输出要再进行分裂
     * 为使分裂后的与P2的前置输入保持,则应对前置输入进行必要的替换! 而且这个替换要等到所有的节点的分析都结束后!
     */

    /*
     * 本程序的两个关键点: 1.顺序算子的设置, 2,linkSource 和 linkTarget的表示! 注意这两个点的相同与区别!
     */

    ActivityMap activityMap = new ActivityMap(); // 全局对象

    /**
     * 
     */
    public Bpel2RemarkPi() {
        super();

        // TODO Auto-generated constructor stub
    }

    public Bpel2RemarkPi(File file) {
        this.file = file;
        Bpel bpel = new Bpel(this.file);
        this.convert(bpel);
    }

    public String getResult() {
        return this.result;
    }

    public String getAgentHead() {
        return this.agentHead;
    }

    /*
     * (non-Javadoc)
     * 
     * @see edu.hunnu.webjetchecker.bpel2pi.Bpel2Pi#convert(edu.hunnu.webjetchecker.Bpel)
     */
    public Agent convert(Bpel bpel) {
        // TODO Auto-generated method stub
        Element root = bpel.getRoot();
        if (root != null) {

            Agent agent = new Agent();
            agent = this.ParseBPEL(root);

            String newString = "P(" + agent.getFreeName() + ")";
            agentHead = newString;
            result = modifyString(agent.toString(), newString);
        } else {
            System.out.println("This file is not a BPEL");
        }
        return null;
    }

    public Agent ParseBPEL(Node root) {

        String engineName = "Engine"; // 设置一个初始的进程agent
        Agent agent = new Agent();
        agent.setSelfAgentName(engineName);

        for (Node child = root.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            int activityType = activityMap.getType(child.getNodeName()
                    .toLowerCase());

            if (activityType != 99) {
                String afterInfo = "";
                agent = ParseNode(child, agent, afterInfo);
            }

        }

        return agent;
    }

    public Agent ParseActivity(Node node, Agent agent, String afterInfo) {

        for (Node child = node.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            int activityType = activityMap.getType(child.getNodeName()
                    .toLowerCase());

            if (activityType != 99) {
                agent = ParseNode(child, agent, afterInfo);
            }
        }
        return agent;
    }

    public Agent GenerateNewAgent() {

        Agent agent = new Agent();
        int agentNum = 1;
        String agentName = "Engine" + String.valueOf(agentNum);
        String nextAgentName = "Engine" + String.valueOf(agentNum + 1);
        agent.setSelfAgentName(agentName);
        agent.setToAgentName(nextAgentName);
        agentNum++;

        return agent;

    }

    public Agent ParseNode(Node node, Agent agent, String afterInfo) {

        int activityType = activityMap.getType(node.getNodeName());
        switch (activityType) {
        case 0:
            agent = ParseSequence(node, agent, afterInfo);
            // str += strSequence;
            break;
        case 1:
            agent = ParseSwitch(node, agent, afterInfo);
            // str += strSwitch;
            break;
        case 2:
            agent = ParsePick(node, agent, afterInfo);
            // str += strPick;
            break;
        case 3:
            agent = ParseWhile(node, agent, afterInfo);
            // str += strWhile;
            break;
        case 4:
            agent = ParseInvoke(node, agent, afterInfo);
            // str += strInvoke;
            break;
        case 5:
            agent = ParseFlow(node, agent, afterInfo);
            // str += strFlow;
            break;
        case 6:
            agent = ParseReceive(node, agent, afterInfo);
            // str += strReceive;
            break;
        case 7:
            agent = ParseReply(node, agent, afterInfo);
            // str += strReply;
            break;
        case 8:
            agent = ParseAssign(node, agent, afterInfo);
            // str += strAssign;
            break;
        case 12:
            agent = ParsePartnerLinks(node, agent, afterInfo);
            break;
        case 13:
            agent = ParseVariables(node, agent, afterInfo);
            break;
        case 14:
            agent = ParseFaultHandlers(node, agent, afterInfo);
            break;
        default:
            break;

        }
        return agent;

    }

    public Agent ParseSequence(Node node, Agent agent, String afterInfo) {
        // 用并发表示顺序
        // 顺序在并发中通过通道的传值来表示通道和传的值用a和b加序号来表示

        sequenceTimes += 1;

        String str = "";
        String sourceStr = "";
        String targetStr = "";
        String sequenceStr = String.valueOf(sequenceTimes);

        for (Node child = node.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeName().equalsIgnoreCase("source")) {
                sourceStr += ParseLinkSource(child, agent);
            }

            if (child.getNodeName().equalsIgnoreCase("target")) {
                targetStr += ParseLinkTarget(child, agent);

            }
        }

        int childsCount = getValidChildsNum(node);
        String inputActionName = "";
        String outputActionName = "";
        String inputValue = "";
        String outputValue = "";
        String seTimeStr = String.valueOf(sequenceTimes);
        String input[] = new String[childsCount + 1];// 从2---------childsCount存储,索取的时候从2开始!
        input[0] = "";
        input[1] = "";
        String output[] = new String[childsCount + 1]; // 从1---------childsCount-1存储
        output[0] = "";
        output[childsCount] = "";
        for (int i = 1; i < childsCount; i++) {// 对n个孩子构造n-1个前置和后置条件对!
            // 用来构造sequence中的所有儿子节点中要用到的并发算子
            // 并发算子的actionName构造采用这种形式:节点的首字母 + 已分析的次数 + 该孩子节点隶属的位置
            // -----保证该名字的唯一性
            // 输出名 :" o " + "se" + + 已分析的次数 + 该孩子节点隶属的位置
            // 输入名 :" i " + "se" + + 已分析的次数 + 该孩子节点隶属的位置
            inputActionName = "se" + seTimeStr + String.valueOf(i);
            inputValue = "ise" + seTimeStr + String.valueOf(i);
            outputActionName = "'se" + seTimeStr + String.valueOf(i);
            outputValue = "ose" + seTimeStr + String.valueOf(i);
            // agent.setFreeName(inputActionName+"&"+outputValue);
            // agent.setBoundName(inputValue);
            // 将用于顺序的算子的通道名改为限制名! update by ly ------13.03.06
            agent.setBoundName(outputValue);
            agent.setBoundName(inputValue);
            agent.setBoundName(inputActionName);

            array1.add(listIndex, new String(inputActionName));
            array2.add(listIndex, new String(""));
            listIndex++;
            input[i + 1] = inputActionName + "(" + inputValue + ")";
            output[i] = outputActionName + "<" + outputValue + ">";
        }

        int childsNum = 0;

⌨️ 快捷键说明

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