View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.p2elv2.model;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.gwe.p2elv2.P2ELDependentVariableNotResolvedException;
27  import org.gwe.p2elv2.PFunction;
28  import org.gwe.utils.VelocityUtils;
29  import org.gwe.utils.rex.REXParser;
30  import org.gwe.utils.rex.config.REXConfig4Class;
31  import org.gwe.utils.rex.config.REXConfig4Field;
32  import org.gwe.utils.rex.config.REXConfig4ListElement;
33  import org.gwe.utils.rex.config.REXConfig4String;
34  
35  /**
36   * @author Marco Ruiz
37   * @since Jul 30, 2008
38   */
39  @REXConfig4Class(rexPieces={"\\$", "functionName", "\\s*\\(", "params", "\\)"})
40  public class PFunctionInvocation implements Serializable {
41  	
42  	public static PFunctionInvocation create(String functionName, String... values) {
43  	    PFunctionInvocation invocation = new PFunctionInvocation();
44  		invocation.setFunctionName(functionName);
45  		List<String> params = new ArrayList<String>();
46  		for (String val : values) params.add(val);
47  		invocation.setParams(params);
48  		return invocation;
49      }
50  
51  	@REXConfig4String(pattern=@REXConfig4Field(field="[a-zA-Z]\\w*"))
52  	private String functionName;
53  	
54  	@REXConfig4ListElement(pattern=@REXConfig4Field(field="[^\\(\\),]*", suffix="[,]?"), min=1)
55  	private List<String> params;
56  	
57  	private Set<String> varDependencyNames = new HashSet<String>();
58  	
59  	public String getFunctionName() { 
60  		return functionName; 
61  	}
62  	
63  	public void setFunctionName(String functionName) { 
64  		this.functionName = functionName; 
65  	}
66  	
67  	public PFunction getFunction() { 
68  		return PFunction.getFunction(functionName); 
69  	}
70  	
71  	public List<String> getParams() { 
72  		return params; 
73  	}
74  	
75  	public void setParams(List<String> params) { 
76  		this.params = params;
77  		
78  		// Resolve params variable references
79          for (String param : params) {
80          	try {
81          		for (PVarRef varRef : REXParser.createModel(PVarReferences.class, param).getVarRefs())
82          			varDependencyNames.add(varRef.getName());
83              } catch (Exception e) {
84              }
85          } 
86  	}
87  	
88      public Set<String> getVarDependencyNames() {
89      	return varDependencyNames;
90      }
91  
92  	public List<String> evalParams(Map<String, Object> vtlModel) throws P2ELDependentVariableNotResolvedException {
93  	    // Verify dependencies resolution
94  		Set<String> dependencies = new HashSet<String>(varDependencyNames);
95          for (String varName : vtlModel.keySet()) 
96          	dependencies.remove(varName);
97          
98          if (!dependencies.isEmpty()) 
99          	throw new P2ELDependentVariableNotResolvedException(dependencies);
100 		
101 		// Evaluate parameters
102 		List<String> evaluatedParams = new ArrayList<String>();
103         for (String param : params) {
104     		String evalParam = (vtlModel.isEmpty()) ? param : VelocityUtils.merge(vtlModel, param);
105 			evaluatedParams.add(evalParam);
106         }
107 	    return evaluatedParams;
108     }
109 	
110 	public String toString() { 
111 		String paramsStr = "";
112 		for (String param : params) {
113 			if (!paramsStr.equals("")) paramsStr += ",";
114             paramsStr += param;
115         }
116 
117 		return "$" + functionName + "(" + paramsStr + ")"; 
118 	}
119 }
120