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