X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.structural2%2Fsrc%2Forg%2Fsimantics%2Fstructural2%2FFunctions.java;h=8bf4e3df68b6ac62dc30b2575ddb3eb75b1b9bf2;hp=d9d8857af77210acfa2be0f1d170981f547ca2a5;hb=45ddae414b7f632d52546c3c46b0f6d2b4ab37af;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java index d9d8857af..8bf4e3df6 100644 --- a/bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java +++ b/bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java @@ -23,11 +23,14 @@ import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.common.issue.StandardIssue; +import org.simantics.db.common.primitiverequest.IsInstanceOf; import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.common.request.PossibleObjectWithType; import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.request.TernaryRead; import org.simantics.db.common.uri.UnescapedChildMapOfResource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.function.All; @@ -47,11 +50,14 @@ import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.VariableMap; import org.simantics.db.layer0.variable.VariableMapImpl; import org.simantics.db.layer0.variable.VariableNode; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.db.service.CollectionSupport; import org.simantics.issues.common.IssueUtils; import org.simantics.layer0.Layer0; import org.simantics.scl.reflection.annotations.SCLValue; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.tuple.Tuple2; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.simulator.variable.NodeManager; import org.simantics.structural.stubs.StructuralResource2; @@ -66,14 +72,12 @@ import org.simantics.structural2.queries.ConnectionPointMapOfResource; import org.simantics.structural2.queries.PossibleConnectionPointInfo; import org.simantics.structural2.scl.CompileStructuralValueRequest; import org.simantics.structural2.scl.procedural.CompileProceduralComponentTypeRequest; +import org.simantics.structural2.utils.StructuralUtils.StructuralComponentClass; import org.simantics.structural2.variables.Connection; -import org.simantics.structural2.variables.ConnectionBrowser; import org.simantics.structural2.variables.StandardProceduralChildVariable; -import org.simantics.structural2.variables.VariableConnectionPointDescriptor; import org.simantics.utils.datastructures.MapList; import gnu.trove.map.hash.THashMap; -import gnu.trove.set.hash.THashSet; public class Functions { @@ -153,39 +157,6 @@ public class Functions { }; - static class ConnectionImpl implements Connection { - - final private StandardGraphPropertyVariable connectionPoint; - - public ConnectionImpl(StandardGraphPropertyVariable connectionPoint) { - this.connectionPoint = connectionPoint; - } - - @Override - public Collection getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException { - Set result = new THashSet(); - for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType)) { - result.add(desc.getVariable(graph)); - } - return result; - } - - @Override - public Collection getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException { - Set result = new THashSet(); - for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType)) { - result.add(desc.getURI(graph)); - } - return result; - } - - @Override - public Collection getConnectionPointDescriptors(ReadGraph graph, Resource relationType) throws DatabaseException { - return ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType); - } - - } - @SCLValue(type="ValueAccessor") public static final ValueAccessor connectionValueAccessor = new ValueAccessor() { @@ -213,7 +184,7 @@ public class Functions { @Override public Object getValue(ReadGraph graph, Variable context) throws DatabaseException { StandardGraphPropertyVariable variable = (StandardGraphPropertyVariable)context; - return new ConnectionImpl(variable); + return new ConnectionImpl(context.getParent(graph), variable.property.predicate); } @Override @@ -233,10 +204,10 @@ public class Functions { public Variable getPossibleConnectionPointFromContext(ReadGraph graph, Variable variable, Resource context, String name) throws DatabaseException { - Map connectionPoints = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.>instance()); - Resource cp = connectionPoints.get(name); + Map connectionPoints = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.>instance()); + PropertyInfo cp = connectionPoints.get(name); if(cp == null) return null; - else return new StandardGraphPropertyVariable(graph, variable, cp); + else return new StandardGraphPropertyVariable(graph, variable, cp.predicate); } @@ -244,16 +215,16 @@ public class Functions { if(graph.isImmutable(context)) { - Map cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.>instance()); + Map cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.>instance()); if(cps.size() == 0) return map; if(map == null) map = new THashMap(cps.size()); - for(Map.Entry entry : cps.entrySet()) { + for(Map.Entry entry : cps.entrySet()) { String name = entry.getKey(); - Resource cp = entry.getValue(); - if(needSynchronized && !graph.isInstanceOf(cp, STR.SynchronizedConnectionRelation)) continue; - map.put(name, new StandardGraphPropertyVariable(graph, variable, cp)); + PropertyInfo cp = entry.getValue(); + if(needSynchronized && !graph.isInstanceOf(cp.predicate, STR.SynchronizedConnectionRelation)) continue; + map.put(name, new StandardGraphPropertyVariable(graph, variable, cp.predicate)); } return map; @@ -313,78 +284,30 @@ public class Functions { }; - static class StructuralChildMapOfResource extends ResourceRead> { - - public StructuralChildMapOfResource(Resource resource) { - super(resource); - } - - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - StructuralResource2 STR = StructuralResource2.getInstance(graph); - Resource type = graph.getPossibleType(resource, STR.Component); - if(type != null) { - Resource definition = graph.getPossibleObject(type, STR.IsDefinedBy); - if(definition != null) { - Map map = graph.syncRequest(new UnescapedChildMapOfResource(definition)); - if (!map.isEmpty()) - return map; - } - } - Map directChildren = graph.syncRequest(new UnescapedChildMapOfResource(resource)); - return directChildren; - } - - } - - static class StructuralChildMapOfResourceT extends ResourceRead> { - - public StructuralChildMapOfResourceT(Resource resource) { - super(resource); - } - - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - StructuralResource2 STR = StructuralResource2.getInstance(graph); - Resource definition = graph.getPossibleObject(resource, STR.IsDefinedBy); - if(definition != null) { - Map map = graph.syncRequest(new UnescapedChildMapOfResource(definition)); - if (!map.isEmpty()) - return map; - } - return Collections.emptyMap(); - } - - } - - static class StructuralRunChildMapOfResource extends ResourceRead> { + static class StructuralRunContext extends ResourceRead { - public StructuralRunChildMapOfResource(Resource resource) { + public StructuralRunContext(Resource resource) { super(resource); } - public Map fromContext(ReadGraph graph, Resource context) throws DatabaseException { - return graph.sync(new StructuralChildMapOfResource(context)); - } - @Override - public Map perform(ReadGraph graph) throws DatabaseException { + public Resource perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); SimulationResource SIMU = SimulationResource.getInstance(graph); Resource model = graph.sync(new PossibleIndexRoot(resource)); if(graph.isInstanceOf(model, L0.RVIContext)) { - return fromContext(graph, model); + return model; } Resource configuration = graph.getPossibleObject(model, SIMU.HasConfiguration); if(configuration != null) { if(graph.isInstanceOf(configuration, L0.RVIContext)) { - return fromContext(graph, configuration); + return configuration; } } - - return Collections.emptyMap(); - + + return null; + } } @@ -410,10 +333,11 @@ public class Functions { public static List getProceduralDesc(ReadGraph graph, final Variable context) throws DatabaseException { StructuralResource2 STR = StructuralResource2.getInstance(graph); - final Resource type = context.getPossibleType(graph); + Variable config = Variables.getConfigurationVariable(graph, context); + final Resource type = config.getPossibleType(graph); if(type != null) { if(graph.isInstanceOf(type, STR.ProceduralComponentType)) { - return graph.syncRequest(new SubstructureRequest(context)); + return graph.syncRequest(new SubstructureRequest(config)); } } return null; @@ -488,60 +412,260 @@ public class Functions { } } + public static class StructuralTypeOverrideMap extends ResourceRead> { + + protected StructuralTypeOverrideMap(Resource composite) { + super(composite); + } + + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + + StructuralResource2 STR = StructuralResource2.getInstance(graph); + + CollectionSupport cs = graph.getService(CollectionSupport.class); + + Map 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; + } + + } + + private static class StructuralOverrideDataWalkRequest + extends TernaryRead { + + 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 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 { + + 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.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 { + + 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 children = graph.syncRequest(new UnescapedChildMapOfResource(def), TransientCacheListener.instance()); + return new Tuple2(clazz, children); + } + return new Tuple2(clazz, null); + } + + } + @SCLValue(type = "VariableMap") public static VariableMap structuralChildDomainChildren = new VariableMapImpl() { - + @Override public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException { + StructuralResource2 STR = StructuralResource2.getInstance(graph); + Resource type = context.getPossibleType(graph, STR.Component); + if(type == null) return null; + + Tuple2 result = graph.syncRequest(new StructureTypeAndChildMapRequest(type), TransientCacheListener.instance()); + StructuralComponentClass clazz = (StructuralComponentClass) result.c0; - final Resource type = context.getPossibleType(graph); - if(type != null) { - StructuralResource2 STR = StructuralResource2.getInstance(graph); - if(graph.isInstanceOf(type, STR.ProceduralComponentType)) { - Map map = graph.syncRequest(new ProceduralSubstructureRequest(context), - TransientCacheListener.>instance()); - if(map != null) return map.get(name); - } - } - - Resource represents = context.getPossibleRepresents(graph); - if(represents == null) { - Map children = graph.syncRequest(new StructuralChildMapOfResourceT(type)); + if(StructuralComponentClass.PROCEDURAL.equals(clazz)) { + Map map = graph.syncRequest(new ProceduralSubstructureRequest(context), + TransientCacheListener.>instance()); + if(map != null) return map.get(name); + return null; + } else if (StructuralComponentClass.DEFINED.equals(clazz)) { + Map children = (Map) result.c1; Resource child = children.get(name); - return All.getStandardChildDomainChildVariable(graph, context, child, name); - } - Map children = graph.syncRequest(new StructuralChildMapOfResource(represents)); - Resource child = children.get(name); - return All.getStandardChildDomainChildVariable(graph, context, child, name); + if(child == null) return null; + return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name); + } else { + Resource represents = context.getPossibleRepresents(graph); + if(represents == null) return null; + Map children = graph.syncRequest(new UnescapedChildMapOfResource(represents), TransientCacheListener.instance()); + Resource child = children.get(name); + return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name); + } + } @Override public Map getVariables(ReadGraph graph, Variable context, Map map) throws DatabaseException { - final Resource type = context.getPossibleType(graph); - if(type != null) { - StructuralResource2 STR = StructuralResource2.getInstance(graph); - if(graph.isInstanceOf(type, STR.ProceduralComponentType)) { - Map mapPrime = graph.syncRequest(new ProceduralSubstructureRequest(context), - TransientCacheListener.>instance()); - if(mapPrime != null) { - if(map != null) { - map.putAll(mapPrime); - return map; - } - else - return mapPrime; - } - } - } - Resource represents = context.getPossibleRepresents(graph); - if(represents == null) { - Map children = graph.syncRequest(new StructuralChildMapOfResourceT(type)); + Resource type = context.getPossibleType(graph); + if(type == null) return null; + + StructuralComponentClass clazz = StructuralComponentClass.get(graph, type); + if(StructuralComponentClass.PROCEDURAL.equals(clazz)) { + Map mapPrime = graph.syncRequest(new ProceduralSubstructureRequest(context), + TransientCacheListener.>instance()); + if(mapPrime != null) { + if(map != null) { + map.putAll(mapPrime); + return map; + } + else + return mapPrime; + } + return map; + } else if (StructuralComponentClass.DEFINED.equals(clazz)) { + StructuralResource2 STR = StructuralResource2.getInstance(graph); + Resource def = graph.getSingleObject(type, STR.IsDefinedBy); + Map children = graph.syncRequest(new UnescapedChildMapOfResource(def), TransientCacheListener.instance()); return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map); - } - Map children = graph.syncRequest(new StructuralChildMapOfResource(represents)); - return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map); + } else { + Resource represents = context.getPossibleRepresents(graph); + if(represents == null) return null; + Map children = graph.syncRequest(new UnescapedChildMapOfResource(represents)); + return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map); + } + } }; @@ -551,15 +675,18 @@ public class Functions { @Override public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException { - Map children = graph.syncRequest(new StructuralRunChildMapOfResource(context.getRepresents(graph))); + Resource ctx = graph.syncRequest(new StructuralRunContext(context.getRepresents(graph))); + if(ctx == null) return null; + Map children = graph.syncRequest(new UnescapedChildMapOfResource(ctx)); Resource child = children.get(name); return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name); } @Override public Map getVariables(ReadGraph graph, Variable context, Map map) throws DatabaseException { - StandardGraphChildVariable variable = (StandardGraphChildVariable)context; - Map children = graph.syncRequest(new StructuralRunChildMapOfResource(variable.resource)); + Resource ctx = graph.syncRequest(new StructuralRunContext(context.getRepresents(graph))); + if(ctx == null) return map; + Map children = graph.syncRequest(new UnescapedChildMapOfResource(ctx)); return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map); } @@ -624,7 +751,7 @@ public class Functions { for(Resource req : requiredConnections) { if(!connections.contains(req)) { - result.add(new StandardIssue(sr.ConnectionConstraint_ErrorIssue, component, req)); + result.add(new StandardIssue(sr.ConnectionValidationConstraint_ErrorIssue, component, req)); } } @@ -743,7 +870,7 @@ public class Functions { } - final Collection interfaces = graph.syncRequest(new DefinedUCInterfaceMap(type)); + final Collection interfaces = graph.syncRequest(new DefinedUCInterfaceMap(type), TransientCacheListener.instance()); if(interfaces != null) return interfaces; } @@ -765,38 +892,6 @@ public class Functions { } - static class DefinedUCInterfaceMap extends ResourceRead> { - - public DefinedUCInterfaceMap(Resource resource) { - super(resource); - } - - @Override - public Collection perform(ReadGraph graph) - throws DatabaseException { - - StructuralResource2 STR = StructuralResource2.getInstance(graph); - Resource definition = graph.getPossibleObject(resource, STR.IsDefinedBy); - if(definition != null) { - Collection result = new ArrayList(); - Layer0 L0 = Layer0.getInstance(graph); - for(Resource cp : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, STR.ConnectionRelation))) { - String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING); - for(Resource conn : graph.getObjects(cp, STR.IsBoundBy)) { - Statement stm = graph.getPossibleStatement(conn, STR.Connects); - if(stm == null) continue; - Resource component = stm.getObject(); - String componentName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING); - result.add(new InterfaceResolution(cp, cpName, componentName, graph.getInverse(stm.getPredicate()))); - } - } - return result; - } - return null; - } - - } - public static final Collection BUILTIN_STRUCTURAL_CPS = new ArrayList(); @SCLValue(type = "ReadGraph -> Resource -> Variable -> a") @@ -808,12 +903,12 @@ public class Functions { SCLContext sclContext = SCLContext.getCurrent(); Object oldGraph = sclContext.get("graph"); try { - Function1 exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) { + Function1 exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) { protected String getExpressionText(ReadGraph graph) throws DatabaseException { return expression; } }, - TransientCacheListener.>instance()); + TransientCacheListener.instance()); sclContext.put("graph", graph); return exp.apply(context); } catch (DatabaseException e) {