X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fmapping%2FDiagramToCompositeMapping3.java.old;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fmapping%2FDiagramToCompositeMapping3.java.old;h=ecd4290e48f7fb8e03fd6a20f74950834438c615;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/DiagramToCompositeMapping3.java.old b/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/DiagramToCompositeMapping3.java.old new file mode 100644 index 000000000..ecd4290e4 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/DiagramToCompositeMapping3.java.old @@ -0,0 +1,262 @@ +/******************************************************************************* + * 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 { + + @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 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 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); + } +}