/***************************************************************************
 * 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 org.apache.commons.lang3.tuple.Pair;

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.ExecutionModel;
import kieker.analysisteetime.model.analysismodel.trace.OperationCall;
import kieker.analysisteetime.model.analysismodel.trace.Trace;
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 ExecutionModel executionModel;

	public ExecutionModelAssembler(final ExecutionModel executionModel) {
		this.executionModel = executionModel;
	}

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

	protected void addExecution(final DeployedOperation source, final DeployedOperation target) {

		final Pair<DeployedOperation, DeployedOperation> key = Pair.of(source, target);
		if (!this.executionModel.getAggregatedInvocations().contains(key)) {
			final AggregatedInvocation invocation = this.factory.createAggregatedInvocation();
			invocation.setSource(source);
			invocation.setTarget(target);

			this.executionModel.getAggregatedInvocations().put(key, invocation);
		}

		// TODO

	}

	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);
			}

		}
	}
}

@@ -2515,16 +2515,37 @@
 %09// TODO
+ Statistics and so on
 %0A%0A%09%7D%0A%0A%09p
@@ -2720,55 +2720,49 @@
 ) %7B%0A
-%0A
 %09%09%09// 
-TODO handle root OperationCalls%0A%0A%09%09%09if (
+Check if operationCall is an entry 
 oper
@@ -2770,37 +2770,41 @@
 tion
-C
+ c
 all.
-getParent() != null) %7B%0A%09
+ If so than source is null%0A
 %09%09%09f
@@ -2834,16 +2834,52 @@
 source =
+ operationCall.getParent() != null ?
  operati
@@ -2911,19 +2911,25 @@
 ration()
+ : null
 ;%0A
-%09
 %09%09%09final
@@ -2990,17 +2990,16 @@
 ();%0A%0A%09%09%09
-%09
 Executio
@@ -3053,14 +3053,8 @@
 t);%0A
-%09%09%09%7D%0A%0A
 %09%09%7D%0A

support for entry operation calls in execution model