⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ruleui.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

         predicateBtn = new JButton("Insert Predicate");
         predicateBtn.addActionListener(this);
         predicateBtn.setToolTipText("Click to insert selected predicate");
	 gbc.gridwidth = 1;
         gbc.anchor = GridBagConstraints.WEST;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = new Insets(8,8,0,0);
         gridBagLayout.setConstraints(predicateBtn,gbc);
         panel.add(predicateBtn);

         predicateList = new JComboBox(booleanWds) {
            public void contentsChanged(ListDataEvent e) {
               selectedItemReminder = null;
               super.contentsChanged(e);
            }
         };
         predicateList.addActionListener(this);
         functionList.setForeground(Color.red);
	 gbc.gridwidth = GridBagConstraints.REMAINDER;
         gbc.anchor = GridBagConstraints.WEST;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = new Insets(8,8,0,0);
         gridBagLayout.setConstraints(predicateList,gbc);
         panel.add(predicateList);

         actionBtn = new JButton("Insert Action");
         actionBtn.addActionListener(this);
         actionBtn.setToolTipText("Click to insert selected action");
	 gbc.gridwidth = 1;
         gbc.anchor = GridBagConstraints.WEST;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = new Insets(8,8,0,0);
         gridBagLayout.setConstraints(actionBtn,gbc);
         panel.add(actionBtn);

         actionList = new JComboBox(precedenceWds) {
            public void contentsChanged(ListDataEvent e) {
               selectedItemReminder = null;
               super.contentsChanged(e);
            }
         };
         actionList.addActionListener(this);
         functionList.setForeground(Color.blue);
	 gbc.gridwidth = GridBagConstraints.REMAINDER;
         gbc.anchor = GridBagConstraints.WEST;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.insets = new Insets(8,8,0,0);
         gridBagLayout.setConstraints(actionList,gbc);
         panel.add(actionList);

	 factPanel= new FactPanel(this,db);
//         factPanel.setPreferredSize(new Dimension(LWIDTH,LHEIGHT+300));
         factPanel.setBorder(makeBorder("Ontology"));

	 gbc.gridwidth = GridBagConstraints.REMAINDER;
         gbc.anchor = GridBagConstraints.NORTHWEST;
         gbc.fill = GridBagConstraints.BOTH;
         gbc.weightx = gbc.weighty = 1;
         gbc.insets = new Insets(8,0,0,0);
         gridBagLayout.setConstraints(factPanel,gbc);
         panel.add(factPanel);

         return panel;
      }
//---------------------------------------------------------------------------
      JTextArea getLastTextAreaWithFocus() {
          if (lhsFocus)
            return lhsArea;
          else if (rhsFocus)
            return rhsArea;
          else
            return null;
      }
//---------------------------------------------------------------------------
      void setFocus(JTextArea textarea){

           if (textarea == lhsArea) {
             lhsFocus = true;
             rhsFocus = false;
           }
           else if (textarea == rhsArea) {
             rhsFocus = true;
             lhsFocus = false;
           }
      }
//---------------------------------------------------------------------------
      void appendTextTo(JTextArea textarea, String text, boolean keyword){
           int pos = textarea.getCaretPosition();
           Document doc = null;

           if (textarea == lhsArea) {
              doc = lhsArea.getDocument();
              insert(doc,text,pos);
           }
           else if (textarea == rhsArea) {
             doc = rhsArea.getDocument();
             insert(doc,text,pos);
           }

           if(keyword && !methodValues.contains(text))
             textarea.setCaretPosition(textarea.getCaretPosition()-1);
           else {
             textarea.setCaretPosition(textarea.getDocument().getLength());
             try {
               doc.insertString(textarea.getCaretPosition(),"\n",null);
             }
             catch (BadLocationException e) {
//               e.printStackTrace();
             }
           }
      }
//---------------------------------------------------------------------------
      void insert(Document doc, String text, int pos) {

          try {
           if (Misc.member(text,booleanWds)) {
              doc.insertString(pos,"(" + text + " )",null);
           }
           else if (Misc.member(text,precedenceWds)) {
              if ( text.equals(Action.types[Action.MESSAGE]) ) {
                 text += " ";
                 for(int i = 0; i < Performative.ATTRIBUTE_TYPES.length; i++ )
                    text += "(" + Performative.ATTRIBUTE_TYPES[i] + " " +
		             Fact.V_STR + db.GenSym().plainId("var") + ")";
                 doc.insertString(pos,"(" + text + ")",null);
              }
              else if ( text.equals(Action.types[Action.ACHIEVE]) ||
                        text.equals(Action.types[Action.BUY]) ||
                        text.equals(Action.types[Action.SELL]) ) {
                 text += " ";
                 for(int i = 0; i < OntologyDb.GOAL_ATTRIBUTES.length; i++ )
                    text += "(" + OntologyDb.GOAL_ATTRIBUTES[i] + " " +
		             Fact.V_STR + db.GenSym().plainId("var") + ")";
                 doc.insertString(pos,"(" + text + ")",null);
              }
              else if ( text.equals(Action.types[Action.IF]) )
                  doc.insertString(pos,"(" + text + "  \n then\n\n else\n\n)",null);
              else if ( text.equals(Action.types[Action.WHILE]) )
                  doc.insertString(pos,"(" + text + "  \n do\n\n)",null);
              else
                 doc.insertString(pos,"(" + text + " )",null);
           }
           else if (methodValues.contains(text))
              doc.insertString(pos,text,null);
           else {
              doc.insertString(pos,text,null);
           }
          }
          catch (BadLocationException e) {
          }
      }

