public void testStrategy()
        {
            FastIDSet itemIDsFromUser123 = new FastIDSet();
            itemIDsFromUser123.Add(1L);

            FastIDSet itemIDsFromUser456 = new FastIDSet();
            itemIDsFromUser456.Add(1L);
            itemIDsFromUser456.Add(2L);

            List<IPreference> prefs = new List<IPreference>();
            prefs.Add(new GenericPreference(123L, 1L, 1.0f));
            prefs.Add(new GenericPreference(456L, 1L, 1.0f));
            IPreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs);

            var dataModelMock = new DynamicMock(typeof(IDataModel));
            dataModelMock.ExpectAndReturn("GetPreferencesForItem", preferencesForItem1,  (1L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser123, (123L));
            dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser456, (456L));

            IPreferenceArray prefArrayOfUser123 =
            new GenericUserPreferenceArray( new List<IPreference>() {new GenericPreference(123L, 1L, 1.0f)} );

            ICandidateItemsStrategy strategy = new PreferredItemsNeighborhoodCandidateItemsStrategy();

            //EasyMock.replay(dataModel);

            FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance);
            Assert.AreEqual(1, candidateItems.Count());
            Assert.True(candidateItems.Contains(2L));

            dataModelMock.Verify(); //  EasyMock.verify(dataModel);
        }
Exemplo n.º 2
0
        private static IPreferenceArray cloneAndMergeInto(IPreferenceArray delegatePrefs,
                                                          long itemID,
                                                          long newUserID,
                                                          float value)
        {
            int length    = delegatePrefs == null ? 0 : delegatePrefs.Length();
            int newLength = length + 1;
            IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(newLength);

            // Set item ID once
            newPreferenceArray.SetItemID(0, itemID);

            int positionToInsert = 0;

            while (positionToInsert < length && newUserID > delegatePrefs.GetUserID(positionToInsert))
            {
                positionToInsert++;
            }

            for (int i = 0; i < positionToInsert; i++)
            {
                newPreferenceArray.SetUserID(i, delegatePrefs.GetUserID(i));
                newPreferenceArray.SetValue(i, delegatePrefs.GetValue(i));
            }
            newPreferenceArray.SetUserID(positionToInsert, newUserID);
            newPreferenceArray.SetValue(positionToInsert, value);
            for (int i = positionToInsert + 1; i < newLength; i++)
            {
                newPreferenceArray.SetUserID(i, delegatePrefs.GetUserID(i - 1));
                newPreferenceArray.SetValue(i, delegatePrefs.GetValue(i - 1));
            }

            return(newPreferenceArray);
        }
 public void testHasPref()
 {
     IPreferenceArray prefs = new GenericItemPreferenceArray(3);
     prefs.Set(0, new GenericPreference(1L, 3L, 5.0f));
     Assert.True(prefs.HasPrefWithItemID(3L));
     Assert.True(prefs.HasPrefWithUserID(1L));
     Assert.False(prefs.HasPrefWithItemID(2L));
     Assert.False(prefs.HasPrefWithUserID(2L));
 }
        public override IPreferenceArray GetPreferencesForItem(long itemID)
        {
            if (tempPrefs.Count == 0)
            {
                return(getDelegate().GetPreferencesForItem(itemID));
            }

            IPreferenceArray delegatePrefs = null;

            try {
                delegatePrefs = getDelegate().GetPreferencesForItem(itemID);
            } catch (NoSuchItemException nsie) {
                // OK. Probably an item that only the anonymous user has
                //if (log.isDebugEnabled()) {
                //  log.debug("Item {} unknown", itemID);
                //}
            }

            List <IPreference> anonymousPreferences = new List <IPreference>();

            foreach (var prefsMap in tempPrefs)
            {
                IPreferenceArray singleUserTempPrefs = prefsMap.Value;
                for (int i = 0; i < singleUserTempPrefs.Length(); i++)
                {
                    if (singleUserTempPrefs.GetItemID(i) == itemID)
                    {
                        anonymousPreferences.Add(singleUserTempPrefs.Get(i));
                    }
                }
            }

            int delegateLength       = delegatePrefs == null ? 0 : delegatePrefs.Length();
            int anonymousPrefsLength = anonymousPreferences.Count;
            int prefsCounter         = 0;

            // Merge the delegate and anonymous preferences into a single array
            IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength);

            for (int i = 0; i < delegateLength; i++)
            {
                newPreferenceArray.Set(prefsCounter++, delegatePrefs.Get(i));
            }

            foreach (IPreference anonymousPreference in anonymousPreferences)
            {
                newPreferenceArray.Set(prefsCounter++, anonymousPreference);
            }

            if (newPreferenceArray.Length() == 0)
            {
                // No, didn't find it among the anonymous user prefs
                throw new NoSuchItemException(itemID);
            }

            return(newPreferenceArray);
        }
