Skip to content

Commit 3e41c19

Browse files
committed
修改java 代码
1 parent 3d6215a commit 3e41c19

File tree

8 files changed

+1368
-36
lines changed

8 files changed

+1368
-36
lines changed

csharp/ToolGood.Algorithm2.Test/ConditionTrees/ConditionTreeTest.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
using PetaTest;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using ToolGood.Algorithm;
82
using ToolGood.Algorithm.Internals;
9-
using static ToolGood.Algorithm.Internals.ConditionTree;
103

114
namespace ToolGood.Algorithm2.Test.ConditionTrees
125
{
@@ -18,12 +11,12 @@ public void Test1()
1811
{
1912
string txt = "AA.IsText()=bb";
2013
var t1 = ConditionTree.Parse(txt);
21-
Assert.AreEqual(t1.Type, ConditionType.String);
14+
Assert.AreEqual(t1.Type, ConditionTreeType.String);
2215
Assert.AreEqual("AA.IsText()=bb", txt.Substring(t1.Start, t1.End - t1.Start + 1));
2316

2417
txt = "[bbb]=bb";
2518
t1 = ConditionTree.Parse(txt);
26-
Assert.AreEqual(t1.Type, ConditionType.String);
19+
Assert.AreEqual(t1.Type, ConditionTreeType.String);
2720
Assert.AreEqual(txt, txt.Substring(t1.Start, t1.End - t1.Start + 1));
2821
}
2922

@@ -33,7 +26,7 @@ public void Test2()
3326
string txt = "AA.IsText()=bb && dd=ss";
3427
var tree = ConditionTree.Parse(txt);
3528

36-
Assert.AreEqual(tree.Type, ConditionType.And);
29+
Assert.AreEqual(tree.Type, ConditionTreeType.And);
3730
var t1 = tree.Nodes[0];
3831
var t2 = tree.Nodes[1];
3932
Assert.AreEqual("AA.IsText()=bb", txt.Substring(t1.Start, t1.End - t1.Start + 1));
@@ -46,7 +39,7 @@ public void Test3()
4639
string txt = "AA.IsText()=bb || dd=ss";
4740
var tree = ConditionTree.Parse(txt);
4841

49-
Assert.AreEqual(tree.Type, ConditionType.Or);
42+
Assert.AreEqual(tree.Type, ConditionTreeType.Or);
5043
var t1 = tree.Nodes[0];
5144
var t2 = tree.Nodes[1];
5245
Assert.AreEqual("AA.IsText()=bb", txt.Substring(t1.Start, t1.End - t1.Start + 1));
@@ -60,7 +53,7 @@ public void Test4()
6053
string txt = "AA.IsText()=bb || (dd=ss && tt=22)";
6154
var tree = ConditionTree.Parse(txt);
6255

63-
Assert.AreEqual(tree.Type, ConditionType.Or);
56+
Assert.AreEqual(tree.Type, ConditionTreeType.Or);
6457
var t1 = tree.Nodes[0];
6558
var t2 = tree.Nodes[1];
6659
var t3 = t2.Nodes[0];
@@ -76,7 +69,7 @@ public void Test5()
7669
string txt = "AA.IsText()=bb || AND(dd=ss , tt=22)";
7770
var tree = ConditionTree.Parse(txt);
7871

79-
Assert.AreEqual(tree.Type, ConditionType.Or);
72+
Assert.AreEqual(tree.Type, ConditionTreeType.Or);
8073
var t1 = tree.Nodes[0];
8174
var t2 = tree.Nodes[1];
8275
Assert.AreEqual("AA.IsText()=bb", txt.Substring(t1.Start, t1.End - t1.Start + 1));
@@ -89,10 +82,10 @@ public void Test6()
8982
string txt = "AA.IsText()==bb && (dd=ss || tt=22)";
9083
var tree = ConditionTree.Parse(txt);
9184

92-
Assert.AreEqual(tree.Type, ConditionType.And);
85+
Assert.AreEqual(tree.Type, ConditionTreeType.And);
9386
var t1 = tree.Nodes[0];
9487
var t2 = tree.Nodes[1];
95-
Assert.AreEqual(t2.Type, ConditionType.Or);
88+
Assert.AreEqual(t2.Type, ConditionTreeType.Or);
9689
var t3 = t2.Nodes[0];
9790
var t4 = t2.Nodes[1];
9891

@@ -106,7 +99,7 @@ public void Test7()
10699
{
107100
string txt = "AA.IsText()==bb ? 1:2";
108101
var t1 = ConditionTree.Parse(txt);
109-
Assert.AreEqual(t1.Type, ConditionType.String);
102+
Assert.AreEqual(t1.Type, ConditionTreeType.String);
110103
Assert.AreEqual(txt, txt.Substring(t1.Start, t1.End - t1.Start + 1));
111104
}
112105

@@ -116,10 +109,10 @@ public void Test8()
116109
string txt = "AA.IsText()==bb && (dd=ss || {tt}=22)";
117110
var tree = ConditionTree.Parse(txt);
118111

119-
Assert.AreEqual(tree.Type, ConditionType.And);
112+
Assert.AreEqual(tree.Type, ConditionTreeType.And);
120113
var t1 = tree.Nodes[0];
121114
var t2 = tree.Nodes[1];
122-
Assert.AreEqual(t2.Type, ConditionType.Or);
115+
Assert.AreEqual(t2.Type, ConditionTreeType.Or);
123116
var t3 = t2.Nodes[0];
124117
var t4 = t2.Nodes[1];
125118

@@ -134,10 +127,10 @@ public void Test9()
134127
string txt = "AA.IsText()==bb && (dd=ss || [tt]==22)";
135128
var tree = ConditionTree.Parse(txt);
136129

137-
Assert.AreEqual(tree.Type, ConditionType.And);
130+
Assert.AreEqual(tree.Type, ConditionTreeType.And);
138131
var t1 = tree.Nodes[0];
139132
var t2 = tree.Nodes[1];
140-
Assert.AreEqual(t2.Type, ConditionType.Or);
133+
Assert.AreEqual(t2.Type, ConditionTreeType.Or);
141134
var t3 = t2.Nodes[0];
142135
var t4 = t2.Nodes[1];
143136

csharp/ToolGood.Algorithm2/Internals/ConditionTree.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ConditionTree
1111
public int Start { get; internal set; }
1212
public int End { get; internal set; }
1313

14-
public ConditionType Type { get; internal set; }
14+
public ConditionTreeType Type { get; internal set; }
1515

1616
public String ErrorMessage { get; internal set; }
1717

@@ -20,19 +20,12 @@ internal ConditionTree()
2020
Nodes = new List<ConditionTree>();
2121
}
2222

