From: villberg Date: Mon, 31 May 2010 10:32:29 +0000 (+0000) Subject: First blood for g2d to h2d transition. X-Git-Tag: simantics-1.2.0~231 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=cbd6ba21603754b1843f5e73b6614f69f11c4df9;p=simantics%2Fsysdyn.git First blood for g2d to h2d transition. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@15982 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF index 9fcc740c..a253c602 100644 --- a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -28,6 +28,9 @@ Require-Bundle: org.simantics.h2d;bundle-version="1.0.0", org.simantics.graphviz.ui;bundle-version="1.0.0", org.simantics.graphviz;bundle-version="1.0.0", org.simantics.diagram;bundle-version="0.9.4", - org.simantics.modeling;bundle-version="1.0.0" + org.simantics.modeling;bundle-version="1.0.0", + org.simantics.mapping;bundle-version="1.0.0", + org.simantics.structural.stubs;bundle-version="1.0.0", + gnu.trove2;bundle-version="2.0.4" Bundle-Activator: org.simantics.sysdyn.ui.Activator Bundle-ActivationPolicy: lazy diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 62f8e6df..61ee59d8 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -10,13 +10,13 @@ VTT Technical Research Centre of Finland - initial API and implementation --> - + - + + + + + symbolGroups = Collections.emptySet(); + + Collection toSymbolGroups(Resource... rs) { + Collection result = new ArrayList(rs.length); + for (int i = 0; i < rs.length; ++i) { + result.add(new SymbolGroup(rs[i])); + } + return result; + } + + @Override + public void configure(ReadGraph g) throws ProjectException { +// IProject p = getProject(); +// ISymbolManager sm = p.getHint(ISymbolManager.KEY_SYMBOL_MANAGER); +// if (sm != null) { +// Collection grps; +// try { +// grps = g.syncRequest(new GroupDiscoveryQuery(p)); +// AprosSymbolsFeature.this.symbolGroups = toSymbolGroups(grps.toArray(Resource.NONE)); +// sm.addEntryPoints(symbolGroups); +// } catch (DatabaseException e) { +// throw new ProjectException(e); +// } +// } + } + + @Override + public void deconfigure() throws ProjectException { + ISymbolManager sm = getProject().getHint(ISymbolManager.KEY_SYMBOL_MANAGER); + if (sm != null) { + sm.removeEntryPoints(symbolGroups); + symbolGroups = Collections.emptySet(); + } + } + + public class SymbolGroup implements ISymbolGroup { + final Resource r; + + public SymbolGroup(Resource r) { + this.r = r; + } + + @Override + public ISymbolItem[] getItems() { + RequestProcessor sgrp = getGraphRequestProcessor(); + IProject project = peekProject(); + if (project == null || sgrp == null) + return new ISymbolItem[0]; + Collection items; + try { + items = sgrp.syncRequest(new ElementDiscoveryQuery(r)); + List result = new ArrayList(); + for (Resource r : items) { + result.add(new SymbolItem(this, r)); + } + Collections.sort(result, new Comparator() { + @Override + public int compare(ISymbolItem o1, ISymbolItem o2) { + String n1 = o1.getName(); + String n2 = o2.getName(); + return n1.compareTo(n2); + } + }); + return result.toArray(new ISymbolItem[result.size()]); + } catch (DatabaseException e) { + throw new RuntimeException(e); + } + } + + @Override + public String getName() { + try { + RequestProcessor sgrp = getGraphRequestProcessor(); + IProject project = peekProject(); + if (project == null || sgrp == null) + return "(no project)"; + return sgrp.syncRequest(new SafeLabelQuery(r)); + } catch (DatabaseException e) { + throw new RuntimeException(e); + } + } + + @Override + public int hashCode() { + return r.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SymbolGroup other = (SymbolGroup) obj; + return r.equals(other.r); + } + } + + public class SymbolItem implements ISymbolItem { + + final ISymbolGroup group; + final Resource r; + + public SymbolItem(ISymbolGroup group, Resource r) { + this.group = group; + this.r = r; + } + + @Override + public ISymbolGroup getGroup() { + return group; + } + + @Override + public ElementClass getElementClass(IHintObservable hints) { + RequestProcessor sgrp = getGraphRequestProcessor(); + if (sgrp == null) + throw new ProvisionException("No RequestProcessor available for querying an ElementClass for resource " + r); + try { + ElementClass ec = sgrp.syncRequest(DiagramRequests.getElementClass(r, hints)); + if (ec == null) + throw new ProvisionException("ElementClass query failed, returned null"); + if (!ec.containsClass(StaticSymbol.class)) + throw new ProvisionException("ElementClass " + ec + " does not provide a StaticSymbol handler"); + return ec; + } catch (DatabaseException e) { + throw new ProvisionException(e); + } + } + + @Override + public String getName() { + try { + RequestProcessor sgrp = getGraphRequestProcessor(); + // Return a safe "" if the query cannot be performed. + return sgrp != null ? sgrp.syncRequest(new SafeLabelQuery(r)) : ""; + } catch (DatabaseException e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("unchecked") + @Override + public Object getAdapter(Class adapter) { + //if (adapter == ElementClass.class) + // return getElementClass(); + if (adapter == Resource.class) + return r; + return null; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + group.hashCode(); + result = prime * result + r.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SymbolItem other = (SymbolItem) obj; + if (!group.equals(other.group)) + return false; + return r.equals(other.r); + } + } + + static class SafeLabelQuery extends ResourceRead { + public SafeLabelQuery(Resource resource) { + super(resource); + } + + @Override + public String perform(ReadGraph g) throws DatabaseException { + try { + return g.adapt(resource, String.class); + } catch (DatabaseException e) { + return GraphUtils.getReadableName(g, resource); + } + } + } + + public static class GroupDiscoveryQuery implements Read> { + + IProject project; + + GroupDiscoveryQuery(IProject project) { + assert project != null; + this.project = project; + } + + @Override + public Collection perform(ReadGraph g) throws DatabaseException { + try { + Resource[] eps = new Resource[] { + g.getResource("http://www.simantics.org/Sysdyn-1.0/SymbolReferences") + }; + + final DiagramResource dr = DiagramResource.getInstance(g); + final ArrayList groups = new ArrayList(); + for (Resource ep : eps) { + GraphUtils.findResources(g, Collections.singletonList(ep), g.getBuiltins().ConsistsOf, new ResourceTester() { + @Override + public boolean test(ReadGraph g, Resource r) throws DatabaseException { + return g.isInstanceOf(r, g.getBuiltins().Library) + || g.isInstanceOf(r, dr.SymbolReferenceLibrary); + } + }, new MultiProcedureAdapter() { + @Override + public void execute(Resource result) { + groups.add(result); + } + @Override + public void exception(Throwable t) { + new Exception().printStackTrace(); + t.printStackTrace(); + } + }); + } + return groups; + + } catch (ResourceNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + GroupDiscoveryQuery other = (GroupDiscoveryQuery) obj; + if (!project.equals(other.project)) + return false; + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((project == null) ? 0 : project.hashCode()); + return result; + } + } + + static public class ElementDiscoveryQuery extends ResourceRead> { + public ElementDiscoveryQuery(Resource resource) { + super(resource); + } + @Override + public Collection perform(ReadGraph g) throws DatabaseException { + DiagramResource dr = DiagramResource.getInstance(g); + Collection objs = g.getObjects(resource, g.getBuiltins().DependsOn); + // Filter out everything besides Elements. + ArrayList result = new ArrayList(objs.size()); + for (Resource r : objs) { + if (g.isInheritedFrom(r, dr.Element)) + result.add( r ); + } + return result; + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynCreationInstruction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynCreationInstruction.java new file mode 100644 index 00000000..ac3a25b7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynCreationInstruction.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.sysdyn.ui.editor; + +import gnu.trove.TIntIntHashMap; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.URIStringUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.mapping.constraint.instructions.TypedBracketInstruction.CreationInstruction; +import org.simantics.modeling.services.ComponentNamingUtil; +import org.simantics.modeling.services.NamingException; +import org.simantics.project.IProject; + +public class SysdynCreationInstruction extends CreationInstruction { + + IProject project; + Resource configurationRoot; + int lComponentType; + int lConfiguration; + + public SysdynCreationInstruction(IProject project, Resource configurationRoot, int variableId, int componentType, + int configuration) { + super(variableId); + this.project = project; + this.configurationRoot = configurationRoot; + lComponentType = componentType; + lConfiguration = configuration; + } + + @Override + public Resource create(WriteGraph g, Object[] bindings) throws DatabaseException { + Resource componentType = (Resource) bindings[lComponentType]; + Resource configuration = (Resource) bindings[lConfiguration]; + + try { + String proposition = URIStringUtils.escape(ComponentNamingUtil.findFreshInstanceName(g, project, configurationRoot, configuration, componentType)); + Resource result = GraphUtils.create(g, + g.getBuiltins().HasName, proposition + ); + return result; + } catch (NamingException e1) { + throw new DatabaseException(e1); + } + } + + @Override + public void mapVariables(TIntIntHashMap map) { + super.mapVariables(map); + lComponentType = map.get(lComponentType); + lConfiguration = map.get(lConfiguration); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 6ace86f6..967fc89c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -55,6 +55,7 @@ import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; +import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.expressionParser.ParseException; @@ -68,6 +69,7 @@ import org.simantics.sysdyn.ui.equation.expressions.ExpressionField; import org.simantics.sysdyn.ui.equation.expressions.StockExpressionViewFactor; import org.simantics.ui.SimanticsUI; import org.simantics.ui.workbench.ResourceEditorInput; +import org.simantics.utils.ui.ISelectionUtils; import org.eclipse.jface.text.Position; public class EquationView extends ViewPart implements ISelectionListener { @@ -173,6 +175,11 @@ public class EquationView extends ViewPart implements ISelectionListener { public Auxiliary perform(ReadGraph graph) throws DatabaseException { Builtins b = graph.getBuiltins(); SysdynResource sr = SysdynResource.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + + Resource map = graph.getPossibleObject(resource, mr.ElementToComponent); + if(map != null) resource = map; + Auxiliary a = new Auxiliary(); variable = resource; a.configuration = graph.getPossibleObject(variable, b.PartOf); @@ -192,10 +199,11 @@ public class EquationView extends ViewPart implements ISelectionListener { IStructuredSelection structuredSelection = (IStructuredSelection)selection; if(structuredSelection.size() == 1) { - Object element = structuredSelection.getFirstElement(); - if(element instanceof Resource) +// Object element = structuredSelection.getFirstElement(); + Resource res = ISelectionUtils.filterSingleSelection(structuredSelection, Resource.class); + if(res != null) SimanticsUI.getSession().asyncRequest( - new UpdateViewRequest((Resource)element), + new UpdateViewRequest(res), new Listener() { @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java index 19d0611e..2168b7b6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java @@ -55,7 +55,7 @@ public class NewModelHandler extends AbstractHandler { graph.claim(model, b.HasConfiguration, conf); Resource mapping = graph.newResource(); - graph.claim(mapping, b.InstanceOf, null, mr.DiagramToCompositeMapping); + graph.claim(mapping, b.InstanceOf, null, sr.DiagramToCompositeMapping); graph.claim(diagram, b.HasTrigger, mapping); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 255a6df5..0a04685d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.trend; import java.awt.Frame; import java.util.HashMap; +import java.util.Set; import javax.swing.SwingUtilities; @@ -38,6 +39,7 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.modelica.data.DataSet; import org.simantics.modelica.data.SimulationResult; +import org.simantics.modeling.ModelingResources; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; @@ -45,6 +47,7 @@ import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.ui.simulation.SimulationScheduler; import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ISelectionUtils; import org.simantics.utils.ui.jface.ActiveSelectionProvider; public class TrendView extends ViewPart { @@ -126,16 +129,17 @@ public class TrendView extends ViewPart { public void selectionChanged(IWorkbenchPart part, ISelection selection) { if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState())) return; - if(selection instanceof IStructuredSelection) { - Object[] els = ((IStructuredSelection) selection).toArray(); - Resource[] resources = new Resource[els.length]; - for(int i=0;i ress = ISelectionUtils.filterSetSelection(selection, Resource.class); +// Object[] els = ((IStructuredSelection) selection).toArray(); +// Resource[] resources = new Resource[els.length]; +// for(int i=0;i +# +# +#""" : L0.String + +#Stock +# MOD.ComponentTypeToSymbol StockSymbol +# +# +#""" : L0.String + + +#Valve +# MOD.ComponentTypeToSymbol ValveSymbol +# +# +#""" : L0.String + + + + + +###################################################################### +# Terminal +###################################################################### + +PhantomTerminal """ : L0.String + L0.PartOf Sysdyn + +###################################################################### +# Macros for component types +###################################################################### + +%define def() + $subject - - -""" : L0.String + G2D.HasSVGDocument $svg : L0.String + +%define terminal($transform, $parent, $target) + $subject + G2D.HasTransform $transform : G2D.Transform + DIA.HasConnectionVariable _ : ST.ConnectionVariable + ST.Binds _ """ : L0.String + _ : PhantomTerminal + terminal("1,0,0,1,-7,0", StockSymbol , IsHeadOf) + _ : PhantomTerminal + terminal("1,0,0,1,7,0", StockSymbol , IsTailOf) - +###################################################################### Valve + def() + connection(IsTailOf) + connection(IsHeadOf) + # symbol MOD.ComponentTypeToSymbol ValveSymbol """ : L0.String + _ : PhantomTerminal + terminal("1,0,0,1,-7,0", ValveSymbol, IsHeadOf) + _ : PhantomTerminal + terminal("1,0,0,1,7,0", ValveSymbol, IsTailOf) + +###################################################################### +Auxiliary + def() + connection(IsTailOf) + connection(IsHeadOf) + # symbol + MOD.ComponentTypeToSymbol AuxiliarySymbol + + +""" : L0.String + _ : PhantomTerminal + terminal("1,0,0,1,-7,0", AuxiliarySymbol, IsHeadOf) + _ : PhantomTerminal + terminal("1,0,0,1,7,0", AuxiliarySymbol, IsTailOf)