From: lempinen Date: Wed, 30 Nov 2011 07:39:04 +0000 (+0000) Subject: Updated issues to new format X-Git-Tag: simantics-1.6~92 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d0650613b391cc2c86f7c2242afc86fc7c54cef8;p=simantics%2Fsysdyn.git Updated issues to new format git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23369 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 983ac388..66dd2706 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph b/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph index a7359115..4d6f3fab 100644 --- a/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph +++ b/org.simantics.sysdyn.ontology/graph/ChartAxisAndVariablesViewpoint.pgraph @@ -28,5 +28,14 @@ CBC @VP.dropActionContribution JFREE.Axis ACTIONS.DropAction 1.0 @VP.dropActionContribution JFREE.Series ACTIONS.DropAction 1.0 +CBC + VP.BrowseContext.HasVisualsContribution _ : VP.VisualsContribution + VP.VisualsContribution.HasNodeType JFREE.Chart + VP.VisualsContribution.HasRule VP.PassThruSorterRule + VP.BrowseContext.HasVisualsContribution _ : VP.VisualsContribution + VP.VisualsContribution.HasNodeType JFREE.Axis + VP.VisualsContribution.HasRule VP.PassThruSorterRule + + ACTIONS = CAC.Actions : L0.Library ACTIONS.DropAction : ACT.DropAction \ No newline at end of file diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index e3ed74d5..c19156a4 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -245,8 +245,11 @@ SYSDYN.ConstantExpression -- SYSDYN.DelayExpression.expression --> L0.String -- SYSDYN.DelayExpression.order --> L0.Integer -- SYSDYN.DelayExpression.delayTime --> L0.String -- SYSDYN.DelayExpression.initialValue --> L0.String -- VALIDATIONS.NoSuchVariableIssue.variableName --> L0.String @@ -1795,9 +1795,17 @@ + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/IssueDecorationStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/IssueDecorationStyle.java index 3cd3d6a7..009bb693 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/IssueDecorationStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/IssueDecorationStyle.java @@ -14,20 +14,21 @@ package org.simantics.sysdyn.ui.elements2.profiles; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.util.Collections; +import java.util.List; import java.util.Set; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.PossibleTypedParent; +import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.profile.StyleBase; import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; -import org.simantics.issues.IssueOld.Severity; import org.simantics.issues.common.ErrorIssues; import org.simantics.issues.common.FatalIssues; -import org.simantics.issues.common.IssueContexts; import org.simantics.issues.common.WarningIssues; +import org.simantics.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.scenegraph.INode; @@ -40,73 +41,120 @@ import org.simantics.sysdyn.ui.Activator; import org.simantics.utils.datastructures.map.Tuple; /** - * @author Tuukka Lehtonen + * Issue Decorations. Display an issue icon on + * a diagram element depending on the type of the issue + * . + * @author Teemu Lempinen + * */ public class IssueDecorationStyle extends StyleBase { private static final String DECORATION_NODE_NAME = "issueDecorations"; @Override - public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource module, Variable configuration) throws DatabaseException { + public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); ModelingResources MOD = ModelingResources.getInstance(graph); - Resource config = graph.getPossibleObject(module, MOD.ElementToComponent); - if (config == null) + + // Find a component for the element + Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); + if (component == null) return null; - AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, module); + // Get the current transform of the element to be able to move the decoration with the element + AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element); - Resource model = graph.syncRequest(new PossibleTypedParent(config, SimulationResource.getInstance(graph).Model)); + // Find the model of the component + Resource model = graph.syncRequest(new PossibleTypedParent(component, SimulationResource.getInstance(graph).Model)); if (model == null) return null; + // Project Resource project = graph.getPossibleObject(model, L0.PartOf); if (project == null) return null; + IssueResult result = null; + + /* + * Search for issues. Start from fatal and move to + * less important issues. This way the most important + * issue will be displayed. + * + * The issue is returned immediately after it is found. + */ + Set fatals = graph.syncRequest(new FatalIssues(project, true)); - Set fatalContexts = graph.syncRequest(new IssueContexts(fatals)); - if (fatalContexts.contains(config)) - return new IssueResult(Severity.FATAL, transform); - + result = getIssue(graph, IssueResult.Severity.FATAL, fatals, component, transform); + if(result != null) return result; + Set errors = graph.syncRequest(new ErrorIssues(project, true)); - Set errorContexts = graph.syncRequest(new IssueContexts(errors)); - if (errorContexts.contains(config)) - return new IssueResult(Severity.ERROR, transform); + result = getIssue(graph, IssueResult.Severity.ERROR, errors, component, transform); + if(result != null) return result; Set warnings = graph.syncRequest(new WarningIssues(project, true)); - Set warningContexts = graph.syncRequest(new IssueContexts(warnings)); - if (warningContexts.contains(config)) - return new IssueResult(Severity.WARNING, transform); + result = getIssue(graph, IssueResult.Severity.WARNING, warnings, component, transform); + if(result != null) return result; + // No issue was found + return null; + } + + /** + * See if a set of issue variables concern this component + * + * @param graph ReadGraph + * @param severity IssueResult.Severity of the issue + * @param issues Collection of issues of type severity + * @param component The component that is evaluated for issues + * @param transform AffineTransform of the diagram element + * @return IssueResult containing the severity and transform of the issue or null if no issue concerned component + * @throws DatabaseException + */ + private IssueResult getIssue(ReadGraph graph, IssueResult.Severity severity, Set issues, Resource component, AffineTransform transform) throws DatabaseException { + IssueResource ISSUE = IssueResource.getInstance(graph); + Resource list, issueResource; + for(Variable issue : issues) { + issueResource = issue.getRepresents(graph); + list = graph.getPossibleObject(issueResource, ISSUE.DependencyIssueSource2_Issue_HasContexts); + List contexts = ListUtils.toList(graph, list); + if(!contexts.isEmpty()) { + if(ListUtils.toList(graph, list).get(0).equals(component)) { + return new IssueResult(severity, transform); + } + } + } return null; } @Override public void applyStyleForNode(EvaluationContext observer, INode node, IssueResult result) { + + // If result == null, remove possible issue decoration if (result == null) { ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); return; } + // Create issue decoration node SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, SVGNode.class, observer); + // Move the decoration to the upper right corner of the element Rectangle2D bounds = NodeUtil.getLocalBounds(node, Collections.singleton(svgNode)); - double tx = bounds.getMaxX(); double ty = bounds.getY(); - svgNode.setZIndex( Integer.MAX_VALUE ); svgNode.setTransform( AffineTransform.getTranslateInstance(tx-1, ty-1)); svgNode.getTransform().scale(0.5, 0.5); - Severity sev = result.getSeverity(); - if (Severity.FATAL.equals(sev)) + // Apply the corresponding svg graphics to the node + IssueResult.Severity sev = result.getSeverity(); + if (IssueResult.Severity.FATAL.equals(sev)) svgNode.setData(Activator.FATAL_SVG_TEXT); - else if (Severity.ERROR.equals(sev)) + else if (IssueResult.Severity.ERROR.equals(sev)) svgNode.setData(Activator.ERROR_SVG_TEXT); - else if (Severity.WARNING.equals(sev)) + else if (IssueResult.Severity.WARNING.equals(sev)) svgNode.setData(Activator.WARNING_SVG_TEXT); } @@ -122,6 +170,9 @@ public class IssueDecorationStyle extends StyleBase { * element moves. */ class IssueResult extends Tuple { + + public enum Severity{FATAL, ERROR, WARNING}; + public IssueResult(Severity severity, AffineTransform transform) { super(severity, transform); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java index ebd5f7ad..8c17feb9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java @@ -11,210 +11,162 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.validation; -import java.util.Collection; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Set; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.issues.common.Issue; +import org.simantics.issues.common.StandardIssue; import org.simantics.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.scl.reflection.annotations.SCLValue; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; +/** + * Evaluates issues related to Dependencies (arrows) + * + * @author Teemu Lempinen + * + */ public class DependencyFunction { - - private static String getMissingLinkLabel(String name, String dependency) throws DatabaseException { - return "Missing link " + dependency + " in " + name; - } - - private static String getNoSuchVariableLabel(String name, String dependency) throws DatabaseException { - return name + " refers to variable " + dependency + " that does not exist"; - } - - private static String getUnusedDependencyLabel(String name, String dependency) throws DatabaseException { - return "Unused dependency " + dependency + " in " + name; - } - /** + * Evaluates dependency-related issues for a component. * - * One issue is enough. The first encounter of a new issue returns Boolean.FALSE. + * Issues include: + * Missing link + * No such variable + * Unused dependency * - * @param _graph - * @param _resource - * @param _existing - * @return + * @param graph ReadGraph + * @param component Evaluated component (Variable) + * @return list of issues related to component * @throws DatabaseException */ - @SCLValue(type = "a -> b -> c -> d") - public static Object dependencyValidator(Object _graph, Object _resource, Object _existing) throws DatabaseException { - - ReadGraph graph = (ReadGraph)_graph; - Resource variable = (Resource)_resource; - Collection existing = (Collection)_existing; - - IssueResource ISSUE = IssueResource.getInstance(graph); + @SCLValue(type = "ReadGraph -> Resource -> [Issue]") + public static List dependencyValidator(ReadGraph graph, Resource component) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); - - if(!graph.isInstanceOf(variable, sr.IndependentVariable)) - return Boolean.TRUE; - - if(!graph.hasStatement(variable) || !graph.hasStatement(variable, l0.PartOf)) - return Boolean.FALSE; - String name = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName, Bindings.STRING); + if(!graph.isInstanceOf(component, sr.IndependentVariable)) + return Collections.emptyList(); - Set dependencies = ValidationUtils.getDependencies(graph, variable); + if(!graph.hasStatement(component) || !graph.hasStatement(component, l0.PartOf)) + return Collections.emptyList(); + // Find all variables that are linked to component with arrows + Set dependencies = ValidationUtils.getDependencies(graph, component); Set references = null; + + // Find all references in equations of component try { - references = ValidationUtils.getReferences(graph, variable); + references = ValidationUtils.getReferences(graph, component); } catch (SyntaxErrorException e) { } catch (UnsupportedCharactersException e) { } catch (UndefinedExpressionException e) { } - - if(references != null && dependencies != null) { - for(String reference : references) { - if(!dependencies.contains(reference) && match(graph, existing, variable, getMissingLinkLabel(name, reference)) == null) - return Boolean.FALSE; - } - } - if(dependencies != null) { - for(String dependency : dependencies) { - if(references == null || - (!references.contains(dependency) && - match(graph, existing, variable, getUnusedDependencyLabel(name, dependency)) == null)) - return Boolean.FALSE; - } - } - - Resource context; - for(Resource exist : existing) { - context = graph.getSingleObject(exist, ISSUE.HasIssueContext); - if(!graph.hasStatement(context) || graph.hasStatement(context, l0.PartOf)) - return Boolean.FALSE; - if(variable.equals(context) && graph.isInstanceOf(exist, sr.DependencyIssue)) { - return Boolean.FALSE; - } - } + ArrayList result = new ArrayList(); - return Boolean.TRUE; - } - - @SCLValue(type = "a -> b -> c -> d -> e -> f") - public static Object dependencySynchronizer(Object _graph, Object _resource, Object _source, Object _model, Object _existing) throws DatabaseException { - - WriteGraph graph = (WriteGraph)_graph; - Resource variable = (Resource)_resource; - Resource model = (Resource)_model; - Resource source = (Resource)_source; - Collection existing = (Collection)_existing; - Layer0 L0 = Layer0.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - String name = graph.getRelatedValue(variable, L0.HasName, Bindings.STRING); - - Set labels = new HashSet(); - String label; - - Set dependencies = ValidationUtils.getDependencies(graph, variable); - Set references = null; - try { - references = ValidationUtils.getReferences(graph, variable); - } catch (SyntaxErrorException e) { - } catch (UnsupportedCharactersException e) { - } catch (UndefinedExpressionException e) { - } - + // Check that all references have corresponding arrows if(references != null && dependencies != null) { for(String reference : references) { if(!dependencies.contains(reference)) { - if(ValidationUtils.isReachable(graph, variable, reference)) { - label = getMissingLinkLabel(name, reference); - labels.add(label); - if(match(graph, existing, variable, label) == null) { - ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Warning, sr.DependencyIssue); - } + Resource variable = null; + if((variable = ValidationUtils.reach(graph, component, reference)) != null) { + result.add(new StandardIssue(sr.Validations_MissingLinkIssue, component, variable)); } else { - label = getNoSuchVariableLabel(name, reference); - labels.add(label); - if(match(graph, existing, variable, label) == null) { - ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Error, sr.DependencyIssue); - } + result.add(new NoSuchVariableIssue(sr.Validations_NoSuchVariableIssue, reference, component)); } } } } + // Check that all arrow dependencies are used in equations if(dependencies != null) { for(String dependency : dependencies) { - label = getUnusedDependencyLabel(name, dependency); if(references == null || !references.contains(dependency)) { - labels.add(label); - if(match(graph, existing, variable, label) == null) { - ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Warning, sr.DependencyIssue); - } + Resource variable = ValidationUtils.reach(graph, component, dependency); + result.add(new StandardIssue(sr.Validations_UnusedDependencyIssue, component, variable)); } } } - Set toBeRemoved = new HashSet(); - Resource context; - for(Resource exist : existing) { - context = graph.getSingleObject(exist, ISSUE.HasIssueContext); - if(!graph.hasStatement(context) || !graph.hasStatement(context, L0.PartOf)) - toBeRemoved.add(exist); - if(graph.isInstanceOf(exist, sr.DependencyIssue) && variable.equals(context)) { - String l = graph.getRelatedValue(exist, L0.HasLabel); - if(!labels.contains(l)) - toBeRemoved.add(exist); - } - } - - for(Resource r : toBeRemoved) { - ValidationUtils.removeIssue(graph, model, source, variable, r, existing); - } - - - return Boolean.TRUE; - + return result; } - @SCLValue(type = "a -> b -> c") - public static Object dependencyBaseRealizationFunction(Object _graph, Object _model) throws DatabaseException { - ReadGraph graph = (ReadGraph)_graph; - Resource model = (Resource)_model; - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.isInstanceOf(model, sr.SysdynModel)) - return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); - else if (graph.isInheritedFrom(model, sr.Module)) - return graph.getSingleObject(model, StructuralResource2.getInstance(graph).IsDefinedBy); - else return null; - } - - private static Resource match(ReadGraph graph, Collection existing, Resource variable, String description) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); + /** + * Missing link description + * + * @param graph ReadGraph + * @param converter + * @param issue Issue + * @return issue description + * @throws DatabaseException + */ + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String missingLinkIssueDescription(ReadGraph graph, Resource converter, Variable issue) throws DatabaseException { + IssueResource ISSUE = IssueResource.getInstance(graph); + Resource issueResource = issue.getRepresents(graph); + Resource list = graph.getSingleObject(issueResource, ISSUE.DependencyIssueSource2_Issue_HasContexts); + List contexts = ListUtils.toList(graph, list); + String result = "Missing a link to "; + if(contexts.size() > 0) { + Resource component = contexts.get(1); + String name = NameUtils.getSafeName(graph, component); + result = result + name; + } + return result; + } + + /** + * Unused dependency description + * + * @param graph ReadGraph + * @param converter + * @param issue Issue + * @return issue description + * @throws DatabaseException + */ + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String unusedDependencyIssueDescription(ReadGraph graph, Resource converter, Variable issue) throws DatabaseException { IssueResource ISSUE = IssueResource.getInstance(graph); - for(Resource exist : existing) { - Resource source = graph.getSingleObject(exist, ISSUE.HasIssueContext); - String desc = graph.getRelatedValue(exist, L0.HasLabel); -// if(source.equals(variable) && desc.startsWith(description.substring(0, description.lastIndexOf(" ")))) return exist; - if(source.equals(variable) && desc.equals(description)) return exist; + Resource issueResource = issue.getRepresents(graph); + Resource list = graph.getSingleObject(issueResource, ISSUE.DependencyIssueSource2_Issue_HasContexts); + List contexts = ListUtils.toList(graph, list); + String result = "Unused dependency: "; + if(contexts.size() > 0) { + Resource component = contexts.get(1); + String name = NameUtils.getSafeName(graph, component); + result = result + name; } - return null; + return result; } + /** + * No such variable description + * @param graph ReadGraph + * @param converter + * @param issue Issue + * @return issue description + * @throws DatabaseException + */ + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String noSuchVariableIssueDescription(ReadGraph graph, Resource converter, Variable issue) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource issueResource = issue.getRepresents(graph); + String variable = graph.getPossibleRelatedValue(issueResource, sr.Validations_NoSuchVariableIssue_variableName, Bindings.STRING); + return "Refers to variable " + variable + " that does not exist"; + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ExpressionIssueFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ExpressionIssueFunction.java index 483f2d74..0cbbe11f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ExpressionIssueFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ExpressionIssueFunction.java @@ -11,20 +11,18 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.validation; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; +import java.util.Collections; +import java.util.List; -import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.issues.common.Issue; +import org.simantics.issues.common.StandardIssue; import org.simantics.issues.ontology.IssueResource; -import org.simantics.layer0.Layer0; import org.simantics.scl.reflection.annotations.SCLValue; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; /** @@ -35,139 +33,47 @@ import org.simantics.sysdyn.SysdynResource; */ public class ExpressionIssueFunction { - private static String getSyntaxErrorLabel(String name) throws DatabaseException { - return "Syntax error in " + name; - } - - private static String getUnsupportedCharacterLabel(String name) throws DatabaseException { - return "Unsupported character(s) in " + name; - } - - private static String getUndefinedExpressionLabel(String name) throws DatabaseException { - return "Undefined expression in " + name; - } + private static String SYNTAX_ERROR = "Syntax error"; + private static String UNSUPPORTED_CHARACTERS = "Unsupported characters"; + private static String UNDEFINED_EXPRESSION = "Undefined expression"; - @SCLValue(type = "a -> b -> c -> d") - public static Object expressionValidator(Object _graph, Object _resource, Object _existing) throws DatabaseException { - - ReadGraph graph = (ReadGraph)_graph; - Resource variable = (Resource)_resource; - Collection existing = (Collection)_existing; + @SCLValue(type = "ReadGraph -> Resource -> [Issue]") + public static List expressionValidator(ReadGraph graph, Resource component) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - if(!graph.isInstanceOf(variable, sr.IndependentVariable)) { - return Boolean.TRUE; + if(!graph.isInstanceOf(component, sr.IndependentVariable)) { + return Collections.emptyList(); } - String name = graph.getRelatedValue(variable, l0.HasName, Bindings.STRING); -System.out.println(name); // Try if there are any errors while parsing the expressions try { - ValidationUtils.getReferences(graph, variable); - } catch (SyntaxErrorException e) { - if(match(graph, existing, variable, getSyntaxErrorLabel(name)) == null) - return Boolean.FALSE; - } catch (UnsupportedCharactersException e) { - if(match(graph, existing, variable, getUnsupportedCharacterLabel(name)) == null) - return Boolean.FALSE; - } catch (UndefinedExpressionException e) { - if(match(graph, existing, variable, getUndefinedExpressionLabel(name)) == null) - return Boolean.FALSE; - } - - Resource context; - // There were no errors -> There should be no expression issues for this variable - for(Resource exist : existing) { - context = graph.getSingleObject(exist, ISSUE.HasIssueContext); - if(!graph.hasStatement(context) || graph.hasStatement(context, l0.PartOf)) - return Boolean.FALSE; - if(variable.equals(context) && graph.isInstanceOf(exist, sr.ExpressionIssue)) { - return Boolean.FALSE; - } + ValidationUtils.getReferences(graph, component); + } catch (Exception e) { + return Collections.singletonList(new StandardIssue(sr.Validations_ExpressionIssue, component)); } - - - return Boolean.TRUE; + return Collections.emptyList(); } - - @SCLValue(type = "a -> b -> c -> d -> e -> f") - public static Object expressionSynchronizer(Object _graph, Object _resource, Object _source, Object _model, Object _existing) throws DatabaseException { - WriteGraph graph = (WriteGraph)_graph; - Resource variable = (Resource)_resource; - Resource model = (Resource)_model; - Resource source = (Resource)_source; - Collection existing = (Collection)_existing; - Layer0 L0 = Layer0.getInstance(graph); + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String expressionIssueDescription(ReadGraph graph, Resource converter, Variable issue) throws DatabaseException { IssueResource ISSUE = IssueResource.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); + Resource issueResource = issue.getRepresents(graph); + Resource list = graph.getSingleObject(issueResource, ISSUE.DependencyIssueSource2_Issue_HasContexts); + List contexts = ListUtils.toList(graph, list); + Resource component = contexts.get(0); - String name = graph.getRelatedValue(variable, L0.HasName, Bindings.STRING); - String label = null; - Resource issue = null; + // There should be an error try { - ValidationUtils.getReferences(graph, variable); + ValidationUtils.getReferences(graph, component); } catch (SyntaxErrorException e) { - label = getSyntaxErrorLabel(name); - if(match(graph, existing, variable, label) == null) - issue = ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Error, sr.ExpressionIssue); + return SYNTAX_ERROR; } catch (UnsupportedCharactersException e) { - label = getUnsupportedCharacterLabel(name); - if(match(graph, existing, variable, label) == null) - issue = ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Error, sr.ExpressionIssue); + return UNSUPPORTED_CHARACTERS; } catch (UndefinedExpressionException e) { - label = getUndefinedExpressionLabel(name); - if(match(graph, existing, variable, label) == null) - issue = ValidationUtils.createIssue(graph, model, source, variable, label, ISSUE.Severity_Error, sr.ExpressionIssue); - } - - Resource context; - Set toBeRemoved = new HashSet(); - for(Resource exist : existing) { - context = graph.getSingleObject(exist, ISSUE.HasIssueContext); - if(!graph.hasStatement(context) || !graph.hasStatement(context, L0.PartOf)) - toBeRemoved.add(exist); - else if(!exist.equals(issue) && graph.isInstanceOf(exist, sr.ExpressionIssue) && variable.equals(context)) { - String l = graph.getRelatedValue(exist, L0.HasLabel); - if(!l.equals(label)) { - toBeRemoved.add(exist); - } - } - } - - for(Resource r : toBeRemoved) { - ValidationUtils.removeIssue(graph, model, source, variable, r, existing); - } - - return Boolean.TRUE; - } - - - @SCLValue(type = "a -> b -> c") - public static Object expressionBaseRealizationFunction(Object _graph, Object _model) throws DatabaseException { - ReadGraph graph = (ReadGraph)_graph; - Resource model = (Resource)_model; - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.isInstanceOf(model, sr.SysdynModel)) - return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); - else if (graph.isInheritedFrom(model, sr.Module)) { - return graph.getSingleObject(model, StructuralResource2.getInstance(graph).IsDefinedBy); - } else return null; - } - - - private static Resource match(ReadGraph graph, Collection existing, Resource variable, String description) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - for(Resource exist : existing) { - Resource source = graph.getSingleObject(exist, ISSUE.HasIssueContext); - String desc = graph.getRelatedValue(exist, L0.HasLabel); - if(source.equals(variable) && desc.equals(description)) return exist; - } - return null; + return UNDEFINED_EXPRESSION; + } + return "Erroneus error"; } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableIssue.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableIssue.java new file mode 100644 index 00000000..8368e095 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableIssue.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * 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: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.validation; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.UUID; + +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.issues.common.StandardIssue; +import org.simantics.issues.ontology.IssueResource; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; + +/** + * Issue class for NoSuchVariableIssues. Extended from StandardIssue to allow + * a string parameter containing the name of the variable in an equation that + * does not correspond to any existing and reachable variable. + * + * @author Teemu Lempinen + * + */ +public class NoSuchVariableIssue extends StandardIssue { + + public String missingVariable; + + public NoSuchVariableIssue(Resource type, String missingVariable, Resource ... contexts) { + super(type, contexts); + this.missingVariable = missingVariable; + } + + /** + * Same as in StandardIssue. Added Validations_NoSuchVariableIssue_variableName. + */ + @Override + public void write(WriteGraph graph, Resource model, Resource source) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + IssueResource IR = IssueResource.getInstance(graph); + Resource issue = graph.newResource(); + graph.claim(issue, L0.InstanceOf, null, type); + graph.claimLiteral(issue, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(issue, IR.DependencyIssueSource2_Issue_HasContexts, ListUtils.create(graph, L0.List, contexts)); + DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + String created = format.format(Calendar.getInstance().getTime()); + graph.claimLiteral(issue, IR.HasCreationTime, created, Bindings.STRING); + graph.claim(source, IR.Manages, issue); + graph.claim(model, L0.ConsistsOf, issue); + + // Modification to standard issue: + graph.claimLiteral(issue, SysdynResource.getInstance(graph).Validations_NoSuchVariableIssue_variableName, this.missingVariable, Bindings.STRING); + } + + @Override + public String toString() { + return Arrays.toString(contexts) + " + missing: " + missingVariable; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java index d81d186e..11eb5f04 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java @@ -12,66 +12,39 @@ package org.simantics.sysdyn.ui.validation; import java.io.StringReader; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; import java.util.Collection; import java.util.HashSet; import java.util.List; -import java.util.UUID; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.RemoverUtil; -import org.simantics.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.expressionParser.TokenMgrError; +/** + * + * @author Teemu Lempinen + * + */ public class ValidationUtils { - public static void removeIssue(WriteGraph graph, Resource model, Resource source, Resource variable, Resource issue, Collection existing) throws DatabaseException { - graph.deny(issue, Layer0.getInstance(graph).PartOf); - graph.deny(source, IssueResource.getInstance(graph).Manages, issue); - RemoverUtil.remove(graph, issue); - existing.remove(issue); - } - - public static Resource createIssue(WriteGraph graph, Resource model, Resource source, Resource variable, String label) throws DatabaseException { - return createIssue(graph, model, source, variable, label, IssueResource.getInstance(graph).Severity_Error); - } - - - public static Resource createIssue(WriteGraph graph, Resource model, Resource source, Resource variable, String label, Resource severity) throws DatabaseException { - return createIssue(graph, model, source, variable, label, severity, SysdynResource.getInstance(graph).SysdynIssue); - } - - - public static Resource createIssue(WriteGraph graph, Resource model, Resource source, Resource variable, String label, Resource severity, Resource type) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - - Resource issue = graph.newResource(); - graph.claim(issue, L0.InstanceOf, null, type); - graph.claim(issue, ISSUE.HasIssueContext, null, variable); - graph.claim(issue, ISSUE.HasSeverity, severity); - graph.claimLiteral(issue, L0.HasLabel, label, Bindings.STRING); - graph.claimLiteral(issue, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING); - DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); - String created = format.format(Calendar.getInstance().getTime()); - graph.claimLiteral(issue, ISSUE.HasCreationTime, created, Bindings.STRING); - graph.claim(source, ISSUE.Manages, issue); - graph.claim(model, L0.ConsistsOf, issue); - return issue; - } - + /** + * Find all variables that are referred to in the expressions of variable r + * @param graph ReadGraph + * @param r Variable resource + * @return All names of the variables that are referred to in the expressions of r + * @throws DatabaseException + * @throws SyntaxErrorException + * @throws UnsupportedCharactersException + * @throws UndefinedExpressionException + */ public static HashSet getReferences(ReadGraph graph, Resource r) throws DatabaseException, SyntaxErrorException, UnsupportedCharactersException, UndefinedExpressionException { HashSet references = new HashSet(); ExpressionParser parser = new ExpressionParser(new StringReader("")); @@ -105,6 +78,13 @@ public class ValidationUtils { return references; } + /** + * Get all expressions of a variable r + * @param graph ReadGraph + * @param r Variable with expressions + * @return List of expression (resources) + * @throws DatabaseException + */ private static List getExpressions(ReadGraph graph, Resource r) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Resource hasExpressions = graph.getPossibleObject(r, sr.HasExpressions); @@ -155,6 +135,22 @@ public class ValidationUtils { * @throws DatabaseException */ public static boolean isReachable(ReadGraph graph, Resource variable, String reference) throws DatabaseException { + if(reach(graph, variable, reference) != null) + return true; + else + return false; + } + + /** + * Find a resource starting from variable and using reference path + * + * @param graph ReadGraph + * @param variable starting point + * @param reference path to another variable + * @return found variable or null + * @throws DatabaseException + */ + public static Resource reach(ReadGraph graph, Resource variable, String reference) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); Resource configuration = graph.getSingleObject(variable, l0.PartOf); @@ -162,8 +158,9 @@ public class ValidationUtils { for(Resource var : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Variable))) { varName = graph.getRelatedValue(var, l0.HasName, Bindings.STRING); if(varName != null && reference.equals(varName)) - return true; + return var; } - return false; + return null; } + }