Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b48c22fe359d722a77c5eb9a588d83507c0c4030 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * <copyright>
 *
 * Copyright (c) 2014 Obeo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Obeo - initial API and implementation 
 *
 * </copyright>
 */
package org.eclipse.ocl.examples.emf.validation.validity.ui.export.util;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.ocl.examples.emf.validation.validity.LeafConstrainingNode;
import org.eclipse.ocl.examples.emf.validation.validity.RootNode;
import org.eclipse.ocl.examples.emf.validation.validity.Severity;
import org.eclipse.ocl.examples.emf.validation.validity.ui.messages.ValidationDebugMessages;

/**
 * Exports ocl validation results as a txt file.
 */
public class TextExport extends AbstractExport {

	/**
	 * Returns a stream containing the initial contents to be given to new
	 * exported validation results file resource instances.
	 * 
	 * @return exported File contents to be given to new exported file resource
	 *         instances
	 */
	@Override
	public String createContents(@NonNull Resource validatedResource, RootNode rootNode, IFile exportedFile) {
		StringBuilder text = new StringBuilder();
		text.append("==== GENERAL INFORMATION ====\n");
		text.append("Output file name: " + exportedFile.getName() + "\n");
		text.append("Author: " + System.getProperty("user.name") + "\n");
		text.append("\n");
		text.append("\n");
		text.append("==== RESOURCES USED ====\n");
		text.append("Model checked: \n");
		text.append("\t\t\t\t" + validatedResource.getURI().lastSegment() + "\n");
		text.append("\n");
		text.append("\n");
		text.append("==== METRICS ====\n");
		
		int total = getConstraintCount();
		text.append("Total number of evaluated constraints: " + total + "\n");
		text.append("- Number of Success: " + validationSuccess.size() + "\n");
		text.append("- Number of Infos: " + validationInfos.size() + "\n");
		text.append("- Number of Warnings: " + validationWarnings.size() + "\n");
		text.append("- Number of Errors: " + validationErrors.size() + "\n");
		text.append("- Number of Failures: " + validationFailures.size() + "\n");
		text.append("\n");
		text.append("\n");
		text.append("==== LOGS ====\n");
		
		if (validationSuccess.size() == total) {
			text.append("No log to display: models has been successfully validated.\n");
		} else {
			Set<Object> loggedConstrainingObjects = new HashSet<Object>();
			if (!validationInfos.isEmpty()) {
				text.append("- Informations:\n");
				for (LeafConstrainingNode infoNode : validationInfos) {
					if (loggedConstrainingObjects.add(infoNode.getConstrainingObject())) {
						appendLogFile(infoNode, text, Severity.INFO.getLiteral());
					}
				}
			}
			if (!validationWarnings.isEmpty()) {
				text.append("- Warnings:\n");
				for (LeafConstrainingNode warningNode : validationWarnings) {
					if (loggedConstrainingObjects.add(warningNode.getConstrainingObject())) {
						appendLogFile(warningNode, text, Severity.WARNING.getLiteral());
					}
				}
			}
			if (!validationErrors.isEmpty()) {
				text.append("- Errors:\n");
				for (LeafConstrainingNode errorNode : validationErrors) {
					if (loggedConstrainingObjects.add(errorNode.getConstrainingObject())) {
						appendLogFile(errorNode, text, Severity.ERROR.getLiteral());
					}
				}
			}
			if (!validationErrors.isEmpty()) {
				text.append("- Failures:\n");
				for (LeafConstrainingNode failureNode : validationFailures) {
					if (loggedConstrainingObjects.add(failureNode.getConstrainingObject())) {
						appendLogFile(failureNode, text, Severity.FATAL.getLiteral());
					}
				}
			}
		}
		return text.toString();
	}

	private void appendLogFile(LeafConstrainingNode node, StringBuilder s,
			String severity) {
		Resource resource = node.getConstraintResource();
		if (resource != null) {
			s.append("\t\t\t Resource: " + resource + "\n");
		} else {
			s.append("\t\t\t Resource: "
				+ ValidationDebugMessages.ValidityView_Constraints_LabelProvider_UnexistingResource
				+ "\n");
		}
		s.append("\t\t\t Invariant: " + node.getLabel() + "\n");
		
		String expression = node.getConstraintString();
		if (expression != null) {
			s.append("\t\t\t Expression: " + expression + "\n");
		} else {
			s.append("\t\t\t Expression: "
				+ ValidationDebugMessages.ValidityView_Constraints_LabelProvider_UnattainableExpression
				+ "\n");
		}
		s.append("\t\t\t Severity: " + severity + "\n");
		s.append("\t\t\t Message: " + getMessage(node.getWorstResult()) + "\n");
		s.append("\t\t\t\t-------------\n");
	}
}

Back to the top