package graphmatcher.matcher;
import graphmatcher.graph.Edge;
import graphmatcher.graph.Graph;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public abstract class AbstractMatcher {
private static Logger logger = Logger.getLogger(AbstractMatcher.class);
public static final byte VERTEX_MATCHING = 1;
public static final byte EDGE_MATCHING = 2;
protected Graph pattern, template;
protected MatchingOptions matchingOptions;
private String matchingName;
static {
logger.setLevel(Level.OFF);
}
public AbstractMatcher(Graph pattern, Graph template) {
this.pattern = pattern;
this.template = template;
matchingName = pattern.getName().substring(0, 5) + " -> " + template.getName().substring(0, 5);
}
public Graph getPattern() {
return pattern;
}
public Graph getTemplate() {
return template;
}
public MatchingResult match(MatchingOptions matchingOptions) {
final String matchingString = getName() + ": " + matchingName;
logger.info("Matche mit Verfahren: " + matchingString);
final boolean[] workDone = new boolean[1];
workDone[0] = false;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
long startTime = System.currentTimeMillis();
while (!workDone[0]) {
try {
Thread.sleep(10000);
if (workDone[0]) {
break;
} else {
System.out.println("Matching " + matchingName + " dauert schon: "
+ (System.currentTimeMillis() - startTime));
}
} catch (InterruptedException e) {
}
}
}
});
thread.start();
this.matchingOptions = matchingOptions;
MatchingResult result = new MatchingResult(pattern, template);
long timeStart = System.currentTimeMillis();
int[] matching = getMatching();
workDone[0] = true;
// printMatching(matching);
// printMatching(getEdgeMatching());
result.setMatching(matching);
result.setMatchingType(getMatchingType());
if (getMatchingType() == EDGE_MATCHING) {
result.setEdgeMatching(getEdgeMatching());
}
long timeFinish = System.currentTimeMillis();
long matchingTime = timeFinish - timeStart;
result.setMatchingTime(matchingTime);
logger.info(" abgeschlossen (" + matchingTime + "ms): " + matchingName + " - " + result.getQuality());
return result;
}
public void printMatching(int[] matching) {
switch (getMatchingType()) {
case VERTEX_MATCHING: {
System.out.println("Knotenmatching");
for (int i = 0; i < matching.length; i++) {
System.out.println(i + " -> " + matching[i]);
}
break;
}
case EDGE_MATCHING: {
System.out.println("Kantenmatching:");
for (int i = 0; i < matching.length; i++) {
Edge patternEdge = pattern.virtualEdges()[i];
Edge templateEdge = null;
if (matching[i] != -1) {
templateEdge = template.virtualEdges()[matching[i]];
}
System.out.println(patternEdge + " -> " + templateEdge);
}
break;
}
}
}
public String getMatchingName() {
return matchingName;
}
protected abstract int[] getMatching();
protected abstract int[] getEdgeMatching();
protected abstract byte getMatchingType();
public abstract String getName();
public abstract String getMatcherID();
}