View Javadoc

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   * This class represents a concurrency constraint, all gestures must be performed in parallel. This means all gestures must
18   * have an overlapping time interval in common.
19   * 
20   * @author Bjorn Puype, bpuype@gmail.com
21   *
22   */
23  public class ConcurrencyConstraint extends DefaultConstraint {
24  	
25  	public ConcurrencyConstraint()
26  	{
27  		super();		
28  	}
29  	
30  	
31  	/* (non-Javadoc)
32  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#determineTimeWindows()
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  	/* (non-Javadoc)
50  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#generatePatterns(java.util.Map)
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  	/* (non-Javadoc)
70  	 * @see org.ximtec.igesture.core.composite.DefaultConstraint#validateConditions(java.util.List)
71  	 */
72  	@Override
73  	public boolean validateConditions(List<Gesture<?>> gestures, IDeviceManager manager) {
74  		
75  		boolean conditionsValid = super.validateConditions(gestures, manager);
76  		
77  		if(conditionsValid)// if previous conditions hold
78  		{
79  		
80  			/* time check */
81  			long minTimestamp = Long.MAX_VALUE;
82  			Gesture<?> minGesture = null;
83  			
84  			//determine gesture that was performed first
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  			//determine if all gestures end before or at the same time as the gesture that started first (overlap)
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 }