?? transformmodel.java
字號(hào):
package reg402;
import java.util.Stack;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.LinkedList;
import java.util.ArrayList;
public class TransformModel implements Constants {
String[] regExp; //正則表達(dá)式的分解串
Graph transformGraph; //圖形結(jié)構(gòu),用于存放結(jié)點(diǎn)和邊
GraphNode[] node;
private int temp;
private Stack identifyStack; //棧結(jié)構(gòu),用于存放標(biāo)識(shí)符
private Stack operatorStack; //棧結(jié)構(gòu),用于存入運(yùn)算符
private Stack boundsStack; //存入圖在視圖中的范圍
private Stack graphStack; //存放圖的結(jié)點(diǎn)集
private int[] set1, set2;
private Point p1, p2;
private Rectangle bounds1, bounds2;
private LinkedList specialEdgeList; //存放特殊邊的鏈表
public TransformModel() {
}
public boolean setRegExpression(String exp) {
//設(shè)置正則表達(dá)式的字符串集形式
int n = 0;
if(canSplit(exp)) regExp = splitExp(exp);
else return false;
for(int i = 0; i < regExp.length; i++)
if(!regExp[i].equals("") && regExp[i].compareTo("(") != 0
&& regExp[i].compareTo(")") != 0) n++;
transformGraph = new Graph(2*n);
node = new GraphNode[2*n];
for(int i = 0; i < 2*n; i++) {
node[i] = new GraphNode();
}
return true;
}
private boolean canSplit(String exp) {
//exp是否可以分解
int n1 = 0, n2 = 0, n3 = 0;
for(int i = 0; i < exp.length(); i++) {
if (exp.charAt(i) == '\'') n1++;
if(exp.charAt(i) == '(') n2++;
if(exp.charAt(i) == ')') n3++;
}
return (n1%2 == 0 && n2 == n3);
}
private String[] splitExp(String exp) {
//把字符串按意愿分解成字符串集
char ch;
int id = 0;
int n = exp.length();
String[] expSet = new String[n+1];
StringBuffer buf;
for(int i = 0; i < n + 1; i++)
expSet[i] = "";
for(int i = 0; i < n; i++) {
ch = exp.charAt(i);
if (ch == '\'') { //可以由''來決定一個(gè)串
buf = new StringBuffer();
ch = exp.charAt(++i);
while (ch != '\'') {
buf.append(ch);
ch = exp.charAt(++i);
}
expSet[id++] = new String(buf);
}
else expSet[id++] = String.valueOf(ch); //也可以由單個(gè)
//字符構(gòu)成
}
return expSet;
}
public Graph getGraph() {
return transformGraph;
}
public boolean createNFA() {
//創(chuàng)建非確定有窮自動(dòng)機(jī)
identifyStack = new Stack();
operatorStack = new Stack();
boundsStack = new Stack();
graphStack = new Stack();
specialEdgeList = new LinkedList();
temp = 0;
int n = regExp.length;
char ch; //從運(yùn)算棧得到的字符
int i = 0;
while(regExp[i] != "") {
if(!isOperator(regExp[i])) { //如是標(biāo)識(shí)符...
GraphNode n1 = node[temp++];
GraphNode n2 = node[temp++];
basicOperate(n1, n2, n, i);
if(regExp[i+1] != "" && (!isOperator(regExp[i+1]) || regExp[i+1].equals("(")))
operatorStack.push(new Character('^'));
}
else if(isOperator(regExp[i])) { //如是運(yùn)算符...
ch = regExp[i].charAt(0); //為了方便處理,把字符串轉(zhuǎn)換成字符
switch(ch) {
case '*': //*的優(yōu)先級(jí)較高, 故先計(jì)算
if(identifyStack.empty())
return false; //標(biāo)識(shí)符棧為空而運(yùn)算符棧不為空,故表達(dá)式錯(cuò)誤
repeatOperate();
if(regExp[i+1] != "" && (!isOperator(regExp[i+1]) || regExp[i+1].equals("(")))
operatorStack.push(new Character('^'));
break;
case '|': //可以進(jìn)行兩種運(yùn)算^和|
if(identifyStack.empty())
return false; //標(biāo)識(shí)符棧為空而運(yùn)算符棧不為空,故表達(dá)式錯(cuò)誤
if(!operatorStack.isEmpty()) {
ch = ((Character)operatorStack.peek()).charValue();
while(ch != '(') {
ch = ((Character)operatorStack.pop()).charValue();;
p2 = (Point)identifyStack.pop();
p1 = (Point)identifyStack.pop();
bounds2 = (Rectangle)boundsStack.pop();
bounds1 = (Rectangle)boundsStack.pop();
set2 = (int[])graphStack.pop();
set1 = (int[])graphStack.pop();
switch(ch) {
case '|':
choiceOperate();
break;
case '^':
appositeOperate();
break;
}
if(!operatorStack.isEmpty())
ch = ((Character)operatorStack.peek()).charValue();
else break;
}
}
operatorStack.push(new Character('|'));
break;
case '^': //進(jìn)行一種運(yùn)算^
if(identifyStack.empty())
return false; //標(biāo)識(shí)符棧為空而運(yùn)算符棧不為空,故表達(dá)式錯(cuò)誤
if(!operatorStack.isEmpty()) {
ch = ((Character)operatorStack.peek()).charValue();
if(ch == '^') {
p2 = (Point) identifyStack.pop();
p1 = (Point) identifyStack.pop();
bounds2 = (Rectangle) boundsStack.pop();
bounds1 = (Rectangle) boundsStack.pop();
set2 = (int[]) graphStack.pop();
set1 = (int[]) graphStack.pop();
ch = ( (Character) operatorStack.peek()).charValue();
if(ch == '^')
appositeOperate();
}
if(!operatorStack.isEmpty())
ch = ((Character)operatorStack.peek()).charValue();
else break;
}
operatorStack.push(new Character('^'));
break;
case '(': //直接進(jìn)入運(yùn)算棧
operatorStack.add(new Character('('));
break;
case ')': //計(jì)算在括號(hào)內(nèi)的值
if(identifyStack.empty())
return false; //標(biāo)識(shí)符棧為空而運(yùn)算符棧不為空,故表達(dá)式錯(cuò)誤
ch = ((Character)operatorStack.pop()).charValue();
while(ch != '(') {
p2 = (Point)identifyStack.pop();
p1 = (Point)identifyStack.pop();
bounds2 = (Rectangle)boundsStack.pop();
bounds1 = (Rectangle)boundsStack.pop();
set2 = (int[])graphStack.pop();
set1 = (int[])graphStack.pop();
switch(ch) {
case '^':
appositeOperate();
break;
case '|':
choiceOperate();
break;
}
ch = ((Character)operatorStack.pop()).charValue();
}
if(regExp[i+1] != "" && (!isOperator(regExp[i+1]) || regExp[i+1].equals("(")))
operatorStack.push(new Character('^'));
break;
default: break;
}
}
i++;
}
while(!operatorStack.isEmpty()) {
ch = ((Character)operatorStack.pop()).charValue();
p2 = (Point)identifyStack.pop();
p1 = (Point)identifyStack.pop();
bounds2 = (Rectangle)boundsStack.pop();
bounds1 = (Rectangle)boundsStack.pop();
set2 = (int[])graphStack.pop();
set1 = (int[])graphStack.pop();
switch(ch) {
case '^':
appositeOperate();
break;
case '|':
choiceOperate();
break;
}
}
return true;
}
private void basicOperate(GraphNode n1, GraphNode n2, int n, int i) {
//處理基本正則表達(dá)式
int[] graphSet = new int[2*n];
for(int loop = 0; loop < 2*n; loop++) {
graphSet[loop] = -1; //將要放入圖的結(jié)點(diǎn)集棧中
}
//設(shè)置結(jié)點(diǎn)在視圖上的初始值
n1.setPosition(new Point(-2*RADIUS, 0));
n2.setPosition(new Point(2*RADIUS, 0));
transformGraph.setStart(n1);
transformGraph.setEnd(n2);
transformGraph.addEdge(n1, n2, regExp[i]);
identifyStack.push(new Point(n1.getID(), n2.getID()));
//把當(dāng)前邊的范圍壓入棧
boundsStack.push(new Rectangle(-3*RADIUS, -RADIUS,
6*RADIUS, 2*RADIUS));
//把邊集壓入棧, 不為-1的表示所對(duì)應(yīng)的結(jié)點(diǎn)存在
graphSet[n1.getID()] = n1.getID();
graphSet[n2.getID()] = n2.getID();
graphStack.push(graphSet);
}
private void repeatOperate() {
//重復(fù)
Point p = (Point)identifyStack.pop(); //標(biāo)識(shí)符棧出棧
Rectangle bounds = (Rectangle)boundsStack.pop(); //范圍棧出棧
int[] set = (int[])graphStack.pop();
EdgeLink special;
GraphNode n1 = node[p.x],
n2 = node[p.y],
n3 = node[temp++],
n4 = node[temp++];
//設(shè)置n3, n4在視圖中的初始位置
n3.setPosition(new Point(n1.getPosition().x - 4*RADIUS,
n1.getPosition().y));
n4.setPosition(new Point(n2.getPosition().x + 4*RADIUS,
n2.getPosition().y));
set[n3.getID()] = n3.getID();
set[n4.getID()] = n4.getID();
transformGraph.setStart(n3);
transformGraph.setEnd(n4);
transformGraph.addRepeatEdge(n1, n2, n3, n4);
identifyStack.push(new Point(n3.getID(), n4.getID()));
boundsStack.push(new Rectangle(n3.getPosition().x - RADIUS,
bounds.y - 2*RADIUS, bounds.width + 8*RADIUS,
bounds.height + 4*RADIUS));
graphStack.push(set);
special = new EdgeLink(n2, n1);
specialEdgeList.add(new SpecialEdge(special, (Rectangle)boundsStack.peek()));
special = new EdgeLink(n3, n4);
specialEdgeList.add(new SpecialEdge(special, (Rectangle)boundsStack.peek()));
}
private void choiceOperate() {
//在各項(xiàng)中選擇
int offset;
offset = -(bounds1.height + bounds1.y + RADIUS);
moveY(set1, offset);
offset = -bounds2.y + RADIUS;
moveY(set2, offset);
GraphNode n3 = node[temp++];
GraphNode n4 = node[temp++];
transformGraph.setStart(n3);
transformGraph.setEnd(n4);
transformGraph.addChoiceEdge(node[p2.x], node[p2.y],
node[p1.x], node[p1.y],
n3, n4);
int width = Math.max(bounds1.width, bounds2.width);
n3.setPosition(new Point(-width/2 - 3*RADIUS, 0));
n4.setPosition(new Point(width/2 + 3*RADIUS, 0));
int[] total = new int[set1.length];
for(int loop = 0; loop < total.length; loop++) {
total[loop] = set1[loop] + set2[loop] + 1;
}
total[n3.getID()] = n3.getID();
total[n4.getID()] = n4.getID();
identifyStack.push(new Point(n3.getID(), n4.getID()));
boundsStack.push(new Rectangle(n3.getPosition().x - RADIUS, -bounds1.height - RADIUS,
width + 8*RADIUS, bounds1.height + bounds2.height + 2*RADIUS));
graphStack.push(total);
}
private void appositeOperate() {
//并置
int offset;
offset = -bounds1.width/2 - RADIUS;
moveX(set1, offset);
offset = bounds2.width/2 + RADIUS;
moveX(set2, offset);
int[] total = new int[set1.length];
for(int loop = 0; loop < total.length; loop++) {
total[loop] = set1[loop] + set2[loop] + 1;
}
int height = Math.max(bounds1.height, bounds2.height);
transformGraph.setStart(node[p1.x]);
transformGraph.setEnd(node[p2.y]);
transformGraph.addAppositiveEdge(node[p1.y], node[p2.x]);
identifyStack.push(new Point(p1.x, p2.y));
graphStack.push(total);
Rectangle bounds = new Rectangle(-bounds1.width - RADIUS, Math.min(bounds1.y, bounds2.y),
bounds1.width + bounds2.width + 2*RADIUS, height);
offset = -bounds.x - bounds.width/2;
moveX(total, offset);
bounds.x += offset;
boundsStack.push(bounds);
}
private void moveX(int[] set, int offset) {
for(int i = 0; i < set.length; i++)
if(set[i] != -1) {
Point p = node[set[i]].getPosition();
node[set[i]].setPosition(new Point(p.x + offset, p.y));
}
}
private void moveY(int[] set, int offset) {
for(int i = 0; i < set.length; i++)
if(set[i] != -1) {
Point p = node[set[i]].getPosition();
node[set[i]].setPosition(new Point(p.x, p.y + offset));
}
}
public Rectangle getBounds() {
//返回圖在視圖中的初始位置
return (Rectangle)boundsStack.peek();
}
public LinkedList getSpecialEdge() {
return specialEdgeList;
}
private boolean isOperator(String str) {
//判斷是否運(yùn)算符(包括())
if(str.compareTo("*") == 0 | str.compareTo("|") == 0
| str.compareTo("(") == 0 | str.compareTo(")") == 0)
return true;
return false;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -