/machinelearning/4.0.x/drools-eclipse3.3/drools-eclipse-plugin/src/main/java/org/drools/eclipse/dsl/editor/completion/DSLRuleCompletionProcessor.java

https://github.com/sotty/droolsjbpm-contributed-experiments · Java · 168 lines · 108 code · 12 blank · 48 comment · 25 complexity · 4aaace4177803e91e9b25a36a5662c1d MD5 · raw file

  1. package org.drools.eclipse.dsl.editor.completion;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import org.drools.eclipse.DroolsPluginImages;
  8. import org.drools.eclipse.dsl.editor.DSLAdapter;
  9. import org.drools.eclipse.dsl.editor.DSLRuleEditor;
  10. import org.drools.eclipse.editors.AbstractRuleEditor;
  11. import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
  12. import org.drools.eclipse.editors.completion.RuleCompletionProposal;
  13. import org.drools.lang.Location;
  14. import org.eclipse.swt.graphics.Image;
  15. /**
  16. * For handling DSL rules.
  17. *
  18. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  19. */
  20. public class DSLRuleCompletionProcessor extends RuleCompletionProcessor {
  21. private static final Image DSL_ICON =
  22. DroolsPluginImages.getImage(DroolsPluginImages.DSL_EXPRESSION);
  23. public DSLRuleCompletionProcessor(AbstractRuleEditor editor) {
  24. super(editor);
  25. }
  26. protected DSLRuleEditor getDSLRuleEditor() {
  27. return (DSLRuleEditor) getEditor();
  28. }
  29. protected void addRHSCompletionProposals(List list, int documentOffset, String prefix, String backText,
  30. String conditions, String consequence) {
  31. // super.addRHSCompletionProposals(list, documentOffset, prefix, backText, conditions, consequence);
  32. DSLAdapter adapter = getDSLRuleEditor().getDSLAdapter();
  33. if (adapter != null) {
  34. List dslConsequences = adapter.getDSLTree().getConsequenceChildrenList(prefix, true);
  35. addDSLProposals(list, documentOffset, prefix, dslConsequences);
  36. }
  37. }
  38. protected void addLHSCompletionProposals(List list, int documentOffset,
  39. Location location, String prefix, String backText) {
  40. // super.addLHSCompletionProposals(list, documentOffset, location, prefix, backText);
  41. DSLAdapter adapter = getDSLRuleEditor().getDSLAdapter();
  42. if (adapter != null) {
  43. String lastobj = this.getLastNonDashLine(backText);
  44. String last = this.getLastLine(backText);
  45. // we have to check if the last line is when. if it is we set
  46. // the last line to zero length string
  47. if (last.equals("when")) {
  48. last = "";
  49. lastobj = "*";
  50. }
  51. // pass the last string in the backText to getProposals
  52. List dslConditions = this.getProposals(adapter, lastobj, last);
  53. // if we couldn't find any matches, we add the list from
  54. // the DSLAdapter so that there's something
  55. if (dslConditions.size() == 0) {
  56. dslConditions.addAll(adapter.listConditionItems());
  57. }
  58. addDSLProposals(list, documentOffset, prefix, dslConditions);
  59. }
  60. }
  61. private void addDSLProposals(final List list, int documentOffset, final String prefix, List dslItems) {
  62. Iterator iterator = dslItems.iterator();
  63. while (iterator.hasNext()) {
  64. String consequence = (String) iterator.next();
  65. RuleCompletionProposal p = new RuleCompletionProposal(
  66. documentOffset - prefix.length(), prefix.length(), consequence);
  67. p.setImage(DSL_ICON);
  68. list.add(p);
  69. }
  70. }
  71. /**
  72. * because of how the backText works, we need to get the last line, so that
  73. * we can pass it to the DSLUtility
  74. *
  75. * @param backText
  76. * @return
  77. */
  78. public String getLastLine(String backText) {
  79. BufferedReader breader = new BufferedReader(new StringReader(backText));
  80. String last = "";
  81. String line = null;
  82. try {
  83. while ((line = breader.readLine()) != null) {
  84. // only if the line has text do we set last to it
  85. if (line.length() > 0) {
  86. last = line;
  87. }
  88. }
  89. } catch (IOException e) {
  90. // TODO need to log this.
  91. // I'm leaving this for mic_hat, so he has something to do
  92. }
  93. // now that all the conditions for a single object are on the same line
  94. // we need to check for the left parenthesis
  95. if (last.indexOf("(") > -1) {
  96. last = last.substring(last.lastIndexOf("(") + 1);
  97. }
  98. // if the string has a comma "," we get the substring starting from
  99. // the index after the last comma
  100. if (last.indexOf(",") > -1) {
  101. last = last.substring(last.lastIndexOf(",") + 1);
  102. }
  103. // if the line ends with right parenthesis, we change it to zero length
  104. // string
  105. if (last.endsWith(")")) {
  106. last = "";
  107. }
  108. return last;
  109. }
  110. /**
  111. * Returns the last line that doesn't start with a dash
  112. *
  113. * @param backText
  114. * @return
  115. */
  116. public String getLastNonDashLine(String backText) {
  117. BufferedReader breader = new BufferedReader(new StringReader(backText));
  118. String last = "";
  119. String line = null;
  120. try {
  121. while ((line = breader.readLine()) != null) {
  122. // there may be blank lines, so we trim first
  123. line = line.trim();
  124. // only if the line has text do we set last to it
  125. if (line.length() > 0 && !line.startsWith("-")) {
  126. last = line;
  127. }
  128. }
  129. } catch (IOException e) {
  130. // TODO need to log this.
  131. // I'm leaving this for mic_hat, so he has something to do
  132. }
  133. if (last.indexOf("(") > -1 && !last.endsWith(")")) {
  134. last = last.substring(0, last.indexOf("("));
  135. } else if (last.indexOf("(") > -1 && last.endsWith(")")) {
  136. last = "";
  137. }
  138. return last;
  139. }
  140. /**
  141. * The DSLTree is configurable. It can either return just the child of the
  142. * last token found, or it can traverse the tree and generate all the
  143. * combinations beneath the last matching node. TODO I don't know how to add
  144. * configuration to the editor, so it needs to be hooked up to the
  145. * configuration for the editor later.
  146. *
  147. * @param last
  148. * @return
  149. */
  150. protected List getProposals(DSLAdapter adapter, String obj, String last) {
  151. if (last.length() == 0) {
  152. last = " ";
  153. }
  154. return adapter.getDSLTree().getChildrenList(obj, last, true);
  155. }
  156. }