📄 behaviourspoolimpl.java
字号:
// Flags
int Flags;
// Role Modifier
int role = 0;
// When Modifier
int when = 0;
while(!patterns.isEmpty()) {
BehavioursPatternImpl pattern = (BehavioursPatternImpl) patterns.remove(0);
Flags = NULL_MASK;
// Role
String roleOf = pattern.getRoleOf();
if (roleOf.equals(BehavioursPatternImpl.ROLE_GOOD)) {
role = ROLE_GOOD;
} else if(roleOf.equals(BehavioursPatternImpl.ROLE_BAD)) {
role = ROLE_BAD;
} else Flags = Flags | ROLE_NEUTRAL_MASK;
// When
String whenTo = pattern.getWhenTo();
if (whenTo.equals(BehavioursPatternImpl.RUN_LOCAL)) {
when = TRAFFIC_LOCAL;
} else if(whenTo.equals(BehavioursPatternImpl.RUN_REMOTE)) {
when = TRAFFIC_REMOTE;
} else Flags = Flags | TRAFFIC_ALWAYS_MASK;
String typeOf = pattern.getTypeOf();
String modeOf = pattern.getModeOf();
boolean[] queues = whichQueues(Flags, role, when);
for(int queue = 0; queue < BEH_MAPPINGS; queue++) {
if (!queues[queue]) continue;
// Type's is '?' wildcard
if(typeOf.equals(BehavioursPatternImpl.COMPLEMENTARY_WILDCARD)) {
for (int typePos = 0; typePos < behSlots; typePos = typePos + Properties.behavioursNumberOfModes) {
boolean empty = true;
for (int modePos = 0; modePos < Properties.behavioursNumberOfModes; modePos++)
if (beh[queue][modePos + typePos].size() > 0) {
empty = false;
break;
}
if (empty) setPattern(queue, typePos, modeOf, pattern);
}
// Type's is '*' wildcard
} else if(typeOf.equals(BehavioursPatternImpl.UNIVERSAL_WILDCARD)) {
for (int pos = 0; pos < behSlots; pos = pos + Properties.behavioursNumberOfModes)
setPattern(queue, pos, modeOf, pattern);
// Type's tag
} else {
int typePos = 0;
try {
typePos = ((java.lang.reflect.Field) Properties.overlayNode.getField(typeOf)).getInt(null);
} catch(Exception e) {
throw new InitializationException("Node class '" + Properties.overlayNode.getName() + "' does " +
"not provide constant '" + typeOf + "'", e);
}
if (inRangeOf(Properties.behavioursNumberOfTypes, typePos)) setPattern(queue, typePos * Properties.behavioursNumberOfModes, modeOf, pattern);
else throw new InitializationException("Type '" + typeOf + "' constant not in range [" + 0 + ".." + Properties.behavioursNumberOfTypes + "]");
}
}
}
}
/**
* Given the initial position for type on mapping <b>typePos</b> and <b>mode</b> as input, sets the
* pattern on the mapping taking into account the grammar tree extracted from wildcards semantics.
* Hence, the maximum number of mappings goes from <b>typePos</b> to <b>typePos + NumberOfModes</b>.
* For example, suppose type is QUERY_JOIN and modes are REQUEST and REPLY the maximun number of
* mappings to set is 2;
* @param queue Identifies where to be queued the pattern.
* @param typePos Initial position on behaviours mapping array, i.e, [typePos..typePos + NumberOfModes]
* @param modeOf Mode of the pattern: Universal wildcard or '*', Complementary wildcard or '?' and Tag.
* @param pattern Pattern to be treat.
*/
protected void setPattern (int queue, int typePos, String modeOf, BehavioursPatternImpl pattern) throws InitializationException {
// Type's is '?' wildcard
if(modeOf.equals(BehavioursPatternImpl.COMPLEMENTARY_WILDCARD)) {
for(int pos = typePos; pos < (typePos + Properties.behavioursNumberOfModes); pos++) {
if (beh[queue][pos].isEmpty()) beh[queue][pos].add(newInvoker(pattern));
}
// Type's is '*' wildcard
} else if(modeOf.equals(BehavioursPatternImpl.UNIVERSAL_WILDCARD)) {
for(int pos = typePos; pos < (typePos + Properties.behavioursNumberOfModes); pos++)
beh[queue][pos].add(newInvoker(pattern));
// Type's tag
} else {
int modePos = 0;
try {
modePos = ((java.lang.reflect.Field) Properties.overlayNode.getField(modeOf)).getInt(null);
} catch(Exception e) {
throw new InitializationException("Node class [" + Properties.overlayNode.getName() + "] does " +
"not provide this constant: '" + modeOf + "'", e);
}
if (inRangeOf(Properties.behavioursNumberOfModes, modePos)) beh[queue][typePos + modePos].add(newInvoker(pattern));
else throw new InitializationException("Mode [" + modeOf + "] constant not in range [" + 0 + ".." + Properties.behavioursNumberOfModes + "]");
}
}
private BehavioursInvoker newInvoker(BehavioursPatternImpl pattern) throws InitializationException {
Class behClass = pattern.getClassOf();
try {
return ((BehavioursInvokerImpl)GenericFactory.buildBehavioursInvoker()).setValues((Behaviour) GenericFactory.newInstance(pattern.getClassOf()), pattern.getPdf());
} catch(Exception e) {
throw new InitializationException("Initialization Error For Behaviour '" + behClass.getName() + "'.", e);
}
}
protected void prettyPrintAll() {
for (int queue = 0; queue < BEH_MAPPINGS; queue++) {
System.out.println("\n____ " + Queues[queue] + "_______________________________________________\n");
printMap(queue);
System.out.println("\n____________________________________________________________________");
}
}
protected void printMap(int queue) {
for (int slot = 0; slot < behSlots; slot++) {
java.util.Iterator it = beh[queue][slot].iterator();
if (it.hasNext()) System.out.print("\n[" + slot + "]: ");
while(it.hasNext()) {
BehavioursInvokerImpl behInvoker = (BehavioursInvokerImpl) it.next();
String behaviour = behInvoker.getName();
System.out.print(behaviour.substring(behaviour.lastIndexOf('.') + 1));
if (it.hasNext()) System.out.print(", ");
else System.out.print("\n");
}
}
}
/**
* Given an integer expression and upper bound as input, inRangeOf checks wether
* this integer literal belongs to [0..upperBound).
* @param upperBound The upper bound of the set range.
* @param toEval Integer literal to evaluate.
* @return True if integer <b>toEval</b> belongs to [0..upperBound].
*/
private boolean inRangeOf(int upperBound, int toEval) {
return toEval >= 0 && toEval < upperBound;
}
/**
* Given a RouteMessage and a Node as input, onMessage's method intends to
* invoke some behaviours only if RouteMessage's type and mode fields matches
* some behaviour's pattern.
* @param msg RoteMessage taken as input.
* @param node Node taken as input.
* @return Returns either an array of RouteMessages or null when no messages
* need to transmit this node.
* @throws NoSuchBehaviourException when no behaviour matches RouteMessage's
* pattern.
*/
public void onMessage(RouteMessage msg, Node node)
throws NoSuchBehaviourException, NoBehaviourDispatchedException {
// ______________Checking RouteMessage's Type and Mode Pattern______________________________
if (!inRangeOf(Properties.behavioursNumberOfModes, msg.getMode())) throw new OutOfRangeError("Mode [" + msg.getMode() + "] of RouteMessage Out of Range [0.." + Properties.behavioursNumberOfModes + ") \n" + msg);
if (!inRangeOf(Properties.behavioursNumberOfTypes, msg.getType())) throw new OutOfRangeError("Type [" + msg.getType() + "] of RouteMessage Out of Range [0.." + Properties.behavioursNumberOfTypes + ") \n" + msg);
// ______________Filtering__________________________________________________________________
boolean toReject = false;
if (filter != null) {
toReject = filter.filter(msg, node);
}
if (toReject) return;
// _____________Dispatch Behaviour__________________________________________________________
int whichQueue = node.playsGoodRole()? ROLE_GOOD: ROLE_BAD;
whichQueue = whichQueue * BEH_MAPPINGS / 2 + (node.isLocalMessage(msg)? TRAFFIC_LOCAL: TRAFFIC_REMOTE);
behaviours = beh[whichQueue][msg.getType() * Properties.behavioursNumberOfModes + msg.getMode()];
if (behaviours.isEmpty()) throw new NoSuchBehaviourException();
boolean anyExec = false;
// behIt = behaviours.iterator();
BehavioursInvokerImpl behInvoker;
for (int i = 0; i < behaviours.size(); i++)
{
// while(behIt.hasNext()) {
behInvoker = (BehavioursInvokerImpl) behaviours.get(i);//behIt.next();
if (props.debug)
System.out.println("Invoking [" + behInvoker.getName() + "] On Node [" + node.getId() + "] " +
"For [" + planet.simulate.Globals.typeToString(msg.getType()) + "]" +
"[" + planet.simulate.Globals.modeToString(msg.getMode()) + "]");
anyExec |= behInvoker.invoke(msg, node);
}
if (!anyExec) throw new NoBehaviourDispatchedException();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -