PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/ISOConsumptionRealign-PCH-Modularization/ISOConsumptionRealign-PCH-Modularization/src/test/java/com/mmpnc/rating/iso/algorithm/checkInputalgofile/TestLoopThroughConditionsInAlgoFile.java

https://bitbucket.org/Sathishsatz/iso-consumption-parser
Java | 360 lines | 242 code | 61 blank | 57 comment | 82 complexity | fa484b744e515f3ae22a2c2f31a6e94e MD5 | raw file
  1. package com.mmpnc.rating.iso.algorithm.checkInputalgofile;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import javax.xml.bind.JAXBContext;
  10. import javax.xml.bind.JAXBException;
  11. import javax.xml.bind.Unmarshaller;
  12. import org.junit.Test;
  13. import com.mmpnc.connection.helper.BasexDatabaseNames;
  14. import com.mmpnc.connection.helper.Connection;
  15. import com.mmpnc.connection.helper.DBFactoryBuilder;
  16. import com.mmpnc.connection.helper.Database;
  17. import com.mmpnc.connection.helper.Query;
  18. import com.mmpnc.connection.xmldb.XMLConnection;
  19. import com.mmpnc.connection.xmldb.XmlQuery;
  20. import com.mmpnc.rating.iso.algorithm.exception.BasexException;
  21. import com.mmpnc.rating.iso.algorithm.vo.ATTACH;
  22. import com.mmpnc.rating.iso.algorithm.vo.Assign;
  23. import com.mmpnc.rating.iso.algorithm.vo.Class;
  24. import com.mmpnc.rating.iso.algorithm.vo.Condition;
  25. import com.mmpnc.rating.iso.algorithm.vo.Else;
  26. import com.mmpnc.rating.iso.algorithm.vo.Expression;
  27. import com.mmpnc.rating.iso.algorithm.vo.Function;
  28. import com.mmpnc.rating.iso.algorithm.vo.If;
  29. import com.mmpnc.rating.iso.algorithm.vo.LOB;
  30. import com.mmpnc.rating.iso.algorithm.vo.Loop;
  31. import com.mmpnc.rating.iso.algorithm.vo.PCH;
  32. import com.mmpnc.rating.iso.algorithm.vo.Ratetable;
  33. import com.mmpnc.rating.iso.algorithm.vo.Reference;
  34. import com.mmpnc.rating.iso.algorithm.vo.Scope;
  35. import com.mmpnc.rating.iso.algorithm.vo.Then;
  36. import com.mmpnc.rating.iso.datastructure.AlgorithmDataStructure;
  37. /**
  38. * @author nilkanth9581
  39. * THIS TEST CASE HAS BEEN WRITTEN TO CHECK THE LOOP THROUGH CONDITIONS
  40. * IN THE ALGORITHM FILE BEFORE WE START THE ALGORITHM CONSUMPTION
  41. *
  42. */
  43. public class TestLoopThroughConditionsInAlgoFile {
  44. public LOB getLOBObject(FileReader algoFileReader){
  45. JAXBContext jaxbContext;
  46. try {
  47. jaxbContext = JAXBContext.newInstance("com.mmpnc.rating.iso.algorithm.vo");
  48. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  49. LOB lob = (LOB)unmarshaller.unmarshal(algoFileReader);
  50. return lob;
  51. } catch (JAXBException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. return null;
  56. }
  57. private Database createDataBaseForTableObjects(LOB lob){
  58. Database dsDB = null;
  59. for(Object lobContent:lob.getContent())
  60. {
  61. if(lobContent instanceof Reference){
  62. Reference ref = (Reference)lobContent;
  63. if (((Reference) ref).getName().equals("Table Objects")) {
  64. AlgorithmDataStructure ds = new AlgorithmDataStructure(
  65. (Reference) ref);
  66. StringBuffer xmlDS = ds.simplifyDataStrucure();
  67. /*try {
  68. Writer writer = new FileWriter(new File("D:\\ds\\ds-xml.xml"));
  69. writer.write(xmlDS.toString());
  70. writer.flush();
  71. writer.close();
  72. } catch (IOException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }*/
  76. Map<String, String> xmlMap = new HashMap<String, String>();
  77. xmlMap.put("DSFile", xmlDS.toString());
  78. try{
  79. dsDB = DBFactoryBuilder.getXMLDatabase(BasexDatabaseNames.RUNTIMEDSDB, xmlMap);
  80. dsDB.buildDatabase();
  81. }catch(BasexException e){
  82. System.out.println("BasexException"+e.getMessage());
  83. }finally{
  84. try {
  85. dsDB.closeDatabase();
  86. } catch (BasexException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return dsDB;
  95. }
  96. public void readLOB(LOB lob) {
  97. Database dsDB = createDataBaseForTableObjects(lob);
  98. for (Object ref : lob.getContent()) {
  99. if (ref instanceof Reference) {
  100. Reference reference = (Reference)ref;
  101. //CHECKING REFERENCE FOR COMMON RATING OR PREMIUMN CALCULATION
  102. if("Common Rating".equals(reference.getType()) || "Premium Calculation".equals(reference.getType())){
  103. readReference((Reference) ref,dsDB);
  104. }
  105. }
  106. }
  107. }
  108. private void readReference(Reference reference, Database dsDB) {
  109. List<Object> scopeList = new ArrayList<Object>();
  110. scopeList.addAll(reference.getContent());
  111. for (Object scope : scopeList) {
  112. if (scope instanceof Scope) {
  113. Scope curScope = (Scope)scope;
  114. readScope(curScope,dsDB);
  115. //currentScopeDB.closeDatabase();
  116. }
  117. }
  118. }
  119. private void readScope(Scope scope,Database dsDB) {
  120. //setting the current scope path
  121. currentScopePath = scope.getDbTables();
  122. for (Object obj : scope.getContent()) {
  123. if (obj instanceof PCH) {
  124. readPCH((PCH) obj,dsDB);
  125. } else if (obj instanceof If) {
  126. //System.out.println("NOT EXPECTED THIS SITUATION");
  127. readIf((If) obj,dsDB);
  128. } else if (obj instanceof Assign) {
  129. //System.out.println("NOT EXPECTED THIS SITUATION");
  130. readAssign((Assign) obj,dsDB);
  131. } else if (obj instanceof Loop) {
  132. //System.out.println("NOT EXPECTED THIS SITUATION");
  133. readLoop((Loop) obj,dsDB);
  134. } else if (obj instanceof Class) {
  135. }
  136. }
  137. }
  138. private void readPCH(PCH pch,Database dsDB) {
  139. for (Object obj : pch.getContent()) {
  140. if (obj instanceof If) {
  141. readIf((If) obj,dsDB);
  142. } else if (obj instanceof Assign) {
  143. readAssign((Assign) obj,dsDB);
  144. } else if (obj instanceof Loop) {
  145. // System.out.println("loop");
  146. readLoop((Loop) obj,dsDB);
  147. } else if (obj instanceof Class) {
  148. // System.out.println("class");
  149. }
  150. }
  151. }
  152. private void readIf(If _if,Database dsDB ) {
  153. // System.out.print("if ");
  154. for (Object obj : _if.getContent()) {
  155. if (obj instanceof Condition) {
  156. readCondition((Condition) obj,dsDB);
  157. } else if (obj instanceof Then) {
  158. readThen((Then) obj,dsDB);
  159. } else if (obj instanceof Else) {
  160. readElse((Else) obj,dsDB);
  161. }
  162. }
  163. }
  164. private void readCondition(Condition condition,Database dsDB) {
  165. for (Object obj : condition.getContent()) {
  166. if (obj instanceof Expression) {
  167. readExpression((Expression) obj,dsDB);
  168. }
  169. }
  170. }
  171. private void readThen(Then then,Database dsDB ) {
  172. // System.out.println("{ ");
  173. for (Object obj : then.getContent()) {
  174. if (obj instanceof Assign) {
  175. readAssign((Assign) obj,dsDB );
  176. } else if (obj instanceof If) {
  177. readIf((If) obj,dsDB );
  178. } else if (obj instanceof Loop) {
  179. readLoop((Loop) obj,dsDB);
  180. } else if (obj instanceof ATTACH) {
  181. // level + 1
  182. }
  183. }
  184. // System.out.println("}");
  185. }
  186. //private int loopIndex = 0;
  187. private String currentScopePath = null;
  188. private static int loopCount =0;
  189. private void readLoop(Loop loop,Database dsDB ) {
  190. loopCount++;
  191. checkLoopThroughCondtion(loop.getThrough(), dsDB);
  192. // System.out.println("{ ");
  193. for (Object obj : loop.getContent()) {
  194. if (obj instanceof Assign) {
  195. readAssign((Assign) obj,dsDB );
  196. } else if (obj instanceof If) {
  197. readIf((If) obj,dsDB);
  198. } else if (obj instanceof Loop) {
  199. String originalScopePath = new String(currentScopePath);
  200. currentScopePath = loop.getThrough();
  201. readLoop((Loop) obj,dsDB);
  202. currentScopePath = originalScopePath;
  203. } else if (obj instanceof ATTACH) {
  204. // level + 1
  205. }
  206. }
  207. }
  208. private void checkLoopThroughCondtion(String loopThroughCond ,Database dsDB){
  209. if(loopThroughCond.contains("/*"))
  210. System.out.println("scope ["+currentScopePath+"] contains loopthoruhg with * ["+loopThroughCond);
  211. /*StringBuffer queryString = new StringBuffer();
  212. queryString.append("//").append(currentScopePath).append("/").append(loopThroughCond).append("/@xpath/data()");
  213. Connection curScopeCon = new XMLConnection(dsDB);
  214. Query query = new XmlQuery(curScopeCon);
  215. query.createQuery(queryString.toString());
  216. Object returnObject = query.execute();
  217. if(returnObject == null || "".equals(returnObject)){
  218. System.out.println("SCOPE NAME:["+currentScopePath+"] LOOP THROGUH CONDITION="+loopThroughCond);
  219. }*/
  220. if(loopThroughCond.contains("//"))
  221. System.out.println("Scope ["+currentScopePath+"] contains loop through with // in xpath");
  222. }
  223. private void readElse(Else _else,Database dsDB) {
  224. // System.out.println("else");
  225. // System.out.println("{ ");
  226. for (Object obj : _else.getContent()) {
  227. if (obj instanceof Assign) {
  228. readAssign((Assign) obj,dsDB );
  229. } else if (obj instanceof If) {
  230. readIf((If) obj,dsDB);
  231. } else if (obj instanceof Loop) {
  232. readLoop((Loop) obj,dsDB);
  233. } else if (obj instanceof ATTACH) {
  234. // level + 1
  235. }
  236. }
  237. // System.out.println("}");
  238. }
  239. private void readAssign(Assign assign,Database dsDB) {
  240. // System.out.print(assign.getLValue() + " = ");
  241. // sb.append(assign.getLValue());
  242. for (Object obj : assign.getContent()) {
  243. if (obj instanceof Expression) {
  244. readExpression((Expression) obj,dsDB);
  245. }
  246. }
  247. }
  248. private void readExpression(Expression expression,Database dsDB ) {
  249. if (expression.getContent().size() > 1
  250. && !(expression.getVariableType() != null && expression
  251. .getVariableType().equals("RT"))) {
  252. // sb.append(" expression -> " + expression.getOp());
  253. readBothSides(expression,dsDB);
  254. } else {
  255. readContent(expression,dsDB );
  256. }
  257. }
  258. private void readBothSides(Expression expression,Database dsDB ) {
  259. for (Object obj : expression.getContent()) {
  260. if (obj instanceof Expression) {
  261. Expression ex = (Expression) obj;
  262. readExpression(ex,dsDB);
  263. } else if (obj instanceof Function) {
  264. //readFunction((Function) obj,dsDB);
  265. } else if (obj instanceof Ratetable) {
  266. //readRatetable((Ratetable) obj,dsDB);
  267. // rateTableList.add((Ratetable)obj);
  268. }
  269. }
  270. }
  271. private void readContent(Expression expression,Database dsDB) {
  272. for (Object obj : expression.getContent()) {
  273. if (obj instanceof String) {
  274. } else if (obj instanceof Expression) {
  275. readExpression((Expression) obj,dsDB);
  276. } else if (obj instanceof Ratetable) {
  277. //readRatetable((Ratetable) obj,dsDB);
  278. } else if (obj instanceof Function) {
  279. //((Function) obj,dsDB);
  280. }
  281. }
  282. }
  283. //private static final String fileName = "D:/files-by-shahsi/ISO-ERC-ALG-FILES/RC-BP-MD-09012012-V01/ALG-BP-MD-09012012-V01_MR.xml";
  284. String fileName = "D:\\files-by-shahsi\\ISO-ERC-ALG-FILES\\RC-CA-NY-10012012-V01\\ALG-CA-NY-10012012-V01_MR_REARRANGED.xml";
  285. //private static final String txAlgoFile = "D:\\files-by-shahsi\\ISO-ERC-ALG-FILES\\ALG-WC-TX-06012013-V01_MR\\ALG-WC-TX-06012013-V01_MR.xml";
  286. //String fileName = "D:\\files-by-shahsi\\ISO-ERC-ALG-FILES\\RC-CA-NY-10012012-V01\\CA_NY_TEST.xml";
  287. @Test
  288. public void testAlgoFileForPCh(){
  289. try {
  290. FileReader fileReader = new FileReader(new File(fileName));
  291. LOB lob = getLOBObject(fileReader);
  292. readLOB(lob);
  293. System.out.println("TOTAL LOOP FOUND="+loopCount);
  294. } catch (FileNotFoundException e) {
  295. // TODO Auto-generated catch block
  296. e.printStackTrace();
  297. }
  298. }
  299. }