* place of the given actor. Use the given options when
* necessary. The given entity is assumed to be an expression actor.
*/
public SootClass createAtomicActor(Entity actor, String newClassName,
ConstVariableModelAnalysis constAnalysis, Map options) {
Expression entity = (Expression) actor;
SootClass entityClass = PtolemyUtilities.actorClass;
// Create a class for the entity instance.
EntitySootClass entityInstanceClass = new EntitySootClass(entityClass,
newClassName, Modifier.PUBLIC);
Scene.v().addClass(entityInstanceClass);
entityInstanceClass.setApplicationClass();
// Create methods that will compute and set the values of the
// parameters of this actor.
ModelTransformer.createAttributeComputationFunctions(entity, entity,
entityInstanceClass, constAnalysis);
// Record everything that the class creates.
HashMap tempCreatedMap = new HashMap();
SootMethod initMethod = entityInstanceClass.getInitMethod();
{
// Populate the initialization method.
JimpleBody body = Jimple.v().newBody(initMethod);
initMethod.setActiveBody(body);
body.insertIdentityStmts();
Chain units = body.getUnits();
Local thisLocal = body.getThisLocal();
// Populate...
// Initialize attributes that already exist in the class.
ModelTransformer.createAttributes(body, entity, thisLocal, entity,
thisLocal, entityInstanceClass, tempCreatedMap);
// Create and initialize ports
ModelTransformer.createPorts(body, thisLocal, entity, thisLocal,
entity, entityInstanceClass, tempCreatedMap);
// return void
units.add(Jimple.v().newReturnVoidStmt());
}
// Add fields to contain the tokens for each port.
Map nameToField = new HashMap();
Map nameToType = new HashMap();
{
Iterator inputPorts = entity.inputPortList().iterator();
while (inputPorts.hasNext()) {
TypedIOPort port = (TypedIOPort) (inputPorts.next());
String name = port.getName(entity);
Type type = PtolemyUtilities.tokenType;
nameToType.put(name, port.getType());
SootField field = new SootField(StringUtilities
.sanitizeName(name)
+ "Token", type);
entityInstanceClass.addField(field);
nameToField.put(name, field);
}
}
// Populate the fire method.
{
SootMethod fireMethod = new SootMethod("fire",
Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
entityInstanceClass.addMethod(fireMethod);
JimpleBody body = Jimple.v().newBody(fireMethod);
fireMethod.setActiveBody(body);
body.insertIdentityStmts();
Chain units = body.getUnits();
Local thisLocal = body.getThisLocal();
Local hasTokenLocal = Jimple.v().newLocal("hasTokenLocal",
BooleanType.v());
body.getLocals().add(hasTokenLocal);
Local tokenLocal = Jimple.v().newLocal("tokenLocal",
PtolemyUtilities.tokenType);
body.getLocals().add(tokenLocal);
Iterator inputPorts = entity.inputPortList().iterator();
while (inputPorts.hasNext()) {
TypedIOPort port = (TypedIOPort) (inputPorts.next());
// FIXME: Handle multiports
if (port.getWidth() > 0) {
String name = port.getName(entity);
// Create an if statement.
//
Local portLocal = Jimple.v().newLocal("port",
PtolemyUtilities.componentPortType);
body.getLocals().add(portLocal);
SootField portField = entityInstanceClass
.getFieldByName(StringUtilities.sanitizeName(name));
units.add(Jimple.v().newAssignStmt(
portLocal,
Jimple.v().newInstanceFieldRef(thisLocal,
portField.makeRef())));
units.add(Jimple.v().newAssignStmt(
hasTokenLocal,
Jimple.v().newVirtualInvokeExpr(portLocal,
PtolemyUtilities.hasTokenMethod.makeRef(),
IntConstant.v(0))));
Stmt target = Jimple.v().newNopStmt();
units.add(Jimple.v().newIfStmt(
Jimple.v().newEqExpr(hasTokenLocal,
IntConstant.v(0)), target));
units.add(Jimple.v().newAssignStmt(
tokenLocal,
Jimple.v().newVirtualInvokeExpr(portLocal,
PtolemyUtilities.getMethod.makeRef(),
IntConstant.v(0))));
SootField tokenField = entityInstanceClass
.getFieldByName(name + "Token");
units.add(Jimple.v().newAssignStmt(
Jimple.v().newInstanceFieldRef(thisLocal,
tokenField.makeRef()), tokenLocal));
units.add(target);
}
}
StringAttribute expressionAttribute = (StringAttribute) entity
.getAttribute("expression");
String expression = expressionAttribute.getExpression();
Local local = DataUtilities.generateExpressionCode(entity,
entityInstanceClass, expression, nameToField, nameToType,