1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.validation;
\r
14 import java.util.Collections;
\r
15 import java.util.List;
\r
17 import org.simantics.db.Issue;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.common.issue.StandardIssue;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.layer0.variable.Variable;
\r
23 import org.simantics.issues.common.IssueUtils;
\r
24 import org.simantics.scl.reflection.annotations.SCLValue;
\r
25 import org.simantics.sysdyn.SysdynResource;
\r
28 * Detects syntax errors, unsupported characters and undefined expressions from variables.
\r
33 public class ExpressionIssueFunction {
\r
35 private static String SYNTAX_ERROR = "Syntax error";
\r
36 private static String UNSUPPORTED_CHARACTERS = "Unsupported characters";
\r
37 private static String UNDEFINED_EXPRESSION = "Undefined expression";
\r
39 @SCLValue(type = "ReadGraph -> Resource -> [Issue]")
\r
40 public static List<Issue> expressionValidator(ReadGraph graph, Resource component) throws DatabaseException {
\r
42 SysdynResource sr = SysdynResource.getInstance(graph);
\r
44 if(!graph.isInstanceOf(component, sr.IndependentVariable)) {
\r
45 return Collections.emptyList();
\r
48 // Try if there are any errors while parsing the expressions
\r
50 ValidationUtils.getAllReferences(graph, component);
\r
51 } catch (Exception e) {
\r
52 return Collections.<Issue>singletonList(new StandardIssue(sr.Validations_ExpressionIssue, component));
\r
54 return Collections.emptyList();
\r
57 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
\r
58 public static String expressionIssueDescription(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
\r
60 List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
\r
61 Resource component = contexts.get(0);
\r
63 // There should be an error
\r
65 ValidationUtils.getAllReferences(graph, component);
\r
66 } catch (SyntaxErrorException e) {
\r
67 return SYNTAX_ERROR;
\r
68 } catch (UnsupportedCharactersException e) {
\r
69 return UNSUPPORTED_CHARACTERS;
\r
70 } catch (UndefinedExpressionException e) {
\r
71 return UNDEFINED_EXPRESSION;
\r
73 return "Erroneus error";
\r