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

📄 simplesetpredicate.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.prudsys.pdm.Core.*;
import com.prudsys.pdm.Input.*;

/**
  * It corresponds to the PMML element SimpleSetPredicate. <p>
  *
  * Extends Predicate for checking whether an attribute value belongs
  * to a specified set. <p>
  *
  * From PMML. <p>
  *
  * Superclasses:
  * <ul>
  *   <li> Predicate
  * </ul>
  *
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate
  * @see com.prudsys.pdm.Adapters.PmmlVersion20.Predicate
  */
public class SimpleSetPredicate extends Predicate implements PmmlPresentable
{
  // -----------------------------------------------------------------------
  //  Constants of simple set predicate operator
  // -----------------------------------------------------------------------
  /** Attribute belongs to the specified set. */
  public static final int IS_IN = 1;

  /** Attribute doesn't belong to the specified set. */
  public static final int IS_NOT_IN = 2;

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

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

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

  /** Set of values. */
  protected Vector values;

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

  /**
   * SimpleSetPredicate with condition elements.
   * Set to be specified via setValue methods.
   *
   * @param attribute attribute for evaluation
   * @param op operator for evaluation
   * @param valuesNumber cardinality of comparison set
   */
  public SimpleSetPredicate(MiningAttribute attribute, int op, int valuesNumber) {

    this.attribute = attribute;
    this.name = attribute.getName();
    this.op = op;
    values = new Vector(valuesNumber);
    values.setSize(valuesNumber);
  }

  // -----------------------------------------------------------------------
  //  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 operator.
   *
   * @return operator
   */
  public int getOperator() {

    return op;
  }

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

    this.op = op;
  }

  /**
   * Returns number of values.
   *
   * @return number of values
   */
  public int getValuesNumber() {

    return values.size();
  }

  /**
   * Sets value at specified position.
   *
   * @param value new string value
   * @param n position
   */
  public void setValue(String value, int n) {

    values.set(n,value);
  }

  /**
   * Sets value at specified position.
   *
   * @param value new double value
   * @param n position
   */
  public void setValue(double value, int n) {

    values.set(n,new Double(value));
  }

  /**
   * Returns string value at specified position.
   *
   * @param n position
   * @return string value
   */
  public String getStringValue(int n) {

    return (String)values.get(n);
  }

  /**
   * Returns double value at specified position.
   *
   * @param n position
   * @return double value
   */
  public double getDoubleValue(int n) {

    return ((Double)values.get(n)).doubleValue();
  }

  // -----------------------------------------------------------------------
  //  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 not 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)
    {
      Category cat = ((CategoricalAttribute)ma).getCategory(v);
      if (cat == null)
        throw new MiningException("SimpleSetPredicate: unknown category");

      int i,n;
      n = values.size();
      for(i=0;i<n;i++)
      {
        String value = (String)values.get(i);
        if(value.equals( cat.getValue().toString()) ) break;
      }
      return (i==n)?(op == IS_IN?true:false):(op == IS_IN?false:true);
    }
    else if(ma instanceof NumericAttribute)
    {
      int i,n;
      n = values.size();
      for(i=0;i<n;i++)
      {
        double value = ((Double)values.get(i)).doubleValue();
        if(value == v) break;
      }
      return (i==n)?(op == IS_IN?true:false):(op == IS_IN?false:true);
    }
    else throw new MiningException("SimpleSetPredicate: unknown MiningVector's attribute type");
  }

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

    com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate ssp = new com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate();

    ssp.setField(name);
    switch(op)
    {
      case IS_IN: ssp.setBooleanOperator("isIn"); break;
      case IS_NOT_IN: ssp.setBooleanOperator("isNotIn"); break;
      default: throw new MiningException("unknown simple set predicate operator specified");
    };

    String svalues = "";
    int vn = getValuesNumber();
    for (int i = 0; i < vn; i++) {
      Object val = values.elementAt(i);
      if (val instanceof String)
        svalues = svalues + val;
      else
        svalues = svalues + val.toString();
      if (i < vn-1)
        svalues = svalues + " ";
    };
    ssp.setArrayText(svalues);

    return ssp;
  }

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

    com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate ssp = (com.prudsys.pdm.Adapters.PmmlVersion20.SimpleSetPredicate)pmmlObject;

    // Temporary variables:
    MiningAttribute att;
    int o;

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

    // Get operator:
    if(ssp.getBooleanOperator().equals("isIn")) o = SimpleSetPredicate.IS_IN;
    else if(ssp.getBooleanOperator().equals("isNotIn")) o = SimpleSetPredicate.IS_NOT_IN;
    else throw new MiningException("Unknown simple set predicate operator");

    // Get values:
    String svalues = ssp.getArrayText();
    StringTokenizer st = new StringTokenizer(svalues," ");
    Vector tokens = new Vector();
    while(st.hasMoreTokens())
      tokens.add(st.nextToken());
    int nval = tokens.size();

    // Assign variables:
    attribute = att;
    name      = attribute.getName();
    op        = o;
    values    = new Vector(nval);
    if (attribute instanceof CategoricalAttribute)
    {
      for(int i=0;i<nval;i++) setValue((String)tokens.get(i),i);
    }
    else if(attribute instanceof NumericAttribute)
    {
      for(int i=0;i<nval;i++) setValue(Double.parseDouble((String)tokens.get(i)),i);
    };
  }

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

    StringBuffer sb = new StringBuffer(name);
    if(op == IS_IN) sb.append(" is in ");
    else if(op == IS_NOT_IN) sb.append(" is not in ");
    sb.append("{ ");
    int n = values.size();
    for(int i=0;i<n;i++)
    {
      sb.append(values.get(i).toString());
      if(i!=(n-1)) sb.append(", ");
    }
    sb.append(" }");
    return sb.toString();
  }
}

⌨️ 快捷键说明

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