Passed
Push — master ( a286a0...c71db0 )
by
unknown
05:40
created

findChildStates(Set,Set)   B

Complexity

Conditions 6

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 27
rs 8.6166
1
package it.cnr.istc.pst.platinum.executive.dc.strategy;
2
3
import java.util.HashMap;
4
import java.util.HashSet;
5
import java.util.Map;
6
import java.util.Set;
7
8
import it.cnr.istc.pst.platinum.executive.dc.strategy.result.Transition;
9
10
public class TreeNodeState {
11
	private StateSet parentState;
12
	private Set<TreeNodeState> childStates;
13
	private Map<String,Map<String,String>> uChildStates;
14
	
15
	public TreeNodeState(StateSet sourceState, Set<StateSet> remainingListStates, Set<String> uStates) {
16
		this.parentState = sourceState;
17
		this.childStates = new HashSet<>();
18
		this.findChildStates(remainingListStates, uStates);
19
		this.uChildStates = new HashMap<>();
20
	}
21
	
22
	private void findChildStates(Set<StateSet> stateList, Set<String> uStates) {
23
		if(this.parentState.getStateSet().toString().contains("rsa=rsa14")) System.out.println("RSA14 REST: " + this.parentState +stateList);
24
		//action transitions in parent node
25
		Set<Transition> transitions = this.parentState.getTransitionStates();
26
		//set to keep found next states
27
		Set<StateSet> nextFound = new HashSet<>();
28
		//generate list of next states compatible with action of the parent node
29
		Set< Map<String,String> > nextStates = new HashSet<>(); 
30
		
31
		for (Transition t : transitions) {
32
			Map<String,String> stat = new HashMap<>();
33
			stat.put(t.getTransitionTo().getTimeline(),t.getTransitionTo().getToken());
34
			nextStates.add(stat);
35
		}
36
		//search for the next states in list in the remaining list of states to insert in the tree
37
		Set<StateSet> output = new HashSet<>();
38
		for( StateSet set : stateList ){
39
			for ( Map<String,String> next : nextStates ) {
40
				if(set.isStateSetStatusControllable(next,uStates)) { //equals with only controllable
41
					nextFound.add(set);
42
				}
43
				else {
44
					output.add(set);
45
				}
46
			}
47
		}
48
		this.setChildStates(nextFound,output, uStates);
49
	}
50
	
51
	private void setChildStates(Set<StateSet> childStates, Set<StateSet> rest, Set<String> uStates) {
52
		for( StateSet set : childStates) {
53
			this.childStates.add(new TreeNodeState(set,rest, uStates));
54
		}
55
		if (childStates.isEmpty() && !rest.isEmpty()) {
56
			//can happen, it's not an error
57
			//System.out.println("Some states are not reachable in this branch: " + rest );
58
		}
59
	}
60
	
61
	public StateSet getParentState() {
62
		return parentState;
63
	}
64
65
	public Set<TreeNodeState> getChildStates() {
66
		return childStates;
67
	}
68
	
69
	public TreeNodeState getChild(Map<String,String> s) {
70
		for(TreeNodeState child : this.childStates) {
71
			if(child.parentState.isStateSetStatus(s)){
72
				return child;
73
			}
74
		}
75
		return null;
76
	}
77
78
	@Override
79
	public String toString() {
80
		return "TreeNodeState [parentState=" + parentState + ", childStates=" + childStates + ", uChildStates="
81
				+ uChildStates + "]";
82
	}
83
84
	public boolean isStateSetStatus(Map<String, String> actualState) {
85
		return this.parentState.isStateSetStatus(actualState);
86
	}
87
	
88
	public boolean isStateSetStatusControllable(Map<String, String> actualState, Set<String> uStates) {
89
		return this.parentState.isStateSetStatusControllable(actualState, uStates);
90
	}
91
92
	public StateStrategy searchNextStepStrategy(Map<String, Long> timelineClocks) throws Exception {
93
		try {
94
			return this.parentState.searchNextStepStrategy(timelineClocks);
95
		} catch (Exception e) {
96
			// TODO Auto-generated catch block
97
			e.printStackTrace();
0 ignored issues
show
Best Practice introduced by
Throwable.printStackTrace writes to the console which might not be available at runtime. Using a logger is preferred.
Loading history...
98
		}
99
		throw new Exception();
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
100
	}
101
102
}
103