package ca.pfv.spmf.algorithms.sequentialpatterns.clospan_AGP.items;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * This class represents a sequence from a projected database (as based in PrefixSpan).
 * Since it is a projected sequence, it makes reference to the original sequence.
 * 
 * This class is inspired in SPMF PrefixSpan implementation.
 *
 * Copyright Antonio Gomariz Peñalver 2013
 *
 * This file is part of the SPMF DATA MINING SOFTWARE
 * (http://www.philippe-fournier-viger.com/spmf).
 *
 * SPMF 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 3 of the License, or (at your option) any later
 * version.
 *
 * SPMF 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
 * SPMF. If not, see .
 *
 * @author agomariz
 */
public class PseudoSequence {

    /**
     * List with the corresponding timeshift that each projection in the sequence has
     */
    private List timeShift = new ArrayList();
    /**
     * Sequence where the different projections of the pseudosequence refer
     */
    private Sequence sequence;
    /**
     * List with the first itemset where each projection starts
     */
    private List firstItemset = new ArrayList();
    /**
     * List with the first item where each projection starts
     */
    private List firstItem = new ArrayList();

    /**
     * It make a pseudosequence from a previous pseudosequence. Therefore, we
     * are making a projection in a previous projection and we have to take into
     * account the previous index of that projection to establish the ours.
     * @param timeShift The time shift of the new pseudosequence
     * @param pseudosequence The pseudosequence where we make another projection
     * @param itemsetIndex The itemset index where our new projection is starting
     * @param itemIndex The item index where our new projection is starting
     * @param firstItemset An index that indicates in which projection of the 
     * previous pseudoseuqence we are making the new pseudosequence
     */
    public PseudoSequence(long timeShift, PseudoSequence pseudosequence, int itemsetIndex, int itemIndex, int firstItemset) {
        //We point to the same original sequence
        this.sequence = pseudosequence.sequence;
        /*We define the new time shift, that is the timeshift given plus the
         * value that the given pseudosequence has. Then we add it to the list 
         * in its corresponding position
         */
        long newTimeShift = timeShift+pseudosequence.timeShift.get(firstItemset);
        this.timeShift.add(newTimeShift);
        /*
         * We establish the first itemset of the projection in absolute terms.
         * We have to add the given itemset index to the first itemset index
         * kept in the given pseudosequence
         */
        this.firstItemset.add(itemsetIndex + pseudosequence.firstItemset.get(firstItemset));
        //If our itemset index is the same that the given pseudosequence had
        if (this.firstItemset.get(0) == pseudosequence.firstItemset.get(firstItemset)) {
            //We compute our first item index from the previous one
            this.firstItem.add(itemIndex + pseudosequence.firstItem.get(firstItemset));
        } else {
            //Otherwise, our first item index is just the given parameter
            this.firstItem.add(itemIndex);
        }
    }

    /**
     * PseudoSequence made from a starndard sequence. Its purpose is make possible
     * the later projections
     * @param timeShift The original timeshift (Normally, it will be 0)
     * @param sequence The original standard sequence
     * @param itemsetIndex The itemset index where the projection it starts
     * @param itemIndex The item index where the projection it starts
     */
    public PseudoSequence(long timeShift, Sequence sequence, int itemsetIndex, int itemIndex) {
        this.timeShift.add(timeShift);
        this.sequence = sequence;
        this.firstItemset.add(itemsetIndex);
        this.firstItem.add(itemIndex);
    }

    /**
     * Method to make a projection in a pseudosequence already projected. 
     * In this way, for a same pseudosequence, we can have several 
     * projections. For example, let us think in a sequence A = (a) (a b) (a b c).
     * If we project by item a, we obtain three different projections:
     * 1)   (a b) (a b c)
     * 2)   (*b) (a b c)
     * 3)   (*b c)
     * That we do is to put all the three projections in the same pseudoseuqence,
     * and we refers each projection with an index. We call to every projection, 
     * projection point.
     * @param firstItemset The projection point of the given pseudosequence 
     * where we are making our new projection
     * @param timeShift The given timeshift for our projection
     * @param pseudosequence The given pseudosequence where we are making our 
     * projection
     * @param itemsetIndex  The itemset index where our projection it starts
     * @param itemIndex The item index where our projection it starts
     */
    public void addProjectionPoint(int firstItemset, long timeShift, PseudoSequence pseudosequence,int itemsetIndex, int itemIndex) {
        /*
         * We update the timeshift from the given pseudosequence time shift and, 
         * them we keep it
         */
        long newTimeShift = timeShift+pseudosequence.timeShift.get(firstItemset);
        this.timeShift.add(newTimeShift);
        /*
         * We establish the starting itemset index (for our projection), from the
         * previous starting itemset index
         */
        this.firstItemset.add(itemsetIndex + pseudosequence.firstItemset.get(firstItemset));
        //If our itemset index is the same index in the previous pseudosequence
        if (this.firstItemset.get(this.firstItemset.size()-1) == pseudosequence.firstItemset.get(firstItemset)) {
            //We compose the item index from the previous one
            this.firstItem.add(itemIndex + pseudosequence.firstItem.get(firstItemset));
        } else {
            //Otherwise we simply use the given item index
            this.firstItem.add(itemIndex);
        }
    }

    /**
     * It returns the first itemset of the projection, in absolute terms
     * @param index the ith projection in which we want to know the first itemset
     * @return the index
     */
    public int getFirstItemset(int index){
        return this.firstItemset.get(index);
    }

   /**
     * It returns the size for the current pseudosequence
     * @param i the ith projection in which we want to know the size
     * @return the size (int)
     */
    public int size(int i) {
        int size = sequence.size() - firstItemset.get(i);
        return size;
    }
    
    public int length(int firstItemset){
        int itemsToNotBeTakenIntoAccount=0;
        /* 
         * We count all the elements that appear before the first element from 
         * which we start the projected sequence
         */
        for(int i=0;i=beginning) {
            //we return it, substracting the beinning of the itemset
            return i-beginning;
        }
        //Otherwise we return -1
        return -1;
    }

    /**
     * It gets the timeShift for a specified projection of the pseudosequence
     * @param firstItemset the projection in which we are interested
     * @return the time shift.
     */
    public long getTimeShift(int firstItemset) {
        return timeShift.get(firstItemset);
    }
}

 

Ads help maintain this website.