Skip to content

Commit aa08f56

Browse files
yujunhao8831yujunhao
authored and
yujunhao
committed
优化
1 parent f1849a2 commit aa08f56

File tree

28 files changed

+649
-371
lines changed

28 files changed

+649
-371
lines changed

.gitignore

-29
This file was deleted.

goblin-common/pom.xml

+6-7
Original file line numberDiff line numberDiff line change
@@ -218,35 +218,34 @@
218218
<dependency>
219219
<groupId>org.antlr</groupId>
220220
<artifactId>antlr4-runtime</artifactId>
221-
<version>4.3</version>
222-
<scope>test</scope>
221+
<version>4.6</version>
223222
</dependency>
224223

225224

226225
<dependency>
227226
<groupId>org.kie</groupId>
228227
<artifactId>kie-api</artifactId>
229-
<scope>provided</scope>
230228
</dependency>
231229
<dependency>
232230
<groupId>org.drools</groupId>
233231
<artifactId>drools-core</artifactId>
234-
<scope>provided</scope>
235232
</dependency>
236233
<dependency>
237234
<groupId>org.drools</groupId>
238235
<artifactId>drools-compiler</artifactId>
239-
<scope>provided</scope>
240236
</dependency>
241237
<dependency>
242238
<groupId>org.drools</groupId>
243239
<artifactId>drools-decisiontables</artifactId>
244-
<scope>provided</scope>
245240
</dependency>
246241
<dependency>
247242
<groupId>org.drools</groupId>
248243
<artifactId>drools-templates</artifactId>
249-
<scope>provided</scope>
244+
</dependency>
245+
246+
<dependency>
247+
<groupId>com.thoughtworks.xstream</groupId>
248+
<artifactId>xstream</artifactId>
250249
</dependency>
251250

252251

goblin-common/src/main/java/com/goblin/common/PagingRequest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.goblin.common;
22

3-
import lombok.Data;
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
46
import lombok.experimental.Accessors;
57

