DemonstratorCD.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * CertainTrust Demonstrator in Java for CONSENSUS and DISCOUNTING
  3. *
  4. * Demonstrates some capabilities of the CertainTrust SDK
  5. * using a Java applet that interactively calculates
  6. * AND and OR of two CertainTrust data objects and
  7. * visually displays both the input objects and the output.
  8. *
  9. * @author Florian Volk <florian.volk@cased.de>
  10. */
  11. import java.awt.FlowLayout;
  12. import java.awt.Font;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.GridBagLayout;
  15. import java.awt.Insets;
  16. import java.awt.Label;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import javax.swing.JApplet;
  20. import javax.swing.JPanel;
  21. import CertainTrust.CertainTrust;
  22. import CertainTrust.CertainTrustHTI;
  23. public class DemonstratorCD extends JApplet {
  24. private static final long serialVersionUID = -7397289934413234935L;
  25. // the CertainTrust data objects
  26. private CertainTrust CONOperand1;
  27. private CertainTrust CONOperand2;
  28. private CertainTrust CONResult;
  29. private CertainTrust DISOperand1;
  30. private CertainTrust DISOperand2;
  31. private CertainTrust DISResult;
  32. private static int N = 10;
  33. // the HTI objects that hold the CertainTrust data
  34. private CertainTrustHTI HTIAndOperand1;
  35. private CertainTrustHTI HTIAndOperand2;
  36. private CertainTrustHTI HTIAndResult;
  37. private CertainTrustHTI HTIOrOperand1;
  38. private CertainTrustHTI HTIOrOperand2;
  39. private CertainTrustHTI HTIOrResult;
  40. public DemonstratorCD() {
  41. setupUI();
  42. setupLogic();
  43. }
  44. @Override
  45. public void init() {
  46. // make sure nothing is cut off due to a too small applet
  47. setSize(1200, 500);
  48. }
  49. /**
  50. * setupUI() creates the user interface and
  51. * binds all HTIs to the related CertainTrust data objects
  52. */
  53. private void setupUI() {
  54. // use GridBayLayout to align all the elements
  55. GridBagConstraints constraints = new GridBagConstraints();
  56. setLayout(new GridBagLayout());
  57. // title line "Demonstrator for CertainTrust"
  58. constraints.gridwidth = GridBagConstraints.REMAINDER;
  59. constraints.gridheight = 1;
  60. constraints.insets = new Insets(10, 0, 0, 0);
  61. Label title = new Label("Demonstrator for CertainTrust");
  62. title.setFont(new Font("Sans Serif", Font.BOLD, 24));
  63. add(title, constraints);
  64. // subtitle line
  65. constraints.insets = new Insets(0, 0, 10, 0);
  66. add(new Label("CertainTrust provides a means for the evaluation of propositional logic terms under uncertainty."), constraints);
  67. // operands and result for CONSENSUS operation
  68. JPanel CONPanel = new JPanel();
  69. CONPanel.setLayout(new FlowLayout());
  70. this.CONOperand1 = new CertainTrust(N);
  71. this.HTIAndOperand1 = new CertainTrustHTI(this.CONOperand1);
  72. CONPanel.add(this.HTIAndOperand1);
  73. CONPanel.add(new Label("CONSENSUS"));
  74. this.CONOperand2 = new CertainTrust(N);
  75. this.HTIAndOperand2 = new CertainTrustHTI(this.CONOperand2);
  76. CONPanel.add(this.HTIAndOperand2);
  77. CONPanel.add(new Label("="));
  78. this.CONResult = new CertainTrust(N);
  79. Map<String,String> htiConfig = new HashMap<String, String>();
  80. htiConfig.put("readonly", "true");
  81. this.HTIAndResult = new CertainTrustHTI(this.CONResult, htiConfig);
  82. CONPanel.add(this.HTIAndResult);
  83. constraints.insets = new Insets(30, 0, 10, 0);
  84. constraints.gridwidth = GridBagConstraints.REMAINDER;
  85. add(CONPanel, constraints);
  86. // operands and result for DISCOUNTING operation
  87. JPanel DISPanel = new JPanel();
  88. DISPanel.setLayout(new FlowLayout());
  89. this.DISOperand1 = new CertainTrust(N);
  90. this.HTIOrOperand1 = new CertainTrustHTI(this.DISOperand1);
  91. DISPanel.add(this.HTIOrOperand1);
  92. DISPanel.add(new Label("DISCOUNTING"));
  93. this.DISOperand2 = new CertainTrust(N);
  94. this.HTIOrOperand2 = new CertainTrustHTI(this.DISOperand2);
  95. DISPanel.add(this.HTIOrOperand2);
  96. DISPanel.add(new Label("="));
  97. this.DISResult = new CertainTrust(N);
  98. this.HTIOrResult = new CertainTrustHTI(this.DISResult, htiConfig);
  99. DISPanel.add(this.HTIOrResult);
  100. add(DISPanel, constraints);
  101. }
  102. /**
  103. * setupLogic() implements the wiring between the different CertainTrust data objects,
  104. * the HTIs get updates automatically due to the used Observer pattern
  105. */
  106. private void setupLogic() {
  107. // wire all components forming the CONSENSUS operation
  108. CONSENSUSObserver conObserver = new CONSENSUSObserver(CONOperand1, CONOperand2, CONResult);
  109. CONOperand1.addObserver(conObserver);
  110. CONOperand2.addObserver(conObserver);
  111. // do an initial update
  112. conObserver.update(null, null);
  113. // same for the OR operation
  114. DISCOUNTINGObserver disObserver = new DISCOUNTINGObserver(DISOperand1, DISOperand2, DISResult);
  115. DISOperand1.addObserver(disObserver);
  116. DISOperand2.addObserver(disObserver);
  117. disObserver.update(null, null);
  118. }
  119. }