Ticket #18138: 18138.8.patch

File 18138.8.patch, 26.1 KB (added by Traaker_L, 6 years ago)

Patch for latest version, finished up javadoc, removed redundant role verification checks

  • resources/data/defaultpresets.xml

     
    76837683                <role key="to" text="to way" requisite="required" count="1" type="way" />
    76847684            </roles>
    76857685        </item> <!-- Turn Restriction -->
     7686         <item name="Lane Connectivity" type="relation" preset_name_label="true" icon="presets/transport/way/relation_connectivity.svg">
     7687            <link wiki="Relation:connectivity" />
     7688            <space />
     7689            <key key="type" value="connectivity" />
     7690            <text key="connectivity" text="Lane Connectivity" />
     7691            <roles>
     7692                <role key="from" text="from way" requisite="required" count="1" type="way" />
     7693                <role key="via" text="via node or ways" requisite="required" type="way,node" />
     7694                <role key="to" text="to way" requisite="required" count="1" type="way" />
     7695            </roles>
     7696        </item> <!-- Lane Connectivity -->
    76867697        <item name="Enforcement" icon="presets/vehicle/restriction/speed_camera.svg" type="relation" preset_name_label="true">
    76877698            <link wiki="Relation:enforcement" />
    76887699            <space />
  • resources/images/presets/transport/way/relation_connectivity.svg

     
     1<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 16 16" width="16" height="16">
     2  <path d="M0,0 h16v16h-16z" fill="#8d8"/>
     3  <path d="M1.5,16v-6.5l4,-4v-6l8,0v16.5" stroke="#fff" fill="#888"/>
     4  <path d="M9.5,1v2m0,2v2m0,2v2m0,2v2m-4,0v-2m0,-2v-2" stroke="#fff"/>
     5</svg>
  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

     
    4242import org.openstreetmap.josm.data.validation.tests.BarriersEntrances;
    4343import org.openstreetmap.josm.data.validation.tests.Coastlines;
    4444import org.openstreetmap.josm.data.validation.tests.ConditionalKeys;
     45import org.openstreetmap.josm.data.validation.tests.ConnectivityRelations;
    4546import org.openstreetmap.josm.data.validation.tests.CrossingWays;
    4647import org.openstreetmap.josm.data.validation.tests.DuplicateNode;
    4748import org.openstreetmap.josm.data.validation.tests.DuplicateRelation;
     
    149150        PublicTransportRouteTest.class, // 3600 .. 3699
    150151        RightAngleBuildingTest.class, // 3700 .. 3799
    151152        SharpAngles.class, // 3800 .. 3899
     153        ConnectivityRelations.class, // 3900 .. 3999
    152154    };
    153155
    154156    /**
  • src/org/openstreetmap/josm/data/validation/tests/ConnectivityRelations.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.validation.tests;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.trn;
     6
     7import java.util.ArrayList;
     8import java.util.Collections;
     9import java.util.Comparator;
     10import java.util.HashMap;
     11import java.util.List;
     12import java.util.Map;
     13import java.util.Map.Entry;
     14import java.util.Set;
     15import java.util.regex.Pattern;
     16
     17import org.openstreetmap.josm.data.osm.Node;
     18import org.openstreetmap.josm.data.osm.OsmPrimitive;
     19import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     20import org.openstreetmap.josm.data.osm.Relation;
     21import org.openstreetmap.josm.data.osm.RelationMember;
     22import org.openstreetmap.josm.data.osm.Way;
     23import org.openstreetmap.josm.data.validation.Severity;
     24import org.openstreetmap.josm.data.validation.Test;
     25import org.openstreetmap.josm.data.validation.TestError;
     26import org.openstreetmap.josm.tools.Logging;
     27
     28/**
     29 * Check for inconsistencies in lane information between relation and members.
     30 */
     31public class ConnectivityRelations extends Test {
     32
     33    protected static final int INCONSISTENT_LANE_COUNT = 3900;
     34
     35    protected static final int UNKNOWN_CONNECTIVITY_ROLE = 3901;
     36
     37    protected static final int NO_CONNECTIVITY_TAG = 3902;
     38
     39    protected static final int MALFORMED_CONNECTIVITY_TAG = 3903;
     40
     41    protected static final int MISSING_COMMA_CONNECTIVITY_TAG = 3904;
     42
     43    protected static final int TOO_MANY_ROLES = 3905;
     44
     45    protected static final int MISSING_ROLE = 3906;
     46
     47    protected static final int MEMBER_MISSING_LANES = 3907;
     48
     49    protected static final int CONNECTIVITY_IMPLIED = 3908;
     50
     51    private static final String CONNECTIVITY_TAG = "connectivity";
     52    private static final String VIA = "via";
     53    private static final String TO = "to";
     54    private static final String FROM = "from";
     55    private static final int BW = -1000;
     56    private static final Pattern OPTIONAL_LANE_PATTERN = Pattern.compile("\\([0-9-]+\\)");
     57    private static final Pattern TO_LANE_PATTERN = Pattern.compile("\\p{Zs}*[,:;]\\p{Zs}*");
     58    private static final Pattern MISSING_COMMA_PATTERN = Pattern.compile("[0-9]+\\([0-9]+\\)|\\([0-9]+\\)[0-9]+");
     59    private static final Pattern LANE_TAG_PATTERN = Pattern.compile(".*:lanes");
     60
     61    /**
     62    * Constructor
     63    */
     64    public ConnectivityRelations() {
     65        super(tr("Connectivity Relations"), tr("Validates connectivity relations"));
     66    }
     67
     68    /**
     69     * Convert the connectivity tag into a map of values
     70     *
     71     * @param relation A relation with a {@code connectivity} tag.
     72     * @return A Map in the form of {@code Map<Lane From, Map<Lane To, Optional>>} May contain nulls when errors are encountered
     73     * @since xxx
     74     */
     75    public static Map<Integer, Map<Integer, Boolean>> parseConnectivityTag(Relation relation) {
     76        final String joined = relation.get(CONNECTIVITY_TAG).replace("bw", Integer.toString(BW));
     77
     78        if (joined == null) {
     79            return Collections.emptyMap();
     80        }
     81
     82        final Map<Integer, Map<Integer, Boolean>> result = new HashMap<>();
     83        String[] lanes = joined.split("\\|", -1);
     84        for (int i = 0; i < lanes.length; i++) {
     85            String[] lane = lanes[i].split(":", -1);
     86            int laneNumber;
     87            //Ignore connections from bw, since we cannot derive a lane number from bw
     88            if (!"bw".equals(lane[0])) {
     89                laneNumber = Integer.parseInt(lane[0].trim());
     90            } else {
     91                laneNumber = BW;
     92            }
     93            Map<Integer, Boolean> connections = new HashMap<>();
     94            String[] toLanes = TO_LANE_PATTERN.split(lane[1]);
     95            for (int j = 0; j < toLanes.length; j++) {
     96                String toLane = toLanes[j].trim();
     97                try {
     98                    if (OPTIONAL_LANE_PATTERN.matcher(toLane).matches()) {
     99                        toLane = toLane.replace("(", "").replace(")", "").trim();
     100                        if (!toLane.equals("bw")) {
     101                            connections.put(Integer.parseInt(toLane), Boolean.TRUE);
     102                        } else
     103                            connections.put(BW, Boolean.TRUE);
     104                    } else {
     105                        if (!toLane.contains("bw")) {
     106                            connections.put(Integer.parseInt(toLane), Boolean.FALSE);
     107                        } else {
     108                            connections.put(BW, Boolean.FALSE);
     109                        }
     110                    }
     111                } catch (NumberFormatException e) {
     112                    if (MISSING_COMMA_PATTERN.matcher(toLane).matches()) {
     113                        connections.put(null, true);
     114                    } else {
     115                        connections.put(null, null);
     116                    }
     117                }
     118            }
     119            result.put(laneNumber, connections);
     120        }
     121        return result;
     122    }
     123
     124    @Override
     125    public void visit(Relation r) {
     126        if (r.hasTag("type", CONNECTIVITY_TAG)) {
     127            if (!r.hasKey(CONNECTIVITY_TAG)) {
     128                errors.add(TestError.builder(this, Severity.WARNING, NO_CONNECTIVITY_TAG)
     129                        .message(tr("No 'connectivity' tag in connectivity relation")).primitives(r).build());
     130            } else if (!r.hasIncompleteMembers()) {
     131                boolean badRole = checkForBadRole(r);
     132                boolean missingRole = checkForMissingRole(r);
     133                if (!badRole && !missingRole) {
     134                    Map<String, Integer> roleLanes = checkForInconsistentLanes(r);
     135                    checkForImpliedConnectivity(r, roleLanes);
     136                }
     137            }
     138        }
     139    }
     140
     141    /**
     142     * Compare lane tags of members to values in the {@code connectivity} tag of the relation
     143     *
     144     * @param relation A relation with a {@code connectivity} tag.
     145     * @return A Map in the form of {@code Map<Role, Lane Count>}
     146     * @since xxx
     147     */
     148    private Map<String, Integer> checkForInconsistentLanes(Relation relation) {
     149        StringBuilder lanelessRoles = new StringBuilder();
     150        int lanelessRolesCount = 0;
     151        // Lane count from connectivity tag
     152        Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation);
     153        // If the ways involved in the connectivity tag are assuming a standard 2-way bi-directional highway
     154        boolean defaultLanes = true;
     155        for (Entry<Integer, Map<Integer, Boolean>> thisEntry : connTagLanes.entrySet()) {
     156            for (Entry<Integer, Boolean> thisEntry2 : thisEntry.getValue().entrySet()) {
     157                Logging.debug("Checking: " + thisEntry2.toString());
     158                if (thisEntry2.getKey() != null && thisEntry2.getKey() > 1) {
     159                    defaultLanes = false;
     160                    break;
     161                }
     162            }
     163            if (!defaultLanes) {
     164                break;
     165            }
     166        }
     167        // Lane count from member tags
     168        Map<String, Integer> roleLanes = new HashMap<>();
     169        for (RelationMember rM : relation.getMembers()) {
     170            // Check lanes
     171            if (rM.getType() == OsmPrimitiveType.WAY) {
     172                OsmPrimitive prim = rM.getMember();
     173                if (!VIA.equals(rM.getRole())) {
     174                    Map<String, String> primKeys = prim.getKeys();
     175                    List<Long> laneCounts = new ArrayList<>();
     176                    long maxLaneCount;
     177                    if (prim.hasTag("lanes")) {
     178                        laneCounts.add(Long.parseLong(prim.get("lanes")));
     179                    }
     180                    for (Entry<String, String> entry : primKeys.entrySet()) {
     181                        String thisKey = entry.getKey();
     182                        String thisValue = entry.getValue();
     183                        if (LANE_TAG_PATTERN.matcher(thisKey).matches()) {
     184                            //Count bar characters
     185                            long count = thisValue.chars().filter(ch -> ch == '|').count() + 1;
     186                            laneCounts.add(count);
     187                        }
     188                    }
     189
     190                    if (!laneCounts.equals(Collections.emptyList())) {
     191                        maxLaneCount = Collections.max(laneCounts);
     192                        roleLanes.put(rM.getRole(), (int) maxLaneCount);
     193                    } else {
     194                        String addString = "'" + rM.getRole() + "'";
     195                        StringBuilder sb = new StringBuilder(addString);
     196                        if (lanelessRoles.length() > 0) {
     197                            sb.insert(0, " and ");
     198                        }
     199                        lanelessRoles.append(sb.toString());
     200                        lanelessRolesCount++;
     201                    }
     202                }
     203            }
     204        }
     205
     206        if (lanelessRoles.toString().isEmpty()) {
     207            boolean fromCheck = roleLanes.get(FROM) < Collections
     208                    .max(connTagLanes.entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey();
     209            boolean toCheck = false;
     210            for (Entry<Integer, Map<Integer, Boolean>> to : connTagLanes.entrySet()) {
     211                if (!to.getValue().containsKey(null)) {
     212                    toCheck = roleLanes.get(TO) < Collections
     213                            .max(to.getValue().entrySet(), Comparator.comparingInt(Map.Entry::getKey)).getKey();
     214                } else {
     215                    if (to.getValue().containsValue(true)) {
     216                        errors.add(TestError.builder(this, Severity.ERROR, MISSING_COMMA_CONNECTIVITY_TAG)
     217                                .message(tr("Connectivity tag missing comma between optional and non-optional values")).primitives(relation)
     218                                .build());
     219                    } else {
     220                        errors.add(TestError.builder(this, Severity.ERROR, MALFORMED_CONNECTIVITY_TAG)
     221                                .message(tr("Connectivity tag contains unusual data")).primitives(relation)
     222                                .build());
     223                    }
     224                }
     225            }
     226            if (fromCheck || toCheck) {
     227                errors.add(TestError.builder(this, Severity.WARNING, INCONSISTENT_LANE_COUNT)
     228                        .message(tr("Inconsistent lane numbering between relation and member tags")).primitives(relation)
     229                        .build());
     230            }
     231        } else if (!defaultLanes) {
     232            errors.add(TestError.builder(this, Severity.WARNING, MEMBER_MISSING_LANES)
     233                    .message(trn("Relation {0} member missing lanes tag", "Relation {0} members missing 'lanes' or '*:lanes' tag",
     234                            lanelessRolesCount, lanelessRoles)).primitives(relation)
     235                    .build());
     236        }
     237        return roleLanes;
     238    }
     239
     240    /**
     241     * Check the relation to see if the connectivity described is already implied by other data
     242     *
     243     * @param relation A relation with a {@code connectivity} tag.
     244     * @param roleLanes The lane counts for each relation role
     245     * @since xxx
     246     */
     247    private void checkForImpliedConnectivity(Relation relation, Map<String, Integer> roleLanes) {
     248        boolean connImplied = true;
     249        Map<Integer, Map<Integer, Boolean>> connTagLanes = parseConnectivityTag(relation);
     250        // Don't flag connectivity as already implied when:
     251        // - Lane counts are different on the roads
     252        // - Placement tags convey the connectivity
     253        // - The relation passes through an intersection
     254        //   - If via member is a node, it's connected to ways not in the relation
     255        //   - If a via member is a way, ways not in the relation connect to its nodes
     256        // - Highways that appear to be merging have a different cumulative number of lanes than
     257        //   the highway that they're merging into
     258
     259        connImplied = checkMemberTagsForImpliedConnectivity(relation, roleLanes) && !checkForIntersectionAtMembers(relation);
     260        // Check if connectivity tag implies default connectivity
     261        if (connImplied) {
     262            for (Entry<Integer, Map<Integer, Boolean>> to : connTagLanes.entrySet()) {
     263                int fromLane = to.getKey();
     264                for (Entry<Integer, Boolean> lane : to.getValue().entrySet()) {
     265                    if (lane.getKey() != null && fromLane != lane.getKey()) {
     266                        connImplied = false;
     267                        break;
     268                    }
     269                }
     270                if (!connImplied)
     271                    break;
     272            }
     273        }
     274
     275        if (connImplied) {
     276            errors.add(TestError.builder(this, Severity.WARNING, CONNECTIVITY_IMPLIED)
     277                    .message(tr("This connectivity may already be implied")).primitives(relation)
     278                    .build());
     279        }
     280    }
     281
     282    /**
     283     * Check to see if there is an intersection present at the via member
     284     *
     285     * @param relation A relation with a {@code connectivity} tag.
     286     * @return A Boolean that indicates whether an intersection is present at the via member
     287     * @since xxx
     288     */
     289    private boolean checkForIntersectionAtMembers(Relation relation) {
     290        OsmPrimitive viaPrim = relation.findRelationMembers("via").get(0);
     291        Set<OsmPrimitive> relationMembers = relation.getMemberPrimitives();
     292
     293        if (viaPrim.getType() == OsmPrimitiveType.NODE) {
     294            Node viaNode = (Node) viaPrim;
     295            List<Way> parentWays = viaNode.getParentWays();
     296            if (parentWays.size() > 2) {
     297                for (Way thisWay : parentWays) {
     298                    if (!relationMembers.contains(thisWay) && thisWay.hasTag("highway")) {
     299                        return true;
     300                    }
     301                }
     302            }
     303        } else if (viaPrim.getType() == OsmPrimitiveType.WAY) {
     304            Way viaWay = (Way) viaPrim;
     305            for (Node thisNode : viaWay.getNodes()) {
     306                List<Way> parentWays = thisNode.getParentWays();
     307                if (parentWays.size() > 2) {
     308                    for (Way thisWay : parentWays) {
     309                        if (!relationMembers.contains(thisWay) && thisWay.hasTag("highway")) {
     310                            return true;
     311                        }
     312                    }
     313                }
     314            }
     315        }
     316        return false;
     317    }
     318
     319    /**
     320     * Check the relation to see if the connectivity described is already implied by the relation members' tags
     321     *
     322     * @param relation A relation with a {@code connectivity} tag.
     323     * @param roleLanes The lane counts for each relation role
     324     * @return Whether connectivity is already implied by tags on relation members
     325     * @since xxx
     326     */
     327    private boolean checkMemberTagsForImpliedConnectivity(Relation relation, Map<String, Integer> roleLanes) {
     328        // The members have different lane counts
     329        if (roleLanes.containsKey(TO) && roleLanes.containsKey(FROM) && (!roleLanes.get(TO).equals(roleLanes.get(FROM)))) {
     330            return false;
     331        }
     332
     333        // The members don't have placement tags defining the connectivity
     334        List<RelationMember> members = relation.getMembers();
     335        Map<String, OsmPrimitive> toFromMembers = new HashMap<>();
     336        for (RelationMember mem : members) {
     337            if (mem.getRole().equals(FROM)) {
     338                toFromMembers.put(FROM, mem.getMember());
     339            } else if (mem.getRole().equals(TO)) {
     340                toFromMembers.put(TO, mem.getMember());
     341            }
     342        }
     343
     344        return toFromMembers.get(TO).hasKey("placement") || toFromMembers.get(FROM).hasKey("placement");
     345    }
     346
     347    /**
     348     * Check if the roles of the relation are appropriate
     349     *
     350     * @param relation A relation with a {@code connectivity} tag.
     351     * @return Whether one or more of the relation's members has an unusual role
     352     * @since xxx
     353     */
     354    private boolean checkForBadRole(Relation relation) {
     355        // Check role names
     356        int viaWays = 0;
     357        int viaNodes = 0;
     358        for (RelationMember relationMember : relation.getMembers()) {
     359            if (relationMember.getMember() instanceof Way) {
     360                if (relationMember.hasRole(VIA))
     361                    viaWays++;
     362                else if (!relationMember.hasRole(FROM) && !relationMember.hasRole(TO)) {
     363                    return true;
     364                }
     365            } else if (relationMember.getMember() instanceof Node) {
     366                if (!relationMember.hasRole(VIA)) {
     367                    return true;
     368                }
     369                viaNodes++;
     370            }
     371        }
     372        return mixedViaNodeAndWay(relation, viaWays, viaNodes);
     373    }
     374
     375    /**
     376     * Check if the relation contains all necessary roles
     377     *
     378     * @param relation A relation with a {@code connectivity} tag.
     379     * @return Whether the relation is missing one or more of the critical {@code from}, {@code via}, or {@code to} roles
     380     * @since xxx
     381     */
     382    private boolean checkForMissingRole(Relation relation) {
     383        List<String> necessaryRoles = new ArrayList<>();
     384        necessaryRoles.add(FROM);
     385        necessaryRoles.add(VIA);
     386        necessaryRoles.add(TO);
     387
     388        List<String> roleList = new ArrayList<>();
     389        for (RelationMember relationMember: relation.getMembers()) {
     390            roleList.add(relationMember.getRole());
     391        }
     392
     393        return !roleList.containsAll(necessaryRoles);
     394    }
     395
     396    /**
     397     * Check if the relation's roles are on appropriate objects
     398     *
     399     * @param relation A relation with a {@code connectivity} tag.
     400     * @param viaWays The number of ways in the relation with the {@code via} role
     401     * @param viaNodes The number of nodes in the relation with the {@code via} role
     402     * @return Whether the relation is missing one or more of the critical 'from', 'via', or 'to' roles
     403     * @since xxx
     404     */
     405    private boolean mixedViaNodeAndWay(Relation relation, int viaWays, int viaNodes) {
     406        String message = "";
     407        if (viaNodes > 1) {
     408            if (viaWays > 0) {
     409                message = tr("Relation should not contain mixed 'via' ways and nodes");
     410            } else {
     411                message = tr("Multiple 'via' roles only allowed with ways");
     412            }
     413        }
     414        if (message.isEmpty()) {
     415            return false;
     416        } else {
     417            errors.add(TestError.builder(this, Severity.WARNING, TOO_MANY_ROLES)
     418                    .message(message).primitives(relation).build());
     419            return true;
     420        }
     421    }
     422
     423}
  • test/unit/org/openstreetmap/josm/data/validation/tests/ConnectivityRelationsTest.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.validation.tests;
     3
     4import org.junit.Assert;
     5import org.junit.Before;
     6import org.junit.Test;
     7import org.openstreetmap.josm.JOSMFixture;
     8import org.openstreetmap.josm.TestUtils;
     9import org.openstreetmap.josm.data.coor.LatLon;
     10import org.openstreetmap.josm.data.osm.Node;
     11import org.openstreetmap.josm.data.osm.Relation;
     12import org.openstreetmap.josm.data.osm.RelationMember;
     13
     14/**
     15 * Test the ConnectivityRelations validation test
     16 *
     17 * @author Taylor Smock
     18 */
     19public class ConnectivityRelationsTest {
     20    private ConnectivityRelations check;
     21    private static final String CONNECTIVITY = "connectivity";
     22    /**
     23     * Setup test.
     24     *
     25     * @throws Exception if an error occurs
     26     */
     27    @Before
     28    public void setUp() throws Exception {
     29        JOSMFixture.createUnitTestFixture().init();
     30        check = new ConnectivityRelations();
     31    }
     32
     33    private Relation createDefaultTestRelation() {
     34        Node connection = new Node(new LatLon(0, 0));
     35        return TestUtils.newRelation("type=connectivity connectivity=1:1",
     36                new RelationMember("from", TestUtils.newWay("lanes=4", new Node(new LatLon(-0.1, -0.1)), connection)),
     37                new RelationMember("via", connection),
     38                new RelationMember("to", TestUtils.newWay("lanes=4", connection, new Node(new LatLon(0.1, 0.1)))));
     39    }
     40
     41    /**
     42     * Test for connectivity relations without a connectivity tag
     43     */
     44    @Test
     45    public void testNoConnectivityTag() {
     46        Relation relation = createDefaultTestRelation();
     47        check.visit(relation);
     48
     49        Assert.assertEquals(0, check.getErrors().size());
     50
     51        relation.remove(CONNECTIVITY);
     52        check.visit(relation);
     53        Assert.assertEquals(1, check.getErrors().size());
     54    }
     55
     56    /**
     57     * Check for lanes that don't make sense
     58     */
     59    @Test
     60    public void testMisMatchedLanes() {
     61        Relation relation = createDefaultTestRelation();
     62        check.visit(relation);
     63        int expectedFailures = 0;
     64
     65        Assert.assertEquals(expectedFailures, check.getErrors().size());
     66
     67        relation.put(CONNECTIVITY, "45000:1");
     68        check.visit(relation);
     69        Assert.assertEquals(++expectedFailures, check.getErrors().size());
     70
     71        relation.put(CONNECTIVITY, "1:45000");
     72        check.visit(relation);
     73        Assert.assertEquals(++expectedFailures, check.getErrors().size());
     74
     75        relation.put(CONNECTIVITY, "1:1,2");
     76        check.visit(relation);
     77        Assert.assertEquals(expectedFailures, check.getErrors().size());
     78
     79        relation.put(CONNECTIVITY, "1:1,(2)");
     80        check.visit(relation);
     81        Assert.assertEquals(expectedFailures, check.getErrors().size());
     82
     83        relation.put(CONNECTIVITY, "1:1,(20000)");
     84        check.visit(relation);
     85        Assert.assertEquals(++expectedFailures, check.getErrors().size());
     86    }
     87
     88    /**
     89     * Check for bad roles (not from/via/to)
     90     */
     91    @Test
     92    public void testForBadRole() {
     93        Relation relation = createDefaultTestRelation();
     94        check.visit(relation);
     95        int expectedFailures = 0;
     96
     97        Assert.assertEquals(expectedFailures, check.getErrors().size());
     98
     99        for (int i = 0; i < relation.getMembers().size(); i++) {
     100            String tRole = replaceMember(relation, i, "badRole");
     101            check.visit(relation);
     102            Assert.assertEquals(++expectedFailures, check.getErrors().size());
     103            replaceMember(relation, i, tRole);
     104            check.visit(relation);
     105            Assert.assertEquals(expectedFailures, check.getErrors().size());
     106        }
     107    }
     108
     109    private String replaceMember(Relation relation, int index, String replacementRole) {
     110        RelationMember relationMember = relation.getMember(index);
     111        String currentRole = relationMember.getRole();
     112        relation.removeMember(index);
     113        relation.addMember(index, new RelationMember(replacementRole, relationMember.getMember()));
     114        return currentRole;
     115    }
     116}