Skip to content

Commit 308fbbb

Browse files
author
Boyce.Zhang
committed
add login expression parser
1 parent 0cc6d3f commit 308fbbb

File tree

5 files changed

+348
-4
lines changed

5 files changed

+348
-4
lines changed

Algorithm.iml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</configuration>
1010
</facet>
1111
</component>
12-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
12+
<component name="NewModuleRootManager" inherit-compiler-output="false">
1313
<output url="file://$MODULE_DIR$/target/classes" />
1414
<output-test url="file://$MODULE_DIR$/target/test-classes" />
1515
<exclude-output />
@@ -20,9 +20,11 @@
2020
<orderEntry type="inheritedJdk" />
2121
<orderEntry type="sourceFolder" forTests="false" />
2222
<orderEntry type="library" name="lib" level="project" />
23-
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
24-
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
2523
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.3.1" level="project" />
24+
<orderEntry type="library" name="Maven: net.sourceforge.jexcelapi:jxl:2.6.12" level="project" />
25+
<orderEntry type="library" name="Maven: log4j:log4j:1.2.14" level="project" />
26+
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-core:5.3.1" level="project" />
27+
<orderEntry type="library" name="Maven: org.mongodb:mongo-java-driver:3.1.0" level="project" />
2628
</component>
2729
</module>
2830

src/common/utils/AssertUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package common.utils;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: Boyce
6+
* Date: 9/11/15
7+
* Time: 15:02
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public abstract class AssertUtils {
11+
12+
public static void assertNotNull(Object obj) {
13+
if (null == obj)
14+
throw new IllegalArgumentException();
15+
}
16+
17+
public static void assertNotNull(Object obj, String msg) {
18+
if (null == obj)
19+
throw new IllegalArgumentException(msg);
20+
}
21+
22+
public static void assertNotEmpty(String str) {
23+
if (StringUtils.isEmpty(str))
24+
throw new IllegalArgumentException();
25+
}
26+
27+
public static void assertNotEmpty(String str, String msg) {
28+
if (StringUtils.isEmpty(str))
29+
throw new IllegalArgumentException(msg);
30+
}
31+
32+
public static void assertTrue(boolean val) {
33+
if (!val) throw new IllegalArgumentException();
34+
}
35+
36+
public static void assertTrue(boolean val, String msg) {
37+
if (!val) throw new IllegalArgumentException(msg);
38+
}
39+
}

