mvcReader

This class reads data from a properly formatted file and returns a MultiValuedRelation, which is a matrix (a vector of vectors) with a name, and row and column labels.

public class MvcReader

 extends AbstractReader
 implements MultiValuedRelationReader {
 public MvcReader(Reader r) {
         super(r);
 }

public MultiValuedRelation readMultiValuedRelation() throws BadInputDataException,IOException {

  1. verify the file is properly identified : if(!getStream().readLine().trim().equals(”[Multi-Valued Relation]”)) throw new IOException(“The MVC file does not start with : [Multi-Valued Relation]\n”);
  2. initialize properties
    1. MultiValuedRelation mvRel=null;
    2. String nomRel=getStream().readLine().trim();
    3. StringTokenizer sk=null;
    4. sk=new StringTokenizer(getStream().readLine().trim(),”|”); columns are separated by “|”, first data line contains the object names (I guess)
    5. Vector lesObjs=new Vector();
    6. add each identifier from the tokenized stream as an object
      1. while(sk.hasMoreElements() ){ lesObjs.add(new DefaultFormalObject(sk.nextElement().toString() ) ); }
    7. now do the same for the attribute names :
      • sk=new StringTokenizer(getStream().readLine().trim(),”|”);
      • Vector lesAtts=new Vector();
      • while(sk.hasMoreElements() ){ lesAtts.add(new DefaultFormalAttribute(sk.nextElement().toString() ) ); }
    8. build the MultiValuedRelation with the rest of the data
      • mvRel=new MultiValuedRelation(lesObjs.size(),lesAtts.size() );
      • mvRel.setRelationName(nomRel);
      • for(int i=0;i<lesObjs.size();i++) { mvRel.replaceObject(i,(FormalObject)lesObjs.elementAt(i) ); }
      • for(int j=0;j<lesAtts.size();j++) { mvRel.replaceAttribute(j,(FormalAttribute)lesAtts.elementAt(j) ); }
      • StringTokenizer st2;
      • String val;
      • for(int i=0;i<lesObjs.size();i++) { 1)
        • int j=0;
        • while(sk.hasMoreElements()) { String vTmp=sk.nextToken().trim(); st2=new StringTokenizer(vTmp,”@”);
          • while(st2.hasMoreElements() ){ val=st2.nextToken().trim(); if(!val.equals(“0”)){ mvRel.addValuedRelation((FormalObject)lesObjs.elementAt(i),(FormalAttribute)lesAtts.elementAt(j),new FormalAttributeValue(val) ); }
          • }
        • j++;
      • }
    9. return mvRel; }

imports

package lattice.io;
 
import java.io.IOException;
import java.io.Reader;
import java.util.StringTokenizer;
import java.util.Vector;

import lattice.util.concept.DefaultFormalAttribute;
import lattice.util.concept.DefaultFormalObject;
import lattice.util.concept.FormalAttribute;
import lattice.util.concept.FormalAttributeValue;
import lattice.util.concept.FormalObject;
import lattice.util.exception.BadInputDataException;
import lattice.util.relation.MultiValuedRelation;
1) check the line is useable : sk=new StringTokenizer(getStream().readLine().trim(),”|”); if(sk==null || sk.countTokens()<lesAtts.size() ) { throw new BadInputDataException(“Some relationships are missing in the MVC file.\n”); }

 
java/mvcreader.txt · Dernière modification: 2010/02/16 13:43 par suitable