19package denoptim.fragspace;
21import java.util.ArrayList;
22import java.util.HashSet;
23import java.util.LinkedHashMap;
27import denoptim.graph.APClass;
28import denoptim.graph.APMapping;
29import denoptim.graph.AttachmentPoint;
30import denoptim.graph.DGraph;
31import denoptim.graph.Vertex;
101 this(
fragSpace, vA, vB,
null, screenAll,
false,
true);
131 APMapping fixedRootAPs,
boolean screenAll,
132 boolean onlyCompleteMappings,
boolean compatibleIfFree)
134 this(
fragSpace, vA, vB, fixedRootAPs, screenAll, onlyCompleteMappings,
150 APMapping fixedRootAPs,
boolean screenAll,
151 boolean onlyCompleteMappings,
boolean compatibleIfFree,
156 List<AttachmentPoint> needyAPsA =
new ArrayList<AttachmentPoint>();
159 List<Vertex> subgraph =
new ArrayList<Vertex>();
163 List<AttachmentPoint> needyAPsB =
164 new ArrayList<AttachmentPoint>();
167 List<Vertex> subgraph =
new ArrayList<Vertex>();
173 fixedRootAPs, screenAll, onlyCompleteMappings, compatibleIfFree);
200 List<AttachmentPoint> lstA,
201 List<AttachmentPoint> needyAPsA,
202 List<AttachmentPoint> lstB,
203 List<AttachmentPoint> needyAPsB,
204 APMapping fixedRootAPs,
boolean screenAll,
205 boolean onlyCompleteMappings,
boolean compatibleIfFree)
207 this(
fragSpace, lstA, needyAPsA, lstB, needyAPsB, fixedRootAPs,
208 screenAll, onlyCompleteMappings, compatibleIfFree,
222 List<AttachmentPoint> lstA,
223 List<AttachmentPoint> needyAPsA,
224 List<AttachmentPoint> lstB,
225 List<AttachmentPoint> needyAPsB,
226 APMapping fixedRootAPs,
boolean screenAll,
227 boolean onlyCompleteMappings,
boolean compatibleIfFree,
233 fixedRootAPs, screenAll, onlyCompleteMappings, compatibleIfFree);
256 List<AttachmentPoint> needyAPsA,
257 List<AttachmentPoint> lstB,
258 List<AttachmentPoint> needyAPsB,
259 APMapping fixedRootAPs,
boolean screenAll,
260 boolean onlyCompleteMappings,
boolean compatibleIfFree)
263 List<AttachmentPoint> purgedLstA =
new ArrayList<AttachmentPoint>(lstA);
264 if (fixedRootAPs!=
null)
266 purgedLstA.removeAll(fixedRootAPs.keySet());
268 List<AttachmentPoint> purgedLstB =
new ArrayList<AttachmentPoint>(lstB);
269 if (fixedRootAPs!=
null)
271 purgedLstB.removeAll(fixedRootAPs.values());
275 LinkedHashMap<AttachmentPoint,List<AttachmentPoint>> apCompatilities =
281 List<AttachmentPoint> keys =
new ArrayList<AttachmentPoint>(
282 apCompatilities.keySet());
283 if (fixedRootAPs!=
null)
287 keys.removeAll(fixedRootAPs.keySet());
291 Set<AttachmentPoint> doableAPsA =
new HashSet<AttachmentPoint>(keys);
292 if (fixedRootAPs!=
null)
293 doableAPsA.addAll(fixedRootAPs.keySet());
294 Set<AttachmentPoint> doableAPsB =
new HashSet<AttachmentPoint>();
295 apCompatilities.values().stream().forEach(l -> doableAPsB.addAll(l));
296 if (onlyCompleteMappings)
300 if (!needyAPsA.contains(oldAp))
301 needyAPsA.add(oldAp);
305 if (!needyAPsB.contains(oldAp))
306 needyAPsB.add(oldAp);
309 Set<AttachmentPoint> mustBeDoableA =
new HashSet<AttachmentPoint>(
311 Set<AttachmentPoint> mustBeDoableB =
new HashSet<AttachmentPoint>(
313 if (fixedRootAPs!=
null)
315 mustBeDoableA.removeAll(fixedRootAPs.keySet());
316 mustBeDoableB.removeAll(fixedRootAPs.values());
318 if (!doableAPsA.containsAll(mustBeDoableA)
319 || !doableAPsB.containsAll(mustBeDoableB))
324 if (!onlyCompleteMappings)
328 if (oldAp.isAvailableThroughout())
333 if (apCompatilities.containsKey(oldAp))
335 apCompatilities.get(oldAp).add(
null);
344 if (fixedRootAPs!=
null)
346 currentMapping = fixedRootAPs.
clone();
351 Boolean stopped =
false;
363 List<APMapping> toRemove =
new ArrayList<APMapping>();
366 if (!c.containsAllKeys(needyAPsA))
370 if (!c.containsAllValues(needyAPsB))
382 for (
int iTry = 0; iTry <
maxCombs; iTry++)
385 List<AttachmentPoint> used =
386 new ArrayList<AttachmentPoint>();
387 List<AttachmentPoint> availKeys =
388 new ArrayList<AttachmentPoint>();
389 availKeys.addAll(needyAPsA);
390 boolean abandon =
false;
391 for (
int jj=0; jj<needyAPsA.size(); jj++)
396 availKeys.remove(ap);
397 List<AttachmentPoint> availPartners =
398 new ArrayList<AttachmentPoint>();
399 availPartners.addAll(apCompatilities.get(ap));
400 boolean done =
false;
401 for (
int j=0; j<apCompatilities.get(ap).size(); j++)
406 availPartners.remove(chosenAvail);
407 if (used.contains(chosenAvail))
411 used.add(chosenAvail);
412 apMap.put(ap,chosenAvail);
452 public static LinkedHashMap<AttachmentPoint,List<AttachmentPoint>>
454 List<AttachmentPoint> lstB,
boolean compatibleIfFree,
457 LinkedHashMap<AttachmentPoint,List<AttachmentPoint>>
459 List<AttachmentPoint>>();
465 boolean compatible =
false;
472 boolean oAPIsSrc = oAP.isSrcInUserThroughout();
473 boolean oAPIsTrg = oAPInUse && !oAP.isSrcInUserThroughout();
475 boolean cAPInUse = !cAP.isAvailableThroughout();
476 boolean cAPIsSrc = cAP.isSrcInUserThroughout();
477 boolean cAPIsTrg = cAPInUse && !cAP.isSrcInUserThroughout();
483 APClass oAPcl = oAP.getAPClass();
484 APClass cAPcl = cAP.getAPClass();
495 if (oAP.getAPClass().equals(cAP.getAPClass()))
499 if (!oAPInUse && !cAPInUse && compatibleIfFree)
502 }
else if (!oAPInUse && cAPInUse && compatibleIfFree)
513 }
else if (oAPInUse && !cAPInUse && compatibleIfFree)
524 }
else if (oAPInUse && cAPInUse)
526 if (oAPIsSrc && cAPIsSrc)
536 }
else if ((!oAPIsSrc && cAPIsSrc)
537 || (oAPIsSrc && !cAPIsSrc))
570 if (apCompatilities.containsKey(oAP))
572 apCompatilities.get(oAP).add(cAP);
574 List<AttachmentPoint> lst =
575 new ArrayList<AttachmentPoint>();
577 apCompatilities.put(oAP,lst);
582 return apCompatilities;
An utility class to encapsulate the search for an AttachmentPoint-AttachmentPoint mapping.
APMapping getChosenAPMapping()
Returns the AttachmentPoint-AttachmentPoint mapping chosen among the possible mappings.
APMapping chosenAPMap
The chosen AttachmentPoint-AttachmentPoint mapping.
List< APMapping > allAPMappings
The collection of all AttachmentPoint-AttachmentPoint mappings that have been found.
APMapFinder(FragmentSpace fragSpace, Vertex vA, Vertex vB, APMapping fixedRootAPs, boolean screenAll, boolean onlyCompleteMappings, boolean compatibleIfFree, int maxCombs)
Constructor that launches the search for a mapping between the AttachmentPoints on the first vertex t...
APMapFinder(FragmentSpace fragSpace, List< AttachmentPoint > lstA, List< AttachmentPoint > needyAPsA, List< AttachmentPoint > lstB, List< AttachmentPoint > needyAPsB, APMapping fixedRootAPs, boolean screenAll, boolean onlyCompleteMappings, boolean compatibleIfFree)
Constructor that launches the search for a mapping between the AttachmentPoints on two lists.
static LinkedHashMap< AttachmentPoint, List< AttachmentPoint > > findMappingCompatibileAPs(List< AttachmentPoint > lstA, List< AttachmentPoint > lstB, boolean compatibleIfFree, FragmentSpace fragSpace)
Compares the AttachmentPoint of two lists searching for all the APs of the second list that are "comp...
boolean foundMapping()
Returns true if any mapping has been found.
List< APMapping > getAllAPMappings()
Returns all AttachmentPoint-AttachmentPoint mapping found.
APMapFinder(FragmentSpace fragSpace, List< AttachmentPoint > lstA, List< AttachmentPoint > needyAPsA, List< AttachmentPoint > lstB, List< AttachmentPoint > needyAPsB, APMapping fixedRootAPs, boolean screenAll, boolean onlyCompleteMappings, boolean compatibleIfFree, int maxCombs)
Constructor that launches the search for a mapping between the AttachmentPoints on two lists.
int maxCombs
Maximum number of combinations.
static final int DEFAULT_MAX_COMBS
Default maximum number of combinations when none is specified.
FragmentSpace fragSpace
Program-specific fragment space.
APMapFinder(FragmentSpace fragSpace, Vertex vA, Vertex vB, APMapping fixedRootAPs, boolean screenAll, boolean onlyCompleteMappings, boolean compatibleIfFree)
Constructor that launches the search for a mapping between the AttachmentPoints on the first vertex t...
void findAllMappings(List< AttachmentPoint > lstA, List< AttachmentPoint > needyAPsA, List< AttachmentPoint > lstB, List< AttachmentPoint > needyAPsB, APMapping fixedRootAPs, boolean screenAll, boolean onlyCompleteMappings, boolean compatibleIfFree)
Searches for mappings between the AttachmentPoints on the two lists.
APMapFinder(FragmentSpace fragSpace, Vertex vA, Vertex vB, boolean screenAll)
Constructor that launches the search for a mapping between the AttachmentPoints on the first vertex t...
Class defining a space of building blocks.
Randomizer getRandomizer()
Returns the program-specific randomizer that is associated with this program-specific fragment space.
boolean useAPclassBasedApproach()
Check usage of APClass-based approach, i.e., uses attachment points with annotated data (i....
Utility class for the fragment space.
static boolean recursiveCombiner(List< AttachmentPoint > keys, int currentKey, Map< AttachmentPoint, List< AttachmentPoint > > possibilities, APMapping combination, List< APMapping > completeCombinations, boolean screenAll, int maxCombs)
Search for all possible combinations of compatible APs.
boolean isCPMapCompatibleWith(APClass other, FragmentSpace fragSpace)
Check compatibility as defined in the compatibility matrix considering this AP as source and the othe...
Class representing a mapping between attachment points (APs).
APMapping clone()
Shallow cloning.
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
APClass getAPClass()
Returns the Attachment Point class.
boolean isAvailableThroughout()
Check availability of this attachment point throughout the graph level, i.e., check also across the i...
AttachmentPoint getLinkedAPThroughout()
Gets the attachment point (AP) that is connected to this AP via the edge user or in any edge user tha...
List< AttachmentPoint > getInterfaceAPs(List< Vertex > subGraphB)
Searches for all AttachmentPoints that represent the interface between a subgraph,...
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
DGraph getGraphOwner()
Returns the graph this vertex belongs to or null.
abstract List< AttachmentPoint > getAttachmentPoints()
public< T > T randomlyChooseOne(Collection< T > c)
Chooses one member among the given collection.