View Javadoc

1   /**
2    * 
3    */
4   package org.ximtec.igesture.core.composite;
5   
6   import java.text.ParseException;
7   import java.util.Calendar;
8   import java.util.Date;
9   import java.util.HashMap;
10  import java.util.HashSet;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Set;
14  
15  import org.ximtec.igesture.core.Gesture;
16  import org.ximtec.igesture.io.IDeviceManager;
17  
18  /**
19   * This class represents an interval constraint. All gestures must be performed within a certain time interval. It does not
20   * matter in which order and how they are related in time (e.g. sequential, concurrent or a mix).
21   * 
22   * @author Bjorn Puype, bpuype@gmail.com
23   *
24   */
25  public class IntervalConstraint extends DefaultConstraint {
26  
27  	public enum Config{
28  		DURATION;
29  	}
30  	
31  	private static final String DURATION = "00:01:00.000";
32  	
33  	private Calendar duration;
34  
35  	public IntervalConstraint() {
36  		super();
37  		DEFAULT_CONFIGURATION.put(Config.DURATION.name(), DURATION);
38  		setterMapping.put(Config.DURATION.name(), "setDuration");
39  		try {
40  			setDuration(DURATION);
41  		} catch (ParseException e) {
42  			e.printStackTrace();
43  		}
44  	}
45  	
46  	/**
47  	 * Set the duration of the interval.
48  	 * @param time	duration in format "hh:mm:ss.SSS"
49  	 * @throws ParseException thrown if the duration could not be parsed
50  	 */
51  	public void setDuration(String time) throws ParseException
52  	{
53  		Date d = df.parse(time);
54  		duration = Calendar.getInstance();
55  		duration.setTime(d);
56  	}
57  
58  	/**
59  	 * Get the duration of the interval
60  	 */
61  	public Calendar getDuration()
62  	{
63  		return duration;
64  	}
65  	
66  	/* (non-Javadoc)
67  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#determineTimeWindows()
68  	 */
69  	@Override
70  	public Map<String, Calendar> determineTimeWindows() {
71  		
72  		Calendar cal = (Calendar) duration.clone();
73  		cal.add(Calendar.SECOND, processingTime.get(Calendar.SECOND));
74  		
75  		Map<String, Calendar> map = new HashMap<String, Calendar>();
76  		
77  		for(DefaultConstraintEntry entry: gestures)
78  		{
79  			map.put(entry.getGesture(), cal);
80  		}
81  		
82  		return map;
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#generatePatterns(java.util.Map)
87  	 */
88  	@Override
89  	public Set<String> generatePatterns(Map<String, String> charMapping) {
90  		Set<String> patterns = new HashSet<String>();
91  		
92  		StringBuilder builder = new StringBuilder();
93  		for (DefaultConstraintEntry entry : gestures) {
94  			builder.append(charMapping.get(entry.getGesture()));			
95  		}
96  		
97  		String pattern = builder.toString();
98  		
99  		ConstraintTool.permute(0, "", new boolean[pattern.length()], pattern, patterns);
100 		
101 		return patterns;
102 	}
103 
104 	/* (non-Javadoc)
105 	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#validateConditions(java.util.List)
106 	 */
107 	@Override
108 	public boolean validateConditions(List<Gesture<?>> gestures, IDeviceManager manager) {
109 		boolean conditionsValid = super.validateConditions(gestures, manager);
110 		
111 		if(conditionsValid) // if previous conditions hold
112 		{
113 			/* time check */
114 			long minTimestamp = Long.MAX_VALUE;
115 			
116 			//determine start time of the gesture that was performed first
117 			for(Gesture<?> gesture : gestures)
118 			{
119 				long timestamp = getTimeStamp(gesture, true);
120 				
121 				if(timestamp < minTimestamp)
122 				{
123 					minTimestamp = timestamp;
124 				}
125 			}
126 			
127 			//determine the end time of the interval
128 			long intervalTime = ConstraintTool.calculateTimeInMillis(duration);
129 			long endTimestamp = minTimestamp+intervalTime;
130 //			Calendar cal = Calendar.getInstance();
131 //			cal.setTimeInMillis(minTimestamp);
132 //			cal.add(Calendar.MILLISECOND, duration.get(Calendar.MILLISECOND));
133 //			cal.add(Calendar.SECOND, duration.get(Calendar.SECOND));
134 //			cal.add(Calendar.MINUTE, duration.get(Calendar.MINUTE));
135 //			cal.add(Calendar.HOUR, duration.get(Calendar.HOUR));
136 //			long endTimestamp = cal.getTimeInMillis();
137 			
138 			//determine if all gestures end before or at the same time as the end time of the interval
139 			for(Gesture<?> gesture : gestures)
140 			{
141 				long timestamp = getTimeStamp(gesture, false);
142 				
143 				if(timestamp > endTimestamp)
144 				{
145 					conditionsValid = false;
146 					break;
147 				}
148 			}
149 		}
150 		
151 		return conditionsValid;
152 	}
153 
154 	public String toString()
155 	{
156 		return IntervalConstraint.class.getSimpleName();
157 	}
158 }