View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.util;
23  
24  import java.util.List;
25  
26  import org.llrp.ltk.types.LLRPEnumeration;
27  import org.llrp.ltk.types.LLRPMessage;
28  import org.llrp.ltk.types.LLRPParameter;
29  import org.llrp.ltk.types.LLRPType;
30  import org.llrp.ltkGenerator.generated.ChoiceParameterReference;
31  import org.llrp.ltkGenerator.generated.FieldDefinition;
32  import org.llrp.ltkGenerator.generated.ChoiceDefinition;
33  
34  /**
35   * This class lets you generate default LLRP messages automatically.
36   *
37   * @author Ulrich Etter, ETHZ
38   *
39   */
40  public class LLRPFactory {
41  	
42  	private static final String MESSAGES_PACKAGE = "org.llrp.ltk.generated.messages";
43  	private static final String PARAMETERS_PACKAGE = "org.llrp.ltk.generated.parameters";
44  	private static final String TYPES_PACKAGE = "org.llrp.ltk.types";
45  	private static final String ENUMERATIONS_PACKAGE = "org.llrp.ltk.generated.enumerations";
46  	
47  	/**
48  	 * Creates a new LLRP message of the given type. All mandatory parameters are set (recursively)
49  	 * and all fields are initialized by default values. Lists that may not be empty are filled with 
50  	 * just one parameter. In case of a choice, just the first choice is made.
51  	 * 
52  	 * @param messageType the type of the message that should be created (e.g. ADD_ROSPEC)
53  	 * @return the newly created message
54  	 */
55  	public static LLRPMessage createLLRPMessage(String messageType){
56  		LLRPMessage message = null;
57  		LLRPTreeMaintainer treeMaintainer = null;
58  		try {
59  			Class messageClass = Class.forName(MESSAGES_PACKAGE + "." + messageType);
60  			message = (LLRPMessage) messageClass.getConstructor(new Class[0]).newInstance(new Object[0]);
61  			treeMaintainer = new LLRPTreeMaintainer(message);
62  			
63  			initializeFields(message, treeMaintainer);
64  			initializeMandatoryParameters(message, treeMaintainer);
65  
66  		} catch (Exception e) {
67  			e.printStackTrace();
68  		}
69  		return message;
70  	}
71  	
72  	private static LLRPParameter createLLRPParameter(String parameterName, LLRPTreeMaintainer treeMaintainer){
73  		LLRPParameter parameter = null;
74  		try {
75  			Class parameterTypeClass = Class.forName(PARAMETERS_PACKAGE + "." + parameterName);
76  			parameter = (LLRPParameter) parameterTypeClass.getConstructor(new Class[0]).newInstance(new Object[0]);
77  
78  			initializeFields(parameter, treeMaintainer);
79  			initializeMandatoryParameters(parameter, treeMaintainer);
80  			
81  		} catch (Exception e) {
82  			e.printStackTrace();
83  		}
84  		return parameter;
85  	}
86  
87  	private static void initializeFields(Object messageOrParameter, LLRPTreeMaintainer treeMaintainer){
88  		Object messageOrParameterDefinition = treeMaintainer.getDefinition(messageOrParameter);
89  		for (FieldDefinition fieldDefinition : LLRP.getFieldDefinitions(messageOrParameterDefinition)){
90  			if (LLRP.isEnumeration(fieldDefinition)){
91  				LLRPEnumeration enumeration = createLLRPEnumeration(fieldDefinition.getEnumeration());
92  				treeMaintainer.setField(messageOrParameter, fieldDefinition.getName(), (LLRPType) enumeration);
93  			}
94  			else {
95  				String fieldType = LLRP.getFieldType(fieldDefinition);
96  				LLRPType fieldValue = null;
97  				LLRPRangeConstraint rangeConstraint = LLRPConstraints.getRangeConstraint(messageOrParameter, fieldDefinition.getName(), treeMaintainer);
98  				LLRPArrayConstraint arrayConstraint = LLRPConstraints.getArrayConstraint(messageOrParameter, fieldDefinition.getName(), treeMaintainer);
99  				if (rangeConstraint != null){
100 					try {
101 						fieldValue = createLLRPType(fieldType, Integer.toString(rangeConstraint.getDefaultValue()));
102 					} catch (Exception e) {
103 						e.printStackTrace();
104 					}
105 				}
106 				else if (arrayConstraint != null){
107 					try {
108 						String s = "";
109 						for (int i = 0; i < arrayConstraint.getDefaultValue().length; i++){
110 							s = s + arrayConstraint.getDefaultValue()[i];
111 							if (i < arrayConstraint.getDefaultValue().length - 1){
112 								s = s + " ";
113 							}
114 						}
115 						fieldValue = createLLRPType(fieldType, s);
116 					} catch (Exception e) {
117 						e.printStackTrace();
118 					}
119 				}
120 				else{
121 					fieldValue = createLLRPType(fieldType);
122 				}
123 				treeMaintainer.setField(messageOrParameter, fieldDefinition.getName(), fieldValue);
124 			}
125 		}
126 	}
127 	
128 	private static void initializeMandatoryParameters(Object messageOrParameter, LLRPTreeMaintainer treeMaintainer) {
129 		Object messageOrParameterDefinition = treeMaintainer.getDefinition(messageOrParameter);
130 		for (String childName : LLRP.getParameterAndChoiceNames(messageOrParameterDefinition)){
131 			
132 			if (LLRP.canOccurMultipleTimes(messageOrParameterDefinition, childName)){
133 				if (LLRP.mustOccurAtLeastOnce(messageOrParameterDefinition, childName)){
134 					String parameterName;
135 					if (LLRP.isChoice(messageOrParameterDefinition, childName)){
136 						ChoiceDefinition choiceDefinition = LLRP.getChoiceDefinition(childName);
137 						List<ChoiceParameterReference> choiceParameterReferences = choiceDefinition.getParameter();
138 						parameterName = choiceParameterReferences.get(0).getType(); // just use first choice
139 					}
140 					else{
141 						parameterName = childName;
142 					}
143 					LLRPParameter parameter = createLLRPParameter(parameterName, treeMaintainer);
144 					List<LLRPParameter> list = (List<LLRPParameter>) treeMaintainer.getChild(messageOrParameter, childName);
145 					treeMaintainer.addChild(list, parameter);
146 				}
147 			}
148 			else{
149 				if (LLRP.mustOccurAtLeastOnce(messageOrParameterDefinition, childName)){
150 					String parameterName;
151 					if (LLRP.isChoice(messageOrParameterDefinition, childName)){
152 						ChoiceDefinition choiceDefinition = LLRP.getChoiceDefinition(childName);
153 						List<ChoiceParameterReference> choiceParameterReferences = choiceDefinition.getParameter();
154 						parameterName = choiceParameterReferences.get(0).getType(); // just use first choice
155 					}
156 					else{
157 						parameterName = childName;
158 					}
159 					LLRPParameter parameter = createLLRPParameter(parameterName, treeMaintainer);
160 					treeMaintainer.setChild(messageOrParameter, childName, parameter);
161 				}
162 			}
163 		}
164 	}
165 	
166 	private static LLRPType createLLRPType(String typeName){
167 		LLRPType lLRPType = null;
168 		try {
169 			Class fieldTypeClass = Class.forName(TYPES_PACKAGE + "." + typeName);
170 			lLRPType = (LLRPType) fieldTypeClass.getConstructor(new Class[0]).newInstance(new Object[0]);
171 		} catch (Exception e) {
172 			e.printStackTrace();
173 		}
174 		return lLRPType;
175 	}
176 	
177 	/**
178 	 * Creates a new LLRPType of the given name with the given value.
179 	 * 
180 	 * @param typeName the name of the type to create (e.g. UnsignedInteger)
181 	 * @param value (e.g. "1")
182 	 * @return the newly created LLRPType
183 	 * @throws Exception if the given value can not be parsed
184 	 */
185 	public static LLRPType createLLRPType(String typeName, String value) throws Exception{
186 		LLRPType result = null;
187 		if (!value.equals("")){
188 			Class fieldTypeClass;
189 			fieldTypeClass =  Class.forName(TYPES_PACKAGE + "." + typeName);
190 			result = (LLRPType) fieldTypeClass.getConstructor(new Class[]{String.class}).newInstance(new Object[]{value});
191 		}
192 		return result;
193 	}
194 	
195 	private static LLRPEnumeration createLLRPEnumeration(String enumerationName){
196 		LLRPEnumeration enumeration = null;
197 		try {
198 			Class enumerationClass = Class.forName(ENUMERATIONS_PACKAGE + "." + enumerationName);
199 			enumeration = (LLRPEnumeration) enumerationClass.getConstructor(new Class[0]).newInstance(new Object[0]);
200 		} catch (Exception e) {
201 			e.printStackTrace();
202 		}
203 		return enumeration;
204 	}
205 }