Package lupos.gui.operatorgraph.visualeditor.ruleeditor.guielements

Examples of lupos.gui.operatorgraph.visualeditor.ruleeditor.guielements.ImplementationPanel


  private void generate_rules() {
    for(JTabbedPane rulePane : this.editor.getRules()) {
      RulePanel rule = (RulePanel) rulePane;
      String ruleClassName = this.capitalizeWord(rule.toString().replaceAll(" ", "") + "Rule");
      String ruleClassFileName = ruleClassName + ".java";
      ImplementationPanel rImplementationPanel = rule.getImplementationPanel();

      // -- validating rule - begin --
      Triple<Boolean, HashMap<String, VariableContainer>, HashMap<String, VariableContainer>> resultTriple = this.validateRule(ruleClassName, rule);

      if(!resultTriple.getFirst()) {
        continue;
      }
      // -- validating rule - end --

      System.out.println(":: generating " + ruleClassFileName + "...");

      this.variableList_left = resultTriple.getSecond();
      this.globalCode = new StringBuffer(this.formatCode(rImplementationPanel.getAdditionalGlobalJavaCode(), "    ") + "\n");
      String additionalImportDeclarations = rImplementationPanel.getAdditionalImportDeclarations()+"\n";

      String startNodeClass = this.generate_check_method(rule);
      this.generate_replace_method(rule, resultTriple.getThird());

      String javaCode = String.format(this.getTemplate("ruleClass"), this.packageName, additionalImportDeclarations, ruleClassName, this.globalCode.toString(), ruleClassName, startNodeClass, rule.toString(), this.checkMethodCode.toString(), this.replaceMethodCode.toString());
View Full Code Here


      File.writeFile(this.targetDirectory + ruleClassFileName, javaCode);
    }
  }

  private String generate_check_method(RulePanel rule) {
    ImplementationPanel rImplementationPanel = rule.getImplementationPanel();
    RuleEditorPane rRuleEditorPane = rule.getRuleEditorPane();
    String startNodeClass = "";

    this.checkMethodCode = new StringBuffer();
    this.buffers = new LinkedList<StringBuffer>();

    if(rImplementationPanel.useGeneratedJavaCode_CheckMethod()) {
      RuleOperator startNode = rRuleEditorPane.getStartNode();
      startNodeClass = startNode.getClassType().getOpClass().getName();

      this.count_private = 0;

      int maxDimension = 0;
      StringBuffer spaces = new StringBuffer("    ");

      for(VariableContainer vc : this.variableList_left.values()) {
        this.globalCode.append(vc.declate_variable(spaces, true));

        maxDimension = Math.max(maxDimension, vc.getDimension());
      }

      for(int i = 0; i < maxDimension; i += 1) {
        this.globalCode.append("    private int _dim_" + i + " = -1;\n");
      }

      spaces = this.generate_checkPrivate(startNode, new HashSet<AbstractRuleOperator>(), new HashMap<Operator, HashSet<Operator>>(), 0);

      for(StringBuffer buffer : this.buffers) {
        this.globalCode.append(buffer);
      }

      if(!rImplementationPanel.getAdditionalCheckJavaCode().equals("")) {
        spaces.append("    ");

        this.checkMethodCode.append(
            spaces + "boolean _result = this._checkPrivate0(_op);\n" +
            "\n" +
            spaces + "if(_result) {\n" +
            spaces + "    // additional check method code...\n" +
            this.formatCode(rImplementationPanel.getAdditionalCheckJavaCode(), spaces.toString() + "    ").toString() + "\n" +
            spaces + "}\n" +
            "\n" +
            spaces + "return _result;"
        );
      }
      else {
        this.checkMethodCode.append(spaces + "    return this._checkPrivate0(_op);");
      }
    }
    else {
      startNodeClass = rImplementationPanel.getStartNodeClass();
      this.checkMethodCode.append(this.formatCode(rImplementationPanel.getCheckJavaCode(), "        ").toString());
    }

    return startNodeClass;
  }