Exemplo n.º 5
0
        public override bool Equals(Object other)
        {
            if (!(other is GenericItemPreferenceArray))
            {
                return(false);
            }
            GenericItemPreferenceArray otherArray = (GenericItemPreferenceArray)other;

            return(id == otherArray.id && Enumerable.SequenceEqual(ids, otherArray.ids) && Enumerable.SequenceEqual(values, otherArray.values));
        }
        public void testHasPref()
        {
            IPreferenceArray prefs = new GenericItemPreferenceArray(3);

            prefs.Set(0, new GenericPreference(1L, 3L, 5.0f));
            Assert.True(prefs.HasPrefWithItemID(3L));
            Assert.True(prefs.HasPrefWithUserID(1L));
            Assert.False(prefs.HasPrefWithItemID(2L));
            Assert.False(prefs.HasPrefWithUserID(2L));
        }
        public void testUserID()
        {
            IPreferenceArray prefs = new GenericItemPreferenceArray(3);

            Assert.AreEqual(3, prefs.Length());
            prefs.SetItemID(0, 1L);
            Assert.AreEqual(1L, prefs.GetItemID(0));
            Assert.AreEqual(1L, prefs.GetItemID(1));
            Assert.AreEqual(1L, prefs.GetItemID(2));
        }
 public void testSetValue()
 {
     IPreferenceArray prefs = new GenericItemPreferenceArray(3);
     Assert.AreEqual(3, prefs.Length());
     prefs.SetValue(0, 1.0f);
     prefs.SetValue(1, 2.0f);
     prefs.SetValue(2, 3.0f);
     Assert.AreEqual(1.0f, prefs.GetValue(0), EPSILON);
     Assert.AreEqual(2.0f, prefs.GetValue(1), EPSILON);
     Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON);
 }
 public void testItemID()
 {
     IPreferenceArray prefs = new GenericItemPreferenceArray(3);
     Assert.AreEqual(3, prefs.Length());
     prefs.SetUserID(0, 1L);
     prefs.SetUserID(1, 2L);
     prefs.SetUserID(2, 3L);
     Assert.AreEqual(1L, prefs.GetUserID(0));
     Assert.AreEqual(2L, prefs.GetUserID(1));
     Assert.AreEqual(3L, prefs.GetUserID(2));
 }
 public void testClone()
 {
     GenericItemPreferenceArray prefs = new GenericItemPreferenceArray(3);
     prefs.Set(0, new GenericPreference(3L, 1L, 5.0f));
     prefs.Set(1, new GenericPreference(1L, 1L, 4.0f));
     prefs.Set(2, new GenericPreference(2L, 1L, 3.0f));
     prefs = (GenericItemPreferenceArray)prefs.Clone();
     Assert.AreEqual(3L, prefs.GetUserID(0));
     Assert.AreEqual(1L, prefs.GetItemID(1));
     Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON);
 }
        public void testSort()
        {
            IPreferenceArray prefs = new GenericItemPreferenceArray(3);

            prefs.Set(0, new GenericPreference(3L, 1L, 5.0f));
            prefs.Set(1, new GenericPreference(1L, 1L, 5.0f));
            prefs.Set(2, new GenericPreference(2L, 1L, 5.0f));
            prefs.SortByUser();
            Assert.AreEqual(1L, prefs.GetUserID(0));
            Assert.AreEqual(2L, prefs.GetUserID(1));
            Assert.AreEqual(3L, prefs.GetUserID(2));
        }
        public void testSetValue()
        {
            IPreferenceArray prefs = new GenericItemPreferenceArray(3);

            Assert.AreEqual(3, prefs.Length());
            prefs.SetValue(0, 1.0f);
            prefs.SetValue(1, 2.0f);
            prefs.SetValue(2, 3.0f);
            Assert.AreEqual(1.0f, prefs.GetValue(0), EPSILON);
            Assert.AreEqual(2.0f, prefs.GetValue(1), EPSILON);
            Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON);
        }
        public void testClone()
        {
            GenericItemPreferenceArray prefs = new GenericItemPreferenceArray(3);

            prefs.Set(0, new GenericPreference(3L, 1L, 5.0f));
            prefs.Set(1, new GenericPreference(1L, 1L, 4.0f));
            prefs.Set(2, new GenericPreference(2L, 1L, 3.0f));
            prefs = (GenericItemPreferenceArray)prefs.Clone();
            Assert.AreEqual(3L, prefs.GetUserID(0));
            Assert.AreEqual(1L, prefs.GetItemID(1));
            Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON);
        }
 public void testSort()
 {
     IPreferenceArray prefs = new GenericItemPreferenceArray(3);
     prefs.Set(0, new GenericPreference(3L, 1L, 5.0f));
     prefs.Set(1, new GenericPreference(1L, 1L, 5.0f));
     prefs.Set(2, new GenericPreference(2L, 1L, 5.0f));
     prefs.SortByUser();
     Assert.AreEqual(1L, prefs.GetUserID(0));
     Assert.AreEqual(2L, prefs.GetUserID(1));
     Assert.AreEqual(3L, prefs.GetUserID(2));
 }
  public override IPreferenceArray GetPreferencesForItem(long itemID) {
    if (tempPrefs.Count==0) {
      return getDelegate().GetPreferencesForItem(itemID);
    }

    IPreferenceArray delegatePrefs = null;

    try {
      delegatePrefs = getDelegate().GetPreferencesForItem(itemID);
    } catch (NoSuchItemException nsie) {
      // OK. Probably an item that only the anonymous user has
      //if (log.isDebugEnabled()) {
      //  log.debug("Item {} unknown", itemID);
      //}
    }

    List<IPreference> anonymousPreferences =  new List<IPreference>();

    foreach (var prefsMap in tempPrefs) {
      IPreferenceArray singleUserTempPrefs = prefsMap.Value;
      for (int i = 0; i < singleUserTempPrefs.Length(); i++) {
        if (singleUserTempPrefs.GetItemID(i) == itemID) {
          anonymousPreferences.Add(singleUserTempPrefs.Get(i));
        }
      }
    }

    int delegateLength = delegatePrefs == null ? 0 : delegatePrefs.Length();
    int anonymousPrefsLength = anonymousPreferences.Count;
    int prefsCounter = 0;

    // Merge the delegate and anonymous preferences into a single array
    IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(delegateLength + anonymousPrefsLength);

    for (int i = 0; i < delegateLength; i++) {
      newPreferenceArray.Set(prefsCounter++, delegatePrefs.Get(i));
    }

    foreach (IPreference anonymousPreference in anonymousPreferences) {
      newPreferenceArray.Set(prefsCounter++, anonymousPreference);
    }

    if (newPreferenceArray.Length() == 0) {
      // No, didn't find it among the anonymous user prefs
      throw new NoSuchItemException(itemID);
    }

    return newPreferenceArray;
  }
