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

📄 compoundpredicate.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * Title: XELOPES Data Mining Library
 * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
 * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
 * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
 * @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
 * @version 1.0
 */

package com.prudsys.pdm.Input.Predicates;

import java.util.*;

import com.borland.xml.toolkit.XmlObject;

import com.prudsys.pdm.Core.*;
import com.prudsys.pdm.Input.*;

/**
  * It corresponds to the PMML element CompoundPredicate. <p>
  *
  * Extends Predicate for combining two or more predicates. <p>
  *
  * From PMML. <p>
  *
  * Superclasses:
  * <ul>
  *   <li> Predicate
  * </ul>
  *
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.Predicate
  */
public class CompoundPredicate extends Predicate implements PmmlPresentable
{
  // -----------------------------------------------------------------------
  //  Constants of operators to merge predicates into compound one
  // -----------------------------------------------------------------------
  /** OR operator. Evaluates to TRUE if all the predicates evaluate to TRUE. */
  public static final int OR = 1;

  /** AND operator. Evaluates to TRUE if one of the predicates evaluates to TRUE. */
  public static final int AND = 2;

  /** XOR operator. Evaluates to TRUE if the predicates evaluate to different values. */
  public static final int XOR = 3;

  /** Operator of surrogated splits. Can be used for cases where a missing value
   * appears in the evaluation of the parent predicate such that an alternative
   * predicate is available. Expression 'surrogate(A,B)' is equivalent to
   * 'if (not unknown(A)) then A, else B'. */
  public static final int SURROGATE = 4;

  // -----------------------------------------------------------------------
  //  Variables declarations
  // -----------------------------------------------------------------------
  /** Predicates array. */
  protected Predicate[] predicates;

  /** Operator of compound predicate. */
  protected int op;

  // -----------------------------------------------------------------------
  //  Constructors
  // -----------------------------------------------------------------------
  /**
   * Empty constructor.
   */
  public CompoundPredicate()
  {
  }

  /**
   * CompoundPredicate from given operator and number of predicates.
   * Predicates must be set using the setPredicate method.
   *
   * @param op operator
   * @param predicatesNumber number of predicates
   */
  public CompoundPredicate(int op, int predicatesNumber) {

    this.predicates = new Predicate[predicatesNumber];
    this.op = op;
  }

  // -----------------------------------------------------------------------
  //  Getter and setter methods
  // -----------------------------------------------------------------------
  /**
   * Returns number of predicates.
   *
   * @return number of predicates
   */
  public int getPredicatesNumber() {

    return ( predicates != null ) ? predicates.length : 0;
  }

  /**
   * Returns a predicate at a given position.
   *
   * @param n position of the predicate
   * @return predicate at given position
   */
  public Predicate getPredicate(int n) {

    return predicates[n];
  }

  /**
   * Sets predicate on a given position.
   *
   * @param predicate predicate to set
   * @param n position of predicate
   */
  public void setPredicate(Predicate predicate, int n) {

    predicates[n] = predicate;
  }

  /**
   * Returns operator.
   *
   * @return operator
   */
  public int getOperator() {

    return op;
  }

  /**
   * Sets operator.
   *
   * @param op new operator
   */
  public void setOperator(int op) {

    this.op = op;
  }

  // -----------------------------------------------------------------------
  //  Evaluate predicate
  // -----------------------------------------------------------------------
  /**
   * Predicate evaluation of given mining vector.
   *
   * @param miningVector vector for evaluation
   * @return predicate evaluation result
   * @exception MiningException can't evaluate mining vector
   */
  public boolean evaluate(MiningVector miningVector) throws MiningException {

    int n = getPredicatesNumber();
    switch(op)
    {
      case OR:
        for(int i=0;i<n;i++)
          if(predicates[i].evaluate(miningVector)) return true;
        return false;
      case AND:
        for(int i=0;i<n;i++)
          if(!predicates[i].evaluate(miningVector)) return false;
        return true;
      case XOR:
        boolean r = false;
        for(int i=0;i<n;i++)
          if(predicates[i].evaluate(miningVector)) r = !r;
        return r;
      case SURROGATE:
        for (int i=0;i<n;i++) {
          try { return predicates[i].evaluate(miningVector); }
          catch (MiningException ex) {};
        };
        throw new MiningException("can't evaluate any predicate (all have missing values!)");
      default:
        throw new MiningException("unknown compound predicate operator");
    }
  }

