From: lempinen Date: Fri, 7 Oct 2011 13:46:18 +0000 (+0000) Subject: Issue view update: more checks working for dependencies. Issues are not updated when... X-Git-Tag: simantics-1.5~15 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=3d5ba4ed7b6f0d79ea02f44eca51d13b03663510;p=simantics%2Fsysdyn.git Issue view update: more checks working for dependencies. Issues are not updated when simply changing an equation. You need to change also the equation type. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22573 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index bb64744d..75a53625 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/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 5be5fa7c..8592edaf 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -208,10 +208,10 @@ SYSDYN.RefersTo +L0X = ISSUE = SYSDYN = @@ -6,5 +7,18 @@ VALIDATIONS = SYSDYN.Validations : L0.Library SYSDYN.SysdynIssue - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index f6fb712b..bec90398 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -1529,4 +1529,10 @@ name="Export Function Library"> + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java index ca0af77e..377d91d6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java @@ -13,7 +13,6 @@ package org.simantics.sysdyn.ui.project; import java.io.File; import java.util.HashMap; -import java.util.Map; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.preferences.IEclipsePreferences; @@ -26,19 +25,21 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; -import org.simantics.db.common.procedure.single.SingleSetSyncListener; +import org.simantics.db.common.changeset.GenericChangeListener; import org.simantics.db.common.request.Queries; +import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.RuntimeValuations; import org.simantics.db.layer0.adapter.TrendVariable; +import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest; +import org.simantics.db.layer0.genericrelation.DependencyChanges; import org.simantics.db.layer0.service.ActivationManager; import org.simantics.db.request.Read; import org.simantics.db.service.GraphChangeListenerSupport; import org.simantics.db.service.LifecycleSupport; import org.simantics.db.service.VirtualGraphSupport; -import org.simantics.issues.common.IssueSource; +import org.simantics.issues.common.IssueUtil; import org.simantics.layer0.Layer0; import org.simantics.modeling.services.CaseInsensitiveComponentNamingStrategy2; import org.simantics.modeling.services.ComponentNamingStrategy; @@ -54,7 +55,6 @@ import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.editor.SysdynEditorNamingService; -import org.simantics.sysdyn.ui.validation.ActiveIssueSources; import org.simantics.ui.workbench.IEditorNamingService; import org.simantics.ui.workbench.action.ChooseActionRequest; import org.simantics.ui.workbench.project.UIModelManager; @@ -196,76 +196,16 @@ public class SysdynProject extends AbstractProjectFeature { // Issues - try { - - session.syncRequest(new ActiveIssueSources(project.get()), new SingleSetSyncListener() { - - Map sources = new HashMap(); - Map listeners = new HashMap(); - - @Override - public void add(ReadGraph graph, final Resource source) throws DatabaseException { - final IssueSource is = graph.adapt(source, IssueSource.class); - if(is != null) { - - Runnable listener = new Runnable() { - - @Override - public void run() { - - project.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - is.update(graph); - } - - }); - - } - - }; - - is.addDirtyListener(listener); - sources.put(source, is); - listeners.put(source, listener); - - } else { - - System.err.println("Issue source " + source + " does not have adapter!"); - - } - - } - + session.syncRequest(new ReadRequest() { @Override - public void remove(ReadGraph graph, final Resource source) throws DatabaseException { - - IssueSource is = sources.get(source); - Runnable listener = listeners.get(source); - if(is != null && listener != null) { - is.removeDirtyListener(listener); - } - sources.remove(source); - - } - - @Override - public void exception(ReadGraph graph, Throwable t) { - Logger.defaultLogError(t); + public void run(ReadGraph graph) throws DatabaseException { + onActivated(graph, getProject()); } - - @Override - public boolean isDisposed() { - return false; - } - }); } catch (DatabaseException e) { - e.printStackTrace(); + throw new ProjectException(e); } - } @Override @@ -339,5 +279,24 @@ public class SysdynProject extends AbstractProjectFeature { } } } + + public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException { + + GraphChangeListenerSupport changeSupport = graph.getService(GraphChangeListenerSupport.class); + changeSupport.addMetadataListener(new GenericChangeListener() { + + @Override + public void onEvent(ReadGraph graph, DependencyChanges event) throws DatabaseException { + + WriteGraph w = (WriteGraph)graph; + w.addMetadata(event); + + } + + }); + + + IssueUtil.listenActiveProjectIssueSources(graph, project.get()); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index ba68d2c9..ea0808eb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -198,6 +198,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { graph.claimLiteral(expression, sr.HasInitialEquation, ""); } OrderedSetUtils.add(graph, expressions, expression); + graph.claim(variable, l0.ConsistsOf, expression); VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); final Session session = graph.getSession(); @@ -305,6 +306,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); if(activeExpression == null) return; @@ -315,6 +318,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { Resource prev = OrderedSetUtils.prev(graph, expressionList, activeExpression); OrderedSetUtils.remove(graph, expressionList, activeExpression); + graph.deny(input, l0.ConsistsOf, activeExpression); + if(prev.equals(expressionList)) { Iterator iterator = OrderedSetUtils.iterator(graph, expressionList); prev = iterator.next(); @@ -365,6 +370,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { graph.claim(newExpression, l0.InstanceOf, sr.NormalExpression); } OrderedSetUtils.add(graph, expressions, newExpression); + graph.claim(input, l0.ConsistsOf, newExpression); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java index e36ffbad..18083697 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java @@ -36,6 +36,7 @@ import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.utils.ExpressionUtils; @@ -125,6 +126,8 @@ public class BasicExpression implements IExpression { public void perform(WriteGraph g) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); + Layer0 l0 = Layer0.getInstance(g); + if(ExpressionUtils.isParameter(currentText)) { if(!expressionType.equals(sr.ConstantExpression)) expressionType = sr.ParameterExpression; @@ -146,18 +149,21 @@ public class BasicExpression implements IExpression { if(arrayRange != null) g.claimLiteral(newExpression, sr.HasArrayRange, arrayRange); + final Resource variable = g.getSingleObject(ownerList, sr.HasExpressions_Inverse); OrderedSetUtils.replace(g, ownerList, expression, newExpression); + g.deny(expression, l0.PartOf); + g.claim(newExpression, l0.PartOf, variable); + - final Resource activefor = g.getPossibleObject(expression, sr.HasActiveExpression_Inverse); VirtualGraph runtime = g.getService(VirtualGraph.class); g.syncRequest(new WriteRequest(runtime) { @Override public void perform(WriteGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - if(activefor != null) { - if(graph.hasStatement(activefor, sr.HasActiveExpression)) - graph.deny(activefor, sr.HasActiveExpression); - graph.claim(activefor, sr.HasActiveExpression, newExpression); + if(variable != null) { + if(graph.hasStatement(variable, sr.HasActiveExpression)) + graph.deny(variable, sr.HasActiveExpression); + graph.claim(variable, sr.HasActiveExpression, newExpression); } } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java index fbee902c..4f9698b3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java @@ -147,7 +147,11 @@ public class StockExpression implements IExpression { g.deny(expression, predicate); } g.claim(expression, l0.InstanceOf, null, sr.StockExpression); - OrderedSetUtils.replace(g, expressionList, temp, expression); + + Resource variable = g.getSingleObject(expressionList, sr.HasExpressions_Inverse); + OrderedSetUtils.replace(g, expressionList, temp, expression); + g.deny(expression, l0.PartOf); + g.claim(expression, l0.PartOf, variable); } g.claimLiteral(expression, sr.HasInitialEquation, currentText); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index c60fca19..9af73a64 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -57,6 +57,7 @@ import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.tableParser.ParseException; @@ -265,6 +266,8 @@ public class WithLookupExpression implements IExpression { public void perform(WriteGraph g) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); + Layer0 l0 = Layer0.getInstance(g); + if(!g.isInstanceOf(expr, sr.WithLookupExpression)) { @@ -278,16 +281,19 @@ public class WithLookupExpression implements IExpression { if(arrayRange != null) g.claimLiteral(newExpression, sr.HasArrayRange, arrayRange); - OrderedSetUtils.replace(g, ownerList, expr, newExpression); - final Resource activefor = g.getPossibleObject(expression, sr.HasActiveExpression_Inverse); + final Resource variable = g.getSingleObject(ownerList, sr.HasExpressions_Inverse); + OrderedSetUtils.replace(g, ownerList, expression, newExpression); + g.deny(expression, l0.PartOf); + g.claim(newExpression, l0.PartOf, variable); + VirtualGraph runtime = g.getService(VirtualGraph.class); g.syncRequest(new WriteRequest(runtime) { @Override public void perform(WriteGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.hasStatement(activefor, sr.HasActiveExpression)) - graph.deny(activefor, sr.HasActiveExpression); - graph.claim(activefor, sr.HasActiveExpression, newExpression); + if(graph.hasStatement(variable, sr.HasActiveExpression)) + graph.deny(variable, sr.HasActiveExpression); + graph.claim(variable, sr.HasActiveExpression, newExpression); } } ); 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 f1fb5d89..088d67fe 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,9 +73,14 @@ public class ModelUtils { l0.PartOf, project, l0.HasName, modelName, l0.HasLabel, modelName, - L0X.IsActivatedBy, project, - L0X.Activates, sr.Validations_DependencyConnections - ); + L0X.IsActivatedBy, project + ); + + GraphUtils.create2(g, + sr.Validations_DependencyConnectionsIssueSource, + L0X.IsActivatedBy, model, + l0.PartOf, model + ); Resource conf = GraphUtils.create2(g, sr.Configuration, diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ActiveIssueSources.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ActiveIssueSources.java deleted file mode 100644 index 06229d9a..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ActiveIssueSources.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.simantics.sysdyn.ui.validation; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.ResourceRead; -import org.simantics.db.exception.DatabaseException; -import org.simantics.issues.ontology.IssueResource; -import org.simantics.layer0.Layer0; -import org.simantics.operation.Layer0X; -import org.simantics.sysdyn.SysdynResource; - -public class ActiveIssueSources extends ResourceRead> { - - Resource project; - - public ActiveIssueSources(Resource project) { - super(project); - this.project = project; - } - - @Override - public Set perform(ReadGraph graph) throws DatabaseException { - - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - Layer0X l0X = Layer0X.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - - HashSet result = new HashSet(); - - for(Resource model : graph.syncRequest(new ObjectsWithType(project, l0.ConsistsOf, sr.SysdynModel))) { - result.addAll(graph.syncRequest(new ObjectsWithType(model, l0X.Activates, ISSUE.IssueSource))); - } - - return result; - } - -} 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 new file mode 100644 index 00000000..e7190584 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java @@ -0,0 +1,261 @@ +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; + } + + private static String getUnusedDependencyLabel(String name, String dependency) throws DatabaseException { + String label = "Unused dependency " + dependency + " in " + name; + return label; + } + + /** + * + * One issue is enough. The first encounter of a new issue returns Boolean.FALSE. + * + * @param _graph + * @param _resource + * @param _existing + * @return + * @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; + + Set references = getReferences(graph, variable); + Set dependencies = getDependencies(graph, variable); + + 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; + } + + + /* + * 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); + + for(Resource exist : existing) { + if(variable.equals(graph.getSingleObject(exist, ISSUE.HasIssueContext))) { + return Boolean.FALSE; + } + } + System.out.println("NOTHING"); + 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); + + Set references = getReferences(graph, variable); + Set dependencies = getDependencies(graph, variable); + + + String name = graph.getRelatedValue(variable, L0.HasName, Bindings.STRING); + + System.out.println("SOME CHANGE IN " + name + "(existing: " + existing.size() + ")"); + + Set labels = new HashSet(); + String label; + Resource issue; + + 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); + } + } + + /*else { + if((issue = match(graph, existing, variable, label)) != null) { + removeIssue(graph, model, source, variable, issue, existing); + } + }*/ + } + + Set toBeRemoved = new HashSet(); + 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)) + toBeRemoved.add(exist); + } + + for(Resource r : toBeRemoved) { + 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 { + System.out.println("CREATE ISSUE: " + label); + 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_Error); + 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 { + System.out.println("REMOVE ISSUE: " + graph.getRelatedValue(issue, Layer0.getInstance(graph).HasLabel)); + 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 { + 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.startsWith(description.substring(0, description.lastIndexOf(" ")))) return exist; + if(source.equals(variable) && desc.equals(description)) return exist; + } + 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/DependencyIssueSource.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyIssueSource.java deleted file mode 100644 index 93b75a54..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyIssueSource.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.simantics.sysdyn.ui.validation; - -import java.util.concurrent.CopyOnWriteArraySet; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.changeset.GenericChangeListener; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest; -import org.simantics.db.layer0.genericrelation.DependencyChanges; -import org.simantics.db.layer0.util.Simantics; -import org.simantics.db.service.GraphChangeListenerSupport; -import org.simantics.issues.common.IssueSource; -import org.simantics.issues.common.IssueSourceUtils; -import org.simantics.operation.Layer0X; -import org.simantics.sysdyn.SysdynResource; - -public class DependencyIssueSource implements IssueSource { - - private Resource source; - private Resource model; - private Resource baseRealization; - - private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet(); - private boolean tracking = false; - - public DependencyIssueSource(ReadGraph graph, Resource source) throws DatabaseException { - Layer0X L0X = Layer0X.getInstance(graph); - this.source = source; - this.model = graph.getSingleObject(source, L0X.IsActivatedBy); - this.baseRealization = graph.getSingleObject(model, L0X.HasBaseRealization); - } - - @Override - public void addDirtyListener(Runnable runnable) { - - boolean added = listeners.add(runnable); - if(added && !tracking) { - - GraphChangeListenerSupport changeSupport = Simantics.getSession().getService(GraphChangeListenerSupport.class); - changeSupport.addMetadataListener(new GenericChangeListener() { - - @Override - public void onEvent(ReadGraph graph, DependencyChanges event) throws DatabaseException { - if(IssueSourceUtils.hasChanges(graph, event, model, baseRealization)) { - for(Runnable r : listeners) { - r.run(); - } - } - - } - - }); - - } - - } - - @Override - public void removeDirtyListener(Runnable runnable) { - listeners.remove(runnable); - } - - @Override - public void update(WriteGraph graph) throws DatabaseException { - - SysdynResource sr = SysdynResource.getInstance(graph); - - for(Resource unit : IssueSourceUtils.getChangedDependencies(graph, source, model, baseRealization, sr.Variable)) { - if(!graph.syncRequest(new DependencyValidator(unit, model))) new DependencySynchronizer(unit, model).perform(graph); - } - - IssueSourceUtils.claimUpdated(graph, source); - - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyIssues.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyIssues.java deleted file mode 100644 index 3ed195ab..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyIssues.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.simantics.sysdyn.ui.validation; - -import java.util.HashSet; -import java.util.Set; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceRead; -import org.simantics.db.exception.DatabaseException; -import org.simantics.issues.common.AllVisibleIssues; -import org.simantics.issues.ontology.IssueResource; - -public class DependencyIssues extends ResourceRead>{ - - public DependencyIssues(Resource resource) { - super(resource); - } - - @Override - public Set perform(ReadGraph graph) throws DatabaseException { - IssueResource ISSUE = IssueResource.getInstance(graph); - HashSet result = new HashSet(); - for (Resource issue : graph.syncRequest(new AllVisibleIssues(resource))) { - if (ISSUE.Severity_Error.equals(graph.getPossibleObject(issue, ISSUE.HasSeverity))) - result.add(issue); - } - return result; - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencySynchronizer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencySynchronizer.java deleted file mode 100644 index 84be096b..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencySynchronizer.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.simantics.sysdyn.ui.validation; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -import org.simantics.databoard.Bindings; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.issues.ontology.IssueResource; -import org.simantics.layer0.Layer0; -import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.utils.datastructures.Pair; - -public class DependencySynchronizer extends WriteRequest { - public static final boolean DEBUG = true; - - final private Resource unit; - final private Resource model; - - public DependencySynchronizer(Resource unit, Resource model) { - this.unit = unit; - this.model = model; - } - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - StructuralResource2 sr = StructuralResource2.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - - if(DEBUG) System.out.println("Running DependencySynchronizer for " + unit); - - Set set = graph.syncRequest(new DependencyIssues(model)); - - if(DEBUG) System.out.println(set.size() + " issues"); - - /* - Collection removed = new HashSet(); - - boolean unitExists = graph.hasStatement(unit); - if(unitExists) { - - removed.addAll(map.values()); - - for(Resource predicate : graph.getPredicates(unit)) { - if(graph.isSubrelationOf(predicate, FB.Parameter)) { - boolean hasIssue = false; - Resource literal = graph.getPossibleObject(unit, predicate); - Boolean required = graph.getPossibleRelatedValue(literal, FB.IsRequired, Bindings.BOOLEAN); - if(required != null && required && !DependencyValidator.excluded(graph, unit)) { - Boolean def = graph.getPossibleRelatedValue(literal, FB.IsDefault, Bindings.BOOLEAN); - if(def != null && def) { - hasIssue = true; - } - } - Resource issue = map.get(Pair.make(unit, predicate)); - if(hasIssue != (issue != null)) { - - if(hasIssue) { - - issue = graph.newResource(); - graph.claim(issue, L0.InstanceOf, null, FB.ParameterIssue); - graph.claim(issue, ISSUE.HasIssueContext, null, unit); - graph.claim(issue, ISSUE.HasIssueContext, null, predicate); - graph.claim(issue, ISSUE.HasSeverity, ISSUE.Severity_Error); - graph.claimValue(issue, L0.HasDescription, "= Missing connection =", Bindings.STRING); - graph.claimValue(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.claimValue(issue, ISSUE.HasCreationTime, created, Bindings.STRING); - graph.claim(model, L0.ConsistsOf, issue); - - } else { - - graph.deny(issue, L0.PartOf); - - } - - } - - } - } - - } else { - - for(Map.Entry, Resource> entry : map.entrySet()) { -// System.err.println("entry " + entry.getKey()); - if(entry.getKey().first.equals(unit)) { - graph.deny(entry.getValue(), L0.PartOf); - } - if(entry.getKey().first == null) { - graph.deny(entry.getValue(), L0.PartOf); - } - } - - } -*/ - - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyValidator.java deleted file mode 100644 index ffac712a..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyValidator.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.simantics.sysdyn.ui.validation; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import org.simantics.databoard.Bindings; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceRead2; -import org.simantics.db.exception.DatabaseException; -import org.simantics.issues.common.impl.DependencyIssue; -import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.utils.datastructures.Pair; - -public class DependencyValidator extends ResourceRead2 { - - public static final boolean DEBUG = true; - - public DependencyValidator(Resource resource, Resource resource2) { - super(resource, resource2); - } - - static boolean excluded(ReadGraph graph, Resource component) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - return !graph.isInstanceOf(component, sr.Variable); - } - - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - if(DEBUG) System.out.println("Running DependencyValidator for " + resource); - - //if(!graph.hasStatement(resource, L0.Represents)) return false; - - if(!graph.hasStatement(resource)) return false; - - Set resources = graph.syncRequest(new DependencyIssues(resource2)); - if(DEBUG) System.out.println(resources.size() + " resources"); - - /* - Set resources = graph.syncRequest(new DependencyIssues(resource2)); - HashMap, Resource> existing = new HashMap, Resource>(existing_); - - for(Resource predicate : graph.getPredicates(resource)) { - if(graph.isSubrelationOf(predicate, FB.Parameter)) { - boolean hasIssue = false; - Resource literal = graph.getPossibleObject(resource, predicate); - Boolean required = graph.getPossibleRelatedValue(literal, FB.IsRequired, Bindings.BOOLEAN); - if(required != null && required && !excluded(graph, resource)) { - Boolean def = graph.getPossibleRelatedValue(literal, FB.IsDefault, Bindings.BOOLEAN); - if(def != null && def) { - hasIssue = true; - return false; - } - } - if(hasIssue != existing.containsKey(Pair.make(resource, predicate))) return false; - } - } - */ - - return true; - - } - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java index 40528043..126dd35e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Variable.java @@ -53,17 +53,6 @@ public abstract class Variable extends Element { getExpressions().add(e); } - for(Expression e : getExpressions()) { - - // Get expression from the variable. They have different types - Resource expression = getExpression(graph, e); - - if(e.getRange() != null) { - graph.claimLiteral(expression, sr.HasArrayRange, "[" + e.getRange().trim() + "]"); - } - OrderedSetUtils.add(graph, expressionList, expression); - } - Resource arrayIndexList = OrderedSetUtils.create(graph, sr.ArrayIndexes); @@ -72,11 +61,22 @@ public abstract class Variable extends Element { l0.HasName, ImportUtils.escapeName(name), sr.HasExpressions, expressionList); graph.claim(variable, mr.Mapped, variable); - - + + for(Expression e : getExpressions()) { + + // Get expression from the variable. They have different types + Resource expression = getExpression(graph, e); + + if(e.getRange() != null) { + graph.claimLiteral(expression, sr.HasArrayRange, "[" + e.getRange().trim() + "]"); + } + OrderedSetUtils.add(graph, expressionList, expression); + graph.claim(variable, l0.ConsistsOf, expression); + } + if(subscripts != null) { - for(Subscript sub : subscripts) { - if(sub.getResource() != null) + for(Subscript sub : subscripts) { + if(sub.getResource() != null) OrderedSetUtils.add(graph, arrayIndexList, sub.getResource()); } graph.claim(variable, sr.HasArrayIndexes, arrayIndexList);