From 21685df8de3f86526817860ef6575b77374fe1be Mon Sep 17 00:00:00 2001
From: Simon Spinner <simon.spinner@uni-wuerzburg.de>
Date: Mon, 6 Jun 2016 13:40:46 +0200
Subject: [PATCH] Add network delays to external calls (if activated)

---
 .../prisma/agent/wildfly/Constants.java       |  4 ++-
 .../service/ModelExtractionService.java       | 27 ++++++++++++++++---
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/Constants.java b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/Constants.java
index 55c3dce..6332920 100644
--- a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/Constants.java
+++ b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/Constants.java
@@ -27,7 +27,9 @@
 package tools.descartes.prisma.agent.wildfly;
 
 public class Constants {
-	 public static final long NANOS_IN_MILLIS = 1000000;
+	public static final long NANOS_IN_MILLIS = 1000000;
 
 	public static final long MONITORING_INTERVAL_IN_MILLIS = 60000;
+
+	public static final boolean ADD_NETWORK_DELAYS = true;
 }
diff --git a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
index e555729..59c0fcd 100644
--- a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
+++ b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
@@ -352,7 +352,7 @@ public class ModelExtractionService extends AgentController implements Service<M
 	private void addComponent(InvocationRecord incomingIrec, InvocationRecord outgoingIrec) {
 		ComponentRecord incomingCrec = incomingIrec.getOperation().getComponent();
 		String incomingInterfaceName = incomingIrec.getOperation().getInterfaceName();
-		boolean isRemoteIncomingCall = !incomingIrec.getOperation().getProtocol().equals("java");
+		boolean isRemoteIncomingCall = isRemoteCall(incomingIrec);
 		ComponentRecord outgoingCrec = null;
 		String outgoingInterfaceName = null;
 		if (outgoingIrec != null) {
@@ -415,8 +415,7 @@ public class ModelExtractionService extends AgentController implements Service<M
 		ExternalCall externalCall = null;
 		if (callerRequiringRole != null) {
 			externalCall = addExternalCall(incomingCrec.getComponentClass(), implementationComponent, behavior,
-					callerRequiringRole,
-					outgoingSignature);
+					callerRequiringRole, outgoingSignature, outgoingIrec);
 		}
 		RepositoryComponent callerComponent = implementationComponent;
 
@@ -771,7 +770,8 @@ public class ModelExtractionService extends AgentController implements Service<M
 	}
 
 	private ExternalCall addExternalCall(String componentName, BasicComponent implementationComponent,
-			FineGrainedBehavior behavior, InterfaceRequiringRole requiringRole, Signature outgoingSignature) {
+			FineGrainedBehavior behavior, InterfaceRequiringRole requiringRole, Signature outgoingSignature,
+			InvocationRecord outgoingInvocation) {
 		ExternalCallAction callAction = ServicebehaviorFactory.eINSTANCE.createExternalCallAction();
 		ExternalCall call = ServicebehaviorFactory.eINSTANCE.createExternalCall();
 		call.setName(toName(requiringRole.getName() + "#" + outgoingSignature.getName()));
@@ -799,6 +799,21 @@ public class ModelExtractionService extends AgentController implements Service<M
 			iterationCount.setCharacterization(ModelVariableCharacterizationType.EMPIRICAL);
 			loop.setLoopIterationCount(iterationCount);
 			loop.setLoopBodyBehavior(ServicebehaviorFactory.eINSTANCE.createComponentInternalBehavior());
+
+			// We may add delay actions to represent the network delays for
+			// remote external calls.
+			if (Constants.ADD_NETWORK_DELAYS) {
+				if (isRemoteCall(outgoingInvocation)) {
+					InternalAction delayAction = ServicebehaviorFactory.eINSTANCE.createInternalAction();
+					ResourceDemand demand = ServicebehaviorFactory.eINSTANCE.createResourceDemand();
+					demand.setCharacterization(ModelVariableCharacterizationType.EMPIRICAL);
+					demand.setName("Network");
+					demand.setResourceType(
+							ModelSkeletonUtil.adopt(this, getContainerScope().getSystemScope().getDelayResourceType()));
+					delayAction.getResourceDemand().add(demand);
+					loop.getLoopBodyBehavior().getActions().add(delayAction);
+				}
+			}
 			loop.getLoopBodyBehavior().getActions().add(callAction);
 
 			// We assume that the external calls are inserted in
@@ -823,6 +838,10 @@ public class ModelExtractionService extends AgentController implements Service<M
 		}
 	}
 
+	private boolean isRemoteCall(InvocationRecord outgoingInvocation) {
+		return !outgoingInvocation.getOperation().getProtocol().equals("java");
+	}
+
 	private CompositeComponent createCompositeComponent(ComponentRecord crec,
 			RepositoryComponent implementationComponent) {
 		String compositeName = getCompositeComponentName(crec);
-- 
GitLab