+ public static class StructuralTypeOverrideMap extends ResourceRead<Map<Resource,Resource>> {
+
+ protected StructuralTypeOverrideMap(Resource composite) {
+ super(composite);
+ }
+
+ @Override
+ public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+ CollectionSupport cs = graph.getService(CollectionSupport.class);
+
+ Map<Resource,Resource> result = null;
+
+ for(Resource override : graph.getObjects(resource, STR.HasTypeOverride)) {
+ Resource original = graph.getSingleObject(override, STR.TypeOverride_HasOriginalType);
+ Resource replacement = graph.getSingleObject(override, STR.TypeOverride_HasReplacementType);
+ if(result == null) result = cs.createMap(Resource.class);
+ result.put(original, replacement);
+ }
+
+ if(result == null) return Collections.emptyMap();
+
+ return result;
+
+ }
+
+ }
+
+ public static class StructuralOverrideData {
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((actualRepresents == null) ? 0 : actualRepresents.hashCode());
+ result = prime * result + ((actualType == null) ? 0 : actualType.hashCode());
+ result = prime * result + ((overrideType == null) ? 0 : overrideType.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;
+ StructuralOverrideData other = (StructuralOverrideData) obj;
+ if (actualRepresents == null) {
+ if (other.actualRepresents != null)
+ return false;
+ } else if (!actualRepresents.equals(other.actualRepresents))
+ return false;
+ if (actualType == null) {
+ if (other.actualType != null)
+ return false;
+ } else if (!actualType.equals(other.actualType))
+ return false;
+ if (overrideType == null) {
+ if (other.overrideType != null)
+ return false;
+ } else if (!overrideType.equals(other.overrideType))
+ return false;
+ return true;
+ }
+ Resource actualRepresents;
+ Resource actualType;
+ Resource overrideType;
+ public StructuralOverrideData(Resource actualRepresents, Resource actualType, Resource overrideType) {
+ this.actualRepresents = actualRepresents;
+ this.actualType = actualType;
+ this.overrideType = overrideType;
+ }
+
+ public static StructuralOverrideData compute(ReadGraph graph, Variable context) throws DatabaseException {
+ return graph.syncRequest(new StructuralOverrideDataRequest(context));
+ }
+
+ public Resource type() {
+ if(overrideType != null)
+ return overrideType;
+ return actualType;
+ }
+
+ public Resource represents() {
+ return actualRepresents;
+ }
+
+ }
+
+ public static class StructuralOverrideDataRequest extends VariableRead<StructuralOverrideData> {
+
+ public StructuralOverrideDataRequest(Variable component) {
+ super(component);
+ }
+
+ public StructuralOverrideData walk(ReadGraph graph, Variable component, Resource actualRepresents, Resource actualType) throws DatabaseException {
+ System.err.println("walk: " + component.getURI(graph));
+ Resource represents = component.getPossibleRepresents(graph);
+ if(represents != null) {
+ Layer0 L0 = Layer0.getInstance(graph);
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ Resource container = graph.syncRequest(new PossibleObjectWithType(represents, L0.PartOf, STR.Composite));
+ if(container != null) {
+ Map<Resource,Resource> overrides = graph.syncRequest(new StructuralTypeOverrideMap(container));
+ if(!overrides.isEmpty()) {
+ System.err.println("Check overrides for " + graph.getPossibleURI(actualType));
+ for(Resource r : overrides.keySet()) {
+ System.err.println("-available OR: " + graph.getPossibleURI(r) + " = > " + graph.getPossibleURI(overrides.get(r)));
+ }
+ }
+ Resource override = overrides.get(actualType);
+ if(override != null) {
+ System.err.println("Override! " + graph.getPossibleURI(actualType) + " = > " + graph.getPossibleURI(override));
+ return new StructuralOverrideData(actualRepresents, actualType, override);
+ }
+ }
+ }
+ Variable parent = component.getParent(graph);
+ if(parent == null) return new StructuralOverrideData(actualRepresents, actualType, null);
+ else return walk(graph, parent, represents, actualType);
+ }
+
+ @Override
+ public StructuralOverrideData perform(ReadGraph graph) throws DatabaseException {
+
+ if(variable.getURI(graph).endsWith("/Alternative/Panel2"))
+ System.err.println("walk1: " + variable.getURI(graph));
+
+ Resource represents = variable.getPossibleRepresents(graph);
+ if(represents == null) {
+ String uri = variable.getPossiblePropertyValue(graph, "typeURI");
+ if(uri != null) {
+ Resource actualType = graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri), TransientCacheAsyncListener.<Resource>instance());
+ return walk(graph, variable, null, actualType);
+ }
+ throw new DatabaseException("No type for " + variable.getURI(graph));
+ } else {
+ return walk(graph, variable, represents, graph.getPossibleType(represents, Layer0.getInstance(graph).Entity));
+ }
+
+ }
+
+ }
+