//---------------------------------------------------------------------------
      void appendTextTo(String text){
         JTextArea textarea = getLastTextAreaWithFocus();
         if (textarea != null) {
             appendTextTo(textarea,text,false);
         }
      }
      public void actionPerformed(ActionEvent e){
         Object source = e.getSource();
         String value = null;
         JTextArea textarea;

         if (source == fsave) {
            writeRulesToFile();
         }
         else if (source == fexit) {
            System.exit(0);
         }
         else if ( source == predicateList || source == predicateBtn ||
	           source == functionList  || source == functionBtn  ||
		   source == actionList    || source == actionBtn ) {
            textarea = getLastTextAreaWithFocus();
            if (textarea != null) {
               if (source == predicateList || source == predicateBtn )
                  value = (String)predicateList.getSelectedItem();
               else if (source == actionList || source == actionBtn )
                  value = (String)actionList.getSelectedItem();
               else if (source == functionList || source == functionBtn )
                  value = (String)functionList.getSelectedItem();
               if ( value != null) {
                 if (Misc.member(value,precedenceWds) && textarea != rhsArea)
                    return;
                 appendTextTo(textarea,value,true);
              }
            }
         }
      }
//---------------------------------------------------------------------------
      public void focusGained(FocusEvent e) {
           JTextArea textarea = (JTextArea) e.getSource();
           setFocus(textarea);
      }
//---------------------------------------------------------------------------
      public void focusLost(FocusEvent e) {
      }
//---------------------------------------------------------------------------
      JPanel getIfThenPanel(){
         JPanel panel = new JPanel(new GridLayout(2,1,5,5));
         panel.setBackground(Color.lightGray);

         lhsArea = new JTextArea(new PlainDocument(),"",6,80);
         lhsSP = new JScrollPane(lhsArea);
         lhsSP.setBorder(makeBorder("Conditions"));
         lhsArea.addFocusListener(this);
         lhsSP.setPreferredSize(new Dimension(500,100));
         panel.add(lhsSP);

         rhsArea = new JTextArea(new PlainDocument(),"",6,80);
         rhsSP = new JScrollPane(rhsArea);
         rhsSP.setBorder(makeBorder("Actions"));
         rhsSP.setPreferredSize(new Dimension(500,100));
         rhsArea.addFocusListener(this);
         panel.add(rhsSP);

         lhsArea.setLineWrap(true);
         lhsArea.setWrapStyleWord(true);
         rhsArea.setLineWrap(true);
         rhsArea.setWrapStyleWord(true);

	 return panel;
      }

//---------------------------------------------------------------------------
      private TitledBorder makeBorder(String title){
          TitledBorder border = (BorderFactory.createTitledBorder(title));
          border.setTitlePosition(TitledBorder.TOP);
	  border.setTitleJustification(TitledBorder.RIGHT);
	  border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
	  border.setTitleColor(Color.black);
          return border;
     }

//---------------------------------------------------------------------------
     void writeRulesToFile(){
         Rule rule;
         String fname;
         File f1 = null;
         String fsep = System.getProperty("file.separator");
         String fdir = SystemProps.getProperty("zeus.dir") + "rete" + fsep + "clp" + fsep;
         String image = SystemProps.getProperty("gir.dir") + "generator" + "kb.gif";

         JFileChooser chooser = new JFileChooser(new File(fdir));
         //chooser.addChoosableFileType("Rule bases (*.kb)", "kb", new ImageIcon(image));
         int val = chooser.showSaveDialog(frame);
         if (val == JFileChooser.APPROVE_OPTION )
          f1 = chooser.getSelectedFile();

         if (f1 == null) {
            JOptionPane.showMessageDialog(null,"File hasn't been specified","Error",JOptionPane.ERROR_MESSAGE);
            return;
         }
         try {

	   PrintWriter out = new PrintWriter(new FileWriter(f1));
           Vector rules = ruleBuffer.getRules();
           for(int i=0;i < rules.size(); i++) {
              rule = (Rule) rules.elementAt(i);
              printRule(rule,out);
           }
           out.flush();
	   out.close();
         }
         catch(IOException e) {
	   e.printStackTrace();
         }
     }
//---------------------------------------------------------------------------
     void printRule(Rule rule, PrintWriter out) {
         out.println("   (" + rule.name );
         out.println();
         out.println(rule.getCondition().trim());
         out.println("=>");
         out.println(rule.getConclusion().trim());
         out.println();
         out.println("   )");
         out.println(); out.println();
     }
//---------------------------------------------------------------------------
      public void valueChanged(ListSelectionEvent e) {
          int row;
          if (e.getSource() == ruleTable.getSelectionModel() ) {
             row = ruleTable.getSelectedRow();
             if (row >= 0  && row < ruleBuffer.getRowCount()) {
              lhsArea.setDocument(ruleBuffer.getRule(row).getLHS());
              rhsArea.setDocument(ruleBuffer.getRule(row).getRHS());
             }
          }
      }
//---------------------------------------------------------------------------
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -