[Demonstration 1]

[Buggy Method]
@Override
public Object[] missing(FFuncProp<E> func, Object... parameters) {
    if (func.getProperty().arity() != parameters.length) {
        throw new InvalidNumberOfArgumentsException();
    }
    final List<IRule<E, E>> rules = new ArrayList<>();
    for (final IRule<E, E> rule : rules) {
        final FFuncProp<E> property = rule.getConsequent().getPropFunc();
        if (property.equals(func)) {
            rules.add(rule);
        }
    }
    int missingParams = 0;
    final int[] indices = new int[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i] == null) {
            indices[i] = missingParams;
            missingParams++;
        } else {
            indices[i] = -1;
        }
    }
    final Map<Object[], Float> map = new HashMap<>(this.universe.size());
    final Set<Entry<List<Object>, IProperty<E>>> inferred = new HashSet<>(0);
    for (final IRule<E, E> rule : rules) {
        final Entry<Map<Variable, Object>, Set<Variable>> entry = this.getInterpretationAndMissingVariables(rule, parameters, inferred);
        final Map<Variable, Object> interpretation = entry.getKey();
        final Set<Variable> missingVars = entry.getValue();
        final FFuncAnt<E> antFunc = rule.getAntecedent();
        final FFuncCons<E> consFunc = rule.getConsequent();
        final Variable[] consVariables = consFunc.variables();
        final Object[] missing = new Object[missingParams];
        int count = 0;
        for (int i = 0; i < parameters.length; i++) {
            final Variable variable = consVariables[i];
            final Object param = parameters[i];
            if (param == null) {
                if (interpretation.containsKey(variable)) {
                    missing[count] = interpretation.get(variable);
                } else {
                    missing[count] = variable;
                }
                count++;
            }
        }
        final Set<Entry<IProperty<E>, Parameter[]>> vars = antFunc.propertiesWithVars();
        if (!missingVars.isEmpty()) {
            //Iterate over all possible interpretation given the variables that ARE known.
            for (final Map<Variable, ?> interpretationIt : this.variableIterator(missingVars, interpretation)) {
                final float membership = this.getMembership(antFunc, vars, interpretationIt, inferred);
                final Object[] finalMissing = new Object[missingParams];
                for (int i = 0; i < missingParams; i++) {
                    final Object inMissing = missing[i];
                    if (inMissing instanceof Variable) {
                        finalMissing[i] = interpretationIt.get(inMissing);
                    } else {
                        finalMissing[i] = inMissing;
                    }
                }
                this.setInMap(map, finalMissing, membership);
            }
        } else {
            final float membership = this.getMembership(antFunc, vars, interpretation, inferred);
            this.setInMap(map, missing, membership);
        }
    }
    Object[] best = null;
    float bestf = -1.0f;
    for (final Entry<Object[], Float> entry : map.entrySet()) {
        final float v = entry.getValue();
        if (v > bestf) {
            bestf = v;
            best = entry.getKey();
        }
    }
    return best;
}

[Buggy Hunk]
for (final IRule<E, E> rule : rules)

[Patch]
for (final IRule<E, E> rule : this.rules)

[Demonstration 2]

[Buggy Method]
@Test
public void testWhereSingleClause1() {
    QueryBuilder qb = new QueryBuilder();
    // A non-sensical query, but one of the simplest to write...
    // Way in the future we may care to define and note that identifier
    // a is a NODE and not an alias of some function, and detect
    // stuff like this and throw an error instead.
    //
    // For now, this is enough of the proof of concept.
    QuerySpec qs = qb.matchNode("a").where().value("a").in("a").getQuerySpec();
    assertEquals("MATCH (a) WHERE ( a IN a ) RETURN a", qs.getQueryString());
}

[Buggy Hunk]
assertEquals("MATCH (a) WHERE ( a IN a ) RETURN a", qs.getQueryString());

[Patch]
assertEquals("MATCH (a) WHERE ( \"a\" IN a ) RETURN a", qs.getQueryString());

[Demonstration 3]

[Buggy Method]
@Override
public List<Location> filterLocations(Entity entity) {
    List<Location> hosts = location.getExtension(AvailabilityZoneExtension.class).getAllSubLocations();
    List<DockerHostLocation> available = Lists.newArrayList();
    // Select hosts that satisfy the affinity rules
    for (DockerHostLocation machine : Iterables.filter(hosts, DockerHostLocation.class)) {
        Optional<String> entityRules = Optional.fromNullable(entity.getConfig(DockerHost.DOCKER_HOST_AFFINITY_RULES));
        Optional<String> hostRules = Optional.fromNullable(machine.getOwner().getConfig(DockerHost.DOCKER_HOST_AFFINITY_RULES));
        Optional<String> infrastructureRules = Optional.fromNullable(machine.getOwner().getInfrastructure().getConfig(DockerHost.DOCKER_HOST_AFFINITY_RULES));
        String combined = Joiner.on('\n').join(Optional.presentInstances(ImmutableList.of(entityRules, hostRules, infrastructureRules)));
        AffinityRules rules = AffinityRules.rules(combined);
        Iterable<Entity> entities = managementContext.getEntityManager().findEntities(EntityPredicates.withLocation(machine));
        if (Iterables.isEmpty(entities)) {
            if (rules.allowEmptyLocations()) {
                available.add(machine);
            }
        } else {
            Iterable<Entity> filtered = Iterables.filter(entities, rules);
            if (Iterables.size(filtered) == Iterables.size(entities)) {
                available.add(machine);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Available Docker hosts: {}", Iterables.toString(available));
    }
    return ImmutableList.<Location>copyOf(available);
}

[Buggy Hunk]
AffinityRules rules = AffinityRules.rules(combined);

[Patch]
AffinityRules rules = AffinityRules.rulesFor(entity).parse(combined);