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

📄 xmlbasicserialization.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      return coll;
   }
   
   /**
    * adds the given Map to a DOM structure. 
    * 
    * @param parent the parent of this object, e.g. the class this object is a
    * member of
    * @param o the Object to describe in XML
    * @param name the name of the object
    * @return the node that was created
    * @throws Exception if the DOM creation fails
    * @see java.util.Map
    */
   public Element writeMap(Element parent, Object o, String name) 
      throws Exception {

      Map            map;
      Object         key;
      Element        node;
      Element        child;
      Iterator       iter;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), name);
      
      m_CurrentNode = parent;
      
      map  = (Map) o;
      iter = map.keySet().iterator();
      node = addElement(parent, name, o.getClass().getName(), false);

      while (iter.hasNext()) {
         key   = iter.next();
         child = addElement(
                     node, VAL_MAPPING, Object.class.getName(), false);
         invokeWriteToXML(child, key,          VAL_KEY);
         invokeWriteToXML(child, map.get(key), VAL_VALUE);
      }
      
      return node;
   }

   /**
    * builds the Map from the given DOM node. 
    * 
    * @param parent the parent object to get the properties for
    * @param node the associated XML node
    * @return the instance created from the XML description
    * @throws Exception if instantiation fails 
    * @see java.util.Map
    */
   public Object readMap(Element node) throws Exception {
      Map                  map;
      Object               key;
      Object               value;
      Vector               children;
      Vector               cchildren;
      Element              child;
      Element              cchild;
      int                  i;
      int                  n;
      String               name;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), node.getAttribute(ATT_NAME));
      
      m_CurrentNode = node;
      
      map      = (Map) Class.forName(
                     node.getAttribute(ATT_CLASS)).newInstance();
      children = XMLDocument.getChildTags(node); 

      for (i = 0; i < children.size(); i++) {
         child     = (Element) children.get(i);
         cchildren = XMLDocument.getChildTags(child);
         key       = null;
         value     = null;
         
         for (n = 0; n < cchildren.size(); n++) {
            cchild = (Element) cchildren.get(n);
            name   = cchild.getAttribute(ATT_NAME);
            if (name.equals(VAL_KEY))
               key = invokeReadFromXML(cchild);
            else if (name.equals(VAL_VALUE))
               value = invokeReadFromXML(cchild);
            else
               System.out.println("WARNING: '" 
                     + name + "' is not a recognized name for maps!");
         }
         
         map.put(key, value);
      }
      
      return map;
   }

   /**
    * adds the given Matrix to a DOM structure. 
    * 
    * @param parent the parent of this object, e.g. the class this object is a
    * member of
    * @param o the Object to describe in XML
    * @param name the name of the object
    * @return the node that was created
    * @throws Exception if the DOM creation fails
    * @see weka.core.matrix.Matrix
    */
   public Element writeMatrix(Element parent, Object o, String name) 
      throws Exception {

      weka.core.matrix.Matrix    matrix;
      Element                    node;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), name);
      
      m_CurrentNode = parent;
      
      matrix = (weka.core.matrix.Matrix) o;
      node   = addElement(parent, name, o.getClass().getName(), false);

      invokeWriteToXML(node, matrix.getArray(), VAL_CELLS);
      
      return node;
   }

   /**
    * builds the Matrix from the given DOM node. 
    * 
    * @param parent the parent object to get the properties for
    * @param node the associated XML node
    * @return the instance created from the XML description
    * @throws Exception if instantiation fails 
    * @see weka.core.matrix.Matrix
    */
   public Object readMatrix(Element node) throws Exception {
      weka.core.matrix.Matrix    matrix;
      Vector                     children;
      Element                    child;
      int                        i;
      String                     name;
      Object                     o;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), node.getAttribute(ATT_NAME));

      m_CurrentNode = node;
      
      matrix   = null;
      children = XMLDocument.getChildTags(node); 
      for (i = 0; i < children.size(); i++) {
         child = (Element) children.get(i);
         name  = child.getAttribute(ATT_NAME);

         if (name.equals(VAL_CELLS)) {
            o = invokeReadFromXML(child);
            matrix = new weka.core.matrix.Matrix(
                        (double[][]) o);
         }
      }
      
      return matrix;
   }
   
   /**
    * adds the given Matrix (old) to a DOM structure. 
    * 
    * @param parent the parent of this object, e.g. the class this object is a
    * member of
    * @param o the Object to describe in XML
    * @param name the name of the object
    * @return the node that was created
    * @throws Exception if the DOM creation fails
    * @see weka.core.Matrix
    */
   public Element writeMatrixOld(Element parent, Object o, String name) 
      throws Exception {

      weka.core.Matrix  matrix;
      Element           node;
      double[][]        array;
      int               i;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), name);
      
      m_CurrentNode = parent;
      
      matrix = (weka.core.Matrix) o;
      node   = addElement(parent, name, o.getClass().getName(), false);

      array = new double[matrix.numRows()][];
      for (i = 0; i < array.length; i++)
         array[i] = matrix.getRow(i);
      invokeWriteToXML(node, array, VAL_CELLS);
      
      return node;
   }

   /**
    * builds the Matrix (old) from the given DOM node. 
    * 
    * @param parent the parent object to get the properties for
    * @param node the associated XML node
    * @return the instance created from the XML description
    * @throws Exception if instantiation fails 
    * @see weka.core.Matrix
    */
   public Object readMatrixOld(Element node) throws Exception {
      weka.core.Matrix           matrix;
      weka.core.matrix.Matrix    matrixNew;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), node.getAttribute(ATT_NAME));

      m_CurrentNode = node;
      
      matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
      matrix    = new weka.core.Matrix(matrixNew.getArrayCopy());
      
      return matrix;
   }
   
   /**
    * adds the given CostMatrix (old) to a DOM structure. 
    * 
    * @param parent the parent of this object, e.g. the class this object is a
    * member of
    * @param o the Object to describe in XML
    * @param name the name of the object
    * @return the node that was created
    * @throws Exception if the DOM creation fails
    * @see weka.classifiers.CostMatrix
    */
   public Element writeCostMatrixOld(Element parent, Object o, String name) 
      throws Exception {

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), name);
      
      m_CurrentNode = parent;
      
      return writeMatrixOld(parent, o, name);
   }

   /**
    * builds the Matrix (old) from the given DOM node. 
    * 
    * @param parent the parent object to get the properties for
    * @param node the associated XML node
    * @return the instance created from the XML description
    * @throws Exception if instantiation fails 
    * @see weka.classifiers.CostMatrix
    */
   public Object readCostMatrixOld(Element node) throws Exception {
      weka.classifiers.CostMatrix   matrix;
      weka.core.matrix.Matrix       matrixNew;
      StringWriter                  writer;

      // for debugging only
      if (DEBUG)
         trace(new Throwable(), node.getAttribute(ATT_NAME));

      m_CurrentNode = node;
      
      matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
      writer    = new StringWriter();
      matrixNew.write(writer);
      matrix    = new weka.classifiers.CostMatrix(new StringReader(writer.toString()));
      
      return matrix;
   }
}

⌨️ 快捷键说明

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