+ /**
+ * Get the generator component. Use {@link #buildSelection(ReadGraph)} to make it available first.
+ */
+ public Generator getGenerator() {
+ return generator;
+ }
+
+ /**
+ * Get the selector component. Use {@link #buildSelection(ReadGraph)} to make it available first.
+ */
+ public Selector getSelector() {
+ return selector;
+ }
+
+ /**
+ * Get the condition component. Use {@link #buildSelection(ReadGraph)} to make it available first.
+ */
+ public Condition getCondition() {
+ return condition;
+ }
+
+ /**
+ *
+ * @param graph
+ * @throws DatabaseException
+ */
+ public void buildSelection(ReadGraph graph) throws DatabaseException {
+ Resource selector = graph.getSingleObject(resource, ES.Selection_HasSelector);
+ Resource generator = graph.getSingleObject(resource, ES.Selection_HasGenerator);
+ Resource condition = graph.getPossibleObject(resource, ES.Selection_HasCondition);
+
+ this.selector = buildSelector(graph, selector);
+ this.generator = buildGenerator(graph, generator);
+ this.condition = buildCondition(graph, condition);
+ }
+
+ public static Map<Resource, String> findDiagrams() {
+ try {
+ return Simantics.getSession().syncRequest(new Read<Map<Resource, String>>() {
+ @Override
+ public Map<Resource, String> perform(ReadGraph graph) throws DatabaseException {
+ Map<Resource, String> result = new HashMap<>();
+ Resource model = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
+ List<Resource> composites = QueryIndexUtils.searchByType(graph, model, StructuralResource2.getInstance(graph).Composite);
+ for (Resource r : composites) {
+ // Get diagram
+ Resource diagram = graph.getPossibleObject(r, ModelingResources.getInstance(graph).CompositeToDiagram);
+ if (diagram == null) continue;
+
+ // Filter out user component diagrams
+ Resource parent = graph.getPossibleObject(r, Layer0.getInstance(graph).PartOf);
+ if (parent == null || graph.isInheritedFrom(parent, StructuralResource2.getInstance(graph).Component))
+ continue;
+
+ result.put(r, graph.getRelatedValue(r, Layer0.getInstance(graph).HasName));
+ }
+
+ return result;
+ }
+ });
+ } catch (DatabaseException e) {
+ LOGGER.error("Query for model diagrams failed", e);
+ return Collections.emptyMap();
+ }
+ }
+
+ public static Variable getVariableForElement(ReadGraph graph, Resource element) throws DatabaseException {
+ Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
+ if (component != null) {
+ Variable var = Variables.getVariable(graph, component);
+ RVI realRvi = var.getRVI(graph);
+
+ for (Resource activeModel : graph.syncRequest(new ActiveModels(Simantics.getProjectResource()))) {
+ for (Variable run : graph.syncRequest(new ActiveRuns(activeModel))) {
+ Variable v = realRvi.resolvePossible(graph, run);
+ if (v != null) {
+ return v;
+ }
+ }
+ Variable configuration = Variables.getPossibleConfigurationContext(graph, activeModel);
+ if (configuration != null) {
+ Variable v = realRvi.resolvePossible(graph, configuration);
+ if (v != null) {
+ return v;
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public static Double getPropertyValue(ReadGraph graph, Resource element, String propertyName) {
+ try {
+ Variable v = getVariableForElement(graph, element);
+ if (v != null) {
+ Number value = v.getPossiblePropertyValue(graph, propertyName);
+ if (value != null)
+ return value.doubleValue();
+ }
+
+ // No property found - try possible mapped element property as well
+ Resource mappedElement = graph.getPossibleObject(element, DN.MappedComponent);
+ if (mappedElement != null)
+ return getPropertyValue(graph, mappedElement, propertyName);
+ }
+ catch (DatabaseException e) {
+ }
+
+ return null;
+ }
+
+ public SelectionResult selectElementsFrom(ReadGraph graph, Resource model) throws DatabaseException {