-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
172 additions
and
103 deletions.
There are no files selected for viewing
113 changes: 26 additions & 87 deletions
113
src/main/java/org/mastodon/mamut/treesimilarity/NodeMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,43 @@ | ||
package org.mastodon.mamut.treesimilarity; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.mastodon.mamut.treesimilarity.tree.Tree; | ||
|
||
abstract class NodeMapping< T > | ||
/** | ||
* Helper class for {@link ZhangUnorderedTreeEditDistance}. | ||
* <p> | ||
* Used to represent a mapping between the nodes of two {@link Tree trees} | ||
* together with the associated costs / edit distance. | ||
* | ||
* @param <T> Attribute type for the trees. | ||
* | ||
* @see NodeMappings | ||
*/ | ||
interface NodeMapping< T > | ||
{ | ||
public static < T > NodeMapping< T > empty( double cost ) | ||
{ | ||
return new EmptyNodeMapping<>( cost ); | ||
} | ||
|
||
public static < T > NodeMapping< T > singleton( double cost, Tree< T > tree1, Tree< T > tree2 ) | ||
{ | ||
return new SingletonNodeMapping<>( tree1, tree2, cost ); | ||
} | ||
|
||
@SafeVarargs | ||
public static < T > NodeMapping< T > compose( NodeMapping< T >... children ) | ||
{ | ||
return compose( Arrays.asList( children ) ); | ||
} | ||
|
||
public static < T > NodeMapping< T > compose( List< NodeMapping< T > > children ) | ||
{ | ||
return new ComposedNodeMapping<>( children ); | ||
} | ||
|
||
private final double cost; | ||
|
||
protected NodeMapping( double cost ) | ||
{ | ||
this.cost = cost; | ||
} | ||
|
||
public double getCost() | ||
{ | ||
return cost; | ||
} | ||
/** | ||
* @return The cost of this mapping. | ||
*/ | ||
double getCost(); | ||
|
||
abstract protected void writeToMap( Map< Tree< T >, Tree< T > > map ); | ||
/** | ||
* This method is needed for an efficient implementation of the | ||
* {@link #asMap()} method. It is not meant to be used directly, | ||
* use {@link #asMap()} instead. | ||
*/ | ||
void writeToMap( Map< Tree< T >, Tree< T > > map ); | ||
|
||
public Map< Tree< T >, Tree< T > > asMap() | ||
/** | ||
* @return The mapping as a {@link Map} from nodes of the first tree to | ||
* nodes of the second tree. | ||
*/ | ||
default Map< Tree< T >, Tree< T > > asMap() | ||
{ | ||
final Map< Tree< T >, Tree< T > > map = new HashMap<>(); | ||
writeToMap( map ); | ||
return map; | ||
} | ||
|
||
static class EmptyNodeMapping< T > extends NodeMapping< T > | ||
{ | ||
private EmptyNodeMapping( double cost ) | ||
{ | ||
super( cost ); | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
// do nothing | ||
} | ||
} | ||
|
||
static class SingletonNodeMapping< T > extends NodeMapping< T > | ||
{ | ||
private final Tree< T > tree1; | ||
|
||
private final Tree< T > tree2; | ||
|
||
private SingletonNodeMapping( Tree< T > tree1, Tree< T > tree2, double cost ) | ||
{ | ||
super( cost ); | ||
this.tree1 = tree1; | ||
this.tree2 = tree2; | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
map.put( tree1, tree2 ); | ||
} | ||
} | ||
|
||
private static class ComposedNodeMapping< T > extends NodeMapping< T > | ||
{ | ||
private final List< NodeMapping< T > > children; | ||
|
||
private ComposedNodeMapping( List< NodeMapping< T > > children ) | ||
{ | ||
super( children.stream().mapToDouble( NodeMapping::getCost ).sum() ); | ||
this.children = children; | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
children.forEach( child -> child.writeToMap( map ) ); | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
src/main/java/org/mastodon/mamut/treesimilarity/NodeMappings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package org.mastodon.mamut.treesimilarity; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.mastodon.mamut.treesimilarity.tree.Tree; | ||
|
||
/** | ||
* Utility class for {@link ZhangUnorderedTreeEditDistance} that provides | ||
* static factory methods for the easy creation of {@link NodeMapping}s. | ||
*/ | ||
class NodeMappings | ||
{ | ||
private NodeMappings() | ||
{ | ||
// prevent from instantiation | ||
} | ||
|
||
/** | ||
* @return An empty {@link NodeMapping} with the specified cost. | ||
* (Please note that the costs for an empty mapping are almost never | ||
* zero!) | ||
*/ | ||
public static < T > NodeMapping< T > empty( double cost ) | ||
{ | ||
return new EmptyNodeMapping<>( cost ); | ||
} | ||
|
||
/** | ||
* @return A {@link NodeMapping} that represents a singleton Map from | ||
* tree1 to tree2 with the specified cost. | ||
*/ | ||
public static < T > NodeMapping< T > singleton( double cost, Tree< T > tree1, Tree< T > tree2 ) | ||
{ | ||
return new SingletonNodeMapping<>( tree1, tree2, cost ); | ||
} | ||
|
||
/** | ||
* @return A {@link NodeMapping} that represents a composed that contains | ||
* all the map entries of the given {@code children}. The costs of the | ||
* composed mapping is the sum of the costs of the children. | ||
*/ | ||
@SafeVarargs | ||
public static < T > NodeMapping< T > compose( NodeMapping< T >... children ) | ||
{ | ||
return compose( Arrays.asList( children ) ); | ||
} | ||
|
||
/** | ||
* @return A {@link NodeMapping} that represents a composed that contains | ||
* all the map entries of the given {@code children}. The costs of the | ||
* composed mapping is the sum of the costs of the children. | ||
*/ | ||
public static < T > NodeMapping< T > compose( List< NodeMapping< T > > children ) | ||
{ | ||
return new ComposedNodeMapping<>( children ); | ||
} | ||
|
||
private abstract static class AbstractNodeMapping< T > implements NodeMapping< T > | ||
{ | ||
private final double cost; | ||
|
||
protected AbstractNodeMapping( double cost ) | ||
{ | ||
this.cost = cost; | ||
} | ||
|
||
@Override | ||
public double getCost() | ||
{ | ||
return cost; | ||
} | ||
|
||
@Override | ||
public abstract void writeToMap( Map< Tree< T >, Tree< T > > map ); | ||
|
||
} | ||
|
||
private static class EmptyNodeMapping< T > extends AbstractNodeMapping< T > | ||
{ | ||
private EmptyNodeMapping( double cost ) | ||
{ | ||
super( cost ); | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
// do nothing | ||
} | ||
} | ||
|
||
private static class SingletonNodeMapping< T > extends AbstractNodeMapping< T > | ||
{ | ||
private final Tree< T > tree1; | ||
|
||
private final Tree< T > tree2; | ||
|
||
private SingletonNodeMapping( Tree< T > tree1, Tree< T > tree2, double cost ) | ||
{ | ||
super( cost ); | ||
this.tree1 = tree1; | ||
this.tree2 = tree2; | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
map.put( tree1, tree2 ); | ||
} | ||
} | ||
|
||
private static class ComposedNodeMapping< T > extends AbstractNodeMapping< T > | ||
{ | ||
private final List< NodeMapping< T > > children; | ||
|
||
private ComposedNodeMapping( List< NodeMapping< T > > children ) | ||
{ | ||
super( children.stream().mapToDouble( NodeMapping::getCost ).sum() ); | ||
this.children = children; | ||
} | ||
|
||
@Override | ||
public void writeToMap( Map< Tree< T >, Tree< T > > map ) | ||
{ | ||
children.forEach( child -> child.writeToMap( map ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters