using System;
using System.Collections.Generic;
using System.Text;
using EPolBase;
using EXCommon;
using EPolTest;
//###9.1
namespace EPolClient
{
///---------------------------------------------------------------------------------------------------------------------------------------------------
///
/// Summary description for Class1.
///
///---------------------------------------------------------------------------------------------------------------------------------------------------
public class CMemeRankVoteInputArray : CVoteInputArray
{
public CMemeRankVoteInputArray()
{
m_sErrorBuilder = new StringBuilder();
m_oMemeCompareArray = new List();
}
List m_oMemeCompareArray;
public override string sError
{
get
{
return m_sErrorBuilder.ToString();
}
}
StringBuilder m_sErrorBuilder;
static SIdentifierIndex m_sInputDataRecordInstance = new SIdentifierIndex(-77);
public override SIdentifierIndex iInputDataRecordInstance
{
get
{
return m_sInputDataRecordInstance;
}
}
public override P dIsChoicePreferTo(SChoiceIndex iSubMeme1, SChoiceIndex iSubMeme2, out ENExecution dExecutionParameter)
{
if (_bIsChoicePreferTo(iSubMeme1, iSubMeme2, out dExecutionParameter))
{
return P.TRUE;
}
else if (_bIsChoicePreferTo(iSubMeme2, iSubMeme1, out dExecutionParameter))
{
return P.FALSE;
}
dExecutionParameter = ENExecution.NOT_USED;
return P.UNKNOWN;
}
internal override string sDump()
{
string sOutput = "";
for(int i=0; i < m_oMemeCompareArray.Count; i++) {
sOutput += m_oMemeCompareArray[i].sDump() + "\n";
}
return sOutput;
}
protected bool _bIsChoicePreferTo(SChoiceIndex iSubMeme1, SChoiceIndex iSubMeme2, out ENExecution dExecutionParameter)
{
m_sErrorBuilder = new StringBuilder();
for (int i = 0; i < m_oMemeCompareArray.Count; i++)
{
SMemeCompare oMemeCompare = (SMemeCompare)m_oMemeCompareArray[i];
if (oMemeCompare.dSubIndex1 == iSubMeme1)
{
if (oMemeCompare.dSubIndex2 == iSubMeme2)
{
dExecutionParameter = oMemeCompare.dExecutionFeature;
return true;
}
else
{
m_sErrorBuilder.AppendLine(oMemeCompare.sDump());
ENExecution dPreviousExecutionParameter;
if (_bIsChoicePreferTo(oMemeCompare.dSubIndex2, iSubMeme2, out dPreviousExecutionParameter))
{
dExecutionParameter = dExecutionFeatureAnd(oMemeCompare.dExecutionFeature, dPreviousExecutionParameter);
return true;
}
}
}
}
dExecutionParameter = ENExecution.NOT_USED;
return false;
}
protected override ec _dAddChoice(SChoiceIndex iSubMeme1, SChoiceIndex iSubMeme2, ENExecution dExecutionParameter)
{
ENExecution dCurrentExecutionParameter;
P dPreference = dIsChoicePreferTo(iSubMeme1, iSubMeme2, out dCurrentExecutionParameter);
if (dPreference == P.TRUE)
{
m_sErrorBuilder.AppendLine(iSubMeme1.ToString() + " is already prefered to " + iSubMeme2.ToString());
if (dExecutionParameter != dCurrentExecutionParameter)
{
m_sErrorBuilder.AppendLine(" The current execution parameter " + dCurrentExecutionParameter + " is different of your execution
parameter " + dExecutionParameter);
return ec.ST_VOTE_PREFERENCE_ALREADY_RECORDED_MISMATCH_EXECUTION_PARAMETER;
}
return ec.ST_VOTE_PREFERENCE_ALREADY_RECORDED;
}
else if (dPreference == P.FALSE)
{
m_sErrorBuilder.AppendLine(iSubMeme1.ToString() + " is currently not prefered to " + iSubMeme2.ToString());
return ec.ST_VOTE_CAN_NOT_ADD_PAIR_OF_CHOICE_DUE_OPPOSITE_CYCLE;
}
else
{
SMemeCompare oNewMemeCompare = new SMemeCompare();
oNewMemeCompare.dSubIndex1 = iSubMeme1;
oNewMemeCompare.dSubIndex2 = iSubMeme2;
oNewMemeCompare.dExecutionFeature = dExecutionParameter;
m_oMemeCompareArray.Add(oNewMemeCompare);
return ec.ST_SUCCEED;
}
}
public override ec dLoad(SProfileIndex dProfileIndex, SIdentifierIndex dMemeId, byte[] dBallotBinary)
{
SMemeCompare[] oMemeCompareArray = (SMemeCompare[])Deserialize(dBallotBinary);
for (int i = 0; i < oMemeCompareArray.Length; i++)
{
m_oMemeCompareArray.Add(oMemeCompareArray[i]);
}
return ec.ST_SUCCEED;
}
public override byte[] GetSerializedBinary()
{
return GetSerializedValue(m_oMemeCompareArray);
}
public override int Length
{
get
{
return m_oMemeCompareArray.Count;
}
}
public SMemeCompare[] ChoiceCompareArray
{
get
{
return (SMemeCompare[])m_oMemeCompareArray.ToArray();
}
}
public SMemeCompare GetMemeCompareArray(int i)
{
return (SMemeCompare)m_oMemeCompareArray[i];
}
public struct SMemeCompare
{
/* public static SMemeIndex iRegister()
{
return CMemeManager.Instance().iAddCodeInstance(P.INPUT_RECORD_CSHARP, new CCSharpCodeMeme(new SMemeCompare()));
}*/
public void vSet(P dComparator)
{
switch (dComparator)
{
case P.EXCLUSIVE_EXECUTION:
m_dExecutionFeature = ENExecution.Exclusive;
break;
case P.DEPENDANT_EXECUTION:
m_dExecutionFeature = ENExecution.DoneOnlyIfOtherDone;
break;
case P.INDEPENDANT_EXECUTION:
default:
m_dExecutionFeature = ENExecution.Independant;
break;
}
}
public void vSet(object[] oObjectArray)
{
m_dSubIndex1 = new SChoiceIndex(Convert.ToInt32(oObjectArray[0]));
m_dSubIndex2 = new SChoiceIndex(Convert.ToInt32(oObjectArray[1]));
P dComparator = (P)Convert.ToInt32(oObjectArray[2]);
vSet(dComparator);
// EPAssert(m_dMemeIndex != m_dReferenceIndex, "You can not compare a meme with himself");
}
public SChoiceIndex dSubIndex1
{
get
{
return m_dSubIndex1;
}
set
{
m_dSubIndex1 = value;
}
}
public SChoiceIndex dSubIndex2
{
get
{
return m_dSubIndex2;
}
set
{
m_dSubIndex2 = value;
}
}
internal string sDump()
{
return m_dExecutionFeature.ToString() + "(" + m_dSubIndex1 + ">" + m_dSubIndex2 + ")";
}
public ENExecution dExecutionFeature
{
get
{
return m_dExecutionFeature;
}
set
{
m_dExecutionFeature = value;
}
}
private SChoiceIndex m_dSubIndex1;
private SChoiceIndex m_dSubIndex2;
private ENExecution m_dExecutionFeature;
}
}
public class CBottomCountArray : CVoteResultArray
{
public CBottomCountArray(SBottomCount[] oIndexMemeRankList)
{
m_oIndexMemeRankList = oIndexMemeRankList;
}
SBottomCount[] m_oIndexMemeRankList;
public override int dCount
{
get
{
return m_oIndexMemeRankList.Length;
}
}
public override CEPBase.SChoiceIndex iGetChoice(int i)
{
return m_oIndexMemeRankList[i].dMemeSubIndex;
}
public override double dGetCountToRank(int i)
{
return m_oIndexMemeRankList[i].dbCount;
}
public override string sGetClassementCriteriaName
{
get
{
return "count_neg";
}
}
internal override string sDump()
{
string sOutput = "";
for(int i=0; i < m_oIndexMemeRankList.Length; i++) {
sOutput += m_oIndexMemeRankList[i].sDump() + "\n";
}
return sOutput;
}
public struct SBottomCount
{
public CEPBase.SChoiceIndex dMemeSubIndex;
public double dbCount;
internal string sDump()
{
return dMemeSubIndex.ToString() + "(" + dbCount + ")";
}
}
/* public override SChoiceIndex iGetChoiceVoted
{
get
{
if (dbCount == 0) return SChoiceIndex.NOT_USED;
return m_oIndexMemeRankList[0].dMemeSubIndex;
}
}*/
public override bool bIsWinnerOn(int i1, int i2)
{
return i1 < i2;
}
public override byte[] GetSerializeResult
{
get
{
return CEPBase.GetSerializedValue(m_oIndexMemeRankList);
}
}
}
///---------------------------------------------------------------------------------------------------------------------------------------------------
public class CRankVoting : CVoting
{
public CRankVoting(CVoteRightList oVoteRightList)
: base(new CProfileVotingList(oVoteRightList), oVoteRightList, new CChoiceSortedList())
{
}
public override CVoteInputArray oCreate()
{
return new CMemeRankVoteInputArray();
}
public override void vSetProfileAt(SProfileIndex iProfileIndex, CVoteInputArray oVoteInputArray)
{
// SProfileIndex dProfileIndex = m_oVoteRightArray[i].iProfileIndex;
// CMemeRankVoteInputArray.SMemeCompare[] oMemeCompareArray = ((CMemeRankVoteInputArray)oVoteInputArray).ChoiceCompareArray;
if (oVoteInputArray != null)
{
CMemeRankVoteInputArray oMemeCompareInputArray = (CMemeRankVoteInputArray)oVoteInputArray;
CProfileRankVoting oProfileRankVoting = new CProfileRankVoting();
CMemeRankVoteInputArray.SMemeCompare[] oMemeCompareArray = oMemeCompareInputArray.ChoiceCompareArray;
for (int j = 0; j < oMemeCompareInputArray.Length; j++)
{
CTrace.i().vWriteLn(ENMessage.Vote, " oMemeCompare[j] = " + oMemeCompareArray[j].sDump());
m_oChoiceIndexList.Add(oMemeCompareArray[j].dSubIndex1, null);
m_oChoiceIndexList.Add(oMemeCompareArray[j].dSubIndex2, null);
}
oProfileRankVoting.vAdd(oMemeCompareInputArray);
m_oProfileVotingList.vAdd(iProfileIndex, oProfileRankVoting);
}
}
//dGetExecutionRelation(int[] dMemeArray, int dMemeIndex)
public override void vVote(out CVoteResultArray oMemeRankResultList, ref CTraceReport oTraceReport)
{
CProfileVotingList oRankVotingList = m_oProfileVotingList;
CBottomCountArray.SBottomCount[] oIndexMemeRankList = new CBottomCountArray.SBottomCount[m_oChoiceIndexList.Count];
int dMemeRankIndex = 0;
while (m_oChoiceIndexList.Count > 0)
{
CBottomCountArray.SBottomCount[] oMemeCountArray = new CBottomCountArray.SBottomCount[m_oChoiceIndexList.Count];
CListReport oDetailProfileReport = new CListReport(3);
oDetailProfileReport.Active = oTraceReport.Active;
oDetailProfileReport.vSetTitle("Bottom popularity detail report meme total=" + oRankVotingList.ProfileCount);
oDetailProfileReport.vSetHeader("Bottom_Meme", "Profile", "Vote_right");
CListReport oDetailReport = new CListReport(2);
oDetailReport.Active = oTraceReport.Active;
oDetailReport.vSetTitle("Bottom popularity detail report meme total=" + oRankVotingList.ProfileCount);
oDetailReport.vSetHeader("Meme", "Bottom_count");
for (int i = 0; i < m_oChoiceIndexList.Count; i++)
{
oMemeCountArray[i].dMemeSubIndex = m_oChoiceIndexList.GetKey(i);
CTrace.i().vWriteLn(ENMessage.Vote, " choice " + oMemeCountArray[i].dMemeSubIndex);
oMemeCountArray[i].dbCount = 0;
for (SProfileRankIndex k = SProfileRankIndex.CreateInitValue(oRankVotingList); k < oRankVotingList.ProfileCount; k++)
{
CTrace.i().vWriteLn(ENMessage.Vote, " oRankVotingList = " + oRankVotingList.sDump());
SProfileIndex dProfileIndex = k.ProfileIndex;
CProfileRankVoting oProfileRankVoting = (CProfileRankVoting)oRankVotingList.GetProfileVoting(k);
if ( oProfileRankVoting.bIsAtPopularityBottom(oMemeCountArray[i].dMemeSubIndex))
{
oMemeCountArray[i].dbCount += m_oVoteRightList[k].dbVoteCoefficient;
oDetailProfileReport.vAdd(CReport.CHOICE + oMemeCountArray[i].dMemeSubIndex, m_oVoteRightList.sGetProfileDisplayName(k), m_oVoteRightList[k]);
}
}
CTrace.i().vWriteLn(ENMessage.Vote, "count " + oMemeCountArray[i].dbCount);
oDetailReport.vAdd(CReport.CHOICE + oMemeCountArray[i].dMemeSubIndex, oMemeCountArray[i].dbCount);
}
oTraceReport.vAdd(oDetailProfileReport); oTraceReport.vAdd(oDetailReport);
if (oMemeCountArray.Length >= 1)
{
CBottomCountArray.SBottomCount oBottomCountMax = oMemeCountArray[0];
for (int j = 1; j < oMemeCountArray.Length; j++)
{
if (oBottomCountMax.dbCount < oMemeCountArray[j].dbCount)
{
oBottomCountMax = oMemeCountArray[j];
}
}
oIndexMemeRankList[dMemeRankIndex] = oBottomCountMax;
oTraceReport.vAddLine(dMemeRankIndex + ":Remove meme " + CReport.CHOICE + oBottomCountMax.dMemeSubIndex + " Count=" + oBottomCountMax.dbCount);
dMemeRankIndex++;
oRankVotingList.vRemove(oBottomCountMax.dMemeSubIndex);
// vRemove(ref oRankVotingList, oBottomCountMax.dMemeSubIndex);
m_oChoiceIndexList.Remove(oBottomCountMax.dMemeSubIndex);
}
}
oMemeRankResultList = new CBottomCountArray(oIndexMemeRankList);
}
}
///---------------------------------------------------------------------------------------------------------------------------------------------------
public class CProfileRankVoting : CProfileVoting
{
public CProfileRankVoting()
: base()
{
m_oChoiceList = new CChoiceSortedList();
}
CChoiceSortedList m_oChoiceList;
public void vAdd(CVoteInputArray oMemeCompareInputArray)
{
CMemeRankVoteInputArray.SMemeCompare[] oMemeCompareArray = ((CMemeRankVoteInputArray)oMemeCompareInputArray).ChoiceCompareArray;
for (int i = 0; i < oMemeCompareArray.Length; i++)
{
vAdd(oMemeCompareArray[i]);
}
}
protected void vAdd(CMemeRankVoteInputArray.SMemeCompare oMemeCompare)
{
m_oChoiceList.Add(oMemeCompare.dSubIndex1, new CChoiceLink(oMemeCompare.dSubIndex2, oMemeCompare.dExecutionFeature));
if (m_oChoiceList.Contains(oMemeCompare.dSubIndex2))
{
CChoiceLink oMemeLink = (CChoiceLink)m_oChoiceList[oMemeCompare.dSubIndex2];
oMemeLink.vAdd(oMemeCompare.dSubIndex1);
m_oChoiceList.Remove(oMemeCompare.dSubIndex2);
m_oChoiceList.Add(oMemeCompare.dSubIndex2, oMemeLink);
}
}
public bool bIsAtPopularityBottom(SChoiceIndex dIndex)
{
return m_oChoiceList[dIndex] == null;
}
public override void vRemove(SChoiceIndex dIndex)
{
CChoiceLink oMemeLink = (CChoiceLink)m_oChoiceList[dIndex];
if (oMemeLink != null)
{
for (int i = 0; i < oMemeLink.MostPopularChoiceCount; i++)
{
SChoiceIndex dMostPopularMemeIndex = (SChoiceIndex)oMemeLink.GetMostPopularChoiceByIndex(i);
CChoiceLink oMemeSupLink = ((CChoiceLink)m_oChoiceList[dMostPopularMemeIndex]);
if (oMemeSupLink != null)
{
oMemeSupLink.m_dLessPopularChoiceIndex = oMemeLink.m_dLessPopularChoiceIndex;
m_oChoiceList.Remove(dMostPopularMemeIndex);
m_oChoiceList.Add(dMostPopularMemeIndex, oMemeSupLink);
}
}
}
}
public ENExecution dGetExecutionFeatureIndex(SChoiceIndex dMemeIndex1, SChoiceIndex dMemeIndex2)
{
SChoiceIndex _dMemeIndex1 = dMemeIndex1;
while (true)
{
CChoiceLink oMeme1 = ((CChoiceLink)m_oChoiceList[_dMemeIndex1]);
if (oMeme1 == null)
{
return ENExecution.Independant;
}
if (dMemeIndex2 == oMeme1.m_dLessPopularChoiceIndex)
{
return oMeme1.m_dExecutionFeatureIndex;
}
else
{
_dMemeIndex1 = oMeme1.m_dLessPopularChoiceIndex;
}
}
}
//-------
public class CChoiceLink
{
public CChoiceLink(SChoiceIndex _dLessPopularIndex, ENExecution _dExecutionFeatureIndex)
{
m_oMostPopularChoiceArrayList = new List();
m_dLessPopularChoiceIndex = _dLessPopularIndex;
m_dExecutionFeatureIndex = _dExecutionFeatureIndex;
}
public void vAdd(SChoiceIndex dMemeSupIndex)
{
m_oMostPopularChoiceArrayList.Add(dMemeSupIndex.dIndex);
}
public SChoiceIndex m_dLessPopularChoiceIndex;
public ENExecution m_dExecutionFeatureIndex;
List m_oMostPopularChoiceArrayList;
public int MostPopularChoiceCount
{
get {
return m_oMostPopularChoiceArrayList.Count;
}
}
public SChoiceIndex GetMostPopularChoiceByIndex(int i)
{
return new SChoiceIndex(m_oMostPopularChoiceArrayList[i]);
}
}
//-------
}
///---------------------------------------------------------------------------------------------------------------------------------------------------
///---------------------------------------------------------------------------------------------------------------------------------------------------
///---------------------------------------------------------------------------------------------------------------------------------------------------
///---------------------------------------------------------------------------------------------------------------------------------------------------
public abstract class CGradeVoting : CVoting
{
public CGradeVoting(CVoteRightList oVoteRightList)
: base(new CProfileVotingList(oVoteRightList), oVoteRightList, new CChoiceSortedList())
{
}
public static SVoteGrade[] ToVoteGrade(List
|