1.ç¨C#åç®åçWeb Service
2.DbUnit (å®è£
| 使ç¨)
3.求决策树源代码。源码最好使用matlab实现。源码
ç¨C#åç®åçWeb Service
å¨å¼å§ä¸é¢è¿ä¸ªä¾åä¹åï¼ä½ çç³»ç»éè¦ï¼
1ãWIN + IISï¼
2ãVS.Netï¼
3ãSQL Serverï¼æè¿éç¨çæ¯SQLæ°æ®åºï¼ï¼
è¿ä¸ªWeb Serviceçä¾åç¨çæ¯MS大å¹çC#åçï¼å¦æä½ å欢VBï¼é£ä¹ç¨VBä¹æ¯ä¸æ ·çå¦ï¼åªä¸è¿è¯æ³ä¸ä¸äºå°çå·®å«èå·²ï¼éçé½æ¯ä¸æ ·çï¼ä¸è¿å³ç¶MSé½é¼å¹C#ï¼å¦æä½ è½å¤ç¨C#åè¿æ¯ç¨è¿ä¸ºå¥½å¦ã
ä¸é¢æ¯åçæ¥éª¤ï¼
ä¸ãæå¼VSãNETçéæå¼åç¯å¢ï¼FILEèåä¸éæ©Newï¼æ°å»ºä¸ä¸ªC#çASP.NET Web Serviceå·¥ç¨ï¼å·¥ç¨å为WebServiceDemoï¼å®æ´çæ¯ponents = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this 源码武魂2 脚本 源码method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// è¿æ¥å符串常é
const string szConn = "server=(local)\\taoyi;uid=sa;pwd=;"
+ "initial catalog=mydata;data source=taoyi";
[WebMethod]
public String About()
{
return "è¿æ¯ä¸ä¸ªC#ç¼åçWeb Serviceæ¼ç¤ºç¨åº!";
}
// è¿åå ¶ä¸ä¸ä¸ªWebServiceDemo表
[WebMethod]
public DataSet GetServiceDemoTable()
{
DataSet sqlDs = new DataSet();
DataAccess dataAcc = new DataAccess(szConn);
string szSql = "Select * From WebServiceDemo";
sqlDs = dataAcc.GetDataset(szSql,"Demo");
return sqlDs;
}
// è¿åç±ç¨æ·æå®çæ¥è¯¢
[WebMethod]
public DataSet GetByUser(string szCommandText)
{
DataSet sqlDs = new DataSet();
DataAccess dataAcc = new DataAccess(szConn);
sqlDs = dataAcc.GetDataset(szCommandText);
return sqlDs;
}
}
æ¯ä¸æ¯å¾ç®åå¦ï¼å°±åªè¿ä¹ç¹ï¼åµåµ~ï¼ä¸è¿ä¹å¯ä»¥è¯´æé®é¢çäºãè¿ä¸ªç±»ä¸å£°æäºä¸ä¸ªWEBæ¹æ³ï¼æ没æåè§å¢ï¼æ¯ä¸ªæ¹æ³çåé¢é½å äº[WebMethod]ï¼è¡¨ç¤ºè¯¥æ¹æ³ä¸ºWEBæ¹æ³ãåµåµï¼å¦æä½ æ³è¦ä½ åçå½æ°å¯ä»¥è®©WEBåºç¨ç¨åºè°ç¨çè¯ï¼è¿ä¸ªå¯ä¸è½å°çå¦~ï¼ä¸ç¶WEBåºç¨ç¨åºå°±æ æ³è°ç¨çã
å°æ¤ä¸ä¸ªWEBæå¡å°±å®æäºï¼ç¹å»è¿è¡ççï¼å¦æ没ä»ä¹éçè¯ï¼å°±ä¼åºç°å¦ä¸çWEB页é¢æå¡æè¿°ï¼
Service1
The following operations are supported. For a formal definition, please review the Service Description.
* GetByUser
* GetServiceDemoTable
* About
.....ï¼ä¸é¢è¿æå¾å¤ï¼
å ¶ä¸ä»£æå·çå°±æ¯å åå¨å½æ°åå äº[WebMethod]çå½æ°ãå¨åºç°å¨é¡µé¢ä¸ä½ å¯ä»¥åå»ç¸åºçå½æ°ï¼ç¶åå°±ä¼è·³å°è°ç¨é¡µé¢ï¼ä½ å¯ä»¥å¨ç¸åºçææ¬æ¡ä¸ï¼å¦æå½æ°æåæ°çè¯ï¼è¾å ¥ç¸åºçåæ°ï¼ç¹èè°ç¨æé®ï¼é£ä¹å°±å¯ä»¥çå°å½æ°è¿åçç»æäºï¼åææ¯å½æ°è°ç¨æ éçè¯ï¼ï¼ä¸è¿å ¨æ¯XMLæ ¼å¼çææ¬ãæ¯å¦æçGetServiceDemoTableå½æ°è°ç¨çç»æå¦ä¸ï¼
<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns=":xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="Demo">
- <xs:complexType>
- <xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="0" />
<xs:element name="szUser" type="xs:string" minOccurs="0" />
<xs:element name="szSex" type="xs:string" minOccurs="0" />
<xs:element name="szAddr" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Demo diffgr:id="Demo1" msdata:rowOrder="0">
<ID>1</ID>
<szUser>taoyi</szUser>
<szSex>ç·</szSex>
<szAddr>åå·æ³¸å·</szAddr>
</Demo>
- <Demo diffgr:id="Demo2" msdata:rowOrder="1">
<ID>2</ID>
<szUser>xiner</szUser>
<szSex>女</szSex>
<szAddr>åå·å®å®¾</szAddr>
</Demo>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
å°æ¤ä¸ºè³ï¼Web Serviceç¨åºå°±å·²ç»ç®æ¯å®æäºã
ä¸é¢è¦åçæ¯åä¸ä¸ªWEBåºç¨ç¨åºæ¥æµè¯æåçè¿ä¸ªWeb Serviceäºï¼ççè½ä¸è½è¾¾å°æ³è¦çç»æã建ç«Webåºç¨ç¨åºçæ¥éª¤å¦ä¸ï¼
ä¸ãæ°å»ºä¸ä¸ªASP.Net Web Applicationå·¥ç¨ï¼ä¸å建Web Serviceç第ä¸æ¥ä¸æ ·ï¼åªæ¯å·¥ç¨ç±»åä¸ä¸æ ·ç½¢äºãæè¿éå·¥ç¨å为WebServiceDemoTest,å®æ´ç为,å¦æ大家æä»ä¹å¥½çèµæºåç»éªæ¬¢è¿åæ交æµ.
å¼å ¥DBUnit
ç°å®ç³»ç»ä¸é常ä¼æä¸äºå ·æå¤é¨ä¾èµæ§ç对象ï¼è¿äºå¯¹è±¡åæ°æ®åºæè å ¶ä»å¯¹è±¡åå¨è¯¸å¤å ³èãå¦ææ们对è¿æ ·ç对象ç¼ååå åç»ä»¶çº§æµè¯çè¯ï¼å¯ä»¥æ³è±¡å°æ¯é常麻ç¦çä¸ä»¶äº.å 为è¿ç§å¤é¨ä¾èµæ§çåå¨ï¼ä½¿çæ们å¾é¾å°å¯¹è±¡å¤ç«åºæ¥è¿è¡æµè¯ãç»å¸¸æåçç½çæµè¯æ³ï¼åºæ¬ä¸å°±æ¯éè¿æ§å¶å¯¹è±¡çå¤é¨ä¾èµæ§æ¥è¾¾å°é离对象çç®çï¼ä½¿çå¯ä»¥æä½è¿äºå¯¹è±¡çç¶æåç¸å ³è¡ä¸ºã
è¿ç¨ 模æ对象(mock objects)
æè stubsï¼å°±æ¯ä¸ä¸ªæ§å¶å¯¹è±¡å¤é¨ä¾èµæ§ç解å³æ¹æ¡ãéè¿é离é£äºå ³èçæ°æ®åºè®¿é®ç±»ï¼è±¡JDBCçç¸å ³æä½ç±»ï¼å¯¹äºæ§å¶å¯¹è±¡å¤é¨ä¾èµæ§å°æ¯å¾ææçãä½æ¨¡æ对象ç解å³æ¹æ¡å¯¹ä¸äºç¹æ®çåºç¨ç³»ç»æ¶æå°±æ¾å¾åä¸ä»å¿äºï¼è±¡é£äºè¿ç¨äºEJBçCMP(container-managed
persistence)æè JDO(java Data
Objects)çåºç¨ç³»ç»æ¶æï¼å¨è¿äºæ¶æéï¼æ°æ®åºç访é®å¯¹è±¡æ¯å¨æåºå±çèä¸å¾éè½ã
ç±Manuel Laflamme
ç¼åçå¼æ¾æºä»£ç çDBUnitæ¶æä½ç³»ï¼å¯¹äºæ§å¶ç³»ç»å é¨çæ°æ®åºä¾èµæ§æä¾äºä¸ä¸ªé常ä¸éç解å³æ¹æ¡ãä»å 许ç¨åºåå¨æ´ä¸ªçæµè¯è¿ç¨ä¸èªç±ç管çæ§å¶æ°æ®åºçç¶æï¼è¿å¾éè¦ãå©ç¨DBUnitï¼å¨æµè¯ä¹åï¼æ们å¯ä»¥ç»ç®æ æ°æ®åºæ¤å ¥æ们éè¦çæ°æ®éï¼èä¸ï¼å¨æµè¯å®æ¯åï¼æ°æ®åºå®å ¨è½å¤å溯å°æµè¯åçç¶æã
å¨å¾å¤æåç软件项ç®ä¸ï¼æµè¯èªå¨åå¾å¾æ¯å ³é®çå±é¢ãDBUnitå 许å¼å人åå建æµè¯ç¨ä¾ä»£ç ï¼å¨è¿äºæµè¯ç¨ä¾ççå½å¨æå æ们å¯ä»¥å¾å¥½çæ§å¶æ°æ®åºçç¶æãèä¸,è¿äºæµè¯ç¨ä¾æ¯å¾å®¹æå®ç°èªå¨åçãè¿æ ·å¨æµè¯è¿ç¨ä¸æ们æ 须对å®è¿è¡äººå·¥çå¹²é¢ï¼ä¸ºäººå·¥å¹²é¢é æçåæèæ å¿å°±æ´æ²¡å¿ è¦äºã
ç®åä»ç»
é 置使ç¨DBUnitç第ä¸æ¥æ们é¦å éè¦ç¥éå¦ä½çææ°æ®åºschemaï¼è¿ä¸ªæ件æ¯XMLæ ¼å¼çï¼å ¶ä¸å æ¬äºæ°æ®åºç表åç¸å ³æ°æ®ä¿¡æ¯ã
ä¾å¦ï¼è¿éæä¸ä¸ªæ°æ®åºè¡¨employee
ï¼æ们å¯ä»¥ç¨SQLçå½¢å¼è¿æ ·å°ä»è¡¨ç¤ºåºæ¥ã
èä¸ï¼æ们å¯ä»¥çå°ï¼ä¸ä¸ªç®åçæ°æ®éå¯ä»¥è¿æ ·è¡¨ç¤º
å¨DBUnitä¸ï¼ä¸é¢è¿ä¸ªè¡¨åæ½æ ·æ°æ®ä¿¡æ¯å¯ä»¥ç¨XMLæ件çå½¢å¼è¿æ ·è¡¨ç¤ºï¼
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Andrew'
ssn='xxx-xx-xxxx'
last_name='Glover' />
è¿ä¸ªçæçXMLæ ¼å¼çæ件å¯ä»¥ä½ä¸ºç³»ç»æéçææç§åæ件(seed
files)çæ ·æ¬æ¨¡ç.
为ç¸äºå ³èçæµè¯åºæ¯å建å¤ä¸ªç§åæ件æ¯ä¸ä¸ªå¾ææççç¥ï¼å°±è±¡éè¿ä¸åçæ°æ®åºæ件æ¥åºåé离æ°æ®åºç¶ææ¯ä¸ä¸ªéçãå¤ç§åæ件çç¥å¯ä»¥å°æ们çæµè¯ç®æ éå®å°è¾å°çèå´ï¼ç®æ æ°æ®å¯ä»¥åªé对æ°æ®åºç表ï¼èä¸æ¯æ´ä¸ªæ°æ®åºã
为äºç»ç®æ æ°æ®åºæ¤å ¥ä¸åçèåè®°å½ï¼æ们éè¦çXMLæ°æ®æ件å¦ä¸æ示ï¼
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Drew' ssn='--'
last_name='Smith' />
<EMPLOYEE employee_uid='2'
start_date='--'
first_name='Nick' ssn='--'
last_name='Marquiss' />
<EMPLOYEE employee_uid='3'
start_date='--'
first_name='Jose' ssn='--'
last_name='Whitson' />
</dataset>
ç°å¨ï¼è¦è®©DBUnitåæ们æéçæ°æ®åºschemaä¸èµ·å·¥ä½äºï¼å¯¹äºç¨åºåæ¥è¯´ï¼æ们使ç¨DBUnitè¿è¡æµè¯å¯ä»¥æ两ç§éæ©ï¼éè¿ç´æ¥ç¼ç æ¹å¼è¿è¡æµè¯æè ä¸Antç»å.
ç¼ç æ¹å¼
DBUnitæ¡æ¶æä¾äºä¸ä¸ªåºæ¬çæ½è±¡æµè¯ç¨ä¾ç±»ï¼å«åDatabaseTestCaseï¼å®æ¯JUnitæ¡æ¶ä¸çåºç¡ç±»TestCaseçåç±»ãå¦ææ们使ç¨è¿ä¸ªç±»å¿ é¡»é¦å å®ç°ä¸¤ä¸ªé©åæ¹æ³(hook
methods)ï¼getConnection()ågetDataSet().
æ¹æ³getConnection()éè¦è¿åä¸ä¸ªIDatabaseConnectionç±»åç对象ï¼è¿ä¸ªå¯¹è±¡æ¯ä¸ä¸ªåºäºæ®éJDBCè¿æ¥çå è£ ç±»ãä¾å¦ï¼ä¸é¢ç代ç 段æ¼ç¤ºäºå¨MySQLæ°æ®åºç¯å¢ä¸ï¼IDatabaseConnectionç±»åè¿æ¥å¯¹è±¡çå建æ¹æ³ã
protected IDatabaseConnection getConnection()
throws Exception {
Class driverClass = Class.forName("org.gjt.mm.mysql.Driver");
Connection jdbcConnection = DriverManager.getConnection(
"jdbc:mysql://.0.0.1/hr", "hr", "hr");
return new DatabaseConnection(jdbcConnection);
}
æ¹æ³getDataSet()è¿åä¸ä¸ªIDataSetç±»å对象ï¼å ¶å®ï¼è¯´ç½äºï¼ä»å°±æ¯æ们å åæå°çXMLæ°æ®çç§åæ件çå¦ä¸ç§è¡¨ç°å½¢å¼ã
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSet(
new
FileInputStream("hr-seed.xml"));
}
æäºè¿ä¸¤ä¸ªåºæ¬çæ¹æ³ä»¥åï¼DBUnitå°±å¯ä»¥æç §å®é¢å 缺ççè¡ä¸ºå·¥ä½äºãDatabaseTestCaseç±»æä¾äºä¸¤ä¸ªfixtureï¼æå«å®åºä»¶ï¼ä¸ç¥ä»å åæå¦ï¼ï¼æ¹æ³æ¥æ§å¶æµè¯ååæµè¯åçæ°æ®åºç¶æãè¿ä¸¤ä¸ªæ¹æ³å°±æ¯ï¼
getSetUpOperation() å getTearDownOperation().
ä¸ç§é«æçå®æ½æ¹æ¡å°±æ¯è®©getSetUpOperation()æ¹æ³æ§è¡REFRESHæä½ï¼éè¿è¿ä¸ªæä½,æ们å¯ä»¥ç¨ç§åæ件ä¸çæ°æ®å»æ´æ°ç®æ æ°æ®åºéçæ°æ®ãæ¥ä¸æ¥ï¼å°±æ¯getTearDownOperation()ï¼è®©ä»å»æ§è¡ä¸ä¸ªNONEæä½ï¼ä¹å°±æ¯ä»ä¹ä¹ä¸æ§è¡ã
protected DatabaseOperation getSetUpOperation()
throws
Exception {
return DatabaseOperation.REFRESH;
}
protected DatabaseOperation getTearDownOperation()
throws
Exception {
return DatabaseOperation.NONE;
}
è¿æä¸ç§ææçæ¹æ³å°±æ¯å¨getSetUpOperation()æ¹æ³ä¸æ§è¡CLEAN_INSERTæä½ï¼è¿æ ·é¦å ä¼å°ç®æ æ°æ®åºä¸ä¸æ们æä¾çç§åæ件ä¸è´çæ°æ®å é¤ï¼ç¶åå°æ们æä¾çæ°æ®æå ¥å°æ°æ®åºä¸ãè¿ä¸ªå®æ½é¡ºåºä¿è¯äºæ们对æ°æ®åºç精确æ§å¶ã
代ç æ ·ä¾
å¨ä¸ä¸ªåºäºJ2EEç人åèµæºç³»ç»ä¸ï¼æ们å¾å¸æ对æ个æ°æ®æä½å¨æå®ç°æµè¯èªå¨å,è¿ä¸ªæä½å¨æå æ¬èåçæ°å¢ï¼æ£ç´¢ï¼æ´æ°åå é¤ãè¿ç¨æ¥å£å®ä¹äºä¸åçä¸å¡æ¹æ³ï¼ä¸ºäºç®æ´æ¸ æ¥ï¼çç¥äºæ¹æ³ä¸çthrowsåå¥ï¼.
//è¯è 注ï¼è¿éçEmployeeValueObjectç±»å对象ï¼è¯è 认为æ¯ä»£è¡¨èåå®ä½ä¿¡æ¯ç对象ã
public void createEmployee( EmployeeValueObject emplVo )
public EmployeeValueObject getEmployeeBySocialSecNum( String ssn )
public void updateEmployee( EmployeeValueObject emplVo )
public void deleteEmployee( EmployeeValueObject emplVo )
æµè¯getEmployeeBySocialSecNum()æ¹æ³
éè¦æ¤å ¥ä¸æ¡æ°æ®å°ç®æ æ°æ®åºä¸ï¼å¦å¤ï¼æµè¯deleteEmployee()æ¹æ³åupdateEmployee()æ¹æ³æ¶ï¼åæ ·ä¹æ¯å¨å åæ¤å ¥çè¿æ¡è®°å½çåºç¡ä¸è¿è¡ãæåï¼æµè¯ç±»ä¼é¦å å©ç¨createEmployee()æ¹æ³å建ä¸æ¡è®°å½ï¼åæ¶æ们éè¦æ ¡éªæ§è¡è¿ä¸ªæ¹æ³æ¶ï¼æ¯å¦ä¼æå¼å¸¸åçã
ä¸é¢è¿ä¸ªDBUnitç§åæ件ï¼å«å"employee_hr_seed.xml",ä¸é¢å°ç¨å°è¿ä¸ªæ件ã
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Drew' ssn='--'
last_name='Smith' />
<EMPLOYEE employee_uid='2'
start_date='--'
first_name='Nick' ssn='--'
last_name='Marquiss' />
<EMPLOYEE employee_uid='3'
start_date='--'
first_name='Jose' ssn='--'
last_name='Whitson' />
</dataset>
æµè¯ç±» EmployeeSessionFacadeTest
,éè¦æ©å±DBUnitçåºç¡ç±»DatabaseTestCase并ä¸å¿ é¡»æä¾å¯¹getConnection()ågetDataSet()æ¹æ³çå®ç°ï¼å¨getConnection()æ¹æ³ä¸å°è·å¾ä¸EJB容å¨åå§åæ¶ä¸æ ·çæ°æ®åºå®ä¾ï¼getDataSet()æ¹æ³è´è´£è¯»åä¸é¢æåçemployee_hr_seed.xmlæ件çæ°æ®ã
æµè¯æ¹æ³ç¸å½ç®åï¼å 为DBUnitå·²ç»ä¸ºæ们å¤çäºå¤æçæ°æ®åºçå½å¨æä»»å¡ã为äºæµè¯getEmployeeBySocialSecNum()æ¹æ³ï¼åªéè¦ç®åçä¼ éä¸ä¸ªåå¨äºç§åæ件ä¸ç社ä¿ä»£ç å·å³å¯,æ¯å¦
"--".
//è¯è 注ï¼EmployeeFacade ç±»å对象ï¼è¯è 认为æ¯ä»£è¡¨åºå±æ°æ®åºæ°æ®çæ å°ä½
public void testFindBySSN() throws Exception{
EmployeeFacade facade = //obtain somehow
EmployeeValueObject vo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo shouldn't be null", vo);
TestCase.assertEquals("should be Drew",
"Drew", vo.getFirstName());
TestCase.assertEquals("should be Smith",
"Smith", vo.getLastName());
}
为äºç¡®ä¿æä½å¨æä¸çå建èåæ¹æ³createEmployee()没æé®é¢ï¼æ们åªéç®åçæ§è¡ä¸ä¸è¿ä¸ªæ¹æ³ï¼ç¶åæ ¡éªä¸ä¸çæ没æå¼å¸¸æåºï¼å¦å¤ï¼ä¸ä¸æ¥æ们è¦åçå°±æ¯å¨è¿æ¡æ°å¢çè®°å½ä¸è¿è¡æ¥æ¾æä½ï¼çæ¯å¦å¯ä»¥æ¾å°åå建çè®°å½ã
public void testEmployeeCreate() throws Exception{
EmployeeValueObject empVo = new EmployeeValueObject();
empVo.setFirstName("Noah");
empVo.setLastName("Awan");
empVo.setSSN("--");
EmployeeFacade empFacade = //obtain from somewhere
empFacade.createEmployee(empVo);
//perform a find by ssn to ensure existence
}
æµè¯updateEmployee()æ¹æ³å æ¬åæ¥ï¼é¦å æ¥æ¾æ们éè¦è¢«æ´æ°çé£æ¡è®°å½ï¼ç¶åæ´æ°å®ï¼ç´§æ¥çï¼éæ°æ¥æ¾è¿æ¡è®°å½ï¼ç¡®è®¤æ´æ°æä½æ¯å¦ææã
public void testUpdateEmployee() throws Exception{
EmployeeFacade facade =//obtain facade
EmployeeValueObject vo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", vo);
TestCase.assertEquals("first name should be Jose", "Jose",
vo.getFirstName());
vo.setFirstName("Ramon");
facade.updateEmployee(vo);
EmployeeValueObject newVo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", newVo);
TestCase.assertEquals("name should be Ramon", "Ramon",
newVo.getFirstName());
}
ç¡®ä¿æ°æ®æä½å¨æä¸çå é¤æä½deleteEmployee()çæ¹æ³åtestUpdateEmployee()æ¹æ³åºæ¬ç±»ä¼¼ã
å®å为ä¸æ¥ï¼é¦å æ¥æ¾ä¸ä¸ªå·²åå¨çè®°å½å®ä½ï¼ç¶å移é¤ï¼æåå对ç¸åçè®°å½è¿è¡æ¥æ¾ï¼ç¡®è®¤è¿æ¡è®°å½æ²¡æ被æ¥å°ã
public void testDeleteEmployee() throws Exception{
EmployeeFacade facade = //obtain facade
EmployeeValueObject vo = facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", vo);
facade.deleteEmployee(vo);
try{
EmployeeValueObject newVo =
facade.getEmployeeBySocialSecNum("--");
TestCase.fail("returned removed employee");
}catch(Exception e){
//ignore
}
}
ä¸è¿°è¿äºæµè¯ä»£ç å¾ç®åä¹å¾å®¹æç解.å 为è¿äºä»£ç å¯ä¸çè责就æ¯æµè¯ï¼å·²ç»å®å ¨ä»ç³»ç»ç¨åºä»£ç ä¸ç¬ç«åºæ¥ï¼è¿ä½¿æµè¯åçç®åã并ä¸ï¼è¿äºæµè¯ç¨ä¾çèªå¨åä¹å¾å®¹æå®ç°ã
ä¸Antçç»å
ç¸å¯¹äºæ©å±DBUnitä¸çåºç¡ç±»DatabaseTestCase,DBUnitæ¡æ¶ä¸èªå¸¦Antåè½,å 许æ们å¯ä»¥å¨Antçbuild.xmlæ件ä¸æ§å¶æ°æ®åºçç¶æ.è¿ä¸ªåè½æ¯ç¸å½å¼ºå¤§çï¼å 为对äºä½æç诸å¤æµè¯ç¨ä¾ï¼å®æä¾äºä¸ä¸ªç¸å½ç®æ´ç解å³æ¹æ¡ãæ¯å¦ãå¨Antä¸è¿è¡JUnitæµè¯,就象ä¸é¢å®ä¹ä¸ä¸ªä»»å¡ä¸æ ·ç®åæäºã
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<batchtest fork="yes"
todir="${ reports.tests}">
<fileset dir="${ src.tests}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
DBUnitä»»å¡è¿ç¨ä¸ï¼ä¸ºäºå¨Junitä»»å¡ååæ§å¶æ°æ®åºçç¶æï¼æ们éè¦å建ä¸ä¸ª"setup"æä½ï¼å¨è¿ä¸ªæä½ä¸ç§åæ件ä¸çæ°æ®å 容ä¼è¢«æå ¥çæ°æ®åºä¸ã
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"/>
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="INSERT"
src="seedFile.xml"/>
</dbunit>
ç¶åï¼è¿éè¦ä¸ä¸ª"tear
down"æä½,å¨è¿ä¸ªæä½ä¸ï¼"setup"æä½æå ¥çè®°å½è¢«ä»ç®æ æ°æ®åºä¸å é¤äºã
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="DELETE"
src="seedFile.xml"/>
</dbunit>
ç¨ä¸é¢ç代ç æ¥å è£ JUnitä»»å¡ï¼è½å¤å¨æ¹éæµè¯åææçè£ è½½æ°æ®å°ç®æ æ°æ®åºä¸ï¼å¹¶ä¸å¨æµè¯ç»æåï¼å°å·²è£ è½½çå ¨é¨æ°æ®å é¤ã
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"/>
<!-- set up operation -->
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="INSERT"
src="seedFile.xml"/>
</dbunit>
<!-- run all tests in the source tree -->
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<batchtest fork="yes" todir="${ reports.tests}">
<fileset dir="${ src.tests}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
<!-- tear down operation -->
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="DELETE"
src="seedFile.xml"/>
</dbunit>
ç»è®º
è½å¤å¨æµè¯å¨æå 管çæ°æ®åºçç¶æï¼DBUnitæ¡æ¶çè¿ä¸ªåè½ç¹æ§ä½¿å¾æµè¯ç¨ä¾ä»£ç çå建ååºç¨çå¨æ大大缩çãèä¸ï¼éè¿æ§å¶æ°æ®åºè¿ä¸ªä¸»è¦çä¾èµå¯¹è±¡ï¼ä½¿çå©ç¨DBUnitæ¡æ¶ççæµè¯æ´å®¹æèªå¨åã
DBUnitç²¾å¦ç设计ï¼ä½¿çå¦ä¹ 使ç¨å®åå¾å¾ç®åãå¨ä½ çæµè¯æ¹æ¡ä¸ï¼å¦æä½ è½å¤æ£ç¡®ç使ç¨å®ï¼é£ä¹å¸¦æ¥çå°æ¯ä»£ç 稳å®æ§æ¹é¢çå¤§å¹ åº¦å¢å¼ºï¼å½ç¶è¿ä¼ä½¿ä½ çå¼åå¢éä¿¡å¿åå¢ã
求决策树源代码。最好使用matlab实现。源码
function [Tree RulesMatrix]=DecisionTree(DataSet,源码源码模组AttributName)
%输入为训练集,为离散后的源码githun源码数字,如记录1:1 1 3 2 1;
%前面为属性列,源码最后一列为类标
if nargin<1
error('请输入数据集');
else
if isstr(DataSet)
[DataSet AttributValue]=readdata2(DataSet);
else
AttributValue=[];
end
end
if nargin<2
AttributName=[];
end
Attributs=[1:size(DataSet,源码2)-1];
Tree=CreatTree(DataSet,Attributs);
disp([char() 'The Decision Tree:']);
showTree(Tree,0,0,1,AttributValue,AttributName);
Rules=getRule(Tree);
RulesMatrix=zeros(size(Rules,1),size(DataSet,2));
for i=1:size(Rules,1)
rule=cell2struct(Rules(i,1),{ 'str'});
rule=str2num([rule.str([1:(find(rule.str=='C')-1)]) rule.str((find(rule.str=='C')+1):length(rule.str))]);
for j=1:(length(rule)-1)/2
RulesMatrix(i,rule((j-1)*2+1))=rule(j*2);
end
RulesMatrix(i,size(DataSet,2))=rule(length(rule));
end
end
function Tree=CreatTree(DataSet,Attributs) %决策树程序 输入为:数据集,属性名列表
%disp(Attributs);
[S ValRecords]=ComputEntropy(DataSet,源码0);
if(S==0) %当样例全为一类时退出,返回叶子节点类标
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)==size(DataSet,源码1))
break;
end
end
Tree.Attribut=i;
Tree.Child=[];
return;
end
if(length(Attributs)==0) %当条件属性个数为0时返回占多数的类标
mostlabelnum=0;
mostlabel=0;
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)>mostlabelnum)
mostlabelnum=length(ValRecords(i).matrix);
mostlabel=i;
end
end
Tree.Attribut=mostlabel;
Tree.Child=[];
return;
end
for i=1:length(Attributs)
[Sa(i) ValRecord]=ComputEntropy(DataSet,i);
Gains(i)=S-Sa(i);
AtrributMatric(i).val=ValRecord;
end
[maxval maxindex]=max(Gains);
Tree.Attribut=Attributs(maxindex);
Attributs2=[Attributs(1:maxindex-1) Attributs(maxindex+1:length(Attributs))];
for j=1:length(AtrributMatric(maxindex).val)
DataSet2=[DataSet(AtrributMatric(maxindex).val(j).matrix',1:maxindex-1) DataSet(AtrributMatric(maxindex).val(j).matrix',maxindex+1:size(DataSet,2))];
if(size(DataSet2,1)==0)
mostlabelnum=0;
mostlabel=0;
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)>mostlabelnum)
mostlabelnum=length(ValRecords(i).matrix);
mostlabel=i;
end
end
Tree.Child(j).root.Attribut=mostlabel;
Tree.Child(j).root.Child=[];
else
Tree.Child(j).root=CreatTree(DataSet2,Attributs2);
end
end
end
function [Entropy RecordVal]=ComputEntropy(DataSet,attribut) %计算信息熵
if(attribut==0)
clnum=0;
for i=1:size(DataSet,1)
if(DataSet(i,size(DataSet,2))>clnum) %防止下标越界
classnum(DataSet(i,size(DataSet,2)))=0;
clnum=DataSet(i,size(DataSet,2));
RecordVal(DataSet(i,size(DataSet,2))).matrix=[];
end
classnum(DataSet(i,size(DataSet,2)))=classnum(DataSet(i,size(DataSet,2)))+1;
RecordVal(DataSet(i,size(DataSet,2))).matrix=[RecordVal(DataSet(i,size(DataSet,2))).matrix i];
end
Entropy=0;
for j=1:length(classnum)
P=classnum(j)/size(DataSet,1);
if(P~=0)
Entropy=Entropy+(-P)*log2(P);
end
end
else
valnum=0;
for i=1:size(DataSet,1)
if(DataSet(i,attribut)>valnum) %防止参数下标越界
clnum(DataSet(i,attribut))=0;
valnum=DataSet(i,attribut);
Valueexamnum(DataSet(i,attribut))=0;
RecordVal(DataSet(i,attribut)).matrix=[]; %将编号保留下来,以方便后面按值分割数据集
end
if(DataSet(i,源码size(DataSet,2))>clnum(DataSet(i,attribut))) %防止下标越界
Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))=0;
clnum(DataSet(i,attribut))=DataSet(i,size(DataSet,2));
end
Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))= Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))+1;
Valueexamnum(DataSet(i,attribut))= Valueexamnum(DataSet(i,attribut))+1;
RecordVal(DataSet(i,attribut)).matrix=[RecordVal(DataSet(i,attribut)).matrix i];
end
Entropy=0;
for j=1:valnum
Entropys=0;
for k=1:length(Value(j).classnum)
P=Value(j).classnum(k)/Valueexamnum(j);
if(P~=0)
Entropys=Entropys+(-P)*log2(P);
end
end
Entropy=Entropy+(Valueexamnum(j)/size(DataSet,1))*Entropys;
end
end
end
function showTree(Tree,level,value,branch,AttributValue,AttributName)
blank=[];
for i=1:level-1
if(branch(i)==1)
blank=[blank ' |'];
else
blank=[blank ' '];
end
end
blank=[blank ' '];
if(level==0)
blank=[' (The Root):'];
else
if isempty(AttributValue)
blank=[blank '|_____' int2str(value) '______'];
else
blank=[blank '|_____' value '______'];
end
end
if(length(Tree.Child)~=0) %非叶子节点
if isempty(AttributName)
disp([blank 'Attribut ' int2str(Tree.Attribut)]);
else
disp([blank 'Attribut ' AttributName{ Tree.Attribut}]);
end
if isempty(AttributValue)
for j=1:length(Tree.Child)-1
showTree(Tree.Child(j).root,level+1,j,[branch 1],AttributValue,AttributName);
end
showTree(Tree.Child(length(Tree.Child)).root,level+1,length(Tree.Child),[branch(1:length(branch)-1) 0 1],AttributValue,AttributName);
else
for j=1:length(Tree.Child)-1
showTree(Tree.Child(j).root,level+1,AttributValue{ Tree.Attribut}{ j},[branch 1],AttributValue,AttributName);
end
showTree(Tree.Child(length(Tree.Child)).root,level+1,AttributValue{ Tree.Attribut}{ length(Tree.Child)},[branch(1:length(branch)-1) 0 1],AttributValue,AttributName);
end
else
if isempty(AttributValue)
disp([blank 'leaf ' int2str(Tree.Attribut)]);
else
disp([blank 'leaf ' AttributValue{ length(AttributValue)}{ Tree.Attribut}]);
end
end
end
function Rules=getRule(Tree)
if(length(Tree.Child)~=0)
Rules={ };
for i=1:length(Tree.Child)
content=getRule(Tree.Child(i).root);
%disp(content);
%disp([num2str(Tree.Attribut) ',' num2str(i) ',']);
for j=1:size(content,1)
rule=cell2struct(content(j,1),{ 'str'});
content(j,1)={ [num2str(Tree.Attribut) ',' num2str(i) ',' rule.str]};
end
Rules=[Rules;content];
end
else
Rules={ ['C' num2str(Tree.Attribut)]};
end
end