Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.40 KB | None | 0 0
  1. package SurlyPackage;
  2.  
  3. import java.util.*;
  4.  
  5. public class Relation {
  6. String name;
  7. LinkedList<Attribute> attribs = new LinkedList<Attribute>();
  8. LinkedList<Tuple> rows = new LinkedList<Tuple>();
  9.  
  10. /***************************************CONSTRUCTORS***********************************/
  11. public Relation(String _name, Attribute[] _attributes)
  12. {
  13. this.name = _name;
  14. for(Attribute a : _attributes)
  15. {
  16. attribs.add(a);
  17. }
  18.  
  19. }
  20.  
  21. public Relation(Relation r)
  22. {
  23. this.attribs = r.attribs;
  24. this.rows = r.rows;
  25. }
  26.  
  27. public Relation(String __name, LinkedList<Attribute> _attribs){
  28. this.name = __name;
  29. this.attribs = _attribs;
  30. }
  31.  
  32. public Relation()
  33. {
  34. this.name = "Temp";
  35. this.attribs = null;
  36. }
  37. /***************************************INSERT***********************************/
  38. public void Insert(Tuple t) {
  39. rows.add(t);
  40. }
  41.  
  42. /***************************************PRINT***********************************/
  43.  
  44. public void Print() {
  45.  
  46. System.out.println("Relation " + this.name);
  47.  
  48. String attribNames = "|";
  49.  
  50. for(int i = 0; i < attribs.size(); i++)
  51. {
  52. if (attribs.get(i).name.length() <= Integer.parseInt(attribs.get(i).formatSpacing)){
  53. attribNames += attribs.get(i).name;
  54. } else {
  55. int diff = attribs.get(i).name.length() - Integer.parseInt(attribs.get(i).formatSpacing) + 1;
  56. String abrvName = attribs.get(i).name.substring(0, attribs.get(i).name.length() - diff);
  57. attribNames += abrvName + ".";
  58. }
  59. int whiteSpace = Integer.parseInt(attribs.get(i).formatSpacing);
  60. int remainingWhiteSpace = whiteSpace - attribs.get(i).name.length();
  61.  
  62. for(int j = 0; j < remainingWhiteSpace; j++)
  63. {
  64. attribNames += " ";
  65. }
  66. attribNames += "|";
  67. }
  68. System.out.println(attribNames);
  69.  
  70. String border = "+";
  71. for(int h = 0; h < attribs.size(); h++)
  72. {
  73. for(int b = 0; b < Integer.parseInt(attribs.get(h).formatSpacing); b++)
  74. {
  75. border += "-";
  76. }
  77. border += "+";
  78. }
  79.  
  80. System.out.println(border);
  81. for(Tuple t : rows)
  82. {
  83. String tString = "|";
  84. String endBit = "+";
  85. for (int i = 0; i < attribs.size(); i++)
  86. {
  87. for (int j = 0; j < Integer.parseInt(attribs.get(i).formatSpacing); j++)
  88. {
  89. if (j < t.data.get(i).length())
  90. {
  91. tString += t.data.get(i).charAt(j);
  92. }
  93.  
  94. else
  95. {
  96. tString += " ";
  97. }
  98. endBit += "-";
  99. }
  100. tString += "|";
  101. endBit += "+";
  102. }
  103. System.out.println(tString + "\n" + endBit);
  104. }
  105. }
  106.  
  107. /***************************************DELETE***********************************/
  108.  
  109. public Relation DeleteWhere(String condition, Relation rel)
  110. {
  111. String[] splitCondition = condition.split(" ");
  112. int conditionIndex = 0;
  113. Relation r = new Relation(rel);
  114. for (Attribute a: r.attribs)
  115. {
  116. if(a.name.equals(splitCondition[0]))
  117. {
  118. break;
  119. }
  120. else
  121. {
  122. conditionIndex++;
  123. }
  124. }
  125. Iterator<Tuple> it = r.rows.iterator();
  126. while(it.hasNext())
  127. {
  128. Tuple t = it.next();
  129. if(splitCondition[1].equals("="))
  130. {
  131. //System.out.println("got this far");
  132. r.evalEqual(it,t,conditionIndex,splitCondition[2],r);
  133. }
  134. else if (splitCondition[1].equals("!="))
  135. {
  136. //System.out.println("got this far");
  137. r.notEqual(it,t,conditionIndex,splitCondition[2],r);
  138. }
  139. else if (splitCondition[1].equals("<"))
  140. {
  141. //System.out.println("got this far");
  142. r.evalEqual(it,t,conditionIndex,splitCondition[2],r);
  143. }
  144. else if (splitCondition[1].equals(">"))
  145. {
  146. //System.out.println("got this far");
  147. r.evalEqual(it,t,conditionIndex,splitCondition[2],r);
  148. }
  149.  
  150. }
  151. return r;
  152. }
  153.  
  154. public void DeleteLoop(Relation rel, String[] Conditions)
  155. {
  156. Database tempDB = new Database("temp");
  157. for(int i = 0; i < Conditions.length; i+=2)
  158. {
  159. tempDB.relations.add(DeleteWhere(Conditions[i],rel));
  160. }
  161.  
  162. tempDB.relations.getFirst().Print();
  163. /*
  164. int iter = 1;
  165. for(int j = 1; j< Conditions.length-1; j+=2)
  166. {
  167. if(Conditions[j].equals("AND"))
  168. {
  169. for(Tuple t : tempDB.relations.get(iter).rows)
  170. {
  171. //add
  172. }
  173. }
  174. else if(Conditions[j].equals("OR"))
  175. {
  176. Iterator<Tuple> t1 = tempDB.relations.getFirst().rows.iterator();
  177. while(t1.hasNext())
  178. {
  179. if(tempDB.relations.get(iter).rows.contains(t1.next()))
  180. {
  181. t1.remove();
  182. }
  183. }
  184. iter++;
  185. }
  186. }
  187. /*
  188. for(Relation R: tempDB.relations)
  189. {
  190. R.Print();
  191. }
  192. */
  193.  
  194. }
  195.  
  196. public void evalEqual(Iterator<Tuple> i, Tuple t, int index, String value, Relation r)
  197. {
  198. //System.out.println("got this far2");
  199. if(isNum(value))
  200. {
  201. if(Integer.parseInt(t.data.get(index)) == Integer.parseInt(value))
  202. {
  203. i.remove();
  204. }
  205. }
  206. if(t.data.get(index).equals(value))
  207. {
  208. //System.out.println("got this far3");
  209. i.remove();
  210. //System.out.println("got this far4");
  211. }
  212. }
  213.  
  214. public void lessThan(Iterator<Tuple> i, Tuple t, int index, String value, Relation r)
  215. {
  216. try {
  217. int v = Integer.parseInt(value);
  218. if(Integer.parseInt(t.data.get(index)) < v)
  219. {
  220. i.remove();
  221. }
  222. }catch (NumberFormatException e)
  223. {
  224. System.out.println("Not a ranged comparison hommie.");
  225. }
  226. }
  227.  
  228. public void greaterThan(Iterator<Tuple> i, Tuple t, int index, String value, Relation r)
  229. {
  230. try {
  231. int v = Integer.parseInt(value);
  232. if(Integer.parseInt(t.data.get(index)) > v)
  233. {
  234. i.remove();
  235. }
  236. }catch (NumberFormatException e)
  237. {
  238. System.out.println("Not a ranged comparison hommie.");
  239. }
  240. }
  241.  
  242. public void notEqual(Iterator<Tuple> i, Tuple t, int index, String value, Relation r)
  243. {
  244. //System.out.println("got this far2");
  245. if(isNum(value))
  246. {
  247. if(Integer.parseInt(t.data.get(index)) != Integer.parseInt(value))
  248. {
  249. i.remove();
  250. }
  251. }
  252. if(!t.data.get(index).equals(value))
  253. {
  254. //System.out.println("got this far3");
  255. i.remove();
  256. //System.out.println("got this far4");
  257. }
  258. }
  259.  
  260. /***************************************PROJECT***********************************/
  261. public Relation Project(String projName, Relation relName, Attribute[] projAttrib){
  262. LinkedList<Attribute> testAttributes = new LinkedList<Attribute>();
  263. LinkedList<Attribute> projAttributes = new LinkedList<Attribute>();
  264. LinkedList<Tuple> projRows = new LinkedList<Tuple>();
  265. LinkedList<String> projData = new LinkedList<String>();
  266. LinkedList<Integer> attIndex = new LinkedList<Integer>();
  267. Tuple newTuple = new Tuple(projData);
  268. for (int i = 0; i < projAttrib.length; i++){
  269. testAttributes.add(projAttrib[i]);
  270. }
  271.  
  272. for (int i = 0; i < projAttrib.length; i++){
  273. // System.out.println("testAttributes[i] = " + testAttributes.get(i).name);
  274. // System.out.println("projAttrib[i] = " + projAttrib[i].name);
  275. if (testAttributes.get(i).equals(projAttrib[i])){
  276. // System.out.println(relName.attribs.indexOf(projAttrib[i]));
  277. attIndex.add(relName.attribs.indexOf(projAttrib[i]));
  278. }
  279. }
  280. Collections.sort(attIndex);
  281. for (Attribute a: relName.attribs){
  282. if(testAttributes.contains(a)){
  283. projAttributes.add(a);
  284. for (Tuple t: relName.rows){
  285. for (Integer i: attIndex){
  286. projData.add(t.data.get(i));
  287. }
  288. newTuple = new Tuple(projData);
  289. projData = new LinkedList<String>();
  290. if (!containsTuple(projRows, newTuple)){
  291. projRows.add(newTuple);
  292. }
  293. }
  294. }
  295. }
  296. Relation projRelation = new Relation(projName, projAttributes);
  297. for (Tuple t: projRows){
  298. projRelation.Insert(t);
  299. }
  300. return projRelation;
  301. }
  302.  
  303. public boolean containsTuple(LinkedList<Tuple> tuples, Tuple tup){
  304. boolean ret = false;
  305. for (Tuple t: tuples){
  306. if (t.data.get(0).equals(tup.data.get(0))){
  307. ret = true;
  308. }
  309. }
  310. return ret;
  311. }
  312.  
  313.  
  314. /***************************************JOIN***********************************/
  315. public Relation Join(String joinName, Relation relationA, Relation relationB, String joinCondition){
  316. LinkedList<Attribute> joinAttributes = new LinkedList<Attribute>();
  317. LinkedList<Tuple> joinRows = new LinkedList<Tuple>();
  318. String[] splitCondition = joinCondition.split(" ");
  319. Tuple joinTuple = new Tuple(new String[0]);
  320.  
  321. int joinCondIndex1 = 0;
  322. int joinCondIndex2 = 0;
  323.  
  324. for (Attribute a: relationA.attribs){
  325. if (splitCondition[0].equals(a.name)){
  326. joinCondIndex1 = relationA.attribs.indexOf(a);
  327. }
  328. joinAttributes.add(a);
  329. }
  330. for (Attribute a: relationB.attribs){
  331. if (splitCondition[2].equals(a.name)){
  332. joinCondIndex2 = relationB.attribs.indexOf(a);
  333. }
  334. joinAttributes.add(a);
  335. }
  336.  
  337. for (Tuple t1: relationA.rows){
  338. for (Tuple t2: relationB.rows){
  339. if (t1.data.get(joinCondIndex1).equals(t2.data.get(joinCondIndex2))){
  340. for (String s1: t1.data){
  341. joinTuple.data.add(s1);
  342. }
  343. for (String s2: t2.data){
  344. joinTuple.data.add(s2);
  345. }
  346. joinRows.add(joinTuple);
  347. joinTuple = new Tuple(new String[0]);
  348. }
  349. }
  350. }
  351. Relation joinRelation = new Relation(joinName, joinAttributes);
  352. for (Tuple t: joinRows){
  353. joinRelation.Insert(t);
  354. }
  355. joinRelation.Print();
  356. return joinRelation;
  357. }
  358.  
  359. /***************************************SELECT***********************************/
  360. public Relation Select(String tempName, Relation rel, String[] conditions){
  361. LinkedList<Relation> tempDB = new LinkedList<Relation>();
  362. LinkedList<Attribute> selectAttributes = new LinkedList<Attribute>();
  363.  
  364. Relation newRelation = new Relation(tempName, selectAttributes);
  365. //create a relation for every condition
  366. for (int i = 0; i < conditions.length; i+=2){
  367. tempDB.add(SelectLoop(tempName, rel, conditions[i]));
  368. }
  369.  
  370. for (Attribute a: tempDB.get(0).attribs){
  371. selectAttributes.add(a);
  372. }
  373.  
  374. if (conditions.length == 1){
  375. return tempDB.get(0);
  376. }
  377.  
  378.  
  379. for (int i = 0; i < conditions.length; i++){
  380. if (tempDB.size() == 1){
  381. return tempDB.get(0);
  382. }
  383. if (conditions[i].equals("OR")){
  384. for (Tuple t: tempDB.get(0).rows){
  385. newRelation.Insert(t);
  386. }
  387. for (Tuple t: tempDB.get(1).rows){
  388. newRelation.Insert(t);
  389. }
  390. if (tempDB.size() == 1){
  391. return tempDB.get(0);
  392. }
  393. tempDB.remove(0);
  394. tempDB.remove(0);
  395. tempDB.addFirst(newRelation);
  396. } else if (conditions[i].equals("AND")){
  397. Relation r1 = tempDB.get(0);
  398. Relation r2 = tempDB.get(1);
  399. newRelation = new Relation(tempName, selectAttributes);
  400. try {
  401. for (Tuple t1: r1.rows){
  402. for (Tuple t2 : r2.rows){
  403. if (t1 == t2){
  404. newRelation.Insert(t1);
  405. }
  406. }
  407. }
  408. } catch (ConcurrentModificationException e){
  409. System.out.println();
  410. }
  411. if (tempDB.size() == 1){
  412. return tempDB.get(0);
  413. }
  414. tempDB.remove(0);
  415. tempDB.remove(0);
  416. tempDB.addFirst(newRelation);
  417. }
  418.  
  419. }
  420. return tempDB.get(0);
  421. }
  422.  
  423.  
  424.  
  425. public Relation SelectLoop(String tempName, Relation r, String conditions){
  426. LinkedList<Attribute> tempAttributes = new LinkedList<Attribute>();
  427. LinkedList<Tuple> tempRows = new LinkedList<Tuple>();
  428. String[] splitCondition = conditions.split(" ");
  429. int condNumber = 0;
  430. //System.out.println(Arrays.toString(splitCondition));
  431. if (isNum(splitCondition[2])){
  432. condNumber = Integer.parseInt(splitCondition[2]);
  433. }
  434.  
  435. for (Attribute a: r.attribs){
  436. tempAttributes.add(a);
  437. if (splitCondition[0].equals(a.name)){
  438. int attIndex = r.attribs.indexOf(a);
  439. for (Tuple t: r.rows){
  440. //System.out.println("t.data: " + t.data);
  441. if (splitCondition[1].equals("=")){
  442. //change member to contains
  443. if (splitCondition[2].equals(t.data.get(attIndex)) && !tempRows.contains(t.data.get(attIndex))){
  444. tempRows.add(t);
  445. }
  446. } else if (splitCondition[1].equals("!=")) {
  447. if (!splitCondition[2].equals(t.data.get(attIndex)) && !tempRows.contains(t.data.get(attIndex))){
  448. tempRows.add(t);
  449. }
  450. } else if (splitCondition[1].equals(">=")) {
  451. if (Integer.parseInt(t.data.get(attIndex)) >= condNumber){
  452. tempRows.add(t);
  453. }
  454. } else if (splitCondition[1].equals("<=")) {
  455. if (Integer.parseInt(t.data.get(attIndex)) <= condNumber){
  456. tempRows.add(t);
  457. }
  458. } else if (splitCondition[1].equals(">")) {
  459. if (Integer.parseInt(t.data.get(attIndex)) > condNumber){
  460. tempRows.add(t);
  461. }
  462. } else if (splitCondition[1].equals("<")) {
  463. if (Integer.parseInt(t.data.get(attIndex)) < condNumber){
  464. tempRows.add(t);
  465. }
  466. }
  467. }
  468. }
  469. }
  470. Relation tempRelation = new Relation(tempName, tempAttributes);
  471. for (Tuple t: tempRows){
  472. tempRelation.Insert(t);
  473. }
  474. // System.out.println("tempAttributes: " + Arrays.toString(tempAttributes.toArray()));
  475. //tempRelation.Print();
  476. return tempRelation;
  477. }
  478.  
  479. public boolean memberAttribute(LinkedList<Attribute> attributes, String s){
  480. for (Attribute a: attributes){
  481. if (a.name.equals(s)){
  482. return true;
  483. }
  484. }
  485. return false;
  486. }
  487.  
  488. public boolean memberTuple(LinkedList<Tuple> rows, String s){
  489. for (Tuple t: rows){
  490. for (String str: t.data){
  491. if (str.equals(s)){
  492. return true;
  493. }
  494. }
  495. }
  496. return false;
  497. }
  498.  
  499. public Tuple popFirst(Tuple t, int attIndex){
  500. for (int i = 0; i < attIndex; i++){
  501. t.data.removeFirst();
  502. }
  503. return t;
  504. }
  505.  
  506. public static boolean isNum(String strNum) {
  507. boolean ret = true;
  508. try {
  509. Double.parseDouble(strNum);
  510. }catch (NumberFormatException e) {
  511. ret = false;
  512. }
  513. return ret;
  514. }
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement