/***************************************************************************
 * Copyright 2015 Kieker Project (http://kieker-monitoring.net)
 *
 * Licensed 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.
 ***************************************************************************/

package kieker.analysisteetime;

import kieker.analysisteetime.model.analysismodel.deployment.DeployedOperation;
import kieker.analysisteetime.model.analysismodel.execution.AggregatedInvocation;
import kieker.analysisteetime.model.analysismodel.execution.ExecutionFactory;
import kieker.analysisteetime.model.analysismodel.execution.ExecutionRoot;
import kieker.analysisteetime.model.analysismodel.trace.OperationCall;
import kieker.analysisteetime.model.analysismodel.trace.TraceRoot;
import kieker.analysisteetime.trace.traversal.OperationCallVisitor;
import kieker.analysisteetime.trace.traversal.TraceTraverser;

/**
 * @author Sren Henning
 *
 * @since 1.13
 */
public class ExecutionModelAssembler {

	private final ExecutionFactory factory = ExecutionFactory.eINSTANCE;

	private final TraceTraverser traceTraverser = new TraceTraverser();
	private final OperationCallInserter operationCallInserter = new OperationCallInserter();

	private final ExecutionRoot executionRoot;

	public ExecutionModelAssembler(final ExecutionRoot executionRoot) {
		this.executionRoot = executionRoot;
	}

	public void addTrace(final TraceRoot trace) {
		this.traceTraverser.traverse(trace, this.operationCallInserter);
	}

	protected void addExecution(final DeployedOperation source, final DeployedOperation target) {
		// TODO
		// this.executionRoot.getAggregatedInvocations().contains(null);

		final AggregatedInvocation invocation = this.factory.createAggregatedInvocation();
		invocation.setSource(source);
		invocation.setTarget(target);

		this.executionRoot.getAggregatedInvocations().add(invocation);

	}

	private class OperationCallInserter extends OperationCallVisitor {
		public OperationCallInserter() {}

		@Override
		public void visit(final OperationCall operationCall) {

			// TODO handle root OperationCalls

			if (operationCall.getParent() != null) {
				final DeployedOperation source = operationCall.getParent().getOperation();
				final DeployedOperation target = operationCall.getOperation();

				ExecutionModelAssembler.this.addExecution(source, target);
			}

		}
	}
}

@@ -804,16 +804,61 @@
 etime;%0A%0A
+import org.apache.commons.lang3.tuple.Pair;%0A%0A
 import k
@@ -2127,23 +2127,98 @@
 ) %7B%0A
+%0A
 %09%09
-// TODO%0A%09%09// 
+final Pair%3CDeployedOperation, DeployedOperation%3E key = Pair.of(source, target);%0A%09%09if (!
 this
@@ -2272,16 +2272,17 @@
 ins(
-null);%0A%0A
+key)) %7B%0A%09
 %09%09fi
@@ -2360,24 +2360,25 @@
 cation();%0A%09%09
+%09
 invocation.s
@@ -2395,16 +2395,17 @@
 ource);%0A
+%09
 %09%09invoca
@@ -2431,16 +2431,17 @@
 et);%0A%0A%09%09
+%09
 this.exe
@@ -2482,12 +2482,17 @@
 s().
-add(
+put(key, 
 invo
@@ -2499,16 +2499,31 @@
 cation);
+%0A%09%09%7D%0A%0A%09%09// TODO
 %0A%0A%09%7D%0A%0A%09p

use new map in ExecutionModelAssembler