--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.mapping;\r
+\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.SyncReadGraph;\r
+import org.simantics.db.exceptions.ResourceNotFoundException;\r
+import org.simantics.db.procedure.SingleProcedure;\r
+import org.simantics.db.requests.SingleRead;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;\r
+import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
+import org.simantics.layer0.utils.binaryPredicates.Relation;\r
+import org.simantics.layer0.utils.binaryPredicates.UnionPredicate;\r
+import org.simantics.layer0.utils.predicates.IUnaryPredicate;\r
+import org.simantics.layer0.utils.predicates.Type;\r
+import org.simantics.layer0.utils.triggers.IModification;\r
+import org.simantics.layer0.utils.triggers.Trigger;\r
+import org.simantics.mapping.constraint.instructions.AndInstruction;\r
+import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction;\r
+import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery;\r
+import org.simantics.mapping.constraint.instructions.ExistsInstruction;\r
+import org.simantics.mapping.constraint.instructions.IInstruction;\r
+import org.simantics.mapping.constraint.instructions.TripletInstruction;\r
+import org.simantics.mapping.constraint.instructions.TripletPredicateObjectQuery;\r
+import org.simantics.mapping.constraint.instructions.TripletSubjectPredicateQuery;\r
+import org.simantics.mapping.constraint.instructions.UnaryPredicateInstruction;\r
+import org.simantics.mapping.rule.instructions.AndRuleInstruction;\r
+import org.simantics.mapping.rule.instructions.ClaimRuleInstruction;\r
+import org.simantics.mapping.rule.instructions.IRuleInstruction;\r
+import org.simantics.mapping.rule.instructions.IfRuleInstruction;\r
+import org.simantics.mapping.rule.instructions.QueryRuleInstruction;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+public class DiagramToCompositeMapping3 extends Trigger {\r
+ static int VARIABLE_COUNT = 0;\r
+ \r
+ final static int Diagram = VARIABLE_COUNT++; // 0\r
+ final static int ParentType = VARIABLE_COUNT++; // 1\r
+ final static int Configuration = VARIABLE_COUNT++; // 2\r
+ final static int Element = VARIABLE_COUNT++; // 3\r
+ final static int ElementType = VARIABLE_COUNT++; // 4\r
+ final static int ComponentType = VARIABLE_COUNT++; // 5\r
+ final static int Component = VARIABLE_COUNT++; // 6\r
+ final static int Component2 = VARIABLE_COUNT++; // 6\r
+ \r
+ final static int DiagramConnectionRelation = VARIABLE_COUNT++; // 7\r
+ final static int DiagramConnectionRelation2 = VARIABLE_COUNT++; // 7\r
+ final static int CElement = VARIABLE_COUNT++; // 8\r
+ final static int CElement2 = VARIABLE_COUNT++; // 8\r
+ final static int ConnectionRelation = VARIABLE_COUNT++; // 9\r
+ final static int ConnectionRelation2 = VARIABLE_COUNT++; // 9\r
+ final static int Connector = VARIABLE_COUNT++; // 10 \r
+ final static int Connector2 = VARIABLE_COUNT++; // 10 \r
+ final static int Connection = VARIABLE_COUNT++; // 11\r
+\r
+ final static int Connection2 = VARIABLE_COUNT++; // 12\r
+ final static int Connection3 = VARIABLE_COUNT++; // 12\r
+ \r
+ \r
+ /* \r
+ * \r
+ */\r
+ static IRuleInstruction createMappingRule(SyncReadGraph g) throws ResourceNotFoundException { \r
+ Builtins b = g.getBuiltins();\r
+ DiagramResource dr = DiagramResource.getInstance(g);\r
+ ModelingResources mr = ModelingResources.getInstance(g);\r
+ Resource stream = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Types#VLStream");\r
+ Resource streamFrom = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamFrom");\r
+ Resource streamTo = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamTo");\r
+ final StructuralResource2 sr = StructuralResource2.getInstance(g);\r
+ IBinaryPredicate instanceOf = new Relation(b.InstanceOf); \r
+ IBinaryPredicate mapsToComponent = new Relation(mr.ElementToComponent);\r
+ IBinaryPredicate mapsToComponentType = new Relation(mr.SymbolToComponentType);\r
+\r
+ IBinaryPredicate mapsToConnection = new Relation(mr.DiagramConnectionToConnection);\r
+ IBinaryPredicate mapsToConnectionRelation = new Relation(mr.DiagramConnectionRelationToConnectionRelation);\r
+ IBinaryPredicate flagToParameter = new Relation(mr.FlagToParameter);\r
+ IUnaryPredicate mapped = new Tag(mr.Mapped);\r
+ IUnaryPredicate connection = new Type(dr.Connection);\r
+ IRuleInstruction rule = \r
+ new AndRuleInstruction( \r
+ new IfRuleInstruction(\r
+ // Condition\r
+ new AndInstruction(\r
+ new BinaryPredicateObjectQuery(Configuration, ParentType, \r
+ new UnionPredicate(\r
+ new Relation(sr.IsDefinedBy).inverse(g),\r
+ new Relation(b.HasConfiguration).inverse(g)\r
+ )),\r
+ new BinaryPredicateObjectQuery(Diagram, Element, OrderedSetElementsPredicate.INSTANCE)\r
+ ),\r
+ // Then rule \r
+ new QueryRuleInstruction(\r
+ new IfRuleInstruction(\r
+ new AndInstruction(\r
+ new BinaryPredicateObjectQuery(Element, ElementType, instanceOf),\r
+ new BinaryPredicateObjectQuery(ElementType, ComponentType, mapsToComponentType)\r
+ ),\r
+ new ClaimRuleInstruction(\r
+ new AndInstruction( \r
+ new ExistsInstruction(\r
+ new BinaryPredicateObjectQuery(Element, Component, mapsToComponent),\r
+ Component\r
+ ),\r
+ new BinaryPredicateInstruction(Component, ComponentType, instanceOf),\r
+ new BinaryPredicateInstruction(Component, Configuration, new Relation(b.PartOf)),\r
+ new UnaryPredicateInstruction(Component, mapped)\r
+ )\r
+ ),\r
+ new IfRuleInstruction(\r
+ new UnaryPredicateInstruction(Element, connection),\r
+ new ClaimRuleInstruction(\r
+ new AndInstruction( \r
+ new ExistsInstruction(\r
+ new BinaryPredicateObjectQuery(Element, Connection, mapsToConnection),\r
+ Connection\r
+ ),\r
+ new UnaryPredicateInstruction(Connection, new Type(stream)),\r
+ new BinaryPredicateInstruction(Connection, Configuration, new Relation(b.PartOf)),\r
+ new UnaryPredicateInstruction(Connection, mapped) \r
+ ),\r
+ new AndRuleInstruction( \r
+ new IfRuleInstruction(\r
+ new AndInstruction(\r
+ new BinaryPredicateObjectQuery(Element, Connector, new Relation(dr.HasInput)),\r
+ new TripletSubjectPredicateQuery(CElement, DiagramConnectionRelation, Connector, sr.IsConnected),\r
+ new BinaryPredicateObjectQuery(DiagramConnectionRelation, ConnectionRelation, mapsToConnectionRelation),\r
+ new BinaryPredicateObjectQuery(CElement, Component, mapsToComponent)\r
+ ),\r
+ new ClaimRuleInstruction(\r
+ new AndInstruction( \r
+ new ExistsInstruction(\r
+ new TripletPredicateObjectQuery(Component, ConnectionRelation, Connection2, sr.IsConnected),\r
+ Connection2\r
+ ),\r
+ new UnaryPredicateInstruction(Connection2, new Type(sr.Connection)),\r
+ new BinaryPredicateInstruction(Connection, Connection2, new Relation(streamFrom)),\r
+ new UnaryPredicateInstruction(Connection2, mapped) \r
+ )\r
+\r
+ )\r
+ ),\r
+ new IfRuleInstruction(\r
+ new AndInstruction(\r
+ new BinaryPredicateObjectQuery(Element, Connector2, new Relation(dr.HasOutput)),\r
+ new TripletSubjectPredicateQuery(CElement2, DiagramConnectionRelation2, Connector2, sr.IsConnected),\r
+ new BinaryPredicateObjectQuery(DiagramConnectionRelation2, ConnectionRelation2, mapsToConnectionRelation),\r
+ new BinaryPredicateObjectQuery(CElement2, Component2, mapsToComponent)\r
+ ),\r
+ new ClaimRuleInstruction(\r
+ new AndInstruction( \r
+ new ExistsInstruction(\r
+ new TripletPredicateObjectQuery(Component2, ConnectionRelation2, Connection3, sr.IsConnected),\r
+ Connection3\r
+ ),\r
+ new UnaryPredicateInstruction(Connection3, new Type(sr.Connection)),\r
+ new BinaryPredicateInstruction(Connection, Connection3, new Relation(streamTo)),\r
+ new UnaryPredicateInstruction(Connection3, mapped) \r
+ )\r
+\r
+ )\r
+ )\r
+ )\r
+ ) \r
+ )\r
+ \r
+ )\r
+ )\r
+ )\r
+ ); \r
+ /*\r
+ StringBuilder sb = new StringBuilder();\r
+ rule.toString(sb, 0);\r
+ System.out.println(sb);\r
+ */\r
+ return rule;\r
+ } \r
+\r
+ static IRuleInstruction profile(String name, IRuleInstruction rule) {\r
+ //return new ProfileRuleInstruction(name, rule);\r
+ return rule;\r
+ }\r
+\r
+ static IInstruction profile(String name, IInstruction inst) {\r
+ //return new ProfileInstruction(name, inst);\r
+ return inst;\r
+ }\r
+\r
+ static class RuleQuery implements SingleRead<IRuleInstruction> {\r
+\r
+ @Override\r
+ public boolean equals(Object other) {\r
+ return other != null && getClass().equals(other.getClass());\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return getClass().hashCode();\r
+ }\r
+\r
+ @Override\r
+ public void perform(SyncReadGraph g,\r
+ SingleProcedure<IRuleInstruction> procedure) {\r
+ try {\r
+ procedure.execute(g, createMappingRule(g));\r
+ return;\r
+ } catch (ResourceNotFoundException e) {\r
+ e.printStackTrace(); \r
+ }\r
+ procedure.execute(g, null);\r
+ \r
+ }\r
+ \r
+ }\r
+ \r
+ IRuleInstruction instruction;\r
+ Resource source;\r
+ Resource target;\r
+ \r
+ public DiagramToCompositeMapping3(SyncReadGraph g, Resource mapping) {\r
+ this.instruction = g.syncRequest(new RuleQuery());\r
+ this.source = g.getSingleObject(mapping, g.getInverse2(g.getBuiltins().HasTrigger));\r
+ this.target = g.getSingleObject(this.source, ModelingResources.getInstance(g).DiagramToComposite);\r
+ }\r
+ \r
+ @Override\r
+ public boolean equals(Object other) {\r
+ if(this==other)\r
+ return true;\r
+ if(!(other instanceof DiagramToCompositeMapping3))\r
+ return false;\r
+ DiagramToCompositeMapping3 map = (DiagramToCompositeMapping3)other;\r
+ return map.source.equals(source) && map.target.equals(target);\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ return source.hashCode() + 31 * target.hashCode();\r
+ }\r
+\r
+ @Override\r
+ public void perform(SyncReadGraph g,\r
+ SingleProcedure<IModification> procedure) {\r
+ final Object[] bindings = new Object[VARIABLE_COUNT];\r
+ bindings[Diagram] = source;\r
+ bindings[Configuration] = target;\r
+ IModification modi = instruction.execute(g, bindings);\r
+// System.out.println("modi = " + modi);\r
+ procedure.execute(g, modi); \r
+ }\r
+}\r