流程、资源、请求和发布列表:
A req R
A req S
A rel R
A rel S
B req S
B req R
B rel S
B rel R
无法重新安排流程中的顺序(在上面的示例中,A 必须始终在请求 S 之前先请求 R),但是可以重新安排跨流程的顺序(B 可以在 A 请求 R 之前请求 S 和 R)
package Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
public class SortStr {
public static void main(String[] args) {
Set<String> s = new HashSet<String>();
List<String> A = List.of("A req R", "A req S", "A rel R", "A rel S");
List<String> B = List.of("B req S", "B req R", "B rel S", "B rel R");
s.addAll(A);
s.addAll(B);
/* List<String> A = List.of("A");
List<String> B = List.of("1");
List<String> C = List.of("X");
s.addAll(A);
s.addAll(B);
s.addAll(C); */
Map<Integer, List<String>> result = new HashMap<>();
permutations(s, new Stack<String>(), s.size(), result);
result.forEach((key, value) ->
System.out.println("ORDER " + key + " : " + value)
);
}
public static void permutations(Set<String> items, Stack<String> permutation, int size,
Map<Integer, List<String>> result) {
/* permutation stack has become equal to size that we require */
if (permutation.size() == size) {
/* print the permutation */
// System.out.println(Arrays.toString(permutation.toArray(new String[0])));
/* add the permutation in the result */
Integer key = (result.size() > 0) ? result.size() + 1 : 0;
result.put(key, Arrays.asList(permutation.toArray(new String[0])));
}
/* items available for permutation */
String[] availableItems = items.toArray(new String[0]);
for (String i : availableItems) {
/* add current item */
permutation.push(i);
/* remove item from available item set */
items.remove(i);
/* pass it on for next permutation */
permutations(items, permutation, size, result);
/* pop and put the removed item back */
items.add(permutation.pop());
}
}
}
以上程序生成所有可能的排序排列。但我的条件标准是
a process can't be rearranged (A must always request R before it can request S in the example above), however orderings across processes can be rearranged (B can request S and R before A requests R)
那么,我怎样才能对所有可能的资源请求/释放顺序进行排序?
输出我想要的结果:-
ORDER 1: A req R, A req S, A rel R, A rel S, B req S, B req R, B rel S, B rel R
ORDER 2: A req R, A req S, A rel R, B req S, A rel S, B req R, B rel S, B rel R
...
ORDER X: A req R, B req S, A req S, B req R ...
测试用例输入:-
案例一:-
A req R A req S A rel R A rel S B req S B req T B rel S B rel T C req T
C req R C rel T C rel R
案例2:-
A req R A req S A rel R A rel S B req T B rel T C req S C rel S D req U
D req S D req T D rel U D rel S D rel T E req T E req V E rel T E rel V
F req W F req S F rel W F rel S G req V G req U G rel V G rel U
我试了很多但我做不到 所以,请指导我实现这种排列生成。 谢谢你。