Commit ea40794a authored by Daniel Le Berre's avatar Daniel Le Berre

Initial import for the XCSP 2.1 reader.

git-svn-id: svn+ssh://svn.forge.ow2.org/svnroot/sat4j/maven/trunk@30 ba638df5-4473-46d1-82f8-c3ae2a17a6e1
parent f1d545a1
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CSPXMLParser</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sat4j</groupId>
<artifactId>org.sat4j.pom</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>org.sat4j.csp.xmlparser</artifactId>
<name>SAT4J csp xml parser</name>
</project>
This diff is collapsed.
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.sat4j.csp.xml.CspXmlParser;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws ParserConfigurationException {
try {
CspXmlParser.parse(new SimpleCallback(),args[0]);
} catch (IOException e) {
System.out.println("pbe d'entree-sortie");
return;
} catch (SAXException e) {
System.out.println(e.getMessage());
System.out.println("pbe du parseur");
return;
}
}
}
package org.sat4j.csp.xml;
class CSPFormatException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public CSPFormatException(String message) {
super(message);
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
class ConstantParameter extends Element {
public ConstantParameter(ICSPCallback out,String tagName) {
super(out,tagName);
}
}
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
import static org.sat4j.csp.xml.TagNames.*;
class Constraint extends Element {
public Constraint(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void startElement(Attributes att){
getCB().beginConstraint(att.getValue(NAME), Integer.parseInt(att.getValue(ARITY)));
String ref = att.getValue(REFERENCE);
if (ref != null)
getCB().constraintReference(ref);
String scope = att.getValue(SCOPE);
if (scope != null){
// traitement des variables dans le scope
String[] tokens = scope.trim().split("\\s+");
for(String tok : tokens)
if (!tok.equals(""))
getCB().addVariableToConstraint(tok);
}
context = "Constraint";
}
public void endElement() {
getCB().endConstraint();
context = "";
}
private String context;
public String getContexte(){
return context;
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
import static org.sat4j.csp.xml.TagNames.NB_CONSTRAINTS;
class Constraints extends Element {
public Constraints(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void startElement(Attributes att){
getCB().beginConstraintsSection(
Integer.parseInt(att.getValue(NB_CONSTRAINTS)));
}
public void endElement() {
getCB().endConstraintsSection();
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class CspXmlParser {
public static void parse(ICSPCallback callback, String filename)
throws SAXException, ParserConfigurationException,
FileNotFoundException, IOException {
// le producteur
XMLReader prod;
// le consommateur
DefaultHandler cons;
// obtenir un parser
SAXParserFactory saxpf = SAXParserFactory.newInstance();
// associer un schema pour la validation
URL url = CspXmlParser.class.getResource("/instance_2_0.xsd");
// System.out.println(url);
saxpf.setSchema(SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(url));
prod = saxpf.newSAXParser().getXMLReader();
cons = new InstanceParser(callback);
prod.setContentHandler(cons);
prod.setErrorHandler(cons);
// et c'est parti!
prod.parse(new InputSource(new FileReader(filename)));
}
}
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
class Domain extends Element {
private static final String INTERVAL_SEPARATOR = "..";
private StringBuilder allValues;
public Domain(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void startElement(Attributes att) {
int nbValues = -1;
String tmpValues = att.getValue("nbValues");
if (tmpValues != null)
nbValues = Integer.parseInt(tmpValues);
getCB().beginDomain(att.getValue("name"), nbValues);
allValues = new StringBuilder();
}
public void endElement() {
if (allValues != null) {
String[] tokens = allValues.toString().trim().split("\\s+");
int index;
for (String token : tokens) {
if (!token.equals("")) {
index = token.indexOf(INTERVAL_SEPARATOR);
if (index > -1)
getCB().addDomainValue(Integer.parseInt(token.substring(0,index)),
Integer.parseInt(token.substring(index+2)));
else
getCB().addDomainValue(Integer.parseInt(token));
}
}
}
getCB().endDomain();
}
public void characters(String allValues) {
this.allValues.append(allValues);
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
import static org.sat4j.csp.xml.TagNames.NB_DOMAINS;
class Domains extends Element {
public Domains(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void startElement(Attributes att){
getCB().beginDomainsSection(Integer.parseInt(att.getValue(NB_DOMAINS)));
}
public void endElement() {
getCB().endDomainsSection();
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
abstract class Element {
private final ICSPCallback out;
private final String tagName;
public Element(ICSPCallback out, String tagName) {
this.out = out;
this.tagName = tagName;
}
public ICSPCallback getCB() {
return out;
}
public void startElement(Attributes attributes) {
}
public void characters(String s) {
}
public void endElement() {
}
public String getContext(){
return "";
}
public String tagName(){
return tagName;
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
class Expression extends Element {
public Expression(ICSPCallback out,String tagName) {
super(out,tagName);
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
import org.xml.sax.Attributes;
class Functional extends Element {
private StringBuilder expr;
public Functional(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void startElement(Attributes attributes) {
expr = new StringBuilder();
}
public void characters(String s) {
expr.append(s);
}
public void endElement() {
getCB().predicateExpression(expr.toString().trim());
}
}
\ No newline at end of file
package org.sat4j.csp.xml;
public interface ICSPCallback {
/**
* signal the beginning of parsing
*
* @param name:
* name of the instance
*/
void beginInstance(String name);
/** ***************************************************************** */
/**
* callback called at the beginning of the domains declarations
*
* @param nbDomains:
* number of domains that will be declared
*/
void beginDomainsSection(int nbDomains);
/**
* callback called at the beginning of the declaration of one domain
*
* @param name:
* identifier of the domain
* @param nbValue:
* number of values in the domain
*/
void beginDomain(String name, int nbValue);
/**
* add a single value to the current domain
*
* @param v:
* value to add to the domain
*/
void addDomainValue(int v);
/**
* add the range of values [first..last] to the current domain
*
* @param first:
* first value to add to the domain
* @param last:
* last value to add to the domain
*/
void addDomainValue(int first, int last);
/**
* ends the definition of the current domain
*/
void endDomain();
/**
* end the definition of all domains
*/
void endDomainsSection();
/** ***************************************************************** */
/**
* callback called at the beginning of the variables declarations
*
* @param nbVariables:
* number of variables that will be declared
*/
void beginVariablesSection(int nbVariables);
/**
* callback called to define a new variable
*
* @param name:
* identifier of the variable
* @param domain:
* identifier of the variable domain
*/
void addVariable(String name, String domain);
/**
* end the definition of all variables
*/
void endVariablesSection();
/** ***************************************************************** */
/**
* callback called at the beginning of the relations declarations
*
* @param nbRelations:
* number of relations that will be declared
*/
void beginRelationsSection(int nbRelations);
/**
* callback called at the beginning of the declaration of one relation
*
* @param name:
* identifier of the relation
* @param arity:
* arity of the relation
* @param nbTuples:
* number of tuples in the relation
* @param isSupport:
* true if tuples represent support, false if tuples represent
* conflicts
*/
void beginRelation(String name, int arity, int nbTuples, boolean isSupport);
/**
* add a single tuple to the current relation
*
* @param tuple:
* tuple to add to the relation (contains arity elements)
*/
void addRelationTuple(int tuple[]);
/**
* ends the definition of the current relation
*/
void endRelation();
/**
* end the definition of all relations
*/
void endRelationsSection();
/** ***************************************************************** */
/**
* callback called at the beginning of the predicates declarations
*
* @param nbPredicates:
* number of predicates that will be declared
*/
void beginPredicatesSection(int nbPredicates);
/**
* callback called at the beginning of the declaration of one predicate
*
* @param name:
* identifier of the predicate
* @param arity:
* arity of the predicate
*/
void beginPredicate(String name);
/**
* add a formal parameter to the current predicate
*
* @param name:
* name of the parameter
* @param type:
* type of the parameter
*/
void addFormalParameter(String name, String type);
/**
* provide the expression of the current predicate
*
* @param tree:
* the abstract syntax tree representing the expression
*/
void predicateExpression(String expr);
/**
* ends the definition of the current predicate
*/
void endPredicate();
/**
* end the definition of all predicates
*/
void endPredicatesSection();
/** ***************************************************************** */
/**
* callback called at the beginning of the constraints declarations
*
* @param nbConstraints:
* number of constraints that will be declared
*/
void beginConstraintsSection(int nbConstraints);
/**
* callback called at the beginning of the declaration of one constraint
*
* @param name:
* identifier of the constraint
* @param arity:
* arity of the constraint
*/
void beginConstraint(String name, int arity);
/**
* provide the definition of the current constraint
*
* @param name:
* the refererence to the definition of this constraint. May be a
* relation, a predicate or the name of a global constraint
*/
void constraintReference(String name);
/**
* declares that a variable is in the constraint scope
*
* @param name:
* name of the variable
*/
void addVariableToConstraint(String name);
/**
* add an effective parameter to the current constraint. The effective
* parameter is described as an expression in a syntax chosen by the solver
* (C,Java,prefix,postfix,...)
* not considered for the 2006 competition
* @param expr:
* the expression
*/
// void addEffectiveParameter(String expr);
/**
* add an effective parameter to the current constraint. The effective
* parameter is described as an abstract syntax tree
*
* @param tree:
* expression that computes the value of the effective parameter
*/
// void addEffectiveParameter(AST *tree);
/**
* add an effective parameter which is a simple variable to the current
* constraint
*
* @param name:
* name of the variable passed as parameter
*/
void addEffectiveParameter(String name);
/**
* add an effective parameter which is a simple integer
*
* @param value:
* value of the parameter
*/
void addEffectiveParameter(int value);
/**
* provide the expression of the current constraint as an abstract syntax
* tree
*
* @param tree:
* the abstract syntax tree representing the expression
*/
// void constraintExpression(AST *tree);
/**
* begins the list tag for parameters of a constraint
*/
void beginParameterList();
/**
* provides an integer value in a parameter list of a constraint
* @param value:
* value of current list item
*/
void addIntegerItem(int value);
/**
* provides the name of a variable in a parameter list of a constraint
* @param name:
* name of the current list item
*/
void addVariableItem(String name);
/**
* ends the list tag for parameters of a constraint
*/
void endParamaterList();
/**
* provides a constant value
*/
void addConstantParameter(String name, int value);
/**
* provide the expression of the current constraint as an expression in a
* syntac chosen by the solver
*
* @param expr:
* the expression
*/
void constraintExpression(String expr);
/**
* ends the definition of the current constraint
*/
void endConstraint();
/**
* end the definition of all constraints
*/
void endConstraintsSection();
/** ***************************************************************** */
/**
* signal the end of parsing
*/
void endInstance();
};
package org.sat4j.csp.xml;
class Instance extends Element {
public Instance(ICSPCallback out,String tagName) {
super(out,tagName);
}
public void endElement() {
getCB().endInstance();
}
}
package org.sat4j.csp.xml;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
class InstanceParser extends DefaultHandler {
/** stacks of elements visited */
private Vector<Element> parents = new Vector<Element>();
/** map which associates tag name to the Element concerns with it */
private Map<String, Element> theElts;
public InstanceParser(ICSPCallback cb) {
theElts = new HashMap<String, Element>();
theElts.put("instance", new Instance(cb, "instance"));
theElts.put("presentation", new Presentation(cb, "presentation"));
theElts.put("domains", new Domains(cb, "domains"));
theElts.put("domain", new Domain(cb, "domain"));
theElts.put("variables", new Variables(cb, "variables"));
theElts.put("variable", new Variable(cb, "variable"));