Rules engine Visual Rules Solution development basic tutorial [serial 15]--VisualRules interface call (4)

VisualRules interface call (4)


1. Java class calls service interface local call

         Create a Test2 class in Eclipse, receive the parameter input name in main, execute the local rule compilation file through the interface RuleService call, and return the result.

1. Add a java test class

         Add a java class to the java project, named Test1.java, whose content is edited as follows:
package com.flagleader;
import java.io.File;
import java.util.ArrayList;
import java.util. List;
import com.flagleader.engine.RuleEngine;
import com.flagleader.engine.RuleEngineException;
import com.flagleader.engine.RuleEngineFactory;
import com.flagleader.engine.RuleEngineManager;
import com.flagleader.engine.RuleService;
import com. flagleader.engine.RuleServiceException;
import com.flagleader.engine.impl.LocalRuleServiceFactory;
/**
* Call the compiled file under the default file in the specified directory
* The interface used is RuleService, which can be used for local calls and service calls
* @author Administrator
*/
public class Test2 {
public static void main(String[] args) {
try {
// The specified directory for the compiled file, the default version The directory is the default
RuleEngineManager.getInstance().init(new File("e:\\rscfile"));
// Factory mode, this interface can be used for local calls, and service calls
RuleService engine = new LocalRuleServiceFactory( ).getRuleService();
// Pass parameters in the form of dto
Student student = new Student(1, "Li Li", 12, 'Male', "Grade 6", "None");
// Execute the rules, among which "student.add" is the full name of the rule, and student is the incoming parameter
engine.executeBeans("student.add", student);
//Determine whether engine.get("studentList") is a collection type
if (engine.get(" studentList").getClass().isAssignableFrom(java.util.ArrayList.class)){
// After executing the rule, to get the data, "studentList" must be the same as the variable name of the memory table data in the rule
List list = (List) engine.get("studentList");
// Since the rule uses list< list> type, need to do two conversions
List list1 = (List) list.get(0);
// The number of loops is the number of fields in the object
for (int i = 0; i < 6; i++) {
System.out. print(list1.get(i) + "\t");
}
}
} catch (RuleServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


2. Execute the java test class

         after clicking execute , you can see the following results:




         说明已经调用了student.add规则包,并且根据传入的参数,返回处理结果以list<list>形式展示。
         同理,任何的其他java类,只需加入上述的代码,即可完成调用规则包的工作。


二、Java类通过不同服务调用-传输数据格式类型

         参数streamtype默认为1,可以不进行配置,可以根据需求配置传输数据格式类型,0表示原始字符串格式。1表示加密压缩字符串。2表示压缩字符串。3表示压缩字节。4表示GZIP压缩流。客户端传输时通过Property.getInstance().setServerSendtype(...)设置传输数据格式类型,默认为1,即
Property.getInstance().setServerSendtype(Property.ENCRYZIPSTRINGSEND),其中Property.RAWSEND的值为0,Property.ZIPSTRINGSEND的值为2, Property.ZIPBYTESEND的值为3,Property.GZIPSEND的值为4,其中客户端传输数据格式类型为1时,参数streamtype的参数值可以是0或1。


传输数据格式类型

         传输数据格式类型有四种,0代表原始格式,在代码中Property. RAWSEND,1代表加密并压缩,在代码中Property. ENCRYZIPSTRINGSEND,2代表压缩字符串,在代码中Property. ZIPSTRINGSEND,3代表压缩二进制,在代码中Property. ZIPBYTESEND,4代表GZIP流,在代码中Property. GZIPSEND。
         Servlet调用:传输数据格式类型五种都可用,需要java代码中的传输数据格式类型和web.xml配置设置传输数据格式类型一致。
         Java代码:Property.getInstance().setServerSendtype(...);
         web.xml配置DBRuleServerServlet的streamtype参数:
<servlet>
<servlet-name>DBRuleServerServlet</servlet-name>
<servlet-class>com.flagleader.webserver.DBRuleServerServlet
</servlet-class>
<init-param>
<param-name>xmltype</param-name>
<param-value>json</param-value>
</init-param>
<init-param>
<param-name>streamtype</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
         Socket调用:传输数据格式类型五种中只能使用0、1、3,需要java代码中的传输数据格式类型和web.xml配置设置传输数据格式类型一致,其中0、1调用的代码,不能在3中使用,需要更改代码。
         Java代码:支持传输数据格式类型0、1
Property.getInstance().setServerSendtype(Property. RAWSEND);
RuleServerPoolFactory.registerServer("localhost", 1508) ;
RuleService  engine=RuleServerPoolFactory.getFactory().getRuleService();
         web.xml配置DBRuleServerServlet的streamtype参数
<servlet>
<servlet-name>WebRuleServerServlet</servlet-name>
<servlet-class>com.flagleader.webserver.WebRuleServerServlet
</servlet-class>
<init-param>
<param-name>servertype</param-name>
<param-value>socket</param-value>
</init-param>
<init-param>
<param-name>ruleServerPort</param-name>
<param-value>1508</param-value>
</init-param>
<init-param>
<param-name>definepath</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>streamtype</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2-startup>
</servlet>
         Java代码:支持传输数据格式类型3
Property.getInstance().setServerSendtype(Property.ZIPBYTESEND);
RuleService engine = new RuleServerZipFactory("127.0.0.1",1508).getRuleService();
        web.xml配置DBRuleServerServlet的streamtype参数
<servlet>
<servlet-name>WebRuleServerServlet</servlet-name>
<servlet-class>com.flagleader.webserver.WebRuleServerServlet
</servlet-class>
<init-param>
<param-name>servertype</param-name>
<param-value>socket</param-value>
</init-param>
<init-param>
<param-name>ruleServerPort</param-name>
<param-value>1508</param-value>
</init-param>
<init-param>
<param-name>definepath</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>streamtype</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326922852&siteId=291194637