X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.mapping%2Fsrc%2Forg%2Fsimantics%2Fmapping%2Fpattern%2FMappingRule.java;h=fdb83fa65a427d2e2cbae1ebc1572e859874a299;hb=1819be12c9a971df1dea5daa8190162ac1e20bb6;hp=445f53e1a26e5621efb8719e2be409274d08e0a5;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.mapping/src/org/simantics/mapping/pattern/MappingRule.java b/bundles/org.simantics.mapping/src/org/simantics/mapping/pattern/MappingRule.java index 445f53e1a..fdb83fa65 100644 --- a/bundles/org.simantics.mapping/src/org/simantics/mapping/pattern/MappingRule.java +++ b/bundles/org.simantics.mapping/src/org/simantics/mapping/pattern/MappingRule.java @@ -1,192 +1,192 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * 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.mapping.pattern; - - -public class MappingRule { - /* - static final THashSet EMPTY_SET = new THashSet(); - - THashMap resourcesByDomains = new THashMap(); - Collection constraints = new ArrayList(); - Collection constraintGroups = new ArrayList(); - - static class ConstraintGroup { - Collection constraints; - Collection targets; - Collection dependencies; - } - - class DirectedRuleBuilder { - THashSet sourceResources = new THashSet(); - THashSet targetResources = new THashSet(); - THashSet sourceConstraints = new THashSet(); - THashSet targetConstraints = new THashSet(); - TObjectIntHashMap boundResources; - int id; - - Instruction root = new DummyInstruction(); - Instruction cur = root; - - public DirectedRuleBuilder(final THashSet sourceDomains, - TObjectIntHashMap boundResources, int id) { - this.boundResources = boundResources; - this.id = id; - - // Classify resources - - resourcesByDomains.forEachEntry(new TObjectObjectProcedure() { - - @Override - public boolean execute(Resource domain, Resource resource) { - if(sourceDomains.contains(domain)) - sourceResources.add(resource); - else - targetResources.add(resource); - return true; - } - - }); - - // Classify constraints - - for(IConstraint constraint : constraints) { - if(sourceResources.containsAll(constraint.binds())) - sourceConstraints.add(constraint); - else - targetConstraints.add(constraint); - } - } - - int bestValue; - IConstraint bestConstraint; - - private void findBestConstraint(THashSet constraints) { - bestValue = 0; - bestConstraint = null; - sourceConstraints.forEach(new TObjectProcedure() { - - @Override - public boolean execute(IConstraint constraint) { - int temp = constraint.isApplicable(boundResources); - if(temp>bestValue) { - bestValue = temp; - bestConstraint = constraint; - } - return true; - } - - }); - } - - private void applyConstraint(THashSet resources, IConstraint constraint) { - THashSet unbounded = new THashSet(); - for(Resource r : constraint.binds()) - if(resources.remove(r)) { - unbounded.add(r); - boundResources.put(r, id++); - } - Instruction temp = constraint.createQueryInstruction(boundResources, unbounded); - cur.next = temp; - cur = temp; - } - - private void applyConstraints(THashSet resources, THashSet constraints) { - while(!constraints.isEmpty()) { - findBestConstraint(constraints); - if(bestConstraint != null) { - applyConstraint(resources, bestConstraint); - constraints.remove(bestConstraint); - } - else - throw new RuntimeException("Cannot form a directed mapping."); - } - } - - public void orderSourceConstraints() { - applyConstraints(sourceResources, sourceConstraints); - if(!sourceResources.isEmpty()) - throw new RuntimeException("Couldn't bind all source resources"); - } - - public void orderTargetConstraints() { - THashSet groups = new THashSet(constraintGroups); - while(!groups.isEmpty()) { - - // Find a suitable group - - final Ref possibleGroup = new Ref(); - final Collection removableGroups = new ArrayList(); - groups.forEach(new TObjectProcedure() { - - @Override - public boolean execute(ConstraintGroup group) { - for(Resource target : group.targets) - if(boundResources.containsKey(target)) { - removableGroups.add(group); - return true; - } - for(Resource dep : group.dependencies) - if(!boundResources.containsKey(dep)) - return true; - possibleGroup.value = group; - return false; - } - - }); - groups.removeAll(removableGroups); - if(possibleGroup.value == null) { - if(!groups.isEmpty()) - throw new RuntimeException("Couldn't find a suitable group."); - break; - } - groups.remove(possibleGroup.value); - ConstraintGroup group = possibleGroup.value; - - // Order group constraints - - THashSet groupConstraints = new THashSet(); - for(IConstraint constraint : group.constraints) - if(targetConstraints.remove(constraint)) - groupConstraints.add(constraint); - - applyConstraints(targetResources, groupConstraints); - - // Apply other constraints - - final Collection removableConstraints = new ArrayList(); - targetConstraints.forEach(new TObjectProcedure() { - - @Override - public boolean execute(IConstraint constraint) { - for(Resource r : constraint.binds()) - if(!boundResources.containsKey(r)) - return true; - - Instruction temp = constraint.createQueryInstruction(boundResources, EMPTY_SET); - cur.next = temp; - cur = temp; - - removableConstraints.add(constraint); - return true; - } - - }); - targetConstraints.removeAll(removableConstraints); - } - if(!targetResources.isEmpty()) - throw new RuntimeException("Couldn't bind all target resources"); - } - - } - */ -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * 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.mapping.pattern; + + +public class MappingRule { + /* + static final THashSet EMPTY_SET = new THashSet(); + + THashMap resourcesByDomains = new THashMap(); + Collection constraints = new ArrayList(); + Collection constraintGroups = new ArrayList(); + + static class ConstraintGroup { + Collection constraints; + Collection targets; + Collection dependencies; + } + + class DirectedRuleBuilder { + THashSet sourceResources = new THashSet(); + THashSet targetResources = new THashSet(); + THashSet sourceConstraints = new THashSet(); + THashSet targetConstraints = new THashSet(); + TObjectIntHashMap boundResources; + int id; + + Instruction root = new DummyInstruction(); + Instruction cur = root; + + public DirectedRuleBuilder(final THashSet sourceDomains, + TObjectIntHashMap boundResources, int id) { + this.boundResources = boundResources; + this.id = id; + + // Classify resources + + resourcesByDomains.forEachEntry(new TObjectObjectProcedure() { + + @Override + public boolean execute(Resource domain, Resource resource) { + if(sourceDomains.contains(domain)) + sourceResources.add(resource); + else + targetResources.add(resource); + return true; + } + + }); + + // Classify constraints + + for(IConstraint constraint : constraints) { + if(sourceResources.containsAll(constraint.binds())) + sourceConstraints.add(constraint); + else + targetConstraints.add(constraint); + } + } + + int bestValue; + IConstraint bestConstraint; + + private void findBestConstraint(THashSet constraints) { + bestValue = 0; + bestConstraint = null; + sourceConstraints.forEach(new TObjectProcedure() { + + @Override + public boolean execute(IConstraint constraint) { + int temp = constraint.isApplicable(boundResources); + if(temp>bestValue) { + bestValue = temp; + bestConstraint = constraint; + } + return true; + } + + }); + } + + private void applyConstraint(THashSet resources, IConstraint constraint) { + THashSet unbounded = new THashSet(); + for(Resource r : constraint.binds()) + if(resources.remove(r)) { + unbounded.add(r); + boundResources.put(r, id++); + } + Instruction temp = constraint.createQueryInstruction(boundResources, unbounded); + cur.next = temp; + cur = temp; + } + + private void applyConstraints(THashSet resources, THashSet constraints) { + while(!constraints.isEmpty()) { + findBestConstraint(constraints); + if(bestConstraint != null) { + applyConstraint(resources, bestConstraint); + constraints.remove(bestConstraint); + } + else + throw new RuntimeException("Cannot form a directed mapping."); + } + } + + public void orderSourceConstraints() { + applyConstraints(sourceResources, sourceConstraints); + if(!sourceResources.isEmpty()) + throw new RuntimeException("Couldn't bind all source resources"); + } + + public void orderTargetConstraints() { + THashSet groups = new THashSet(constraintGroups); + while(!groups.isEmpty()) { + + // Find a suitable group + + final Ref possibleGroup = new Ref(); + final Collection removableGroups = new ArrayList(); + groups.forEach(new TObjectProcedure() { + + @Override + public boolean execute(ConstraintGroup group) { + for(Resource target : group.targets) + if(boundResources.containsKey(target)) { + removableGroups.add(group); + return true; + } + for(Resource dep : group.dependencies) + if(!boundResources.containsKey(dep)) + return true; + possibleGroup.value = group; + return false; + } + + }); + groups.removeAll(removableGroups); + if(possibleGroup.value == null) { + if(!groups.isEmpty()) + throw new RuntimeException("Couldn't find a suitable group."); + break; + } + groups.remove(possibleGroup.value); + ConstraintGroup group = possibleGroup.value; + + // Order group constraints + + THashSet groupConstraints = new THashSet(); + for(IConstraint constraint : group.constraints) + if(targetConstraints.remove(constraint)) + groupConstraints.add(constraint); + + applyConstraints(targetResources, groupConstraints); + + // Apply other constraints + + final Collection removableConstraints = new ArrayList(); + targetConstraints.forEach(new TObjectProcedure() { + + @Override + public boolean execute(IConstraint constraint) { + for(Resource r : constraint.binds()) + if(!boundResources.containsKey(r)) + return true; + + Instruction temp = constraint.createQueryInstruction(boundResources, EMPTY_SET); + cur.next = temp; + cur = temp; + + removableConstraints.add(constraint); + return true; + } + + }); + targetConstraints.removeAll(removableConstraints); + } + if(!targetResources.isEmpty()) + throw new RuntimeException("Couldn't bind all target resources"); + } + + } + */ +}