org.apache.mahout.cf.taste.impl.model
Class GenericDataModel

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.model.GenericDataModel
All Implemented Interfaces:
java.io.Serializable, Refreshable, DataModel

public final class GenericDataModel
extends java.lang.Object
implements DataModel, java.io.Serializable

A simple DataModel which uses a given List of users as its data source. This implementation is mostly useful for small experiments and is not recommended for contexts where performance is important.

See Also:
Serialized Form

Constructor Summary
GenericDataModel(DataModel dataModel)
           Creates a new containing an immutable copy of the data from another given DataModel.
GenericDataModel(FastByIDMap<PreferenceArray> userData)
           Creates a new from the given users (and their preferences).
 
Method Summary
 LongPrimitiveArrayIterator getItemIDs()
           
 FastIDSet getItemIDsFromUser(long userID)
           
 int getNumItems()
           
 int getNumUsers()
           
 int getNumUsersWithPreferenceFor(long... itemIDs)
           
 PreferenceArray getPreferencesForItem(long itemID)
           
 PreferenceArray getPreferencesFromUser(long userID)
           
 java.lang.Float getPreferenceValue(long userID, long itemID)
          Retrieves the preference value for a single user and item.
 FastByIDMap<PreferenceArray> getRawUserData()
          This is used mostly internally to the framework, and shouldn't be relied upon otherwise.
 LongPrimitiveArrayIterator getUserIDs()
           
 boolean hasPreferenceValues()
           
 void refresh(java.util.Collection<Refreshable> alreadyRefreshed)
           Triggers "refresh" -- whatever that means -- of the implementation.
 void removePreference(long userID, long itemID)
           Removes a particular preference for a user.
 void setPreference(long userID, long itemID, float value)
           Sets a particular preference (item plus rating) for a user.
static FastByIDMap<PreferenceArray> toDataMap(FastByIDMap<java.util.Collection<Preference>> data, boolean byUser)
          Swaps, in-place, Lists for arrays in Map values .
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GenericDataModel

public GenericDataModel(FastByIDMap<PreferenceArray> userData)

Creates a new from the given users (and their preferences). This DataModel retains all this information in memory and is effectively immutable.

Parameters:
userData - users to include in this (see also toDataMap(FastByIDMap, boolean))

GenericDataModel

public GenericDataModel(DataModel dataModel)
                 throws TasteException

Creates a new containing an immutable copy of the data from another given DataModel.

Parameters:
dataModel - DataModel to copy
Throws:
TasteException - if an error occurs while retrieving the other DataModel's users
Method Detail

toDataMap

public static FastByIDMap<PreferenceArray> toDataMap(FastByIDMap<java.util.Collection<Preference>> data,
                                                     boolean byUser)
Swaps, in-place, Lists for arrays in Map values .

Returns:
input value

getRawUserData

public FastByIDMap<PreferenceArray> getRawUserData()
This is used mostly internally to the framework, and shouldn't be relied upon otherwise.


getUserIDs

public LongPrimitiveArrayIterator getUserIDs()
Specified by:
getUserIDs in interface DataModel
Returns:
all user IDs in the model, in order

getPreferencesFromUser

public PreferenceArray getPreferencesFromUser(long userID)
                                       throws NoSuchUserException
Specified by:
getPreferencesFromUser in interface DataModel
Parameters:
userID - ID of user to get prefs for
Returns:
user's preferences, ordered by item ID
Throws:
NoSuchUserException - if there is no such user

getItemIDsFromUser

public FastIDSet getItemIDsFromUser(long userID)
                             throws TasteException
Specified by:
getItemIDsFromUser in interface DataModel
Parameters:
userID - ID of user to get prefs for
Returns:
IDs of items user expresses a preference for
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getItemIDs

public LongPrimitiveArrayIterator getItemIDs()
Specified by:
getItemIDs in interface DataModel
Returns:
a List of all item IDs in the model, in order

getPreferencesForItem

public PreferenceArray getPreferencesForItem(long itemID)
                                      throws NoSuchItemException
Specified by:
getPreferencesForItem in interface DataModel
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by user ID, as an array
Throws:
NoSuchItemException - if the item does not exist

getPreferenceValue

public java.lang.Float getPreferenceValue(long userID,
                                          long itemID)
                                   throws TasteException
Description copied from interface: DataModel
Retrieves the preference value for a single user and item.

Specified by:
getPreferenceValue in interface DataModel
Parameters:
userID - user ID to get pref value from
itemID - item ID to get pref value for
Returns:
preference value from the given user for the given item or null if none exists
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getNumItems

public int getNumItems()
Specified by:
getNumItems in interface DataModel
Returns:
total number of items known to the model. This is generally the union of all items preferred by at least one user but could include more.

getNumUsers

public int getNumUsers()
Specified by:
getNumUsers in interface DataModel
Returns:
total number of users known to the model.

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long... itemIDs)
Specified by:
getNumUsersWithPreferenceFor in interface DataModel
Parameters:
itemIDs - item IDs to check for
Returns:
the number of users who have expressed a preference for all of the items

removePreference

public void removePreference(long userID,
                             long itemID)
Description copied from interface: DataModel

Removes a particular preference for a user.

Specified by:
removePreference in interface DataModel
Parameters:
userID - user from which to remove preference
itemID - item to remove preference for

setPreference

public void setPreference(long userID,
                          long itemID,
                          float value)
Description copied from interface: DataModel

Sets a particular preference (item plus rating) for a user.

Specified by:
setPreference in interface DataModel
Parameters:
userID - user to set preference for
itemID - item to set preference for
value - preference value

refresh

public void refresh(java.util.Collection<Refreshable> alreadyRefreshed)
Description copied from interface: Refreshable

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.

Specified by:
refresh in interface Refreshable
Parameters:
alreadyRefreshed - s that are known to have already been refreshed as a result of an initial call to a method on some object. This ensure that objects in a refresh dependency graph aren't refreshed twice needlessly.

hasPreferenceValues

public boolean hasPreferenceValues()
Specified by:
hasPreferenceValues in interface DataModel

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.