src/expression/LogicExpression.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package expression;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: Boyce
9+
* Date: 3/11/15
10+
* Time: 15:35
11+
* To change this template use File | Settings | File Templates.
12+
*/
13+
public abstract class LogicExpression {
14+
15+
public abstract boolean matches(String source);
16+
17+
public static class Primitives extends LogicExpression {
18+
private String target;
19+
public Primitives(String target) {
20+
this.target = target;
21+
}
22+
23+
@Override
24+
public boolean matches(String source) {
25+
return null != source && source.toLowerCase().contains(target.toLowerCase());
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return target;
31+
}
32+
}
33+
34+
public static class Regex extends LogicExpression {
35+
private Pattern pattern;
36+
37+
public Regex(Pattern pattern) {
38+
this.pattern = pattern;
39+
}
40+
41+
@Override
42+
public boolean matches(String source) {
43+
Matcher m = pattern.matcher(source);
44+
return m.find();
45+
}
46+
}
47+
48+
public static class Not extends LogicExpression {
49+
private LogicExpression expression;
50+
51+
public Not(LogicExpression expression) {
52+
this.expression = expression;
53+
}
54+
55+
@Override
56+
public boolean matches(String source) {
57+
return !expression.matches(source);
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "^" + expression.toString();
63+
}
64+
}
65+
66+
public static class And extends LogicExpression {
67+
private LogicExpression expression1;
68+
private LogicExpression expression2;
69+
70+
public And(LogicExpression expression1, LogicExpression expression2) {
71+
this.expression1 = expression1;
72+
this.expression2 = expression2;
73+
}
74+
75+
@Override
76+
public boolean matches(String source) {
77+
return expression1.matches(source)
78+
&& expression2.matches(source);
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return expression1.toString() + "&" + expression2.toString();
84+
}
85+
}
86+
87+
public static class Or extends LogicExpression {
88+
private LogicExpression expression1;
89+
private LogicExpression expression2;
90+
91+
public Or(LogicExpression expression1, LogicExpression expression2) {
92+
this.expression1 = expression1;
93+
this.expression2 = expression2;
94+
}
95+
96+
@Override
97+
public boolean matches(String source) {
98+
return expression1.matches(source)
99+
|| expression2.matches(source);
100+
}
101+
102+
@Override
103+
public String toString() {
104+
return expression1.toString() + "|" + expression2.toString();
105+
}
106+
}
107+
108+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package expression;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: Boyce
10+
* Date: 3/11/15
11+
* Time: 15:59
12+
* To change this template use File | Settings | File Templates.
13+
*/
14+
public class LogicExpressionParser {
15+
private ExpressionReader reader;
16+
private LogicExpressionParser(String expression) {
17+
this.reader = new ExpressionReader(expression);
18+
}
19+
20+
public static LogicExpression parse(String expression) {
21+
try {
22+
LogicExpressionParser parser = new LogicExpressionParser(expression);
23+
LogicExpression le = parser.calculatePostfixExpression();
24+
return le;
25+
} catch (Exception e) {
26+
System.err.println("Parse expression:("+expression+") error.");
27+
e.printStackTrace();
28+
}
29+
return null;
30+
}
31+
32+
private LogicExpression calculatePostfixExpression() {
33+
List<String> postfixExpression = new ArrayList<String>();
34+
Stack<Character> operators = new Stack<Character>();
35+
while(!reader.isEmpty()) {
36+
String str = reader.consumeToAny(operators());
37+
if (!"".equals(str)) {
38+
postfixExpression.add(str);
39+
}
40+
char c = reader.consume();
41+
if ((char)-1 == c) break;
42+
43+
if (operators.isEmpty() || isLeftBracket(c))
44+
operators.push(c);
45+
else {
46+
char pop = operators.peek();
47+
//如果遇到右括号,将所有的操作符弹出直到遇到左括号
48+
if (isRightBracket(c)) {
49+
while (!isLeftBracket(operators.peek()))
50+
postfixExpression.add(String.valueOf(operators.pop()));
51+
//将栈中的左括号pop删除
52+
operators.pop();
53+
} else if (compareOperator(c, pop) > 0)
54+
operators.push(c);
55+
else{
56+
while (!operators.isEmpty()
57+
&& compareOperator(c, operators.peek()) <= 0
58+
&& !isLeftBracket(operators.peek())) {
59+
60+
postfixExpression.add(String.valueOf(operators.pop()));
61+
}
62+
operators.push(c);
63+
}
64+
}
65+
}
66+
67+
while (!operators.isEmpty())
68+
postfixExpression.add(String.valueOf(operators.pop()));
69+
70+
//
71+
Stack<LogicExpression> resultStack = new Stack<LogicExpression>();
72+
for (String s: postfixExpression) {
73+
if (isOperator(s)) {
74+
LogicExpression le1 = resultStack.pop();
75+
if ("^".equals(s))
76+
resultStack.push(new LogicExpression.Not(le1));
77+
if ("&".equals(s)) {
78+
LogicExpression le2 = resultStack.pop();
79+
resultStack.push(new LogicExpression.And(le1, le2));
80+
}
81+
if ("|".equals(s)) {
82+
LogicExpression le2 = resultStack.pop();
83+
resultStack.push(new LogicExpression.Or(le1, le2));
84+
}
85+
} else
86+
resultStack.push(new LogicExpression.Primitives(s));
87+
}
88+
LogicExpression expression = resultStack.pop();
89+
return expression;
90+
}
91+
92+
private static int compareOperator(char c1, char c2) {
93+
return "|&^".indexOf(c1) - "|&^".indexOf(c2);
94+
}
95+
96+
private static char[] operators() {
97+
return new char[]{'^', '&', '|', '(', ')'};
98+
}
99+
100+
private static boolean isOperator(String s) {
101+
return "|&^".indexOf(s) != -1;
102+
}
103+
104+
private static boolean isLeftBracket(char c) {
105+
return c == '(';
106+
}
107+
108+
private static boolean isRightBracket(char c) {
109+
return c == ')';
110+
}
111+
112+
static class ExpressionReader {
113+
private char[] input;
114+
private int pos;
115+
private int length;
116+
117+
public ExpressionReader(String expression) {
118+
if (null == expression || expression.trim().equals(""))
119+
throw new IllegalArgumentException("Expression cannot be null or empty.");
120+
121+
this.input = expression.toCharArray();
122+
this.pos = 0;
123+
this.length = expression.length();
124+
}
125+
126+
public boolean isEmpty() {
127+
return pos >= length;
128+
}
129+
130+
public int pos() {
131+
return pos;
132+
}
133+
134+
public char current() {
135+
char c = isEmpty()?(char)-1:input[pos];
136+
return c;
137+
}
138+
139+
public char consume() {
140+
char c = current();
141+
pos ++;
142+
return c;
143+
}
144+
145+
public void unConsume() {
146+
pos --;
147+
}
148+
149+
public void advance() {
150+
pos++;
151+
}
152+
153+
int nextIndexOf(char c) {
154+
for (int i = pos; i < length; i++) {
155+
if (c == input[i])
156+
return i - pos;
157+
}
158+
return -1;
159+
}
160+
161+
String consumeTo(char c) {
162+
int offset = nextIndexOf(c);
163+
if (offset != -1) {
164+
String consumed = new String(input, pos, offset);
165+
pos += offset;
166+
return consumed;
167+
} else {
168+
return "";
169+
}
170+
}
171+
172+
String consumeToAny(final char... chars) {
173+
final int start = pos;
174+
final int remaining = length;
175+
176+
OUTER:
177+
while (pos < remaining) {
178+
for (char c : chars) {
179+
if (input[pos] == c)
180+
break OUTER;
181+
}
182+
pos++;
183+
}
184+
return pos > start ? new String(input, start, pos-start) : "";
185+
}
186+
}
187+
188+
public static void main(String[] args) {
189+
String str = "万里|^长城";
190+
LogicExpression expression = LogicExpressionParser.parse(str);
191+
System.out.println(expression);
192+
System.out.println(expression.matches("万里长城万里长"));
193+
System.out.println(expression.matches("万里喝酒抽烟电源"));
194+
}
195+
}

src/structure/graph/AbstractGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void addAdjVertex(T element) {
5959
vertex = (Vertex<T>) AbstractGraph.this.vertexs.get(element);
6060
}
6161
else {
62-
throw new IllegalArgumentException("");
62+
throw new IllegalArgumentException();
6363
}
6464

6565
vertex.inDegree += 1;

0 commit comments

Comments
 (0)