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 public Relation(ICSPCallback out,String tagName) {
12 super(out,tagName);
13 }
14
15 public void startElement(Attributes att) {
16 int nbTuples = -1;
17 String tmpTuples = att.getValue(NB_TUPLES);
18 if (tmpTuples != null)
19 nbTuples = Integer.parseInt(tmpTuples);
20 String semantics = att.getValue(SEMANTICS);
21 boolean isSupport = (semantics != null && semantics.equals(SUPPORT));
22 int arity = Integer.parseInt(att.getValue(ARITY));
23 getCB().beginRelation(att.getValue(NAME), arity, nbTuples, isSupport);
24
25 allTuples = new StringBuilder();
26 }
27
28 public void characters(String allTuples) {
29 this.allTuples.append(allTuples);
30 }
31
32 public void endElement() {
33 String[] tuples = allTuples.toString().trim().split(TUPLE_SEPARATOR);
34 int[] oneTuple;
35 for (String tuple : tuples) {
36 if (!tuple.equals("")) {
37 oneTuple = toIntArray(tuple.split("\\s+"));
38
39
40
41 getCB().addRelationTuple(oneTuple);
42 }
43 }
44 getCB().endRelation();
45 }
46
47 private int[] toIntArray(String[] str) {
48 int[] res = new int[str.length];
49 for (int i = 0; i < str.length; i++)
50 res[i] = Integer.parseInt(str[i]);
51 return res;
52 }
53
54 }