package org.simantics.district.network.techtype;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import java.util.stream.Collectors;
import org.simantics.Simantics;
import org.simantics.databoard.Bindings;
import org.simantics.db.layer0.variable.Variable;
import org.simantics.district.network.ontology.DistrictNetworkResource;
import org.simantics.district.network.techtype.requests.PossibleTechTypeItem;
+import org.simantics.district.network.techtype.requests.PossibleTechTypeKeyName;
import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
import org.simantics.district.network.techtype.requests.TechTypeTableData;
import org.simantics.district.network.techtype.requests.TechTypeTableKeyName;
+import org.simantics.district.network.techtype.requests.WriteTechTypeTable;
import org.simantics.layer0.Layer0;
import org.simantics.scl.runtime.SCLContext;
import org.simantics.structural.stubs.StructuralResource2;
public static String DEFAULT_KEY_NAME = "pipeCode";
+ /**
+ * Load a tech type table from a CSV file and write it in the active model.
+ *
+ * @param componentType The component type that the tech type table is associated with
+ * @param filePath A path to a CSV file
+ * @throws DatabaseException
+ * @throws IOException
+ */
+ public static void loadTechTypeTable(Resource componentType, String filePath) throws DatabaseException, IOException {
+ String data;
+ try {
+ data = Files.lines(Paths.get(filePath), Charset.defaultCharset()).collect(Collectors.joining("\n"));
+ } catch (IOException e) {
+ LOGGER.error("Failed to read contents of file '{}' as {}", filePath, Charset.defaultCharset(), e);
+ return;
+ }
+
+ Simantics.getSession().syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ graph.syncRequest(new WriteTechTypeTable(componentType, data));
+ }
+ });
+ }
+
/**
* Execute a cached query for a possible tech type table item in a table with a given item key.
*
}
/**
- * Reset all components that address the given tech type table to the table values.
+ * Reset all diagram elements to tech type table values.
*
* @param table A tech type table
* @throws DatabaseException
String key = graph.getPossibleRelatedValue2(element, keyRelation);
Map<String, String> values = data.get(key);
if (values == null) {
- LOGGER.info("Key {} no found in tech type table {}", key, table);
+ LOGGER.info("Key {} not found in tech type table {}", key, table);
continue;
}
}
}
+ /**
+ * Find the tech type table key property name for a table.
+ *
+ * @param graph A read interface to the graph db
+ * @param table A tech type table resource
+ * @return Name of the key property
+ * @throws DatabaseException
+ */
public static String getKeyPropertyName(ReadGraph graph, Resource table) throws DatabaseException {
String name = graph.syncRequest(new TechTypeTableKeyName(table), TransientCacheListener.instance());
if (name == null) name = "_" + DEFAULT_KEY_NAME;
return name;
}
+
+ /**
+ * Get a possible tech type table key property for a component type
+ *
+ * @param graph A read interface to the graph db
+ * @param componentType An STR.ComponentType instance
+ * @return The name of a property that uses DN.TechType.Functions.techTypeCodeValueAccessor or null if not found
+ * @throws DatabaseException
+ */
+ public static String getPossibleTechTypeKeyName(ReadGraph graph, Resource componentType) throws DatabaseException {
+ return graph.syncRequest(new PossibleTechTypeKeyName(componentType), TransientCacheListener.instance());
+ }
+
+ /**
+ * Get a possible tech type table from a given model, for a given component type.
+ *
+ * @param graph A read interface to the graph db
+ * @param model A model resource (or other index root)
+ * @param componentType An STR.ComponentType instance
+ * @return A tech type table resource or null if not found
+ * @throws DatabaseException
+ */
+ public static Resource getPossibleTechTypeTable(ReadGraph graph, Resource model, Resource componentType) throws DatabaseException {
+ return graph.syncRequest(new PossibleTechTypeTable(model, componentType), TransientCacheListener.instance());
+ }
+
+ /**
+ * Get the contents of a tech type table, indexed by item code and property name.
+ *
+ * @param graph A read interface to the graph db
+ * @param table A tech type table resource
+ * @return A map from item code to a map of property values
+ * @throws DatabaseException
+ */
+ public static Map<String, Map<String, String>> getTechTypeData(ReadGraph graph, Resource table) throws DatabaseException {
+ return graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance());
+ }
+
+ /**
+ * Get the enabled contents of a tech type table, indexed by item code and property name.
+ *
+ * The returned map is a subset of the one returned by {@link #getTechTypeData(ReadGraph, Resource)}
+ * with only the items whose index is listed in the HasEnabledItems property.
+ *
+ * @param graph A read interface to the graph db
+ * @param table A tech type table resource
+ * @return A map from item code to a map of property values
+ * @throws DatabaseException
+ */
+ public static Map<String, Map<String, String>> getEnabledTechTypeData(ReadGraph graph, Resource table) throws DatabaseException {
+ return graph.syncRequest(new TechTypeTableData(table, true), TransientCacheListener.instance());
+ }
+
+ /**
+ * Get a single tech type table item that has the given item code, or null, if not found.
+ *
+ * The returned map is a subset of the one returned by {@link #getTechTypeData(ReadGraph, Resource)}
+ * with only the items whose index is listed in the HasEnabledItems property.
+ *
+ * @param graph A read interface to the graph db
+ * @param table A tech type table resource
+ * @return A map of property values or null if not found
+ * @throws DatabaseException
+ */
+ public static Map<String, String> getPossibleTechTypeItem(ReadGraph graph, Resource table, String itemCode) throws DatabaseException {
+ return graph.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
+ }
+
+ /**
+ * Compare strings so that contained numbers are sorted in numerical order instead of lexicographic.
+ * (From https://stackoverflow.com/questions/104599/sort-on-a-string-that-may-contain-a-number)
+ */
+ public static final int compareNatural(String s1, String s2) {
+ // Skip all identical characters
+ int len1 = s1.length();
+ int len2 = s2.length();
+ int i;
+ char c1, c2;
+ for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
+ ;
+
+ // Check end of string
+ if (c1 == c2)
+ return (len1 - len2);
+
+ // Check digit in first string
+ if (Character.isDigit(c1)) {
+ // Check digit only in first string
+ if (!Character.isDigit(c2))
+ return (1);
+
+ // Scan all integer digits
+ int x1, x2;
+ for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
+ ;
+ for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
+ ;
+
+ // Longer integer wins, first digit otherwise
+ return (x2 == x1 ? c1 - c2 : x1 - x2);
+ }
+
+ // Check digit only in second string
+ if (Character.isDigit(c2))
+ return (-1);
+
+ // No digits
+ return (c1 - c2);
+ }
}