Search in sources :

Example 1 with CharacteristicWritable

use of org.apache.jena.hadoop.rdf.types.CharacteristicWritable in project jena by apache.

the class AbstractCharacteristicSetGeneratingReducer method reduce.

@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);
    }
}
Also used : NodeWritable(org.apache.jena.hadoop.rdf.types.NodeWritable) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) CharacteristicWritable(org.apache.jena.hadoop.rdf.types.CharacteristicWritable)

Example 2 with CharacteristicWritable

use of org.apache.jena.hadoop.rdf.types.CharacteristicWritable in project jena by apache.

the class AbstractCharacteristicSetGeneratingReducer method outputSets.

/**
     * Output all sets of a given size
     * 
     * @param cs
     *            Characteristics
     * @param perSet
     *            Set size
     * @param context
     *            Context to output sets to
     * @throws IOException
     * @throws InterruptedException
     */
protected void outputSets(List<CharacteristicWritable> cs, int perSet, Context context) throws IOException, InterruptedException {
    if (perSet == 1) {
        for (CharacteristicWritable c : cs) {
            CharacteristicSetWritable set = new CharacteristicSetWritable(c);
            context.write(set, NullWritable.get());
            if (this.tracing) {
                LOG.trace("Key = {}", set);
            }
        }
    } else if (perSet == cs.size()) {
        CharacteristicSetWritable set = new CharacteristicSetWritable();
        for (CharacteristicWritable c : cs) {
            set.add(c);
        }
        context.write(set, NullWritable.get());
        if (this.tracing) {
            LOG.trace("Key = {}", set);
        }
    } else {
        CharacteristicWritable[] members = new CharacteristicWritable[perSet];
        this.combinations(cs, perSet, 0, members, context);
    }
}
Also used : CharacteristicSetWritable(org.apache.jena.hadoop.rdf.types.CharacteristicSetWritable) CharacteristicWritable(org.apache.jena.hadoop.rdf.types.CharacteristicWritable)

Example 3 with CharacteristicWritable

use of org.apache.jena.hadoop.rdf.types.CharacteristicWritable in project jena by apache.

the class CharacteristicTests method checkRoundTrip.

/**
     * Checks whether a writable round trips successfully
     * 
     * @param cw
     *            Characteristic writable
     * @throws IOException
     */
private void checkRoundTrip(CharacteristicWritable cw) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream output = new DataOutputStream(outputStream);
    cw.write(output);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    DataInputStream input = new DataInputStream(inputStream);
    CharacteristicWritable actual = CharacteristicWritable.read(input);
    Assert.assertEquals(cw, actual);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) CharacteristicWritable(org.apache.jena.hadoop.rdf.types.CharacteristicWritable)

Example 4 with CharacteristicWritable

use of org.apache.jena.hadoop.rdf.types.CharacteristicWritable in project jena by apache.

the class CharacteristicTests method characteristic_set_writable_01.

/**
     * Tests characteristic sets
     * 
     * @throws IOException
     */
@Test
public void characteristic_set_writable_01() throws IOException {
    CharacteristicSetWritable set = new CharacteristicSetWritable();
    // Add some characteristics
    CharacteristicWritable cw1 = new CharacteristicWritable(NodeFactory.createURI("http://example.org"));
    CharacteristicWritable cw2 = new CharacteristicWritable(NodeFactory.createURI("http://example.org/other"));
    set.add(cw1);
    set.add(cw2);
    this.checkCharacteristicSet(set, 2, new long[] { 1, 1 });
    this.checkRoundTrip(set);
}
Also used : CharacteristicSetWritable(org.apache.jena.hadoop.rdf.types.CharacteristicSetWritable) CharacteristicWritable(org.apache.jena.hadoop.rdf.types.CharacteristicWritable) Test(org.junit.Test)

Example 5 with CharacteristicWritable

use of org.apache.jena.hadoop.rdf.types.CharacteristicWritable in project jena by apache.

the class CharacteristicTests method characteristic_writable_02.

/**
     * Tests characteristic properties
     * 
     * @throws IOException
     */
@Test
public void characteristic_writable_02() throws IOException {
    Node n = NodeFactory.createURI("http://example.org");
    CharacteristicWritable cw1 = new CharacteristicWritable(n);
    CharacteristicWritable cw2 = new CharacteristicWritable(n, 100);
    this.checkRoundTrip(cw1);
    this.checkRoundTrip(cw2);
    // Should still be equal since equality is only on the node not the
    // count
    Assert.assertEquals(cw1, cw2);
}
Also used : Node(org.apache.jena.graph.Node) CharacteristicWritable(org.apache.jena.hadoop.rdf.types.CharacteristicWritable) Test(org.junit.Test)

Aggregations

CharacteristicWritable (org.apache.jena.hadoop.rdf.types.CharacteristicWritable)10 Test (org.junit.Test)6 CharacteristicSetWritable (org.apache.jena.hadoop.rdf.types.CharacteristicSetWritable)4 Node (org.apache.jena.graph.Node)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 NodeWritable (org.apache.jena.hadoop.rdf.types.NodeWritable)1