package org.simantics.interop.test; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; /** * Object comparator that uses type and name of objects to check if objects are comparable. * If objects have no name, compare returns unreliable as result. * * * Difference value is amount of properties that have different values. * * @author Marko Luukkainen * */ public class NameComparator extends TypeComparator { @Override public int compare(ReadGraph g, Resource o1, Resource o2) throws DatabaseException { if (!compareType(g,o1, o2)) { return NO_MATCH; } if (hasName(g, o1, o2)) { if(!compareName(g, o1, o2)) return NO_MATCH; return propsDiffCount(g,o1, o2); } return UNDECISIVE; } protected boolean compareName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING); String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING); if (n1 != null && n2 != null) return n1.equals(n2); if (n1 == null && n2 == null) return true; return false; } protected boolean hasName(ReadGraph g, Resource o1, Resource o2) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); String n1 = g.getPossibleRelatedValue(o1, l0.HasName, Bindings.STRING); String n2 = g.getPossibleRelatedValue(o2, l0.HasName, Bindings.STRING); if (n1 != null && n2 != null) return true; return false; } }