View Full Code Here

    return spaces;
  }

  @SuppressWarnings("unchecked")
  private void generate_replace_method(RulePanel rule, HashMap<String, VariableContainer> variableList_right) {
    ImplementationPanel rImplementationPanel = rule.getImplementationPanel();
    RuleEditorPane rRuleEditorPane = rule.getRuleEditorPane();

    this.replaceMethodCode = new StringBuffer();
    StringBuffer spaces = new StringBuffer("        ");

    if(rImplementationPanel.useGeneratedJavaCode_ReplaceMethod()) {
      VisualGraph<Operator> leftGraph = rRuleEditorPane.getVisualGraphs().get(0);
      VisualGraph<Operator> rightGraph = rRuleEditorPane.getVisualGraphs().get(1);

      HashMap<String, AbstractRuleOperator> leftOperatorsMap = this.getOperatorsMap(leftGraph);
      HashMap<String, AbstractRuleOperator> rightOperatorsMap = this.getOperatorsMap(rightGraph);

      HashSet<String> leftOperators = new HashSet<String>(leftOperatorsMap.keySet());
      HashSet<String> rightOperators = new HashSet<String>(rightOperatorsMap.keySet());

      HashSet<String> originalLeftOperators = (HashSet<String>) leftOperators.clone();

      leftOperators.removeAll(rightOperators); // determine operators which are only on the left side
      rightOperators.removeAll(originalLeftOperators); // determine operators which are only on the right side


      LinkedList<HashSet<ConnectionContainer>> connections = rRuleEditorPane.getConnections();
      HashSet<ConnectionContainer> leftConnections = connections.get(0);
      HashSet<ConnectionContainer> rightConnections = connections.get(1);

      HashSet<ConnectionContainer> originalLeftConnections = (HashSet<ConnectionContainer>) leftConnections.clone();

      leftConnections.removeAll(rightConnections); // determine connections which are only on the left side
      rightConnections.removeAll(originalLeftConnections); // determine connections which are only on the right side


      // --- remove all connections that only occur on the left side - begin ---
      this.replaceMethodCode.append(
          spaces + "// remove obsolete connections...\n"
      );

      for(ConnectionContainer conn : leftConnections) {
        AbstractRuleOperator parentOp = conn.getParent();
        AbstractRuleOperator childOp = conn.getChild();
        String parentName = this.getJumpOverName(parentOp, false);
        String childName = this.getJumpOverName(childOp, true);

        String opIDLabel = conn.getOpIDLabel();
        boolean hasLabel = conn.getIsActive() && !opIDLabel.equals("");
        StringBuffer labelBrackets = new StringBuffer();

        String succeedingString = null;
        int dim = 0;
        StringBuffer arrayAccess = new StringBuffer();

        switch(conn.getMode()) {
        case ALL_PRECEDING:
          dim = this.variableList_left.get(childName).getDimension();

          if(hasLabel) {
            for(int i = 0; i <= dim; i += 1) {
              labelBrackets.append("[]");
            }

            this.replaceMethodCode.append(
                spaces + "int" + labelBrackets + " _label_" + opIDLabel + " = null;\n" +
                "\n"
            );
          }

          for(int i = 0; i < dim; i += 1) {
            if(hasLabel) {
              labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

              this.replaceMethodCode.append(
                  spaces + "_label_" + opIDLabel + arrayAccess + " = new int[this." + childName + ".length]" + labelBrackets + ";\n" +
                  "\n"
              );
            }

            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < this." + childName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          succeedingString = (conn.getIsActive() && conn.getOpID() != -1) ? "new OperatorIDTuple(this." + childName + arrayAccess + ", " + conn.getOpID() + ")" : "this." + childName + arrayAccess;

          if(hasLabel) {
            labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

            this.replaceMethodCode.append(
                spaces + "int _label_" + opIDLabel + "_count" + " = 0;\n" +
                spaces + "_label_" + opIDLabel + arrayAccess + " = new int[this." + parentName + arrayAccess + ".length]" + labelBrackets + ";\n" +
                "\n"
            );
          }

          this.replaceMethodCode.append(spaces + "for(" + parentOp.getClassType().getOpClass().getName() + " _parent : this." + parentName + arrayAccess + ") {\n");

          if(hasLabel) {
            this.replaceMethodCode.append(
                spaces + "    _label_" + opIDLabel + arrayAccess + "[_label_" + opIDLabel + "_count] = _parent.getOperatorIDTuple(this." + childName + arrayAccess + ").getId();\n" +
                spaces + "    _label_" + opIDLabel + "_count += 1;\n" +
                "\n"
            );
          }

          this.replaceMethodCode.append(
              spaces + "    _parent.removeSucceedingOperator(" + succeedingString + ");\n" +
              spaces + "    this." + childName + arrayAccess + ".removePrecedingOperator(_parent);\n" +
              spaces + "}\n" +
              "\n"
          );

          break;

        case ALL_SUCCEEDING:
          dim = this.variableList_left.get(parentName).getDimension();

          if(hasLabel) {
            for(int i = 0; i <= dim; i += 1) {
              labelBrackets.append("[]");
            }

            this.replaceMethodCode.append(
                spaces + "int" + labelBrackets + " _label_" + opIDLabel + " = null;\n" +
                "\n"
            );
          }

          for(int i = 0; i < dim; i += 1) {
            if(hasLabel) {
              labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

              this.replaceMethodCode.append(
                  spaces + "_label_" + opIDLabel + arrayAccess + " = new int[this." + childName + ".length]" + labelBrackets + ";\n" +
                  "\n"
              );
            }

            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < this." + parentName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          succeedingString = (conn.getIsActive() && conn.getOpID() != -1) ? "new OperatorIDTuple(_child, " + conn.getOpID() + ")" : "_child";

          if(hasLabel) {
            labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

            this.replaceMethodCode.append(
                spaces + "int _label_" + opIDLabel + "_count = 0;\n" +
                spaces + "_label_" + opIDLabel + arrayAccess + " = new int[this." + childName + arrayAccess + ".length]" + labelBrackets + ";\n" +
                "\n"
            );
          }

          this.replaceMethodCode.append(spaces + "for(" + childOp.getClassType().getOpClass().getName() + " _child : this." + childName + arrayAccess + ") {\n");

          if(hasLabel) {
            this.replaceMethodCode.append(
                spaces + "    _label_" + opIDLabel + arrayAccess + "[_label_" + opIDLabel + "_count] = this." + parentName + arrayAccess + ".getOperatorIDTuple(_child).getId();\n" +
                spaces + "    _label_" + opIDLabel + "_count += 1;\n" +
                "\n"
            );
          }

          this.replaceMethodCode.append(
              spaces + "    this." + parentName + arrayAccess + ".removeSucceedingOperator(" + succeedingString + ");\n" +
              spaces + "    _child.removePrecedingOperator(this." + parentName + arrayAccess + ");\n" +
              spaces + "}\n" +
              "\n"
          );

          break;

        default:
          dim = this.variableList_left.get(parentName).getDimension();

          if(hasLabel) {
            for(int i = 0; i <= dim; i += 1) {
              labelBrackets.append("[]");
            }

            this.replaceMethodCode.append(
                spaces + "int" + labelBrackets + " _label_" + opIDLabel + " = null;\n" +
                "\n"
            );
          }

          for(int i = 0; i < dim; i += 1) {
            if(hasLabel) {
              labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

              this.replaceMethodCode.append(
                  spaces + "_label_" + opIDLabel + arrayAccess + " = new int[this." + childName + ".length]" + labelBrackets + ";\n" +
                  "\n"
              );
            }

            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < this." + parentName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          succeedingString = (conn.getIsActive() && conn.getOpID() != -1) ? "new OperatorIDTuple(this." + childName + arrayAccess + ", " + conn.getOpID() + ")" : "this." + childName + arrayAccess;

          if(hasLabel) {
            labelBrackets.delete(labelBrackets.length()-2, labelBrackets.length());

            this.replaceMethodCode.append(
                spaces + "    _label_" + opIDLabel + arrayAccess + " = this." + parentName + arrayAccess + ".getOperatorIDTuple(this." + childName + arrayAccess + ").getId();\n" +
                "\n"
            );
          }

          this.replaceMethodCode.append(
              spaces + "this." + parentName + arrayAccess + ".removeSucceedingOperator(" + succeedingString + ");\n" +
              spaces + "this." + childName + arrayAccess + ".removePrecedingOperator(this." + parentName + arrayAccess + ");\n"
          );

          break;
        }

        for(int i = 0; i < dim; i += 1) {
          spaces.delete(spaces.length()-4, spaces.length());

          this.replaceMethodCode.append(spaces + "}\n");
        }
      }
      // --- remove all connections that only occur on the left side - end ---

      // --- add operators that only occur on the right side - begin ---
      this.replaceMethodCode.append(
          "\n" +
          spaces + "// add new operators...\n"
      );

      for(String opName : rightOperators) {
        AbstractRuleOperator op = rightOperatorsMap.get(opName);
        String opClass = op.getClassType().getOpClass().getName();
        VariableContainer vc = variableList_right.get(opName);
        int dim = vc.getDimension();
        StringBuffer arrayAccess = new StringBuffer();

        for(int i = 0; i < dim; i += 1) {
          arrayAccess.append("[]");
        }

        this.replaceMethodCode.append(spaces + opClass + arrayAccess + " " + opName + " = null;\n");

        arrayAccess = new StringBuffer();

        for(int i = 0; i < dim; i += 1) {
          this.replaceMethodCode.append(
              vc.initiate_next_dimension(spaces, i, this.getOpName(vc.getCountProvider(), rightOperators, true) + arrayAccess + ".length", false) +
              "\n" +
              spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < " + opName + arrayAccess + ".length; this._dim_" + i + " += 1) {\n"
          );

          spaces.append("    ");
          arrayAccess.append("[this._dim_" + i + "]");
        }

        this.replaceMethodCode.append(spaces + opName + arrayAccess + " = new " + opClass + "();\n");

        if(this.generateStartMap) {
          this.replaceMethodCode.append(spaces + "this.addNodeToStartNodeMap(" + opName + arrayAccess + ", _startNodes);\n");
        }

        for(int i = 0; i < dim; i += 1) {
          spaces.delete(spaces.length()-4, spaces.length());

          this.replaceMethodCode.append(spaces + "}\n");
        }
      }

      this.replaceMethodCode.append("\n");
      // --- add operators that only occur on the right side - end ---

      // --- add connections that only occur on the right side - begin ---
      this.replaceMethodCode.append(
          "\n" +
          spaces + "// add new connections...\n"
      );

      for(ConnectionContainer conn : rightConnections) {
        AbstractRuleOperator parentOp = conn.getParent();
        AbstractRuleOperator childOp = conn.getChild();
        String parentName = this.getOpName(parentOp, rightOperators, false);
        String childName = this.getOpName(childOp, rightOperators, true);
        VariableContainer parentVC = variableList_right.get(this.getJumpOverName(parentOp, false));
        VariableContainer childVC = variableList_right.get(this.getJumpOverName(childOp, true));

        String opIDLabel = conn.getOpIDLabel();
        boolean hasLabel = conn.getIsActive() && !opIDLabel.equals("");

        int dim = 0;
        StringBuffer arrayAccess = new StringBuffer();
        String succeedingString = "";

        switch(conn.getMode()) {
        case ALL_PRECEDING:
          dim = childVC.getDimension();

          for(int i = 0; i < dim; i += 1) {
            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < " + childName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          if(conn.getIsActive()) {
            if(hasLabel) {
              succeedingString = "new OperatorIDTuple(" + childName + arrayAccess + ", _label_" + conn.getOpIDLabel() + arrayAccess + "[_label_" + opIDLabel + "_count])";

              this.replaceMethodCode.append(
                  spaces + "_label_" + opIDLabel + "_count = 0;\n" +
                  "\n"
              );
            }
            else {
              succeedingString = "new OperatorIDTuple(" + childName + arrayAccess + ", " + conn.getOpID() + ")";
            }
          }
          else {
            succeedingString = childName + arrayAccess;
          }

          this.replaceMethodCode.append(
              spaces + "for(" + parentOp.getClassType().getOpClass().getName() + " _parent : " + parentName + arrayAccess + ") {\n" +
              spaces + "    _parent.addSucceedingOperator(" + succeedingString + ");\n" +
              spaces + "    " + childName + arrayAccess + ".addPrecedingOperator(_parent);\n"
          );

          if(hasLabel) {
            this.replaceMethodCode.append(
                "\n" +
                spaces + "    _label_" + opIDLabel + "_count += 1;\n"
            );
          }

          this.replaceMethodCode.append(
              spaces + "}\n" +
              "\n"
          );

          break;

        case ALL_SUCCEEDING:
          dim = parentVC.getDimension();

          for(int i = 0; i < dim; i += 1) {
            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < " + parentName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          if(conn.getIsActive()) {
            if(hasLabel) {
              succeedingString = "new OperatorIDTuple(_child, _label_" + conn.getOpIDLabel() + arrayAccess + "[_label_" + opIDLabel + "_count])";

              this.replaceMethodCode.append(
                  spaces + "_label_" + opIDLabel + "_count = 0;\n" +
                  "\n"
              );
            }
            else {
              succeedingString = "new OperatorIDTuple(_child, " + conn.getOpID() + ")";
            }
          }
          else {
            succeedingString = "_child";
          }

          this.replaceMethodCode.append(
              spaces + "for(" + childOp.getClassType().getOpClass().getName() + " _child : " + childName + arrayAccess + ") {\n" +
              spaces + "    " + parentName + arrayAccess + ".addSucceedingOperator(" + succeedingString + ");\n" +
              spaces + "    _child.addPrecedingOperator(" + parentName + arrayAccess + ");\n"
          );

          if(hasLabel) {
            this.replaceMethodCode.append(
                "\n" +
                spaces + "    _label_" + opIDLabel + "_count += 1;\n"
            );
          }

          this.replaceMethodCode.append(
              spaces + "}\n" +
              "\n"
          );

          break;

        default:
          dim = parentVC.getDimension();

          for(int i = 0; i < dim; i += 1) {
            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < " + parentName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          if(conn.getIsActive()) {
            if(hasLabel) {
              succeedingString = "new OperatorIDTuple(" + childName + arrayAccess + ", _label_" + conn.getOpIDLabel() + arrayAccess + ")";
            }
            else {
              succeedingString = "new OperatorIDTuple(" + childName + arrayAccess + ", " + conn.getOpID() + ")";
            }
          }
          else {
            succeedingString = childName + arrayAccess;
          }

          this.replaceMethodCode.append(
              spaces + parentName + arrayAccess + ".addSucceedingOperator(" + succeedingString + ");\n" +
              spaces + childName + arrayAccess + ".addPrecedingOperator(" + parentName + arrayAccess + ");\n"
          );
        }

        for(int i = 0; i < dim; i += 1) {
          spaces.delete(spaces.length()-4, spaces.length());

          this.replaceMethodCode.append(spaces + "}\n");
        }

        this.replaceMethodCode.append("\n");
      }
      // --- add connections that only occur on the right side - begin ---

      // --- delete operators from left side which are not needed anymore - begin ---
      if(leftOperators.size() > 0) {
        this.replaceMethodCode.append(
            "\n" +
            spaces + "// delete unreachable operators...\n"
        );

        for(String opName : leftOperators) {
          int dim = this.variableList_left.get(opName).getDimension();
          StringBuffer arrayAccess = new StringBuffer();

          for(int i = 0; i < dim; i += 1) {
            this.replaceMethodCode.append(spaces + "for(this._dim_" + i + " = 0; this._dim_" + i + " < " + opName + ".length; this._dim_" + i + " += 1) {\n");

            arrayAccess.append("[this._dim_" + i + "]");

            spaces.append("    ");
          }

          this.replaceMethodCode.append(spaces + "this.deleteOperatorWithoutParentsRecursive(this." + opName + arrayAccess + ", _startNodes);\n");

          for(int i = 0; i < dim; i += 1) {
            spaces.delete(spaces.length()-4, spaces.length());

            this.replaceMethodCode.append(spaces + "}\n");
          }

          if(dim > 0) {
            this.replaceMethodCode.append("\n");
          }
        }

        this.replaceMethodCode.append("\n");
      }
      // --- delete operators from left side which are not needed anymore - end ---

      this.replaceMethodCode.append(
          "\n" +
          spaces + "// additional replace method code...\n" +
          this.formatCode(rImplementationPanel.getAdditonalReplaceJavaCode(), spaces.toString()).toString()
      );
    }
    else {
      this.replaceMethodCode.append(this.formatCode(rImplementationPanel.getReplaceJavaCode(), spaces.toString()).toString());
    }
  }
View Full Code Here

    if(resultTriple == null) {
      System.out.print(":: validating rule '" + ruleName + "'... ");

      RuleEditorPane ruleEditorPane = rule.getRuleEditorPane();
      ImplementationPanel implementationPanel = rule.getImplementationPanel();

      resultTriple = ruleEditorPane.validateGraphs();

      if(resultTriple.getFirst()) {
        if(ruleEditorPane.getVisualGraphs().get(0).isEmpty() && ruleEditorPane.getVisualGraphs().get(1).isEmpty()
            && implementationPanel.getAdditionalCheckJavaCode().isEmpty() && implementationPanel.getCheckJavaCode().isEmpty()
            && implementationPanel.getAdditonalReplaceJavaCode().isEmpty() && implementationPanel.getReplaceJavaCode().isEmpty()) {
          JOptionPane.showOptionDialog(this.editor, "The rule '" + ruleName + "' has no visual representation and no code defined.\nIt will therefore be ignored in the code generation", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);

          resultTriple = new Triple<Boolean, HashMap<String, VariableContainer>, HashMap<String, VariableContainer>>(false, null, null);

          System.out.println("PASSED WITH WARNING");
View Full Code Here

TOP

Related Classes of lupos.gui.operatorgraph.visualeditor.ruleeditor.guielements.ImplementationPanel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.