/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//
/**
* @author Neeraj Joshi <jneeraj@us.ibm.com>
*
*/
package org.apache.imperius.spl.parser.expression.primary;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.compiler.IdentPrimaryTuple;
import org.apache.imperius.spl.parser.compiler.symboltable.SPLSymbolTable;
import org.apache.imperius.spl.parser.compiler.symboltable.Symbol;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.util.TypeInfoImpl;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;
/*
* #( i1:IDENT
(
#(m2:METHOD_CALL
(paramList = exprList[symTab])?
)
|
(
i2:IDENT { secondId = i2.getText(); }
(
arrexp=arrayIndex[symTab]
|
#( m:METHOD_CALL
(paramList = exprList[symTab])?
)
)?
{
if(m != null)
isMethod = true;
tp = new IdentPrimaryTuple(arrexp, paramList, secondId, isMethod);
identTupleList.add(tp);
}
)*
)
*
*/
public class PrimaryExpression implements Expression
{
private String _classNameOrInstanceVariableName;
private List _identTupleList = null;
private SPLSymbolTable _symTab;
private Object evaluationResult = null;
private static Logger logger = SPLLogger.getSPLLogger().getLogger();
private static final String sourceClass = "PrimaryExpression";
private TypeInfoImpl _dataType = new TypeInfoImpl();
private String _referenceTypeName;
// public boolean _isArray = false;
public PrimaryExpression(String id1, List identTupleList,
SPLSymbolTable sTab) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "PrimaryExpression");
_identTupleList = identTupleList;
_classNameOrInstanceVariableName = id1;
this._symTab = sTab;
if(sTab ==null)
{
//System.out.println("PrimaryExpression: sTab is null");
}
if (sTab.getSymbolTableType() != SPLSymbolTable.COLLECT)
{
validate();
}
else if (sTab.getSymbolTableType() == SPLSymbolTable.BASICCOLLECT)
{
// _isArray = true;
this._dataType.setIsArray(true);
validate();
}
if (sTab.getSymbolTableType() == SPLSymbolTable.COLLECT)
{
// _isArray = true;
this._dataType.setIsArray(true);
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "PrimaryExpression");
}
public String getPropertyName()
{
String propName = null;
Iterator idIt = _identTupleList.iterator();
while(idIt.hasNext())
{
IdentPrimaryTuple idt = (IdentPrimaryTuple)idIt.next();
propName = idt.getPropertyName();
break;
}
//System.out.println("returning property name "+propName);
return propName;
}
public String getclassNameOrInstanceVariableName()
{
return _classNameOrInstanceVariableName;
}
public boolean isArray()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "isArray");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "isArray");
return this._dataType.getIsArray();
}
public boolean validate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "validate");
boolean isArray = false;
// case 1: ident1
// case 2: ident1.ident2
// case 3: ident1[expr]
// case 4: ident1.ident2[expr]
if(!_identTupleList.isEmpty())
{
TypeInfo returnType = new TypeInfoImpl(TypeConstants.referenceType,
_classNameOrInstanceVariableName,
isArray);
int i = 0;
Iterator identTupleIt = _identTupleList.iterator();
while(identTupleIt.hasNext())
{
if(returnType.getType() == TypeConstants.referenceType)
{
String referenceTypeName = returnType.getReferenceTypeName();
IdentPrimaryTuple ipt = (IdentPrimaryTuple)identTupleIt.next();
if(i++ == 0) // first element in the list has the same symbol table
{ // as the primary expression
ipt.setSymbolTable(_symTab);
}
returnType = ipt.validate(referenceTypeName);
} else {
identTupleIt.next(); // <--- Added by dawood
}
}
_dataType.copy(returnType);
//_isArray = returnType.getIsArray();
_referenceTypeName = returnType.getReferenceTypeName();
}
else // local constant
{
boolean symbolExists = _symTab.symbolExists(_classNameOrInstanceVariableName, true);
if( symbolExists )
{
Symbol sym = (Symbol)_symTab.getSymbol(_classNameOrInstanceVariableName);
// // xiping's modification, 06/19/09
// // contains only one local constant
// Collection c = _symTab.getSymbol(_classNameOrInstanceVariableName);
// Iterator it = c.iterator();
// Symbol sym = (Symbol) it.next();
_dataType.copy(sym.getType());
// _isArray = sym.isArray();
_referenceTypeName = sym.getReferenceTypeName();
}
else if(_symTab.getInstance(_classNameOrInstanceVariableName)!=null)
{
// validate! happens when the identifier is an import variable
}
else
{
throw new SPLException(Messages.getString(
"SPL_SYMBOL_DOES_NOT_EXIST_EXCEPTION_MSG",
new Object[] { _classNameOrInstanceVariableName }));
}
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
return true;
}
public Object evaluate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
int i = 0;
Object intermediateObject = null;
if(!_identTupleList.isEmpty())
{
Iterator idIter = _identTupleList.iterator();
while(idIter.hasNext())
{
IdentPrimaryTuple ipt = (IdentPrimaryTuple)idIter.next();
if(i++ == 0)
{
intermediateObject = ipt.evaluate();
}
else
{
intermediateObject = ipt.evaluate(intermediateObject);
}
}
}
else
{
if( _symTab.symbolExists(_classNameOrInstanceVariableName,true) )
{
// Symbol sym = _symTab.getSymbol(_classNameOrInstanceVariableName);
// xiping's test 06/19/09
// return value must be a PropertySymbol
Symbol sym = (Symbol)_symTab.getSymbol(_classNameOrInstanceVariableName);
//
intermediateObject = sym.getValue();
}
else
{
intermediateObject = _symTab.getInstance(_classNameOrInstanceVariableName);
}
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
this.evaluationResult=intermediateObject;
return intermediateObject;
}
public TypeInfo getType()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getType");
if (_dataType != null)
{
return _dataType;
}
else
{
try
{
validate();
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"Exception cought");
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getType");
return _dataType;
}
}
public String getClassName()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getClassName");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getClassName");
return _classNameOrInstanceVariableName;
}
public void setClassName(String className)
{
logger.entering(sourceClass, Thread.currentThread().getName() + " "
+ "setClassName");
this._classNameOrInstanceVariableName = className;
logger.exiting(sourceClass, Thread.currentThread().getName() + " "
+ "setClassName");
}
public SPLSymbolTable getSymTab()
{
logger.entering(sourceClass, Thread.currentThread().getName() + " "
+ "getSymTab");
logger.exiting(sourceClass, Thread.currentThread().getName() + " "
+ "getSymTab");
return _symTab;
}
public void setSymTab(SPLSymbolTable symTab)
{
logger.entering(sourceClass, Thread.currentThread().getName() + " "
+ "setSymTab");
this._symTab = symTab;
logger.exiting(sourceClass, Thread.currentThread().getName() + " "
+ "setSymTab");
}
// Xiping 10/20/2008
public String toString() {
return getSensorName();
}
public String getReferenceTypeName() throws SPLException
{
return _referenceTypeName;
}
public String getPropertyString()
{
String propName = "";
Iterator idIt = _identTupleList.iterator();
while(idIt.hasNext())
{
IdentPrimaryTuple idt = (IdentPrimaryTuple)idIt.next();
propName += idt.getPropertyName()+".";
}
int length=propName.length();
if(length > 0){
propName=propName.substring(0, (length-2));
}
return propName;
}
//Xiping 05/25/08
public String getSensorName() {
String sensorName = _classNameOrInstanceVariableName;
if(!_identTupleList.isEmpty()) {
Iterator identTupleIt = _identTupleList.iterator();
while(identTupleIt.hasNext()) {
IdentPrimaryTuple ipt = (IdentPrimaryTuple)identTupleIt.next();
boolean isMethod = ipt.isMethod();
List paramList = ipt.getParamList();
String identifier = ipt.getIdentifier();
sensorName +="." + identifier;
if (isMethod) {
sensorName += "(";
Iterator ite = paramList.iterator();
while (ite.hasNext()) {
Object obj = (Object)ite.next();
sensorName += obj + ",";
}
if (sensorName.endsWith(",")) {
sensorName = sensorName.substring(0, sensorName.length()-1);
}
sensorName += ")";
}
}
}
return sensorName;
}
// public String getSensorName() {
// String sensorName = _classNameOrInstanceVariableName;
//
// if(!_identTupleList.isEmpty()) {
// Iterator identTupleIt = _identTupleList.iterator();
// while(identTupleIt.hasNext()) {
// IdentPrimaryTuple ipt = (IdentPrimaryTuple)identTupleIt.next();
// boolean isMethod = ipt.isMethod();
// List paramList = ipt.getParamList();
// String identifier = ipt.getIdentifier();
//
// String param = "";
// if (isMethod) {
// Iterator ite = paramList.iterator();
// while (ite.hasNext()) {
// Object obj = (Object)ite.next();
// param += obj + ",";
// }
// if (sensorName.endsWith(",")) {
// param = param.substring(0, param.length()-1);
// }
// identifier.replaceAll("()", "("+param+")");
// sensorName += "." + identifier;
// }
// }
// }
//
// return sensorName;
// }
//Xiping 06/04/08
public boolean equals(Object that) {
if (!(that instanceof PrimaryExpression)) {
return false;
}
//TODO SPLSymbolTable must overwrite hashCode() and equals()
// boolean classNameOrInstanceVariableName = this._classNameOrInstanceVariableName.equals(((PrimaryExpression)that)._classNameOrInstanceVariableName);
// boolean identTupleList = this._identTupleList.equals(((PrimaryExpression)that)._identTupleList);
// boolean symTab = this._symTab.equals(((PrimaryExpression)that)._symTab);
// if (classNameOrInstanceVariableName && identTupleList) {// && symTab) {
// return true;
// }
if (this.getSensorName().equals(((PrimaryExpression)that).getSensorName())) {
return true;
}
return false;
}
public int hashCode() {
//TODO see equals()
int hash = this.getSensorName().hashCode();
return hash;
}
}