+ 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;
+ }
+
+ }
+
+ private static class StructuralOverrideDataWalkRequest
+ extends TernaryRead<Variable, Resource, Resource, StructuralOverrideData> {
+
+ public StructuralOverrideDataWalkRequest(Variable component, Resource actualRepresents, Resource actualType) {
+ super(component, actualRepresents, actualType);
+ }
+
+ @Override
+ public StructuralOverrideData perform(ReadGraph graph) throws DatabaseException {
+ Variable component = parameter;
+ Resource actualRepresents = parameter2;
+ Resource actualType = parameter3;
+// System.err.println(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), TransientCacheListener.instance());
+ Resource override = overrides.get(actualType);
+ if (override != null) {
+ return new StructuralOverrideData(actualRepresents, actualType, override);
+ }
+ }
+ }
+ Variable parent = component.getParent(graph);
+ if (parent == null)
+ return new StructuralOverrideData(actualRepresents, actualType, null);
+ else
+ return graph.syncRequest(new StructuralOverrideDataWalkRequest(parent, represents, actualType), TransientCacheListener.instance());
+ }
+
+ }
+
+ public static class StructuralOverrideDataRequest extends VariableRead<StructuralOverrideData> {
+
+ public StructuralOverrideDataRequest(Variable component) {
+ super(component);
+ }
+
+ @Override
+ public StructuralOverrideData perform(ReadGraph graph) throws DatabaseException {
+
+ 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());
+ if (graph.syncRequest(new IsInstanceOf(actualType, StructuralResource2.getInstance(graph).ReplaceableDefinedComponentType), TransientCacheListener.instance()) ) {
+ return graph.syncRequest(new StructuralOverrideDataWalkRequest(variable, null, actualType), TransientCacheListener.instance());
+ } else {
+ // can not have replaceable type
+ return null;
+ }
+ }
+ throw new DatabaseException("No type for " + variable.getURI(graph));
+ } else {
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ Resource possibleType = graph.getPossibleType(represents, STR.Component);
+ if (possibleType == null) {
+ possibleType = graph.getPossibleType(represents, Layer0.getInstance(graph).Entity);
+ if (possibleType == null)
+ return null;
+ }
+ if (graph.syncRequest(new IsInstanceOf(possibleType, STR.ReplaceableDefinedComponentType), TransientCacheListener.instance()) ) {
+ return graph.syncRequest(new StructuralOverrideDataWalkRequest(variable, represents, possibleType), TransientCacheListener.instance());
+ } else {
+ return null;
+ }
+ }
+
+ }
+
+ }
+
+
+ private static class StructureTypeAndChildMapRequest extends ResourceRead<Tuple2> {
+
+ protected StructureTypeAndChildMapRequest(Resource resource) {
+ super(resource);
+ }
+
+ @Override
+ public Tuple2 perform(ReadGraph graph) throws DatabaseException {
+ StructuralComponentClass clazz = StructuralComponentClass.get(graph, resource);
+ if (StructuralComponentClass.DEFINED.equals(clazz)) {
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ Resource def = graph.getSingleObject(resource, STR.IsDefinedBy);
+ Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(def), TransientCacheListener.instance());
+ return new Tuple2(clazz, children);
+ }
+ return new Tuple2(clazz, null);
+ }
+
+ }
+