/** * Creates a new instance and reads in its data from the given input * * @param input * Input * @return New instance * @throws IOException */ public static CharacteristicWritable read(DataInput input) throws IOException { CharacteristicWritable cw = new CharacteristicWritable(); cw.readFields(input); return cw; }
/** * Adds a characteristic to the set merging it into the appropriate existing * characteristic if applicable * * @param characteristic * Characteristics */ public void add(CharacteristicWritable characteristic) { if (this.characteristics.containsKey(characteristic.getNode())) { this.characteristics.get(characteristic.getNode()).increment(characteristic.getCount().get()); } else { this.characteristics.put(characteristic.getNode(), characteristic); } }
@Override public boolean equals(Object other) { if (!(other instanceof CharacteristicWritable)) return false; return this.compareTo((CharacteristicWritable) other) == 0; }
@Override protected void reduce(NodeWritable key, Iterable<T> values, Context context) throws IOException, InterruptedException { Map<NodeWritable, CharacteristicWritable> characteristics = new TreeMap<NodeWritable, CharacteristicWritable>(); // Firstly need to find individual characteristics Iterator<T> iter = values.iterator(); while (iter.hasNext()) { T tuple = iter.next(); NodeWritable predicate = this.getPredicate(tuple); if (characteristics.containsKey(predicate)) { characteristics.get(predicate).increment(); } else { characteristics.put(predicate, new CharacteristicWritable(predicate.get())); } } // Then we need to produce all the possible characteristic sets based on // this information List<CharacteristicWritable> cs = new ArrayList<CharacteristicWritable>(characteristics.values()); if (cs.size() == 0) return; for (int i = 1; i <= cs.size(); i++) { this.outputSets(cs, i, context); } }
@Override public void readFields(DataInput input) throws IOException { // Read size, then count, then characteristics int size = WritableUtils.readVInt(input); this.count.readFields(input); this.characteristics.clear(); for (int i = 0; i < size; i++) { CharacteristicWritable cw = CharacteristicWritable.read(input); this.characteristics.put(cw.getNode(), cw); } }
/** * Creates a set consisting of the given predicates * * @param predicates * Predicates * @return Set */ protected CharacteristicSetWritable createSet(MapReduceDriver<LongWritable, T, NodeWritable, T, CharacteristicSetWritable, NullWritable> driver, int occurrences, String... predicates) { CharacteristicSetWritable set = new CharacteristicSetWritable(); for (String predicateUri : predicates) { set.add(new CharacteristicWritable(NodeFactory.createURI(predicateUri))); } for (int i = 1; i <= occurrences; i++) { driver.addOutput(set, NullWritable.get()); } return set; }
/** * Creates a new characteristic set * * @param count * Count * @param characteristics * Characteristics */ public CharacteristicSetWritable(long count, CharacteristicWritable... characteristics) { this.count.set(count); for (CharacteristicWritable characteristic : characteristics) { this.characteristics.put(characteristic.getNode(), characteristic); } }
/** * Increments the count by 1 */ public void increment() { this.increment(1); }
@Override public int hashCode() { // Build a hash code from characteristics if (this.characteristics.size() == 0) return 0; Iterator<CharacteristicWritable> iter = this.getCharacteristics(); int hash = 17; while (iter.hasNext()) { hash = hash * 31 + iter.next().hashCode(); } return hash; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("{ "); builder.append(this.count.get()); Iterator<CharacteristicWritable> iter = this.getCharacteristics(); while (iter.hasNext()) { builder.append(" , "); builder.append(iter.next().toString()); } builder.append(" }"); return builder.toString(); }
@Override public void readFields(DataInput input) throws IOException { // Read size, then count, then characteristics int size = WritableUtils.readVInt(input); this.count.readFields(input); this.characteristics.clear(); for (int i = 0; i < size; i++) { CharacteristicWritable cw = CharacteristicWritable.read(input); this.characteristics.put(cw.getNode(), cw); } }
/** * Creates a set consisting of the given predicates * * @param predicates * Predicates * @return Set */ protected CharacteristicSetWritable createSet( MapReduceDriver<CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, NullWritable> driver, int inputOccurrences, int outputOccurrences, String... predicates) { CharacteristicSetWritable set = new CharacteristicSetWritable(); for (String predicateUri : predicates) { set.add(new CharacteristicWritable(NodeFactory.createURI(predicateUri))); } for (int i = 1; i <= inputOccurrences; i++) { driver.addInput(set, set); } for (int i = 1; i <= outputOccurrences; i++) { driver.addOutput(set, NullWritable.get()); } return set; }
/** * Creates a new characteristic set * * @param count * Count * @param characteristics * Characteristics */ public CharacteristicSetWritable(long count, CharacteristicWritable... characteristics) { this.count.set(count); for (CharacteristicWritable characteristic : characteristics) { this.characteristics.put(characteristic.getNode(), characteristic); } }
/** * Increments the count by 1 */ public void increment() { this.increment(1); }
@Override public int hashCode() { // Build a hash code from characteristics if (this.characteristics.size() == 0) return 0; Iterator<CharacteristicWritable> iter = this.getCharacteristics(); int hash = 17; while (iter.hasNext()) { hash = hash * 31 + iter.next().hashCode(); } return hash; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("{ "); builder.append(this.count.get()); Iterator<CharacteristicWritable> iter = this.getCharacteristics(); while (iter.hasNext()) { builder.append(" , "); builder.append(iter.next().toString()); } builder.append(" }"); return builder.toString(); }
/** * Adds a characteristic to the set merging it into the appropriate existing * characteristic if applicable * * @param characteristic * Characteristics */ public void add(CharacteristicWritable characteristic) { if (this.characteristics.containsKey(characteristic.getNode())) { this.characteristics.get(characteristic.getNode()).increment(characteristic.getCount().get()); } else { this.characteristics.put(characteristic.getNode(), characteristic); } }
/** * Creates a new instance and reads in its data from the given input * * @param input * Input * @return New instance * @throws IOException */ public static CharacteristicWritable read(DataInput input) throws IOException { CharacteristicWritable cw = new CharacteristicWritable(); cw.readFields(input); return cw; }
/** * Test characteristic set reduction * * @throws IOException */ @Test public void characteristic_set_reducer_04() throws IOException { MapReduceDriver<CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, CharacteristicSetWritable, NullWritable> driver = this .getMapReduceDriver(); this.createSet(driver, 2, 1, "http://predicate"); this.createSet(driver, 1, 1, "http://other"); driver.runTest(false); driver = getMapReduceDriver(); createSet(driver, 2, 1, "http://predicate"); createSet(driver, 1, 1, "http://other"); List<Pair<CharacteristicSetWritable, NullWritable>> results = driver.run(); for (Pair<CharacteristicSetWritable, NullWritable> pair : results) { CharacteristicSetWritable cw = pair.getFirst(); boolean expectTwo = cw.getCharacteristics().next().getNode().get().hasURI("http://predicate"); Assert.assertEquals(expectTwo ? 2 : 1, cw.getCount().get()); } }
@Override public boolean equals(Object other) { if (!(other instanceof CharacteristicWritable)) return false; return this.compareTo((CharacteristicWritable) other) == 0; }