23-
public enum ConditionType
24-
{
25-
String,
26-
And,
27-
Or,
28-
Error
29-
}
3023

3124
public static ConditionTree Parse(string condition)
3225
{
3326
ConditionTree tree = new ConditionTree();
3427
if (string.IsNullOrWhiteSpace(condition)) {
35-
tree.Type = ConditionType.Error;
28+
tree.Type = ConditionTreeType.Error;
3629
tree.ErrorMessage = "condition 为空";
3730
return tree;
3831
}
@@ -47,18 +40,18 @@ public static ConditionTree Parse(string condition)
4740

4841
var context = parser.prog();
4942
if (antlrErrorListener.IsError) {
50-
tree.Type = ConditionType.Error;
43+
tree.Type = ConditionTreeType.Error;
5144
tree.ErrorMessage = antlrErrorListener.ErrorMsg;
5245
return tree;
5346
}
5447
var visitor = new MathSplitVisitor();
5548
return visitor.Visit(context);
5649

5750
} catch (Exception ex) {
58-
tree.Type = ConditionType.Error;
51+
tree.Type = ConditionTreeType.Error;
5952
tree.ErrorMessage = ex.Message + "\r\n" + ex.StackTrace;
6053
}
6154
return tree;
6255
}
6356
}
64-
}
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ToolGood.Algorithm.Internals
2+
{
3+
public enum ConditionTreeType
4+
{
5+
String,
6+
And,
7+
Or,
8+
Error
9+
10+
}
11+
}

csharp/ToolGood.Algorithm2/Internals/MathSplitVisitor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace ToolGood.Algorithm.Internals
99
{
1010
class MathSplitVisitor : AbstractParseTreeVisitor<ConditionTree>, ImathVisitor<ConditionTree>
1111
{
12-
internal string Text;
13-
1412
public ConditionTree VisitProg(mathParser.ProgContext context)
1513
{
1614
return Visit(context.expr());
@@ -20,9 +18,9 @@ public ConditionTree VisitAndOr_fun(mathParser.AndOr_funContext context)
2018
ConditionTree tree = new ConditionTree();
2119
var t = context.op.Text;
2220
if (CharUtil.Equals(t, "&&", "and")) {
23-
tree.Type = ConditionTree.ConditionType.And;
21+
tree.Type = ConditionTreeType.And;
2422
} else {
25-
tree.Type = ConditionTree.ConditionType.Or;
23+
tree.Type = ConditionTreeType.Or;
2624
}
2725
tree.Nodes.Add(this.Visit(context.expr(0)));
2826
tree.Nodes.Add(this.Visit(context.expr(1)));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package toolgood.algorithm.internals;
2+
3+
import org.antlr.v4.runtime.CharStreams;
4+
import org.antlr.v4.runtime.CommonTokenStream;
5+
import toolgood.algorithm.math.mathLexer;
6+
import toolgood.algorithm.math.mathParser;
7+
import toolgood.algorithm.math.mathParser2;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class ConditionTree {
13+
public List<ConditionTree> Nodes;
14+
15+
public int Start;
16+
public int End;
17+
18+
public ConditionTreeType Type;
19+
20+
public String ErrorMessage;
21+
22+
public ConditionTree() {
23+
Nodes = new ArrayList<>();
24+
}
25+
26+
27+
public static ConditionTree Parse(String condition) {
28+
ConditionTree tree = new ConditionTree();
29+
if (condition == null || condition.equals("")) {
30+
tree.Type = ConditionTreeType.Error;
31+
tree.ErrorMessage = "condition 为空";
32+
return tree;
33+
}
34+
try {
35+
final AntlrCharStream stream = new AntlrCharStream(CharStreams.fromString(condition));
36+
final mathLexer lexer = new mathLexer(stream);
37+
final CommonTokenStream tokens = new CommonTokenStream(lexer);
38+
final mathParser parser = new mathParser(tokens);
39+
final AntlrErrorListener antlrErrorListener = new AntlrErrorListener();
40+
parser.removeErrorListeners();
41+
parser.addErrorListener(antlrErrorListener);
42+
final mathParser2.ProgContext context = parser.prog();
43+
44+
if (antlrErrorListener.IsError) {
45+
tree.ErrorMessage = antlrErrorListener.ErrorMsg;
46+
return tree;
47+
}
48+
49+
final MathSplitVisitor visitor = new MathSplitVisitor();
50+
return visitor.visit(context);
51+
} catch (Exception ex) {
52+
tree.Type = ConditionTreeType.Error;
53+
tree.ErrorMessage = ex.getMessage();
54+
}
55+
return tree;
56+
}
57+
58+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package toolgood.algorithm.internals;
2+
3+
public enum ConditionTreeType {
4+
String,
5+
And,
6+
Or,
7+
Error
8+
}

0 commit comments

Comments
 (0)