1 package com.tonbeller.jpivot.table.span; 2 3 import java.io.PrintWriter ; 4 import java.util.Iterator ; 5 6 import junit.framework.TestCase; 7 8 import com.tonbeller.jpivot.olap.model.Axis; 9 import com.tonbeller.jpivot.olap.model.Displayable; 10 import com.tonbeller.jpivot.olap.model.Hierarchy; 11 import com.tonbeller.jpivot.olap.model.Level; 12 import com.tonbeller.jpivot.olap.model.Member; 13 import com.tonbeller.jpivot.olap.model.Visitor; 14 import com.tonbeller.jpivot.table.LevelAxisDecorator; 15 import com.tonbeller.jpivot.test.olap.DimensionBuilder; 16 import com.tonbeller.jpivot.test.olap.TestDimension; 17 import com.tonbeller.jpivot.test.olap.TestHierarchy; 18 import com.tonbeller.jpivot.test.olap.TestMember; 19 import com.tonbeller.jpivot.test.olap.TestMemberTree; 20 import com.tonbeller.jpivot.test.olap.TestOlapModelUtils; 21 22 27 public class SpanCalcTest extends TestCase { 28 29 33 public SpanCalcTest(String arg0) { 34 super(arg0); 35 } 36 37 public static void main(String [] args) { 38 junit.textui.TestRunner.run(SpanCalcTest.class); 39 } 40 41 protected void setUp() throws Exception { 42 43 } 44 45 46 63 64 public static Axis createAxis1() { 65 DimensionBuilder db = new DimensionBuilder(); 66 67 TestDimension dim1 = db.build("A", new String []{"B", "C"}, new int[]{2, 3}); 68 TestHierarchy hier1 = (TestHierarchy)dim1.getHierarchies()[0]; 69 TestMember[] members = hier1.getRootMembers(); 70 71 for (Iterator it = members[0].getChildMember().iterator(); it.hasNext(); ) 73 ((TestMember)it.next()).setVisible(true); 74 75 TestDimension dim2 = db.build("X", new String []{"Y"}, new int[]{2}); 76 77 TestDimension dims[] = new TestDimension[]{ dim1, dim2 }; 78 79 return TestOlapModelUtils.createAxis(dims); 80 } 81 82 99 public static Axis createAxis2() { 100 Axis axis = createAxis1(); 101 return new LevelAxisDecorator(axis, new TestMemberTree()); 102 } 103 104 static class SpanElem implements Displayable { 105 String s; 106 SpanElem(String s) { 107 this.s = s; 108 } 109 public String toString() { 110 return s; 111 } 112 public String getLabel() { 113 return s; 114 } 115 public void accept(Visitor visitor) { 116 throw new UnsupportedOperationException (); 117 } 118 } 119 120 static class A extends SpanElem { public A() { super("a"); } } 121 static class B extends SpanElem { public B() { super("b"); } } 122 static class C extends SpanElem { public C() { super("c"); } } 123 static class D extends SpanElem { public D() { super("d"); } } 124 static class E extends SpanElem { public E() { super("e"); } } 125 static class F extends SpanElem { public F() { super("f"); } } 126 static class G extends SpanElem { public G() { super("g"); } } 127 128 129 static final A a = new A(); 130 static final B b = new B(); 131 static final C c = new C(); 132 static final D d = new D(); 133 static final E e = new E(); 134 static final F f = new F(); 135 static final G g = new G(); 136 137 145 public static SpanCalc createTriangle() { 146 Span[][] spans = new Span[4][4]; 147 A a = new A(); 148 B b = new B(); 149 for (int posIndex = 0; posIndex < spans.length; posIndex++) { 150 for (int hierIndex = 0; hierIndex < spans[posIndex].length; hierIndex++) { 151 if (posIndex < hierIndex) 152 spans[posIndex][hierIndex] = new Span(a); 153 else 154 spans[posIndex][hierIndex] = new Span(b); 155 } 156 } 157 return new SpanCalc(spans); 158 } 159 160 161 162 163 164 void check(SpanCalc sc, int posIndex, int hierIndex, boolean significant, int posSpan, int hierSpan) { 165 sc.initialize(); 166 Span s = sc.getSpan(posIndex, hierIndex); 167 assertEquals("significance: ", significant, s.isSignificant()); 168 assertEquals("positionSpan:" , posSpan, s.getPositionSpan()); 169 assertEquals("hierarchySpan: ", hierSpan, s.getHierarchySpan()); 170 } 171 172 void assertMember(SpanCalc sc, int posIndex, int hierIndex) { 173 sc.initialize(); 174 Object o = sc.getSpan(posIndex, hierIndex).getObject(); 175 assertTrue("Member expected", o instanceof Member); 176 } 177 178 void assertLevel(SpanCalc sc, int posIndex, int hierIndex) { 179 sc.initialize(); 180 Object o = sc.getSpan(posIndex, hierIndex).getObject(); 181 assertTrue("Level expected", o instanceof Level); 182 } 183 184 void assertHierarchy(SpanCalc sc, int posIndex, int hierIndex) { 185 sc.initialize(); 186 Object o = sc.getSpan(posIndex, hierIndex).getObject(); 187 assertTrue("Hierarchy expected", o instanceof Hierarchy); 188 } 189 190 void checkPlausibility(SpanCalc sc) { 191 sc.initialize(); 192 Span[][] spans = sc.spans; 194 int length = sc.spans[0].length; 195 for (int i = 1; i < sc.spans.length; i++) 196 assertEquals("spans[" + i + "].length: ", length, sc.spans[i].length); 197 198 int[] sumPos = new int[sc.getHierarchyCount()]; 199 int[] sumHier = new int[sc.getPositionCount()]; 200 201 for (int posIndex = 0; posIndex < sc.getPositionCount(); posIndex ++) { 202 for (int hierIndex = 0; hierIndex < sc.getHierarchyCount(); hierIndex ++) { 203 Span s = sc.getSpan(posIndex, hierIndex); 204 assertEquals("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Position Index: ", posIndex, s.getPositionIndex()); 205 assertEquals("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Hierarchy Index: ", hierIndex, s.getHierarchyIndex()); 206 assertTrue("span[posIndex=" + posIndex + ", hierIndex=" + hierIndex + "] Significance: ", s.isSignificant() || (s.getHierarchySpan() == 0 && s.getPositionSpan() == 0)); 207 if (s.isSignificant()) { 208 for (int pi = 0; pi < s.getPositionSpan(); pi++) { 209 for (int hi = 0; hi < s.getHierarchySpan(); hi++) { 210 sumPos[hierIndex + hi] += 1; 211 sumHier[posIndex + pi] += 1; 212 } 213 } 214 } 215 } 216 } 217 218 int sum = sumPos[0]; 220 for (int i = 0; i < sumPos.length; i++) 221 assertEquals("span sumPositions[hierarchy=" + i + "]: ", sum, sumPos[i]); 222 223 sum = sumHier[0]; 224 for (int i = 0; i < sumHier.length; i++) 225 assertEquals("span sumHier[position=" + i + "]: ", sum, sumHier[i]); 226 } 227 228 229 230 public void testPlausibility() { 231 checkPlausibility(new SpanCalc(createAxis1())); 232 checkPlausibility(new SpanCalc(createAxis2())); 233 checkPlausibility(createTriangle()); 234 } 235 236 239 public void testTriangle_AN_BN() { 240 SpanCalc sc = createTriangle(); 241 checkPlausibility(sc); 242 for (int pi = 0; pi < sc.getPositionCount(); pi++) 243 for (int hi = 0; hi < sc.getHierarchyCount(); hi++) 244 check(sc, pi, hi, true, 1, 1); 245 } 246 247 248 257 public void testTriangle_AH_BH() { 258 259 SpanCalc sc = createTriangle(); 260 SpanConfigSupport scs = new SpanConfigSupport(); 261 scs.setDirection(A.class, SpanConfig.HIERARCHY_SPAN); 262 scs.setDirection(B.class, SpanConfig.HIERARCHY_SPAN); 263 sc.setConfig(scs); 264 checkPlausibility(sc); 265 267 check(sc, 0, 0, true, 1, 1); 268 check(sc, 0, 1, true, 1, 3); 269 check(sc, 0, 2, false, 0, 0); 270 check(sc, 0, 3, false, 0, 0); 271 272 check(sc, 1, 0, true, 1, 2); 273 check(sc, 1, 1, false, 0, 0); 274 check(sc, 1, 2, true, 1, 2); 275 check(sc, 1, 3, false, 0, 0); 276 277 check(sc, 2, 0, true, 1, 3); 278 check(sc, 2, 1, false, 0, 0); 279 check(sc, 2, 2, false, 0, 0); 280 check(sc, 2, 3, true, 1, 1); 281 282 check(sc, 3, 0, true, 1, 4); 283 check(sc, 3, 1, false, 0, 0); 284 check(sc, 3, 2, false, 0, 0); 285 check(sc, 3, 3, false, 0, 0); 286 } 287 288 297 public void testTriangle_AP_BP() { 298 299 SpanCalc sc = createTriangle(); 300 SpanConfigSupport scs = new SpanConfigSupport(); 301 scs.setDirection(A.class, SpanConfig.POSITION_SPAN); 302 scs.setDirection(B.class, SpanConfig.POSITION_SPAN); 303 sc.setConfig(scs); 304 checkPlausibility(sc); 305 check_AP_BP(sc); 307 308 scs.setDirection(B.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 309 sc.setConfig(scs); 310 checkPlausibility(sc); 311 check_AP_BP(sc); 312 313 } 314 315 void check_AP_BP(SpanCalc sc) { 316 check(sc, 0, 0, true, 4, 1); 317 check(sc, 1, 0, false, 0, 0); 318 check(sc, 2, 0, false, 0, 0); 319 check(sc, 3, 0, false, 0, 0); 320 321 check(sc, 0, 1, true, 1, 1); 322 check(sc, 1, 1, true, 3, 1); 323 check(sc, 2, 0, false, 0, 0); 324 check(sc, 3, 0, false, 0, 0); 325 326 check(sc, 0, 2, true, 1, 1); 327 check(sc, 1, 2, true, 1, 1); 328 check(sc, 2, 2, true, 2, 1); 329 check(sc, 3, 2, false, 0, 0); 330 331 check(sc, 0, 3, true, 1, 1); 332 check(sc, 1, 3, true, 1, 1); 333 check(sc, 2, 3, true, 1, 1); 334 check(sc, 3, 3, true, 1, 1); 335 } 336 337 346 public void testTriangle_AH_BP() { 347 348 SpanCalc sc = createTriangle(); 349 SpanConfigSupport scs = new SpanConfigSupport(); 350 scs.setDirection(A.class, SpanConfig.HIERARCHY_SPAN); 351 scs.setDirection(B.class, SpanConfig.POSITION_SPAN); 352 sc.setConfig(scs); 353 checkPlausibility(sc); 354 356 check(sc, 0, 0, true, 4, 1); 357 check(sc, 1, 0, false, 0, 0); 358 check(sc, 2, 0, false, 0, 0); 359 check(sc, 3, 0, false, 0, 0); 360 361 check(sc, 0, 1, true, 1, 3); 362 check(sc, 1, 1, true, 3, 1); 363 check(sc, 2, 0, false, 0, 0); 364 check(sc, 3, 0, false, 0, 0); 365 366 check(sc, 0, 2, false, 0, 0); 367 check(sc, 1, 2, true, 1, 2); 368 check(sc, 2, 2, true, 2, 1); 369 check(sc, 3, 2, false, 0, 0); 370 371 check(sc, 0, 3, false, 0, 0); 372 check(sc, 1, 3, false, 0, 0); 373 check(sc, 2, 3, true, 1, 1); 374 check(sc, 3, 3, true, 1, 1); 375 } 376 377 378 public void testTriangle_AHP_BHP() { 379 SpanCalc sc = createTriangle(); 380 SpanConfigSupport scs = new SpanConfigSupport(); 381 scs.setDirection(A.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 382 scs.setDirection(B.class, SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 383 sc.setConfig(scs); 384 checkPlausibility(sc); 385 387 check(sc, 0, 0, true, 4, 1); 388 check(sc, 0, 1, true, 1, 3); 389 check(sc, 0, 2, false, 0, 0); 390 check(sc, 0, 3, false, 0, 0); 391 check(sc, 1, 0, false, 0, 0); 392 check(sc, 1, 1, true, 3, 1); 393 check(sc, 1, 2, true, 1, 2); 394 check(sc, 1, 3, false, 0, 0); 395 check(sc, 2, 0, false, 0, 0); 396 check(sc, 2, 1, false, 0, 0); 397 check(sc, 2, 2, true, 2, 1); 398 check(sc, 2, 3, true, 1, 1); 399 check(sc, 3, 0, false, 0, 0); 400 check(sc, 3, 1, false, 0, 0); 401 check(sc, 3, 2, false, 0, 0); 402 check(sc, 3, 3, true, 1, 1); 403 404 } 405 406 414 public void testHierarchicalPositionSpans1() { 415 Span[][] spans = new Span[3][2]; 416 spans[0][0] = new Span(a); 417 spans[0][1] = new Span(b); 418 spans[1][0] = new Span(a); 419 spans[1][1] = new Span(c); 420 spans[2][0] = new Span(b); 421 spans[2][1] = new Span(c); 422 423 424 SpanCalc sc = new SpanCalc(spans); 425 SpanConfigSupport scs = new SpanConfigSupport(); 426 scs.setDefaultDirection(SpanConfig.POSITION_SPAN); 427 sc.setConfig(scs); 428 checkPlausibility(sc); 429 430 check(sc, 0, 0, true, 2, 1); 431 check(sc, 1, 0, false, 0, 0); 432 check(sc, 2, 0, true, 1, 1); 433 434 check(sc, 0, 1, true, 1, 1); 436 check(sc, 1, 1, true, 1, 1); 437 check(sc, 2, 1, true, 1, 1); 438 439 } 440 441 447 public void testHierarchicalPositionSpans2() { 448 Span[][] spans = new Span[2][2]; 449 spans[0][0] = new Span(a); 450 spans[1][0] = new Span(b); 451 spans[0][1] = new Span(c); 452 spans[1][1] = new Span(c); 453 SpanCalc sc = new SpanCalc(spans); 454 455 SpanConfigSupport scs = new SpanConfigSupport(); 456 scs.setDefaultDirection(SpanConfig.POSITION_SPAN); 457 sc.setConfig(scs); 458 sc.initialize(); 459 checkPlausibility(sc); 460 461 check(sc, 0, 0, true, 1, 1); 463 check(sc, 1, 0, true, 1, 1); 464 check(sc, 0, 1, true, 1, 1); 465 check(sc, 1, 1, true, 1, 1); 466 467 } 468 469 477 public void testBug1() { 478 Span[][] spans = new Span[4][3]; 479 480 spans[0][0] = new Span(a); 481 spans[1][0] = new Span(a); 482 spans[2][0] = new Span(a); 483 spans[3][0] = new Span(a); 484 485 spans[0][1] = new Span(a); 486 spans[1][1] = new Span(b); 487 spans[2][1] = new Span(c); 488 spans[3][1] = new Span(d); 489 490 spans[0][2] = new Span(e); 491 spans[1][2] = new Span(e); 492 spans[2][2] = new Span(e); 493 spans[3][2] = new Span(e); 494 495 SpanCalc sc = new SpanCalc(spans); 496 497 SpanConfigSupport scs = new SpanConfigSupport(); 498 scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 499 sc.setConfig(scs); 500 sc.initialize(); 501 checkPlausibility(sc); 502 503 check(sc, 0, 2, true, 1, 1); 505 check(sc, 1, 2, true, 1, 1); 506 check(sc, 2, 2, true, 1, 1); 507 check(sc, 3, 2, true, 1, 1); 508 509 } 510 511 512 513 519 public void testMultiSpan1() { 520 Span[][] spans = new Span[2][2]; 521 spans[0][0] = new Span(a); 522 spans[1][0] = new Span(a); 523 spans[0][1] = new Span(a); 524 spans[1][1] = new Span(a); 525 SpanCalc sc = new SpanCalc(spans); 526 527 SpanConfigSupport scs = new SpanConfigSupport(); 528 scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 529 sc.setConfig(scs); 530 checkPlausibility(sc); 531 533 check(sc, 0, 0, true, 2, 2); 535 check(sc, 1, 0, false, 0, 0); 536 check(sc, 0, 1, false, 0, 0); 537 check(sc, 1, 1, false, 0, 0); 538 539 } 540 541 549 public SpanCalc createMulti1() { 550 Span[][] spans = new Span[4][4]; 551 spans[0][0] = new Span(a); 552 spans[1][0] = new Span(a); 553 spans[2][0] = new Span(a); 554 spans[3][0] = new Span(a); 555 556 spans[0][1] = new Span(a); 557 spans[1][1] = new Span(a); 558 spans[2][1] = new Span(b); 559 spans[3][1] = new Span(b); 560 561 spans[0][2] = new Span(c); 562 spans[1][2] = new Span(c); 563 spans[2][2] = new Span(c); 564 spans[3][2] = new Span(c); 565 566 spans[0][3] = new Span(d); 567 spans[1][3] = new Span(e); 568 spans[2][3] = new Span(f); 569 spans[3][3] = new Span(g); 570 571 return new SpanCalc(spans); 572 } 573 574 575 576 584 public void testMultiSpan2() { 585 SpanCalc sc = createMulti1(); 586 587 SpanConfigSupport scs = new SpanConfigSupport(); 588 scs.setDefaultDirection(SpanConfig.HIERARCHY_THEN_POSITION_SPAN); 589 sc.setConfig(scs); 590 sc.initialize(); 591 checkPlausibility(sc); 592 593 check(sc, 0, 2, true, 2, 1); 595 check(sc, 1, 2, false, 0, 0); 596 check(sc, 2, 2, true, 2, 1); 597 check(sc, 3, 1, false, 0, 0); 598 } 599 600 601 602 603 604 static class MySpanCalc extends SpanCalc { 605 int counter; 606 public MySpanCalc(Axis axis) { 607 super(axis); 608 } 609 public void initialize() { 610 ++ counter; 611 super.initialize(); 612 } 613 } 614 615 public void testInitializer() { 616 Axis axis1 = createAxis1(); 617 MySpanCalc sc = new MySpanCalc(axis1); 618 assertEquals(sc.counter, 0); 619 sc.getHierarchyCount(); 620 sc.getHierarchyCount(); 621 assertEquals(sc.counter, 1); 622 623 sc = new MySpanCalc(axis1); 624 assertEquals(sc.counter, 0); 625 sc.getPositionCount(); 626 sc.getPositionCount(); 627 assertEquals(sc.counter, 1); 628 629 sc = new MySpanCalc(axis1); 630 assertEquals(sc.counter, 0); 631 sc.getSpan(0,0); 632 sc.getSpan(0,0); 633 assertEquals(sc.counter, 1); 634 635 } 636 637 638 class AllEqual implements Displayable { 639 public boolean equals(Object o) { 640 return o instanceof AllEqual; 641 } 642 public String getLabel() { 643 return this.toString(); 644 } 645 public void accept(Visitor visitor) { 646 throw new UnsupportedOperationException (); 647 } 648 } 649 650 SpanHeaderFactory allEqualFactory = new SpanHeaderFactory() { 651 public Span create(Span span) { 652 AllEqual ae = new AllEqual(); 653 return new Span(span.getAxis(), span.getPosition(), ae); 654 } 655 }; 656 657 672 public void testAddAllEqualToHierarchy() { 673 SpanCalc span2 = new SpanCalc(createAxis2()); 674 span2.addHierarchyHeader(allEqualFactory, true); 675 checkPlausibility(span2); 676 assertEquals(4, span2.getHierarchyCount()); 677 } 678 679 680 681 695 public void testAddHierarchiesToHierarchy() { 696 SpanCalc sc = new SpanCalc(createAxis2()); 697 sc.addHierarchyHeader(new HierarchyHeaderFactory(), true); 698 SpanConfigSupport scs = new SpanConfigSupport(); 699 scs.setDefaultDirection(SpanConfig.POSITION_SPAN); 700 sc.setConfig(scs); 701 checkPlausibility(sc); 702 703 assertEquals(5, sc.getHierarchyCount()); 704 assertEquals(10, sc.getPositionCount()); 705 706 assertHierarchy(sc, 0, 0); 707 assertMember(sc, 0, 1); 708 assertMember(sc, 0, 2); 709 assertHierarchy(sc, 0, 3); 710 assertMember(sc, 0, 4); 711 712 715 check(sc, 0, 0, true, 10, 1); 716 check(sc, 0, 1, true, 8, 1); 717 check(sc, 0, 2, true, 2, 1); 718 check(sc, 0, 3, true, 2, 1); 719 check(sc, 0, 4, true, 1, 1); 720 } 721 722 723 724 725 739 public void testAddLevelsToHierarchy() { 740 SpanCalc sc = new SpanCalc(createAxis2()); 741 sc.addHierarchyHeader(new LevelHeaderFactory(), true); 742 SpanConfigSupport scs = new SpanConfigSupport(); 743 scs.setDefaultDirection(SpanConfig.POSITION_SPAN); 744 sc.setConfig(scs); 745 checkPlausibility(sc); 746 assertEquals(6, sc.getHierarchyCount()); 747 assertEquals(10, sc.getPositionCount()); 748 749 assertLevel(sc, 0, 0); 750 assertLevel(sc, 1, 0); 751 assertLevel(sc, 1, 4); 752 753 assertMember(sc, 0, 2); 754 assertMember(sc, 1, 2); 755 assertLevel (sc, 2, 2); 756 assertLevel (sc, 3, 2); 757 assertLevel (sc, 4, 2); 758 assertLevel (sc, 5, 2); 759 assertLevel (sc, 6, 2); 760 assertLevel (sc, 7, 2); 761 assertMember(sc, 8, 2); 762 assertMember(sc, 9, 2); 763 764 assertMember(sc, 0, 1); 765 assertMember(sc, 1, 1); 766 767 770 check(sc, 0, 0, true, 10, 1); 771 check(sc, 0, 1, true, 8, 1); 772 check(sc, 0, 2, true, 2, 1); 773 check(sc, 0, 3, true, 2, 1); 774 check(sc, 0, 4, true, 2, 1); 775 check(sc, 0, 5, true, 1, 1); 776 777 check(sc, 1, 0, false, 0, 0); 778 check(sc, 1, 1, false, 0, 0); 779 check(sc, 1, 2, false, 0, 0); 780 check(sc, 1, 3, false, 0, 0); 781 check(sc, 1, 4, false, 0, 0); 782 check(sc, 1, 5, true, 1, 1); 783 784 check(sc, 2, 2, true, 6, 1); 785 786 } 787 788 789 790 807 808 public void testCreatePositionHeader1() { 809 SpanCalc sc = new SpanCalc(createAxis2()); 810 811 SpanCalc h = sc.createPositionHeader(new LevelHeaderFactory()); 812 checkPlausibility(h); 813 assertEquals(1, h.getPositionCount()); 814 assertEquals(3, h.getHierarchyCount()); 815 816 check(h, 0, 0, true, 1, 1); 817 check(h, 0, 1, true, 1, 1); 818 check(h, 0, 2, true, 1, 1); 819 assertEquals("B", ((Level)h.getSpan(0,0).getObject()).getLabel()); 820 assertEquals("C", ((Level)h.getSpan(0,1).getObject()).getLabel()); 821 assertEquals("Y", ((Level)h.getSpan(0,2).getObject()).getLabel()); 822 } 823 824 841 842 public void testCreatePositionHeader2() { 843 SpanCalc sc = new SpanCalc(createAxis2()); 844 845 SpanCalc h = sc.createPositionHeader(new HierarchyHeaderFactory()); 846 checkPlausibility(h); 847 assertEquals(1, h.getPositionCount()); 848 assertEquals(3, h.getHierarchyCount()); 849 850 check(h, 0, 0, true, 1, 1); 851 check(h, 0, 1, true, 1, 1); 852 check(h, 0, 2, true, 1, 1); 853 assertEquals("A", ((Hierarchy)h.getSpan(0,0).getObject()).getLabel()); 854 assertEquals("A", ((Hierarchy)h.getSpan(0,1).getObject()).getLabel()); 855 assertEquals("X", ((Hierarchy)h.getSpan(0,2).getObject()).getLabel()); 856 } 857 858 public void toString(String title, SpanCalc sc) { 859 PrintWriter pw = new PrintWriter (System.out); 860 pw.println("*** " + title + " ***"); 861 pw.println("objects:"); 862 printObjects(sc, pw); 863 pw.println("spans:"); 864 printSpans(sc, pw); 865 pw.println("breaks"); 866 printBreaks(sc, pw); 867 pw.println("code:"); 868 printCode(sc, pw); 869 pw.flush(); 871 } 872 873 void printObjects(SpanCalc sc, PrintWriter out) { 874 for (int pi = 0; pi < sc.getPositionCount(); pi++) { 875 for (int hi = 0; hi < sc.getHierarchyCount(); hi++) { 876 Span s = sc.getSpan(pi, hi); 877 out.print(" "); 878 out.print(s.getObject().toString()); 879 } 880 out.println(); 881 } 882 } 883 884 void printBreaks(SpanCalc sc, PrintWriter out) { 885 for (int pi = 0; pi < sc.getPositionCount(); pi++) { 886 for (int hi = 0; hi < sc.getHierarchyCount(); hi++) { 887 Span s = sc.getSpan(pi, hi); 888 out.print(" "); 889 out.print(sc.forcePositionBreak[pi][hi] ? "x" : "."); 890 } 891 out.println(); 892 } 893 } 894 895 void printCode(SpanCalc sc, PrintWriter out) { 896 for (int pi = 0; pi < sc.getPositionCount(); pi++) { 897 for (int hi = 0; hi < sc.getHierarchyCount(); hi++) { 898 Span s = sc.getSpan(pi, hi); 899 out.println(" check(sc, " 900 + s.getPositionIndex() + ", " 901 + s.getHierarchyIndex() + ", " 902 + s.isSignificant() + ", " 903 + s.getPositionSpan() + ", " 904 + s.getHierarchySpan() + ");"); 905 } 906 } 907 } 908 909 void printSpans(SpanCalc sc, PrintWriter out) { 910 911 char[][] carr = new char[sc.getPositionCount()][sc.getHierarchyCount()]; 912 char letter = 'a'; 913 914 for (int pi = 0; pi < sc.getPositionCount(); pi++) { 915 for (int hi = 0; hi < sc.getHierarchyCount(); hi++) { 916 Span s = sc.getSpan(pi, hi); 917 assertEquals(pi, s.getPositionIndex()); 918 assertEquals(hi, s.getHierarchyIndex()); 919 if (s.isSignificant()) { 920 for (int pspan = 0; pspan < s.getPositionSpan(); pspan++) { 921 for (int hspan = 0; hspan < s.getHierarchySpan(); hspan++) { 922 carr[pi + pspan][hi + hspan] = letter; 923 } 924 } 925 letter ++; 926 } 927 } 928 } 929 930 for (int i = 0; i < carr.length; i++) { 931 for (int j = 0; j < carr[i].length; j++) { 932 out.print(" "); 933 out.print(carr[i][j]); 934 } 935 out.println(); 936 } 937 938 } 939 940 } 941 | Popular Tags |