68
import java.io.Serializable;
@@ -11,7 +13,9 @@
1113
* @author : 披荆斩棘
1214
* @date : 2017/7/13
1315
*/
14-
@Data
16+
@Getter
17+
@Setter
18+
@ToString
1519
@Accessors( chain = true )
1620
public class PagingRequest implements Serializable {
1721

goblin-common/src/main/java/com/goblin/common/cron/CustomizeCron.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.goblin.common.cron;
22

3-
import lombok.Data;
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import lombok.Setter;
6+
import lombok.ToString;
47
import lombok.experimental.Accessors;
58
import org.springframework.scheduling.support.CronSequenceGenerator;
69

@@ -13,7 +16,9 @@
1316
* @author : 披荆斩棘
1417
* @date : 2017/8/9
1518
*/
16-
@Data
19+
@Getter
20+
@Setter
21+
@ToString
1722
@Accessors( chain = true )
1823
public class CustomizeCron implements Serializable {
1924

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.goblin.common.drools;
2+
3+
import com.goblin.common.util.LogUtils;
4+
import org.drools.core.base.RuleNameStartsWithAgendaFilter;
5+
import org.kie.api.KieServices;
6+
import org.kie.api.builder.KieBuilder;
7+
import org.kie.api.builder.KieFileSystem;
8+
import org.kie.api.builder.Message;
9+
import org.kie.api.runtime.KieContainer;
10+
import org.kie.api.runtime.KieSession;
11+
import org.kie.api.runtime.rule.AgendaFilter;
12+
13+
import java.util.Arrays;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.stream.Collectors;
18+
19+
/**
20+
* @author pijingzhanji
21+
*/
22+
public class DroolsUtils {
23+
24+
25+
public static void main ( String[] args ) throws Exception {
26+
27+
Map< String, Object > factMap = new HashMap<>();
28+
factMap.put( "edu", "大专" );
29+
factMap.put( "age", 18 );
30+
31+
System.err.println( "factMap = " + factMap );
32+
33+
execute( factMap , new RuleNameStartsWithAgendaFilter( "authenticate" ) );
34+
35+
System.err.println( "factMap = " + factMap );
36+
}
37+
38+
private static final Object LOCK = new Object();
39+
private static KieContainer kieContainer;
40+
private static List< String > ruleContents = Arrays.asList(
41+
"package com.goblin\n" +
42+
"import java.util.Map\n" +
43+
"import java.lang.String\n" +
44+
"\n" +
45+
"rule \"authenticate_1\"\n" +
46+
"salience 1\n" +
47+
"when map:Map(true)\n" +
48+
"then\n" +
49+
"if(((String)(map.get(\"edu\"))).equals(\"大学\")){\n" +
50+
"map.put(\"eduResult\",true);\n" +
51+
"}\n" +
52+
"else{\n" +
53+
"map.put(\"eduResult\",false);\n" +
54+
"}\n" +
55+
"end" ,
56+
"package com.goblin\n" +
57+
"import java.util.Map\n" +
58+
"import java.lang.Integer\n" +
59+
"\n" +
60+
"rule \"authenticate_2\"\n" +
61+
"salience 2\n" +
62+
"when\n" +
63+
"map:Map(true)\n" +
64+
"then\n" +
65+
"if(((Integer)(map.get(\"age\"))) >= 18 && ((Integer)(map.get(\"age\"))) <= 50){\n" +
66+
"map.put(\"ageResult\",true);\n" +
67+
"}\n" +
68+
"else{\n" +
69+
"map.put(\"ageResult\",false);\n" +
70+
"}\n" +
71+
"end\n"
72+
);
73+
74+
75+
/**
76+
* @param fact 事实
77+
* @param agendaFilter 具体选用拿个规则进行处理
78+
*/
79+
public static void execute ( Object fact , AgendaFilter agendaFilter ) {
80+
KieContainer kieContainer = getKieContainer();
81+
final KieSession kieSession = kieContainer.newKieSession();
82+
// 事实
83+
kieSession.insert( fact );
84+
// 开始计算
85+
kieSession.fireAllRules( agendaFilter );
86+
// 销毁
87+
kieSession.destroy();
88+
}
89+
90+
public static KieContainer getKieContainer () {
91+
if ( null == kieContainer ) {
92+
synchronized ( LOCK ) {
93+
if ( null == kieContainer ) {
94+
return kieContainer = buildKieContainer( ruleContents );
95+
}
96+
}
97+
}
98+
return kieContainer;
99+
}
100+
101+
private static KieContainer buildKieContainer ( List< String > ruleContents ) {
102+
// 获取的各种对象来完成规则构建、管理和执行等操作
103+
KieServices kieServices = KieServices.Factory.get();
104+
// 用于以编程方式定义组成KieModule的资源
105+
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
106+
for ( String ruleContent : ruleContents ) {
107+
kieFileSystem.write( "src/main/resources/" + System.currentTimeMillis() + ".drl" , ruleContent );
108+
}
109+
// KieBuilder是KieModule中包含的资源的构建者
110+
KieBuilder kieBuilder = kieServices.newKieBuilder( kieFileSystem ).buildAll();
111+
112+
// 如果有错误
113+
if ( kieBuilder.getResults().hasMessages() ) {
114+
LogUtils.getLogger().warn( kieBuilder.getResults().getMessages() );
115+
if ( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ) {
116+
throw new RuntimeException( kieBuilder.getResults()
117+
.getMessages()
118+
.parallelStream()
119+
.map( Message::getText ).collect( Collectors.joining( "," ) )
120+
);
121+
}
122+
}
123+
// 用来访问KBase和KSession等信息
124+
return kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
125+
}
126+
127+
}
128+
129+
130+
131+
132+
133+
134+
135+
136+

goblin-common/src/main/java/com/goblin/common/util/BeanProUtils.java

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.goblin.common.util;
22

3+
import org.apache.commons.beanutils.BeanUtilsBean2;
34
import org.apache.commons.collections4.CollectionUtils;
45

56
import java.lang.reflect.Field;
6-
import java.util.Collections;
7-
import java.util.HashMap;
8-
import java.util.Map;
9-
import java.util.Set;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.util.*;
109

1110
import static com.goblin.common.util.ReflectionProUtils.invokeFieldGettersMethod;
1211
import static org.reflections.ReflectionUtils.getAllFields;
@@ -40,4 +39,45 @@ public static Map< String, Object > toMap ( Object bean ) {
4039
}
4140

4241

42+
/**
43+
* bean转换成Map{@code <String,String>}
44+
*
45+
* <b style="color:red">注意,转换过程中,value直接取toString()方法</b>
46+
*
47+
* @param bean 要转换的Bean
48+
* @return 返回bean字段getters方法的全部属性 , 字段名为key , 其属性为value的<code>Map</code>
49+
*/
50+
public static Map< String, String > toStringMap( Object bean ) {
51+
final Map< String, Object > map = toMap( bean );
52+
Map<String,String> stringMap = new HashMap<>( map.size() );
53+
map.forEach( ( key , value ) -> stringMap.put( key, Objects.toString( value , null ) ) );
54+
return stringMap;
55+
}
56+
57+
/**
58+
* 拷贝属性
59+
*
60+
* @param sources 源
61+
* @param targetClass 目标 class
62+
* @param <T> 目标类型
63+
* @return 目标集合
64+
*/
65+
public static < T > List< T > copyPropertiesToList ( List< ? > sources , Class<T> targetClass) {
66+
AssertUtils.isTrue( CollectionUtils.isEmpty( sources ), "sources is null" );
67+
AssertUtils.isTrue( targetClass == null, "targetClass is null" );
68+
List< T > targets = new ArrayList<>( sources.size() );
69+
for ( Object source : sources ) {
70+
T target;
71+
try {
72+
target = targetClass.newInstance();
73+
BeanUtilsBean2.getInstance().copyProperties( target , source );
74+
} catch ( InstantiationException | IllegalAccessException | InvocationTargetException e ) {
75+
throw new RuntimeException( e.getMessage() , e );
76+
}
77+
targets.add( target );
78+
}
79+
return targets;
80+
}
81+
82+
4383
}

goblin-common/src/main/java/com/goblin/common/util/JsonUtils.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import com.fasterxml.jackson.databind.DeserializationFeature;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.github.bohnman.squiggly.Squiggly;
9-
import com.google.gson.Gson;
10-
import com.google.gson.GsonBuilder;
119

1210
import java.text.DateFormat;
1311
import java.text.SimpleDateFormat;

0 commit comments

Comments
 (0)