  // -----------------------------------------------------------------------
  //  Methods of PMML handling
  // -----------------------------------------------------------------------
  /**
   * Create PMML object CompoundPredicate.
   *
   * @return PMML Object CompoundPredicate
   * @exception MiningException can't create PMML object
   * @see com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate
   */
  public Object createPmmlObject() throws MiningException {

    com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate cp = new com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate();

    switch(op)
    {
      case OR: cp.setBooleanOperator("op"); break;
      case AND: cp.setBooleanOperator("and"); break;
      case XOR: cp.setBooleanOperator("xor"); break;
      case SURROGATE: cp.setBooleanOperator("surrogate"); break;
      default: throw new MiningException("unknown compound boolean operator");
    }
    for(int i=0;i<predicates.length;i++)
    {
      Predicate pred = predicates[i];
      com.borland.xml.toolkit.XmlObject pmml = (com.borland.xml.toolkit.XmlObject)pred.createPmmlObject();
      if(pred instanceof ConstantPredicate)
      {
        if(((ConstantPredicate)pred).getConstant())
        {
          if(cp.getTrue()==null) cp.setTrue((com.prudsys.pdm.Adapters.PmmlVersion20.True)pmml);
          else cp.addTrue1((com.prudsys.pdm.Adapters.PmmlVersion20.True)pmml);
        }
        else
        {
          if(cp.getFalse()==null) cp.setFalse((com.prudsys.pdm.Adapters.PmmlVersion20.False)pmml);
          else cp.addFalse1((com.prudsys.pdm.Adapters.PmmlVersion20.False)pmml);
        }
      }
      else if(pred instanceof SimplePredicate)
      {
        if(cp.getSimplePredicate()==null) cp.setSimplePredicate((com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate)pmml);
        else cp.addSimplePredicate1((com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate)pmml);
      }
      else if(pred instanceof SimpleSetPredicate)
      {
        if(cp.getSimpleSetPredicate()==null) cp.setSimpleSetPredicate((com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate)pmml);
        else cp.addSimpleSetPredicate1((com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate)pmml);
      }
      else if(pred instanceof CompoundPredicate)
      {
        if(cp.getCompoundPredicate()==null) cp.setCompoundPredicate((com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate)pmml);
        else cp.addCompoundPredicate1((com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate)pmml);
      }
    }
    return cp;
  }

  /**
   * Read PMML object CompoundPredicate.
   *
   * @param pmmlObject CompoundPredicate to read
   * @exception MiningException always thrown
   * @see com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate
   */
  public void parsePmmlObject(Object pmmlObject) throws MiningException {

    com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate cp = (com.prudsys.pdm.Adapters.PmmlVersion20.CompoundPredicate)pmmlObject;

    // Temporaray variables:
    int o;
    Vector preds = new Vector();

    // Get operator:
    if(cp.getBooleanOperator().equals("or")) o = CompoundPredicate.OR;
    else if(cp.getBooleanOperator().equals("and")) o = CompoundPredicate.AND;
    else if(cp.getBooleanOperator().equals("xor")) o = CompoundPredicate.XOR;
    else if(cp.getBooleanOperator().equals("surrogate")) o = CompoundPredicate.SURROGATE;
    else throw new MiningException("Unknown compound predicate operator");

    // Get predicates:
    List children = cp._getChildren();
    XmlObject xml = null;
    String tag;
    if(children.size()!=0)
    {
      try {
        children = (List)children.get(1);
      }
      catch(ClassCastException ex) {
        throw new MiningException("bad compound predicate: second child is not a list");
      }
      int n = children.size();
      for(int i=0;i<n;i++)
      {
        xml = (XmlObject)children.get(i);
        tag = xml.get_TagName();
        Predicate pred;
        if(tag.equals("True") || tag.equals("False")) pred = new ConstantPredicate();
        else if(tag.equals("SimplePredicate")) pred        = new SimplePredicate();
        else if(tag.equals("SimpleSetPredicate")) pred     = new SimpleSetPredicate();
        else if(tag.equals("CompoundPredicate")) pred      = new CompoundPredicate();
    	else throw new MiningException("unexpected tag <" + tag + "> in CompoundPredicate");
        pred.setMetaData(metaData);
        pred.parsePmmlObject(xml);

        preds.addElement(pred);
      }
    }
    else
      throw new MiningException("Compound predicate must contain at least one child");
    int npred = preds.size();

    // Assign variables:
    op = o;
    predicates = new Predicate[npred];
    for(int i=0;i<npred;i++)
      setPredicate((Predicate)preds.get(i),i);
  }

  /**
   * Create string representation of compound predicate.
   *
   * @return PMML representation of predicate
   */
  public String toString() {

    StringBuffer sb = new StringBuffer();
    String ops;
    switch(op)
    {
      case OR: ops = " or "; break;
      case AND: ops = " and "; break;
      case XOR: ops = " xor "; break;
      case SURROGATE: ops = " surrogate "; break;
      default: ops = " unknown ";
    }
    for(int i=0;i<predicates.length;i++)
    {
      sb.append("( ");
      sb.append(predicates[i].toString());
      sb.append(" )");
      if(i != (predicates.length-1)) sb.append(ops);
    }
    return sb.toString();
  }


}

⌨️ 快捷键说明

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