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

📄 simplepredicate.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 com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Core.PmmlPresentable;
import com.prudsys.pdm.Input.MiningVector;

/**
  * It corresponds to the PMML element SimplePredicate. <p>
  *
  * Extends Predicate for evaluating an attribute value
  * in form of a simple boolean expression. <p>
  *
  * From PMML. <p>
  *
  * Superclasses:
  * <ul>
  *   <li> Predicate
  * </ul>
  *
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.Predicate
  */
public class SimplePredicate extends Predicate implements PmmlPresentable {

  // -----------------------------------------------------------------------
  //  Constants of simple predicate operator
  // -----------------------------------------------------------------------
  public static final int EQUAL = 1;
  public static final int NOT_EQUAL = 2;
  public static final int LESS = 3;
  public static final int LESS_OR_EQUAL = 4;
  public static final int GREATER = 5;
  public static final int GREATER_OR_EQUAL = 6;

  // -----------------------------------------------------------------------
  //  Variables declarations
  // -----------------------------------------------------------------------
  /** Attribute to evaluate. */
  protected MiningAttribute attribute;

  /** Name of the evaluation attribute. */
  protected String name;

  /** Value to compare with. */
  protected String value;

  /** Simple predicate operator. */
  protected int op;

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

  /**
   * Simple predicate with given condition.
   *
   * @param attribute attribute to be evaluated
   * @param value attribute's value for evaluation
   * @param op evaluation operator
   */
  public SimplePredicate(MiningAttribute attribute, String value, int op) {

    this.attribute = attribute;
    this.name      = attribute.getName();
    this.value     = value;
    this.op        = op;
  }

  // -----------------------------------------------------------------------
  //  Getter and setter methods
  // -----------------------------------------------------------------------
  /**
   * Returns evaluation attribute.
   *
   * @return evaluation attribute
   */
  public MiningAttribute getAttribute() {
    return attribute;
  }

  /**
   * Sets evaluation attribute.
   *
   * @param attribute new evaluation attribute
   */
  public void setAttribute(MiningAttribute attribute) {
    this.attribute = attribute;
    this.name      = attribute.getName();
  }

  /**
   * Returns evaluation value.
   *
   * @return evaluation value
   */
  public String getValue() {
    return value;
  }

  /**
   * Sets evaluation value.
   *
   * @param value new evaluation value
   */
  public void setValue(String value) {
    this.value = value;
  }

  /**
   * Returns evaluation operator.
   *
   * @return evaluation 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 {

    MiningDataSpecification metaData = miningVector.getMetaData();
    if (metaData == null)
      throw new MiningException("MiningVector has no meta data");
    MiningAttribute ma = metaData.getMiningAttribute(name);
    if (ma == null)
      throw new MiningException("MiningAttribute "+name+" not found in vector");
    double v = miningVector.getValue(ma);
    if ( Category.isMissingValue(v) )
      throw new MiningException("MiningAttribute "+name+" is missing value");

    if (ma instanceof CategoricalAttribute)
    {
      if (op != SimplePredicate.EQUAL && op != SimplePredicate.NOT_EQUAL)
        throw new MiningException("SimplePredicate: categorical attribute - equal operators are supported only");
      Category c = ((CategoricalAttribute)ma).getCategory(v);
      if (c == null)
        throw new MiningException("SimplePredicate: unknown category");
      return c.getValue().toString().equals(value)?(op == EQUAL?true:false):(op == EQUAL?false:true);
    }
    else if (ma instanceof NumericAttribute)
    {
      double d = Double.parseDouble(value);
      switch(op)
      {
        case EQUAL: return d == v;
        case NOT_EQUAL: return d != v;
        case LESS: return v < d;
        case GREATER: return v > d;
        case LESS_OR_EQUAL: return v <= d;
        case GREATER_OR_EQUAL: return v >= d;
        default: throw new MiningException("SimplePredicate: unknown operator");
      }
    }
    else throw new MiningException("SimplePredicate: unknown attribute type");
  }

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

    com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate sp = new com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate();

    sp.setField(name);
    sp.setValue(value);
    switch(op)
    {
      case EQUAL: sp.setOperator("equal"); break;
      case NOT_EQUAL: sp.setOperator("notEqual"); break;
      case LESS: sp.setOperator("lessThan"); break;
      case LESS_OR_EQUAL: sp.setOperator("lessOrEqual"); break;
      case GREATER: sp.setOperator("greaterThan"); break;
      case GREATER_OR_EQUAL: sp.setOperator("greaterOrEqual"); break;
      default: throw new MiningException("unknown simple predicate operator specified");
    };

    return sp;
  }

  /**
   * Read PMML object SimplePredicate.
   *
   * @param pmmlObject Simple predicate to read
   * @exception MiningException can't parse PMML object
   * @see com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate
   */
  public void parsePmmlObject(Object pmmlObject) throws MiningException {

    com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate sp = (com.prudsys.pdm.Adapters.PmmlVersion20.SimplePredicate)pmmlObject;

    // Temporary variables:
    MiningAttribute att;
    String val;
    int o;

    // Get evaluation attribute:
    att = metaData.getMiningAttribute( sp.getField() );
    if (att == null)
      throw new MiningException("mining attribute '"+sp.getField()+"' not found in meta data");

    // Get value:
    val = sp.getValue();
    if (val == null)
      throw new MiningException("Empty evaluation value");

    // Get operator:
    if(sp.getOperator().equals("equal")) o = SimplePredicate.EQUAL;
    else if(sp.getOperator().equals("notEqual")) o = SimplePredicate.NOT_EQUAL;
    else if(sp.getOperator().equals("lessThan")) o = SimplePredicate.LESS;
    else if(sp.getOperator().equals("lessOrEqual")) o = SimplePredicate.LESS_OR_EQUAL;
    else if(sp.getOperator().equals("greaterThan")) o = SimplePredicate.GREATER;
    else if(sp.getOperator().equals("greaterOrEqual")) o = SimplePredicate.GREATER_OR_EQUAL;
    else throw new MiningException("Unknown simple predicate operator");

    // Assign variables:
    attribute = att;
    name      = attribute.getName();
    value     = val;
    op        = o;
  }

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

    StringBuffer sb = new StringBuffer(name);
    switch(op)
    {
      case EQUAL: sb.append(" equals "); break;
      case NOT_EQUAL: sb.append(" not equals "); break;
      case LESS: sb.append(" less "); break;
      case LESS_OR_EQUAL: sb.append(" less or equals "); break;
      case GREATER: sb.append(" greater "); break;
      case GREATER_OR_EQUAL: sb.append(" greater or equals "); break;
      default: sb.append(" unknown operator ");
    }
    sb.append(value);
    return sb.toString();
  }
}

⌨️ 快捷键说明

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