1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.modeling.mapping;
13 import org.simantics.db.Builtins;
14 import org.simantics.db.Resource;
15 import org.simantics.db.SyncReadGraph;
16 import org.simantics.db.exceptions.ResourceNotFoundException;
17 import org.simantics.db.procedure.SingleProcedure;
18 import org.simantics.db.requests.SingleRead;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.layer0.utils.binaryPredicates.IBinaryPredicate;
21 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
22 import org.simantics.layer0.utils.binaryPredicates.Relation;
23 import org.simantics.layer0.utils.binaryPredicates.UnionPredicate;
24 import org.simantics.layer0.utils.predicates.IUnaryPredicate;
25 import org.simantics.layer0.utils.predicates.Type;
26 import org.simantics.layer0.utils.triggers.IModification;
27 import org.simantics.layer0.utils.triggers.Trigger;
28 import org.simantics.mapping.constraint.instructions.AndInstruction;
29 import org.simantics.mapping.constraint.instructions.BinaryPredicateInstruction;
30 import org.simantics.mapping.constraint.instructions.BinaryPredicateObjectQuery;
31 import org.simantics.mapping.constraint.instructions.ExistsInstruction;
32 import org.simantics.mapping.constraint.instructions.IInstruction;
33 import org.simantics.mapping.constraint.instructions.TripletInstruction;
34 import org.simantics.mapping.constraint.instructions.TripletPredicateObjectQuery;
35 import org.simantics.mapping.constraint.instructions.TripletSubjectPredicateQuery;
36 import org.simantics.mapping.constraint.instructions.UnaryPredicateInstruction;
37 import org.simantics.mapping.rule.instructions.AndRuleInstruction;
38 import org.simantics.mapping.rule.instructions.ClaimRuleInstruction;
39 import org.simantics.mapping.rule.instructions.IRuleInstruction;
40 import org.simantics.mapping.rule.instructions.IfRuleInstruction;
41 import org.simantics.mapping.rule.instructions.QueryRuleInstruction;
42 import org.simantics.modeling.ModelingResources;
43 import org.simantics.structural.stubs.StructuralResource2;
45 public class DiagramToCompositeMapping3 extends Trigger {
46 static int VARIABLE_COUNT = 0;
48 final static int Diagram = VARIABLE_COUNT++; // 0
49 final static int ParentType = VARIABLE_COUNT++; // 1
50 final static int Configuration = VARIABLE_COUNT++; // 2
51 final static int Element = VARIABLE_COUNT++; // 3
52 final static int ElementType = VARIABLE_COUNT++; // 4
53 final static int ComponentType = VARIABLE_COUNT++; // 5
54 final static int Component = VARIABLE_COUNT++; // 6
55 final static int Component2 = VARIABLE_COUNT++; // 6
57 final static int DiagramConnectionRelation = VARIABLE_COUNT++; // 7
58 final static int DiagramConnectionRelation2 = VARIABLE_COUNT++; // 7
59 final static int CElement = VARIABLE_COUNT++; // 8
60 final static int CElement2 = VARIABLE_COUNT++; // 8
61 final static int ConnectionRelation = VARIABLE_COUNT++; // 9
62 final static int ConnectionRelation2 = VARIABLE_COUNT++; // 9
63 final static int Connector = VARIABLE_COUNT++; // 10
64 final static int Connector2 = VARIABLE_COUNT++; // 10
65 final static int Connection = VARIABLE_COUNT++; // 11
67 final static int Connection2 = VARIABLE_COUNT++; // 12
68 final static int Connection3 = VARIABLE_COUNT++; // 12
74 static IRuleInstruction createMappingRule(SyncReadGraph g) throws ResourceNotFoundException {
75 Builtins b = g.getBuiltins();
76 DiagramResource dr = DiagramResource.getInstance(g);
77 ModelingResources mr = ModelingResources.getInstance(g);
78 Resource stream = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Types#VLStream");
79 Resource streamFrom = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamFrom");
80 Resource streamTo = g.getResourceByURI2("http://www.vtt.fi/Simantics/Balas/1.0/Relations#StreamTo");
81 final StructuralResource2 sr = StructuralResource2.getInstance(g);
82 IBinaryPredicate instanceOf = new Relation(b.InstanceOf);
83 IBinaryPredicate mapsToComponent = new Relation(mr.ElementToComponent);
84 IBinaryPredicate mapsToComponentType = new Relation(mr.SymbolToComponentType);
86 IBinaryPredicate mapsToConnection = new Relation(mr.DiagramConnectionToConnection);
87 IBinaryPredicate mapsToConnectionRelation = new Relation(mr.DiagramConnectionRelationToConnectionRelation);
88 IBinaryPredicate flagToParameter = new Relation(mr.FlagToParameter);
89 IUnaryPredicate mapped = new Tag(mr.Mapped);
90 IUnaryPredicate connection = new Type(dr.Connection);
91 IRuleInstruction rule =
92 new AndRuleInstruction(
93 new IfRuleInstruction(
96 new BinaryPredicateObjectQuery(Configuration, ParentType,
98 new Relation(sr.IsDefinedBy).inverse(g),
99 new Relation(b.HasConfiguration).inverse(g)
101 new BinaryPredicateObjectQuery(Diagram, Element, OrderedSetElementsPredicate.INSTANCE)
104 new QueryRuleInstruction(
105 new IfRuleInstruction(
107 new BinaryPredicateObjectQuery(Element, ElementType, instanceOf),
108 new BinaryPredicateObjectQuery(ElementType, ComponentType, mapsToComponentType)
110 new ClaimRuleInstruction(
112 new ExistsInstruction(
113 new BinaryPredicateObjectQuery(Element, Component, mapsToComponent),
116 new BinaryPredicateInstruction(Component, ComponentType, instanceOf),
117 new BinaryPredicateInstruction(Component, Configuration, new Relation(b.PartOf)),
118 new UnaryPredicateInstruction(Component, mapped)
121 new IfRuleInstruction(
122 new UnaryPredicateInstruction(Element, connection),
123 new ClaimRuleInstruction(
125 new ExistsInstruction(
126 new BinaryPredicateObjectQuery(Element, Connection, mapsToConnection),
129 new UnaryPredicateInstruction(Connection, new Type(stream)),
130 new BinaryPredicateInstruction(Connection, Configuration, new Relation(b.PartOf)),
131 new UnaryPredicateInstruction(Connection, mapped)
133 new AndRuleInstruction(
134 new IfRuleInstruction(
136 new BinaryPredicateObjectQuery(Element, Connector, new Relation(dr.HasInput)),
137 new TripletSubjectPredicateQuery(CElement, DiagramConnectionRelation, Connector, sr.IsConnected),
138 new BinaryPredicateObjectQuery(DiagramConnectionRelation, ConnectionRelation, mapsToConnectionRelation),
139 new BinaryPredicateObjectQuery(CElement, Component, mapsToComponent)
141 new ClaimRuleInstruction(
143 new ExistsInstruction(
144 new TripletPredicateObjectQuery(Component, ConnectionRelation, Connection2, sr.IsConnected),
147 new UnaryPredicateInstruction(Connection2, new Type(sr.Connection)),
148 new BinaryPredicateInstruction(Connection, Connection2, new Relation(streamFrom)),
149 new UnaryPredicateInstruction(Connection2, mapped)
154 new IfRuleInstruction(
156 new BinaryPredicateObjectQuery(Element, Connector2, new Relation(dr.HasOutput)),
157 new TripletSubjectPredicateQuery(CElement2, DiagramConnectionRelation2, Connector2, sr.IsConnected),
158 new BinaryPredicateObjectQuery(DiagramConnectionRelation2, ConnectionRelation2, mapsToConnectionRelation),
159 new BinaryPredicateObjectQuery(CElement2, Component2, mapsToComponent)
161 new ClaimRuleInstruction(
163 new ExistsInstruction(
164 new TripletPredicateObjectQuery(Component2, ConnectionRelation2, Connection3, sr.IsConnected),
167 new UnaryPredicateInstruction(Connection3, new Type(sr.Connection)),
168 new BinaryPredicateInstruction(Connection, Connection3, new Relation(streamTo)),
169 new UnaryPredicateInstruction(Connection3, mapped)
183 StringBuilder sb = new StringBuilder();
184 rule.toString(sb, 0);
185 System.out.println(sb);
190 static IRuleInstruction profile(String name, IRuleInstruction rule) {
191 //return new ProfileRuleInstruction(name, rule);
195 static IInstruction profile(String name, IInstruction inst) {
196 //return new ProfileInstruction(name, inst);
200 static class RuleQuery implements SingleRead<IRuleInstruction> {
203 public boolean equals(Object other) {
204 return other != null && getClass().equals(other.getClass());
208 public int hashCode() {
209 return getClass().hashCode();
213 public void perform(SyncReadGraph g,
214 SingleProcedure<IRuleInstruction> procedure) {
216 procedure.execute(g, createMappingRule(g));
218 } catch (ResourceNotFoundException e) {
221 procedure.execute(g, null);
227 IRuleInstruction instruction;
231 public DiagramToCompositeMapping3(SyncReadGraph g, Resource mapping) {
232 this.instruction = g.syncRequest(new RuleQuery());
233 this.source = g.getSingleObject(mapping, g.getInverse2(g.getBuiltins().HasTrigger));
234 this.target = g.getSingleObject(this.source, ModelingResources.getInstance(g).DiagramToComposite);
238 public boolean equals(Object other) {
241 if(!(other instanceof DiagramToCompositeMapping3))
243 DiagramToCompositeMapping3 map = (DiagramToCompositeMapping3)other;
244 return map.source.equals(source) && map.target.equals(target);
248 public int hashCode() {
249 return source.hashCode() + 31 * target.hashCode();
253 public void perform(SyncReadGraph g,
254 SingleProcedure<IModification> procedure) {
255 final Object[] bindings = new Object[VARIABLE_COUNT];
256 bindings[Diagram] = source;
257 bindings[Configuration] = target;
258 IModification modi = instruction.execute(g, bindings);
259 // System.out.println("modi = " + modi);
260 procedure.execute(g, modi);