From: lempinen Date: Tue, 2 Nov 2010 14:11:03 +0000 (+0000) Subject: First take on Enumerations in System Dynamics. X-Git-Tag: simantics-1.2.1~7 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=b7f8a2b6c6018cc06b06eaf0dee1641e62b66643;p=simantics%2Fsysdyn.git First take on Enumerations in System Dynamics. Enumerations can be created to Configurations and deleted from the model browser. Properties of the enumerations can be modified from the property view (add & remove indexes). Enumeration is added to the modelica code, but it cannot be used yet. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18557 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedOrderedSetElements.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedOrderedSetElements.java new file mode 100644 index 00000000..21b3e171 --- /dev/null +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedOrderedSetElements.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 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.objmap.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.simantics.objmap.annotations.factories.RelatedOrderedSetElementsRuleFactory; +import org.simantics.objmap.annotations.meta.HasFieldRuleFactory; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@HasFieldRuleFactory(RelatedOrderedSetElementsRuleFactory.class) +public @interface RelatedOrderedSetElements { + boolean composition() default false; +} diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedOrderedSetElementsRuleFactory.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedOrderedSetElementsRuleFactory.java new file mode 100644 index 00000000..fb78847e --- /dev/null +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedOrderedSetElementsRuleFactory.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 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.objmap.annotations.factories; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.ResourceNotFoundException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.exception.ValidationException; +import org.simantics.objmap.IMappingRule; +import org.simantics.objmap.annotations.RelatedOrderedSetElements; +import org.simantics.objmap.rules.MappedElementsRule; +import org.simantics.objmap.rules.domain.RelatedOrderedSetElementsAccessor; +import org.simantics.objmap.rules.factory.IFieldRuleFactory; +import org.simantics.objmap.rules.range.FieldAccessor; + +public class RelatedOrderedSetElementsRuleFactory implements IFieldRuleFactory { + + @Override + public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException { + RelatedOrderedSetElements annotation = (RelatedOrderedSetElements)_annotation; + return new MappedElementsRule( + new RelatedOrderedSetElementsAccessor(annotation.composition()), + new FieldAccessor>(field) + ); + } + +} diff --git a/org.simantics.objmap/src/org/simantics/objmap/rules/domain/RelatedOrderedSetElementsAccessor.java b/org.simantics.objmap/src/org/simantics/objmap/rules/domain/RelatedOrderedSetElementsAccessor.java new file mode 100644 index 00000000..a92d2d27 --- /dev/null +++ b/org.simantics.objmap/src/org/simantics/objmap/rules/domain/RelatedOrderedSetElementsAccessor.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 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.objmap.rules.domain; + +import java.util.Collection; + +import org.apache.log4j.Logger; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.MappingException; + +/** + * Accesses the set of objects attached to the element by the given relation. + * @author Hannu Niemistö + */ +public class RelatedOrderedSetElementsAccessor implements IDomainAccessor> { + + static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); + + boolean deleteExtraObjects; + + public RelatedOrderedSetElementsAccessor(boolean deleteExtraObjects) { + super(); + this.deleteExtraObjects = deleteExtraObjects; + } + + @Override + public Collection get(ReadGraph g, Resource element) throws MappingException { + try { + LOGGER.info(" RelatedOrderedSetElementsAccessor.get"); + return OrderedSetUtils.toList(g, element); + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public boolean set(WriteGraph g, Resource element, Collection value) + throws MappingException { + try { + LOGGER.info(" RelatedOrderedSetElementsAccessor.set"); + return OrderedSetUtils.set(g, element, value); + // FIXME Implement deleteExtraObjects + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + +} diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 05c8dcb9..ee9c3913 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 c87108f1..e76fb290 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -80,17 +80,10 @@ SYSDYN.DefaultRealization + + + + + + + + + + @@ -456,6 +477,10 @@ args="org.simantics.image.ui.modelBrowser.ImageNode" property="org.simantics.sysdyn.ui.nodeClass"> + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java index 619de4f0..4106a455 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java @@ -12,14 +12,17 @@ package org.simantics.sysdyn.ui.browser; import org.eclipse.core.runtime.IAdaptable; +import org.simantics.browsing.ui.NodeContext; import org.simantics.db.Resource; public class BrowserSelection implements IAdaptable { private Resource resource; private Resource model; private String URI; + private Object originalInput; - public BrowserSelection(Resource resource, String URI, Resource model) { + public BrowserSelection(Object originalInput, Resource resource, String URI, Resource model) { + this.originalInput = originalInput; this.resource = resource; this.URI = URI; this.model = model; @@ -30,6 +33,12 @@ public class BrowserSelection implements IAdaptable { public Object getAdapter(Class adapter) { if (adapter == Resource.class) return resource; + if (NodeContext.class.equals(adapter)) + return originalInput; + if (originalInput instanceof IAdaptable) { + IAdaptable input = (IAdaptable)originalInput; + return input.getAdapter(adapter); + } return null; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java index 6905e7e6..dac17e5e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java @@ -54,7 +54,7 @@ public class SysdynBrowser extends GraphExplorerView { if(vn != null) { Resource resource = vn.getResource() == null ? vn.data : vn.getResource(); context = new AdaptableHintContext(SelectionHints.KEY_MAIN, SelectionHints.KEY_SELECTION_PROPERTY); - context.setHint(SelectionHints.KEY_MAIN, new BrowserSelection(resource, vn.getURI(), vn.getModel())); + context.setHint(SelectionHints.KEY_MAIN, new BrowserSelection(objects[i], resource, vn.getURI(), vn.getModel())); context.setHint(SelectionHints.KEY_SELECTION_PROPERTY, vn.getVariable()); } else { context = new AdaptableHintContext(SelectionHints.KEY_MAIN); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java index 89edaaa4..7baf1048 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java @@ -22,6 +22,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; +import org.simantics.sysdyn.ui.browser.nodes.EnumerationNode; import org.simantics.sysdyn.ui.browser.nodes.InputNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; import org.simantics.sysdyn.ui.browser.nodes.VariableNode; @@ -39,6 +40,7 @@ public class Configuration extends ViewpointContributor variables = new ArrayList(); ArrayList inputs = new ArrayList(); ArrayList modules = new ArrayList(); + ArrayList enumerations = new ArrayList(); for(Resource r : graph.getObjects(configuration.data, l0.ConsistsOf)) { Resource represents = graph.getSingleObject(r, l0.Represents); @@ -48,6 +50,8 @@ public class Configuration extends ViewpointContributor implements IDeletableNode { + + public EnumerationNode(Resource resource) { + super(resource); + } + + @Override + public void delete() throws DeleteException { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException { + Layer0 l0 = Layer0.getInstance(graph); + graph.deny(resource, l0.PartOf); + } + }); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java new file mode 100644 index 00000000..bbccf0b7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java @@ -0,0 +1,50 @@ +package org.simantics.sysdyn.ui.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.AdaptionUtils; + +public class NewEnumerationNodeHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + ISelection sel = HandlerUtil.getCurrentSelection(event); + + final Resource configuration = AdaptionUtils.adaptToSingle(sel, Resource.class); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Layer0 l0 = Layer0.getInstance(g); + + + Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes); + + Resource enumeration = GraphUtils.create2(g, + sr.Enumeration, + l0.HasName, "Enumeration", + sr.HasEnumerationIndexes, enumerationIndexes); + + g.claim(configuration, l0.ConsistsOf, enumeration); + + } + }); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java new file mode 100644 index 00000000..ffabdef0 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java @@ -0,0 +1,281 @@ +package org.simantics.sysdyn.ui.properties; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.TableEditor; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.Table; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.binding.java.StringBindingDefault; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier; +import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.ui.ISelectionUtils; + +public class EnumerationTab extends PropertyTabContributorImpl { + + Table table; + @Override + public void createControls(Composite body, IWorkbenchSite site, + final ISessionContext context, WidgetSupport support) { + + Composite container = new Composite(body, SWT.None); + GridDataFactory.fillDefaults().grab(true, true).applyTo(container); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(container); + + TrackedText nameText = new TrackedText(container, support, SWT.BORDER); + nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); + nameText.addModifyListener(new VariableNamePropertyModifier(context, Builtins.URIs.HasName)); + nameText.setInputValidator(new VariableNameValidator(support)); + GridDataFactory.fillDefaults().grab(true, false).span(2,1).applyTo(nameText.getWidget()); + + + table = new Table(container, support, SWT.BORDER); + table.setItemFactory(new ReadFactoryImpl>>() { + + @Override + public List> perform(ReadGraph graph, + Object input) throws DatabaseException { + Resource enumeration = (Resource)input; + SysdynResource sr = SysdynResource.getInstance(graph); + Resource list = graph.getSingleObject(enumeration, sr.HasEnumerationIndexes); + List resourceList = OrderedSetUtils.toList(graph, list); + + List> result = new ArrayList>(); + for(Resource r : resourceList) { + result.add(new Pair( + (String)graph.getRelatedValue(r, Layer0.getInstance(graph).HasName, StringBindingDefault.INSTANCE) + , r)); + } + return result; + } + }); + table.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + return graph.getRelatedValue(input, Layer0.getInstance(graph).HasName); + } + }); + GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(table.getWidget()); + + + final TableEditor editor = new TableEditor(table.getWidget()); + // The editor must have the same size as the cell and must + // not be any smaller than 50 pixels. + editor.horizontalAlignment = SWT.LEFT; + editor.grabHorizontal = true; + editor.minimumWidth = 50; + // editing the second column + final int EDITABLECOLUMN = 0; + + table.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + // Clean up any previous editor control + Control oldEditor = editor.getEditor(); + if (oldEditor != null) + oldEditor.dispose(); + + // Identify the selected row + final TableItem item = (TableItem) e.item; + if (item == null) + return; + + // The control that will be the editor must be a child of the + // Table + Text newEditor = new Text(item.getParent(), SWT.NONE); + final String originalText = item.getText(EDITABLECOLUMN); + final Resource resource = (Resource) item.getData(); + newEditor.setText(originalText); + newEditor.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent me) { + Text text = (Text) editor.getEditor(); + if(!text.isDisposed()) { + TableItem item = editor.getItem(); + if(!item.isDisposed()) + item.setText(EDITABLECOLUMN, text.getText()); + } + } + }); + newEditor.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(FocusEvent e) { + final Text text = (Text) editor.getEditor(); + if(!text.getText().equals(originalText)) { + final String newText = text.getText(); + try { + context.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.claimLiteral( + resource, + Layer0.getInstance(graph).HasName, + newText); + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + } + }); + newEditor.selectAll(); + newEditor.setFocus(); + editor.setEditor(newEditor, item, EDITABLECOLUMN); + } + }); + + + Button add = new Button(container, support, SWT.None); + add.setText("Add index"); + add.addSelectionListener(new addEnumerationIndexListener(support)); + + Button remove = new Button(container, support, SWT.None); + remove.setText("Remove"); + remove.addSelectionListener(new removeEnumerationIndexListener(support)); + } + + private class addEnumerationIndexListener implements SelectionListener, Widget { + + Resource enumerationIndexes; + + public addEnumerationIndexListener(WidgetSupport support) { + support.register(this); + } + + @Override + public void setInput(ISessionContext context, Object input) { + final Resource enumeration = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + try { + context.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + enumerationIndexes = graph.getSingleObject(enumeration, SysdynResource.getInstance(graph).HasEnumerationIndexes); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + @Override + public void widgetSelected(SelectionEvent e) { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + + Resource ei = GraphUtils.create2(graph, + sr.EnumerationIndex, + l0.HasName, "index"); + OrderedSetUtils.add(graph, enumerationIndexes, ei); + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + + } + + } + + private class removeEnumerationIndexListener implements SelectionListener, Widget { + + Resource enumerationIndexes; + + public removeEnumerationIndexListener(WidgetSupport support) { + support.register(this); + } + + @Override + public void setInput(ISessionContext context, Object input) { + final Resource enumeration = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + try { + context.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + enumerationIndexes = graph.getSingleObject(enumeration, SysdynResource.getInstance(graph).HasEnumerationIndexes); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + @Override + public void widgetSelected(SelectionEvent e) { + TableItem[] items = table.getWidget().getSelection(); + final ArrayList resources = new ArrayList(); + for(int i = 0; i < items.length; i++) { + if(items[i].getData() instanceof Resource) + resources.add((Resource)items[i].getData()); + } + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + for(Resource r : resources) + OrderedSetUtils.remove(graph, enumerationIndexes, r); + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + + } + + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index cf603073..1bb77cb0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -78,6 +78,13 @@ public class ResourceSelectionProcessor implements SelectionProcessor inputs = new ArrayList(); ArrayList modules = new ArrayList(); ArrayList stocks = new ArrayList(); + ArrayList enumerations = new ArrayList(); ArrayList inputDependencies = new ArrayList(); ArrayList outputDependencies = new ArrayList(); HashMap> moduleInputs = new HashMap>(); @@ -136,6 +138,8 @@ public class ModelicaWriter { } } else if (element instanceof Input) { inputs.add((Input)element); + } else if (element instanceof Enumeration) { + enumerations.add((Enumeration)element); } else if (element instanceof Dependency && ((Dependency)element).refersTo() != null) { Dependency dependency = (Dependency)element; if(dependency.getHead() instanceof Module) { @@ -172,6 +176,13 @@ public class ModelicaWriter { b.append(" " + i.getType() + " " + i.getName() + ";\n"); } } + + if(!enumerations.isEmpty()) { + b.append("// Enumeration definitions\n"); + for(Enumeration e : enumerations) { + b.append(e.getDeclaration()); + } + } for(Stock stock : stocks) { IExpression expr = stock.getExpression(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java new file mode 100644 index 00000000..4176277f --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java @@ -0,0 +1,31 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/Enumeration") +public class Enumeration extends Variable { + + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasEnumerationIndexes") + private EnumerationIndexes enumerationIndexes; + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } + + public String getDeclaration() { + StringBuilder sb = new StringBuilder(); + sb.append(" " + this.getType()); + sb.append(" " + this.name); + sb.append(" = enumeration("); + for(int i = 0; i < enumerationIndexes.getEnumerationIndexes().size(); i++) { + sb.append(enumerationIndexes.getEnumerationIndexes().get(i).getName()); + if(i < enumerationIndexes.getEnumerationIndexes().size() - 1) + sb.append(", "); + } + sb.append(");\n"); + return sb.toString(); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java new file mode 100644 index 00000000..9cbff869 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndex.java @@ -0,0 +1,16 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedValue; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/EnumerationIndex") +public class EnumerationIndex { + + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") + protected String name; + + public String getName() { + return this.name; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndexes.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndexes.java new file mode 100644 index 00000000..024a7b3e --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/EnumerationIndexes.java @@ -0,0 +1,17 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedOrderedSetElements; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/EnumerationIndexes") +public class EnumerationIndexes { + + @RelatedOrderedSetElements + private ArrayList enumerationIndexes = new ArrayList(); + + public ArrayList getEnumerationIndexes() { + return enumerationIndexes; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java index 745fd6ad..3c0888e0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -37,6 +37,9 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, Input.class)); addLinkType(MappingSchemas.fromAnnotations(g, ModuleType.class)); addLinkType(MappingSchemas.fromAnnotations(g, Model.class)); + addLinkType(MappingSchemas.fromAnnotations(g, Enumeration.class)); + addLinkType(MappingSchemas.fromAnnotations(g, EnumerationIndex.class)); + addLinkType(MappingSchemas.fromAnnotations(g, EnumerationIndexes.class)); addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, ParameterExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, StockExpression.class)); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java index fe3afd76..7a224f95 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java @@ -15,6 +15,7 @@ import org.simantics.sysdyn.representation.Auxiliary; import org.simantics.sysdyn.representation.Cloud; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.Dependency; +import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.Flow; import org.simantics.sysdyn.representation.Input; import org.simantics.sysdyn.representation.Module; @@ -58,4 +59,8 @@ public class ElementVisitorVoidAdapter implements IElementVisitorVoid { @Override public void visit(Configuration configuration) { } + + @Override + public void visit(Enumeration enumeration) { + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java index d7320342..0d560b2b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java @@ -15,6 +15,7 @@ import org.simantics.sysdyn.representation.Auxiliary; import org.simantics.sysdyn.representation.Cloud; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.Dependency; +import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.Flow; import org.simantics.sysdyn.representation.Input; import org.simantics.sysdyn.representation.Module; @@ -32,4 +33,5 @@ public interface IElementVisitorVoid { void visit(Flow flow); void visit(Module module); void visit(Configuration configuration); + void visit(Enumeration enumeration); }