1
2
3
4 package org.ximtec.igesture.core.composite;
5
6 import java.util.Calendar;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.ximtec.igesture.core.Gesture;
14 import org.ximtec.igesture.io.IDeviceManager;
15
16
17
18
19
20
21
22
23 public class ConcurrencyConstraint extends DefaultConstraint {
24
25 public ConcurrencyConstraint()
26 {
27 super();
28 }
29
30
31
32
33
34 @Override
35 public Map<String, Calendar> determineTimeWindows() {
36 Calendar cal = (Calendar) gestureTime.clone();
37 cal.add(Calendar.SECOND, processingTime.get(Calendar.SECOND));
38
39 Map<String, Calendar> map = new HashMap<String, Calendar>();
40
41 for(DefaultConstraintEntry entry : gestures)
42 {
43 map.put(entry.getGesture(), cal);
44 }
45
46 return map;
47 }
48
49
50
51
52 @Override
53 public Set<String> generatePatterns(Map<String, String> charMapping) {
54
55 Set<String> patterns = new HashSet<String>();
56
57 StringBuilder builder = new StringBuilder();
58 for (DefaultConstraintEntry entry : gestures) {
59 builder.append(charMapping.get(entry.getGesture()));
60 }
61
62 String pattern = builder.toString();
63
64 ConstraintTool.permute(0, "", new boolean[pattern.length()], pattern, patterns);
65
66 return patterns;
67 }
68
69
70
71
72 @Override
73 public boolean validateConditions(List<Gesture<?>> gestures, IDeviceManager manager) {
74
75 boolean conditionsValid = super.validateConditions(gestures, manager);
76
77 if(conditionsValid)
78 {
79
80
81 long minTimestamp = Long.MAX_VALUE;
82 Gesture<?> minGesture = null;
83
84
85 for(Gesture<?> gesture : gestures)
86 {
87 long timestamp = getTimeStamp(gesture, true);
88
89 if(timestamp < minTimestamp)
90 {
91 minTimestamp = timestamp;
92 minGesture = gesture;
93 }
94 }
95
96
97 if(minGesture != null)
98 {
99 long endTime = getTimeStamp(minGesture, false);
100
101 for(Gesture<?> gesture : gestures)
102 {
103 long timestamp = getTimeStamp(gesture, false);
104
105 if(timestamp > endTime)
106 {
107 conditionsValid = false;
108 break;
109 }
110 }
111 }
112 }
113
114 return conditionsValid;
115 }
116
117 public String toString()
118 {
119 return ConcurrencyConstraint.class.getSimpleName();
120 }
121
122 }