Exemplo n.º 16
0
 internal PreferenceView(GenericItemPreferenceArray arr, int i)
 {
     this.i   = i;
     this.arr = arr;
 }
    internal PreferenceView(GenericItemPreferenceArray arr, int i) {
      this.i = i;
	  this.arr = arr;
    }
  private static IPreferenceArray cloneAndMergeInto(IPreferenceArray delegatePrefs,
                                                   long itemID,
                                                   long newUserID,
                                                   float value) {

    int length = delegatePrefs == null ? 0 : delegatePrefs.Length();
    int newLength = length + 1;
    IPreferenceArray newPreferenceArray = new GenericItemPreferenceArray(newLength);

    // Set item ID once
    newPreferenceArray.SetItemID(0, itemID);

    int positionToInsert = 0;
    while (positionToInsert < length && newUserID > delegatePrefs.GetUserID(positionToInsert)) {
      positionToInsert++;
    }

    for (int i = 0; i < positionToInsert; i++) {
      newPreferenceArray.SetUserID(i, delegatePrefs.GetUserID(i));
      newPreferenceArray.SetValue(i, delegatePrefs.GetValue(i));
    }
    newPreferenceArray.SetUserID(positionToInsert, newUserID);
    newPreferenceArray.SetValue(positionToInsert, value);
    for (int i = positionToInsert + 1; i < newLength; i++) {
      newPreferenceArray.SetUserID(i, delegatePrefs.GetUserID(i - 1));
      newPreferenceArray.SetValue(i, delegatePrefs.GetValue(i - 1));
    }

    return newPreferenceArray;
  }