From: lempinen Date: Fri, 14 Oct 2011 14:03:10 +0000 (+0000) Subject: More Validations and separated validations for dependencies and expressions X-Git-Tag: simantics-1.5~10 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=4d2765927aca9ba98ca0b4026a9bcadb175add56;p=simantics%2Fsysdyn.git More Validations and separated validations for dependencies and expressions git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22745 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index a8cc35be..14b2d4f3 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/Validation.pgraph b/org.simantics.sysdyn.ontology/graph/Validation.pgraph index 7ee8e1ff..3ff190ea 100644 --- a/org.simantics.sysdyn.ontology/graph/Validation.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Validation.pgraph @@ -7,18 +7,32 @@ VALIDATIONS = SYSDYN.Validations : L0.Library SYSDYN.SysdynIssue - + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java index cfa0f15f..3ce5b532 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java @@ -73,7 +73,13 @@ public class ModelUtils { ); GraphUtils.create2(g, - sr.Validations_DependencyConnectionsIssueSource, + sr.Validations_Dependencies_DependencyConnectionsIssueSource, + L0X.IsActivatedBy, model, + l0.PartOf, model + ); + + GraphUtils.create2(g, + sr.Validations_Expressions_ExpressionIssueSource, L0X.IsActivatedBy, model, l0.PartOf, model ); 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 edd32658..a6431ad4 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 @@ -1,44 +1,35 @@ 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.Set; -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.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.scl.reflection.annotations.SCLValue; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.expressionParser.ExpressionParser; -import org.simantics.sysdyn.expressionParser.ParseException; -import org.simantics.sysdyn.expressionParser.TokenMgrError; public class DependencyFunction { private static String getMissingLinkLabel(String name, String dependency) throws DatabaseException { - String label = "Missing link " + dependency + " in " + name; - return label; + return "Missing link " + dependency + " in " + name; } - private static String getUnusedDependencyLabel(String name, String dependency) throws DatabaseException { - String label = "Unused dependency " + dependency + " in " + name; - return label; + 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; + } + /** * * One issue is enough. The first encounter of a new issue returns Boolean.FALSE. @@ -55,38 +46,51 @@ public class DependencyFunction { ReadGraph graph = (ReadGraph)_graph; Resource variable = (Resource)_resource; Collection existing = (Collection)_existing; - - Set references = getReferences(graph, variable); - Set dependencies = getDependencies(graph, variable); + if(!graph.hasStatement(variable) || !graph.hasStatement(variable, Layer0.getInstance(graph).PartOf)) + return Boolean.FALSE; + String name = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName, Bindings.STRING); - - for(String reference : references) { - if(!dependencies.contains(reference) && match(graph, existing, variable, getMissingLinkLabel(name, reference)) == null) - return Boolean.FALSE; - } - - for(String dependency : dependencies) { - if(!references.contains(dependency) && match(graph, existing, variable, getUnusedDependencyLabel(name, dependency)) == null) - return Boolean.FALSE; + + Set references; + Set dependencies; + try { + references = ValidationUtils.getReferences(graph, variable); + dependencies = ValidationUtils.getDependencies(graph, variable); + + + for(String reference : references) { + if(!dependencies.contains(reference) && match(graph, existing, variable, getMissingLinkLabel(name, reference)) == null) + return Boolean.FALSE; + } + + for(String dependency : dependencies) { + if(!references.contains(dependency) && match(graph, existing, variable, getUnusedDependencyLabel(name, dependency)) == null) + return Boolean.FALSE; + } + } catch (SyntaxErrorException e) { + } catch (UnsupportedCharactersException e) { + } catch (UndefinedExpressionException e) { } - - /* - * See if there are any unnecessary issues in existing - * - * FIXME: Currently there are no other issues, so this can check ALL issues. - */ IssueResource ISSUE = IssueResource.getInstance(graph); - + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource context; for(Resource exist : existing) { - if(variable.equals(graph.getSingleObject(exist, ISSUE.HasIssueContext))) { + 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; } } + 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 { @@ -96,87 +100,72 @@ public class DependencyFunction { Resource source = (Resource)_source; Collection existing = (Collection)_existing; Layer0 L0 = Layer0.getInstance(graph); - - Set references = getReferences(graph, variable); - Set dependencies = getDependencies(graph, variable); - + 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; - for(String reference : references) { - label = getMissingLinkLabel(name, reference); - if(!dependencies.contains(reference)) { - labels.add(label); - if(match(graph, existing, variable, label) == null) { - createIssue(graph, model, source, variable, label); - } - } - /*else { - if((issue = match(graph, existing, variable, label)) != null) { - removeIssue(graph, model, source, variable, issue, existing); - } - }*/ - } - - for(String dependency : dependencies) { - label = getUnusedDependencyLabel(name, dependency); - if(!references.contains(dependency)) { - labels.add(label); - if(match(graph, existing, variable, label) == null) { - createIssue(graph, model, source, variable, label); - } + Set references; + Set dependencies; + try { + references = ValidationUtils.getReferences(graph, variable); + dependencies = ValidationUtils.getDependencies(graph, variable); + 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); + } + } 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); + } + } + } } - - /*else { - if((issue = match(graph, existing, variable, label)) != null) { - removeIssue(graph, model, source, variable, issue, existing); + + for(String dependency : dependencies) { + label = getUnusedDependencyLabel(name, dependency); + if(!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); + } } - }*/ + } + } catch (SyntaxErrorException e) { + } catch (UnsupportedCharactersException e) { + } catch (UndefinedExpressionException e) { } - + Set toBeRemoved = new HashSet(); + Resource context; for(Resource exist : existing) { - String l = graph.getRelatedValue(exist, L0.HasLabel); - Resource i = graph.getSingleObject(exist, IssueResource.getInstance(graph).HasIssueContext); - if(variable.equals(i) && !labels.contains(l)) + 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) { - removeIssue(graph, model, source, variable, r, existing); + ValidationUtils.removeIssue(graph, model, source, variable, r, existing); } - + return Boolean.TRUE; } - - private static Resource createIssue(WriteGraph graph, Resource model, Resource source, Resource variable, String label) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - - Resource issue = graph.newResource(); - graph.claim(issue, L0.InstanceOf, null, ISSUE.Issue); - graph.claim(issue, ISSUE.HasIssueContext, null, variable); - graph.claim(issue, ISSUE.HasSeverity, ISSUE.Severity_Warning); - 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; - } - - private 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); - existing.remove(issue); - } @SCLValue(type = "a -> b -> c") public static Object dependencyBaseRealizationFunction(Object _graph, Object _model) throws DatabaseException { @@ -198,58 +187,5 @@ public class DependencyFunction { } return null; } - - // Returns the names of the related variables (dependencies) - private static HashSet getDependencies(ReadGraph graph, Resource r) throws DatabaseException { - HashSet variables = new HashSet(); - if(graph != null && r != null) { - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - - Collection dependencies = graph.getObjects(r, sr.IsHeadOf); - - for(Resource d : dependencies) { - if(graph.isInstanceOf(d, sr.Dependency)) { - Resource tail = graph.getPossibleObject(d, sr.HasTail); - if(tail != null) { - Object name = graph.getPossibleRelatedValue(tail, l0.HasName); - if(name != null) - variables.add((String)name); - } - } - } - } - return variables; - } - - private static HashSet getReferences(ReadGraph graph, Resource r) throws DatabaseException { - HashSet references = new HashSet(); - ExpressionParser parser = new ExpressionParser(new StringReader("")); - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - Resource hasExpressions = graph.getPossibleObject(r, sr.HasExpressions); - if(hasExpressions != null){ - List expressionList = OrderedSetUtils.toList(graph, hasExpressions); - for(Resource expression : expressionList) { - for(Resource s : graph.syncRequest(new ObjectsWithType(expression, sr.HasEquation, l0.String))) { - String value = graph.getValue(s, Bindings.STRING); - parser.ReInit(new StringReader(value)); - try { - parser.expr(); - references.addAll(parser.getReferences().keySet()); - } catch (ParseException e1) { - // TODO: Issue - System.out.println("SYNTAX ERROR"); -// ef.setSyntaxError(e1.currentToken, "Syntax Error"); - } catch (TokenMgrError err) { - // TODO: Issue - System.out.println("UNSUPPORTED CHARACTERS"); -// ef.setSyntaxError(0, textString.length(), ExpressionField.SYNTAX_ERROR, "Expression contains unsupported characters"); - } - } - } - } - return references; - } } 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 new file mode 100644 index 00000000..344310fb --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ExpressionIssueFunction.java @@ -0,0 +1,151 @@ +package org.simantics.sysdyn.ui.validation; + +import java.util.Collection; +import java.util.HashSet; +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.exception.DatabaseException; +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.sysdyn.SysdynResource; + +/** + * Detects syntax errors, unsupported characters and undefined expressions from variables. + * + * @author tlteemu + * + */ +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; + } + + @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; + String name = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName, Bindings.STRING); + + // 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; + } + + SysdynResource sr = SysdynResource.getInstance(graph); + IssueResource ISSUE = IssueResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + 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; + } + } + + + return Boolean.TRUE; + } + + + @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); + IssueResource ISSUE = IssueResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + String name = graph.getRelatedValue(variable, L0.HasName, Bindings.STRING); + String label = null; + Resource issue = null; + try { + ValidationUtils.getReferences(graph, variable); + } 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); + } 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); + } 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; + return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); + } + + + 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; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableException.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableException.java new file mode 100644 index 00000000..668ddee4 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/NoSuchVariableException.java @@ -0,0 +1,6 @@ +package org.simantics.sysdyn.ui.validation; + +public class NoSuchVariableException extends RuntimeException { + private static final long serialVersionUID = -5766352512554068379L; + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/SyntaxErrorException.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/SyntaxErrorException.java new file mode 100644 index 00000000..8d577c98 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/SyntaxErrorException.java @@ -0,0 +1,7 @@ +package org.simantics.sysdyn.ui.validation; + +public class SyntaxErrorException extends RuntimeException { + private static final long serialVersionUID = -6466653179001958636L; + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UndefinedExpressionException.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UndefinedExpressionException.java new file mode 100644 index 00000000..bffa074e --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UndefinedExpressionException.java @@ -0,0 +1,6 @@ +package org.simantics.sysdyn.ui.validation; + +public class UndefinedExpressionException extends RuntimeException { + private static final long serialVersionUID = 7352486116119189105L; + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnsupportedCharactersException.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnsupportedCharactersException.java new file mode 100644 index 00000000..e96b6096 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnsupportedCharactersException.java @@ -0,0 +1,6 @@ +package org.simantics.sysdyn.ui.validation; + +public class UnsupportedCharactersException extends RuntimeException { + private static final long serialVersionUID = 8210873686720503188L; + +} 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 new file mode 100644 index 00000000..349e0303 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java @@ -0,0 +1,152 @@ +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.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; + +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); + 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; + } + + public static HashSet getReferences(ReadGraph graph, Resource r) throws DatabaseException, SyntaxErrorException, UnsupportedCharactersException, UndefinedExpressionException { + HashSet references = new HashSet(); + ExpressionParser parser = new ExpressionParser(new StringReader("")); + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + List expressionList = getExpressions(graph, r); + if(expressionList == null || expressionList.isEmpty()) + throw new UndefinedExpressionException(); + for(Resource expression : expressionList) { + for(Resource s : graph.syncRequest(new ObjectsWithType(expression, sr.HasEquation, l0.String))) { + String value = graph.getValue(s, Bindings.STRING); + if(value.length() == 0) + throw new UndefinedExpressionException(); + + parser.ReInit(new StringReader(value)); + try { + parser.expr(); + references.addAll(parser.getReferences().keySet()); + } catch (ParseException e1) { + throw new SyntaxErrorException(); + } catch (TokenMgrError err) { + throw new UnsupportedCharactersException(); + } + } + } + return references; + } + + private static List getExpressions(ReadGraph graph, Resource r) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource hasExpressions = graph.getPossibleObject(r, sr.HasExpressions); + if(hasExpressions != null) + return OrderedSetUtils.toList(graph, hasExpressions); + else + return null; + } + + + /** + * Returns the names of the related variables (dependencies) + * + * @param graph + * @param r + * @return + * @throws DatabaseException + */ + public static HashSet getDependencies(ReadGraph graph, Resource r) throws DatabaseException { + HashSet variables = new HashSet(); + if(graph != null && r != null) { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Collection dependencies = graph.getObjects(r, sr.IsHeadOf); + + for(Resource d : dependencies) { + if(graph.isInstanceOf(d, sr.Dependency)) { + Resource tail = graph.getPossibleObject(d, sr.HasTail); + if(tail != null) { + Object name = graph.getPossibleRelatedValue(tail, l0.HasName); + if(name != null) + variables.add((String)name); + } + } + } + } + return variables; + } + + /** + * Is reference reachable from variable + * + * @param graph + * @param variable + * @param reference + * @return + * @throws DatabaseException + */ + public static boolean isReachable(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); + String varName; + 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 false; + } +}