我听说过很多关于阿卡的好事。 我有一个价格流,我需要创建两个移动平均价格。 最后我有一个策略来监听这些信号=>价格,移动平均价格1和移动平均价格2并作出一些决定。
我通过演员实现了这个,但不幸的是我遇到了订单问题,a)我不知道如何为生成的消息或事件指定时间戳或优先级顺序。
public static void main(String[] args) {
String[] array1 =
{"15", "1", "D1", "Wine", "1", "0",
"15", "3", "D3", "Tap water", "2", "2",
"15", "1", "M1", "Fish", "3", "0",
"9", "5", "D4", "Coffee", "2", "2",
"9", "2", "P2", "Cake", "2", "1"
};
List<String> list = Arrays.asList(array1);
Comparator<List<String>> comparator = new Comparator<List<String>>(){
@Override
public int compare(List<String>a1, List<String> a2) {
return Integer.valueOf(a1.get(1)).compareTo(Integer.valueOf(a2.get(1)));
}
};
int lineLength=6;
System.out.println(list.toString());
bubbleSort(list,lineLength,comparator);
System.out.println(list.toString());
}
//classic bubble-sort algorithm
public static void bubbleSort(List<String> list,int lineLength,Comparator<List<String>> comparator){
int numRow=list.size()/lineLength;
for(int i=0;i<numRow;i++){
for(int j=i+1;j<numRow;j++){
List<String> extractArrayI = extractArray(list, i, lineLength);
List<String> extractArrayJ = extractArray(list, j, lineLength);
if(comparator.compare(extractArrayI, extractArrayJ)>0){
swichLines(list,i,j,lineLength);
}
}
}
}
//extract i-th row
public static List<String> extractArray(List<String> list,int i, int lineLength){
return list.subList(i*lineLength, i*lineLength+lineLength);
}
//Switch line i,j
public static void swichLines(List<String>list,int i, int j,int lineLength){
List<String>tempI = new ArrayList<String>(list.subList(i*lineLength, i*lineLength+lineLength));
List<String>tempJ = new ArrayList<String>(list.subList(j*lineLength, j*lineLength+lineLength));
replaceSublist(list,tempJ,i,lineLength);
replaceSublist(list,tempI,j,lineLength);
}
//replace sublist
private static void replaceSublist(List<String> list, List<String> temp, int line, int lineLength) {
for (int k=0; k<lineLength; k++)
{
list.set(line*lineLength+k, temp.get(k));
}
}
其中PriceEvent是一个带有字符串id和double值的简单类。 移动平均线收听价格并计算移动平均线并每次创建一个新的PriceEvent。
策略会监听所有这些PriceEvent,并使用它们的ID可以区分它们。