blob: 1f657c53d20ae7d6ab5947627113153ac3ee9071 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Akos Horvath, Gergely Varro and Daniel Varro
* 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:
* Akos Horvath, Gergely Varro - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.searchgraph;
import java.io.Serializable;
import java.util.Comparator;
/**
* @author Akos Horvath
*
*/
public class SearchGraphNodeComparator implements Comparator<SearchGraphNode>, Serializable {
/**
*
*/
private static final long serialVersionUID = 4964712646210992592L;
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(SearchGraphNode node0, SearchGraphNode node1) {
if(node1.equals(node0))
return 0;
else
if(node0.getTreeEdge() == null && node1.getTreeEdge() == null)
{
if(node0 instanceof ConstantSearchGraphNode && node1 instanceof ConstantSearchGraphNode)
return node0.getName().compareTo(node1.getName());
else
if(node0 instanceof ConstantSearchGraphNode && node1 instanceof VariableSearchGraphNode)
return -1;
else
if(node0 instanceof VariableSearchGraphNode && node1 instanceof ConstantSearchGraphNode)
return 1;
else //both are variable constant nodes
return ((VariableSearchGraphNode)node0).getId().compareTo( ((VariableSearchGraphNode)node1).getId() );
}
else
if(node0.getTreeEdge()== null)
return -1;
else
if(node1.getTreeEdge() == null)
return 1;
else
//the normal comapre
if(node0.getTreeEdge().getOldWeight() < node1.getTreeEdge().getOldWeight())
return -1;
else
if(node0.getTreeEdge().getOldWeight() > node1.getTreeEdge().getOldWeight())
return 1;
else
//this is not a smooth solution
if(node0.getTreeEdge().getOldWeight() == node1.getTreeEdge().getOldWeight())
{if(node0 instanceof ConstantSearchGraphNode && node1 instanceof ConstantSearchGraphNode)
return node0.getName().compareTo(node1.getName());
else
if(node0 instanceof ConstantSearchGraphNode && node1 instanceof VariableSearchGraphNode)
return -1;
else
if(node0 instanceof VariableSearchGraphNode && node1 instanceof ConstantSearchGraphNode)
return 1;
else //both are variable constant nodes
return ((VariableSearchGraphNode)node0).getId().compareTo( ((VariableSearchGraphNode)node1).getId());
}
return -1;
}
}