View Javadoc

1   package org.sat4j.csp.xml;
2   import org.xml.sax.Attributes;
3   
4   import static org.sat4j.csp.xml.TagNames.*;
5   
6   class Relation extends Element {
7   
8   
9   	private StringBuilder allTuples;
10  
11  	private int arity;
12  
13  	public Relation(ICSPCallback out,String tagName) {
14  		super(out,tagName);
15  	}
16  
17  	public void startElement(Attributes att) {
18  		int nbTuples = -1;
19  		String tmpTuples = att.getValue(NB_TUPLES);
20  		if (tmpTuples != null)
21  			nbTuples = Integer.parseInt(tmpTuples);
22  		String semantics = att.getValue(SEMANTICS);
23  		boolean isSupport = (semantics != null && semantics.equals(SUPPORT));
24  		arity = Integer.parseInt(att.getValue(ARITY));
25  		getCB().beginRelation(att.getValue(NAME), arity, nbTuples, isSupport);
26  		// reinitialization of the string for the tuples
27  		allTuples = new StringBuilder();
28  	}
29  
30  	public void characters(String allTuples) {
31  		this.allTuples.append(allTuples);
32  	}
33  
34  	public void endElement() {
35  		String[] tuples = allTuples.toString().trim().split(TUPLE_SEPARATOR);
36  		int[] oneTuple;
37  		for (String tuple : tuples) {
38  			if (!tuple.equals("")) {
39  				oneTuple = toIntArray(tuple.split("\\s+"));
40  				// is the following OK?
41  //				if (oneTuple.length != arity)
42  //					throw new CSPFormatException("At least one tuple doesn't have the right arity.");
43  				getCB().addRelationTuple(oneTuple);
44  			}
45  		}
46  		getCB().endRelation();
47  	}
48  
49  	private int[] toIntArray(String[] str) {
50  		int[] res = new int[str.length];
51  		for (int i = 0; i < str.length; i++)
52  			res[i] = Integer.parseInt(str[i]);
53  		return res;
54  	}
55  
56  }