Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* AbsCon - Copyright (c) 2017, CRIL-CNRS - [email protected]
*
* All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL which accompanies this
* distribution, and is available at http://www.cecill.info
*/
package org.xcsp.modeler;
import static org.xcsp.common.Constants.ANNOTATIONS;
import static org.xcsp.common.Constants.ARGS;
import static org.xcsp.common.Constants.ARRAY;
import static org.xcsp.common.Constants.BLOCK;
import static org.xcsp.common.Constants.CONSTRAINTS;
import static org.xcsp.common.Constants.DECISION;
import static org.xcsp.common.Constants.DOMAIN;
import static org.xcsp.common.Constants.GROUP;
import static org.xcsp.common.Constants.INSTANCE;
import static org.xcsp.common.Constants.OBJECTIVES;
import static org.xcsp.common.Constants.VAR;
import static org.xcsp.common.Constants.VARIABLES;
import static org.xcsp.common.Utilities.element;
import static org.xcsp.modeler.definitions.ICtr.CONDITION;
import static org.xcsp.modeler.definitions.ICtr.EXTENSION;
import static org.xcsp.modeler.definitions.ICtr.FUNCTION;
import static org.xcsp.modeler.definitions.ICtr.INDEX;
import static org.xcsp.modeler.definitions.ICtr.INTENSION;
import static org.xcsp.modeler.definitions.ICtr.LIST;
import static org.xcsp.modeler.definitions.ICtr.SLIDE;
import static org.xcsp.modeler.definitions.ICtr.VALUE;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xcsp.common.Condition;
import org.xcsp.common.IVar;
import org.xcsp.common.Softening;
import org.xcsp.common.Softening.SofteningGlobal;
import org.xcsp.common.Softening.SofteningSimple;
import org.xcsp.common.Types.TypeAtt;
import org.xcsp.common.Types.TypeChild;
import org.xcsp.common.Types.TypeClass;
import org.xcsp.common.Types.TypeFramework;
import org.xcsp.common.Types.TypeVar;
import org.xcsp.common.Utilities;
import org.xcsp.common.domains.Values.IntegerInterval;
import org.xcsp.common.predicates.XNodeParent;
import org.xcsp.modeler.api.ProblemAPI;
import org.xcsp.modeler.definitions.DefXCSP;
import org.xcsp.modeler.definitions.DefXCSP.Son;
import org.xcsp.modeler.definitions.ICtr;
import org.xcsp.modeler.definitions.ICtr.ICtrExtension;
import org.xcsp.modeler.definitions.ICtr.ICtrInstantiation;
import org.xcsp.modeler.definitions.ICtr.ICtrIntension;
import org.xcsp.modeler.definitions.ICtr.ICtrMdd;
import org.xcsp.modeler.definitions.ICtr.ICtrRegular;
import org.xcsp.modeler.definitions.ICtr.ICtrSlide;
import org.xcsp.modeler.definitions.ICtr.ICtrSmart;
import org.xcsp.modeler.definitions.ICtr.Meta;
import org.xcsp.modeler.entities.CtrEntities.CtrAlone;
import org.xcsp.modeler.entities.CtrEntities.CtrArray;
import org.xcsp.modeler.entities.CtrEntities.CtrEntity;
import org.xcsp.modeler.entities.ModelingEntity;
import org.xcsp.modeler.entities.ModelingEntity.TagDummy;
import org.xcsp.modeler.entities.ObjEntities.ObjEntity;
import org.xcsp.modeler.entities.VarEntities.VarAlone;
import org.xcsp.modeler.entities.VarEntities.VarArray;
import org.xcsp.modeler.entities.VarEntities.VarEntity;
import org.xcsp.modeler.implementation.ProblemIMP;
import org.xcsp.modeler.implementation.ProblemIMP3;
import org.xcsp.modeler.implementation.ProblemIMP3.MVariable;
import org.xcsp.modeler.problems.AllInterval;
import org.xcsp.modeler.problems.Bibd;
public class Compiler {
/**********************************************************************************************
* Constants
*********************************************************************************************/
public static final String FORMAT = TypeAtt.format.name();
public static final String XCSP3 = "XCSP3";
public static final String TYPE = TypeAtt.type.name();
public static final String ID = TypeAtt.id.name();
public static final String CLASS = TypeAtt.CLASS.name().toLowerCase();
public static final String NOTE = TypeAtt.note.name();
public static final String AS = TypeAtt.as.name();
public static final String FOR = TypeAtt.FOR.name().toLowerCase();
public static final String CIRCULAR = TypeAtt.circular.name();
public static final String OFFSET = TypeAtt.offset.name();
public static final String COLLECT = TypeAtt.collect.name();
public static final String VIOLATION_COST = TypeAtt.violationCost.name();
public static final String VIOLATION_MEASURE = TypeAtt.violationMeasure.name();
public static final String SUPPORTS = TypeChild.supports.name();
public static final String CONFLICTS = TypeChild.conflicts.name();
public static final String VAR_ARGS = "%...";
public static final int LIMIT_FOR_VAR_ARGS = 3;
public static final String VARIANT = "-variant";
public static final String DATA = "-data";
public static final String DATA_FORMAT = "-dataFormat";
public static final String DATA_EXPORT = "-dataexport";
public static final String OUTPUT = "-output";
public static final String EV = "-ev";
public static final String MUST_CANONIZE = "-mc";
public static final String IC = "-ic";
/**********************************************************************************************
* Fields and Constructor
*********************************************************************************************/
protected final ProblemIMP imp;
protected Document doc;
protected Map tuplesReferents = new HashMap<>();
protected int nBuiltTuplesReferents;
// HARD CODING/VALUES BELOW
protected int limitForUsingAs = 12;
protected boolean discardIntegerType = true, discardAsRelation = true, printNotes = true;
protected boolean doubleAbstraction = true, saveImmediatelyStored = true, ignoreAutomaticGroups = true, monoformGroups = false;
private boolean noGroupAtAllForExtension = false, noGroupAtAllForIntension = false, noGroupAtAllForGlobal = false;
private boolean uncompactDomainFor = false;
private boolean mustEraseIdsOfConstraints = false;
private boolean mergeSuccessiveInstantiations = false;
// sometimes, for efficiency reasons, it is important to set noGroupAtAllForExtension to true and uncompactDomainFor to true
/**
* Builds an object that allow us to generate XCSP3 instances from the specified MCSP3 model. Data are expected to be provided at the command
* line.
*
* @param api
* the object denoting the model of the problem
*/
public Compiler(ProblemAPI api) {
this.imp = api.imp();
}
protected Document buildDocument() {
// TODO control that ids are all different
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Element root = element(doc, INSTANCE, FORMAT, XCSP3, TYPE, imp.objEntities.active() ? TypeFramework.COP.name() : imp.typeFramework().name());
root.appendChild(variables());
root.appendChild(constraints());
if (imp.objEntities.active())
root.appendChild(objectives());
if (imp.annotations.active())
root.appendChild(annotations());
doc.appendChild(root);
doc.normalize();
return doc;
}
/**********************************************************************************************
* Managing (groups of) predicates, relations and globals
*********************************************************************************************/
private List storedP = new ArrayList<>();
private List storedR = new ArrayList<>();
private List storedG = new ArrayList<>();
private void saveStored(Element parent, boolean immediatly, boolean br, boolean bp, boolean bg) {
if (!immediatly)
return;
if (br && storedR.size() > 0)
parent.appendChild(buildingStoredRelations());
if (bp && storedP.size() > 0)
parent.appendChild(buildingStoredPredicates());
if (bg && storedG.size() > 0)
parent.appendChild(buildingStoredGlobals());
}
private void saveStored(Element parent) {
saveStored(parent, true, true, true, true);
}
private abstract class Similarable {
protected abstract boolean isSimilarTo(T object);
protected boolean haveSimilarAttributes(ICtr c1, ICtr c2) {
CtrAlone ca1 = imp.ctrEntities.ctrToCtrAlone.get(c1), ca2 = imp.ctrEntities.ctrToCtrAlone.get(c2);
if (ca1.id != null || ca2.id != null)
return false;
if (!TypeClass.equivalent(ca1.classes, ca2.classes))
return false;
if ((ca1.note == null) != (ca2.note == null) || (ca1.note != null && !ca1.note.equals(ca2.note)))
return false;
if ((ca1.softening == null) != (ca2.softening == null))
return false;
if (ca1.softening != null) { // and necessarily ca2.softening != null too
if (ca1.softening.getClass() != ca2.softening.getClass())
return false;
if (ca1.softening.cost != null || ca2.softening.cost != null)
return false; // relaxed constraints are considered as being not similar (do not see how it could be different)
// we have to check cost functions now
if (ca1.softening instanceof SofteningSimple) {
if (((SofteningSimple) ca1.softening).violationCost != ((SofteningSimple) ca2.softening).violationCost)
return false;
} else if (ca1.softening instanceof SofteningGlobal) {
if (((SofteningGlobal) ca1.softening).type != ((SofteningGlobal) ca2.softening).type)
return false;
if (((SofteningGlobal) ca1.softening).parameters != null || ((SofteningGlobal) ca2.softening).parameters != null)
return false;
} else
return false;
}
return true;
}
}
/**
* A class used to handle constraints {@code intension}, with the objective of building groups of similar intension constraints
*/
private class Predicate extends Similarable {
private ICtrIntension c;
private XNodeParent> abstractTree;
private List