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.
/*
* Copyright (c) 2016 XCSP3 Team ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.xcsp.parser.entries;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.xcsp.common.Condition.ConditionPar1;
import org.xcsp.common.Condition.ConditionVar;
import org.xcsp.common.Softening;
import org.xcsp.common.Types.TypeChild;
import org.xcsp.common.Types.TypeCtr;
import org.xcsp.common.Types.TypeExpr;
import org.xcsp.common.Types.TypeReification;
import org.xcsp.common.Utilities;
import org.xcsp.common.predicates.XNode;
import org.xcsp.common.predicates.XNodeParent;
import org.xcsp.parser.entries.ParsingEntry.CEntry;
import org.xcsp.parser.entries.XVariables.XVar;
/**
* In this class, we find intern classes for managing stand-alone constraints, groups of constraints, and meta-constraints.
*
* @author Christophe Lecoutre
*/
public class XConstraints {
/** Collects the variables involved in the specified object, and add them to the specified set. */
private static LinkedHashSet collectVarsIn(Object obj, LinkedHashSet set) {
if (obj instanceof Object[])
IntStream.range(0, Array.getLength(obj)).forEach(i -> collectVarsIn(Array.get(obj, i), set));
else if (obj instanceof XNode) // possible if view
// XNode.class.cast(obj).collectVars(set);
((XNode) obj).collectVarsToSet(set);
else if (obj instanceof XVar)
set.add((XVar) obj);
else if (obj instanceof ConditionVar)
set.add((XVar) ((ConditionVar) obj).x);
return set;
}
/** The class used for representing parameters (tokens of the form %i or %...) when handling constraint templates. */
public static final class XParameter {
/** The number associated with the parameter. We have -1 for %..., 0 for %0, 1 for %1, and so on. */
public final int number;
public XParameter(int number) {
this.number = number;
}
@Override
public String toString() {
return "%" + (number == -1 ? "..." : number);
}
}
/** The class used for representing reification. */
public static final class XReification {
public final TypeReification type;
/** The 0-1 variable used for reification */
public final XVar var;
public XReification(TypeReification type, XVar var) {
this.type = type;
this.var = var;
}
@Override
public String toString() {
return "Reification:" + var + " (" + type + ")";
}
}
/**
* The class used for handling abstraction in constraint templates. Currently, it is possible to manage any number of abstract childs that are either
* totally abstract or abstract functional. Note that a child is totally abstract iff it only contains parameters (tokens of the form %i or %...), and that
* an abstract functional child is a child which has 'function' as type and which contains at least one parameter. When for a child a single value is
* expected, %... cannot be used. %... stands for all effective parameters that come after the one corresponding to the highest encountered numbered
* parameter.
*/
public static final class XAbstraction {
/** The abstract child elements from the list of child elements of a constraint template. */
public final CChild[] abstractChilds;
/** The initial values of the abstract childs. We stored them because the values are lost after a first concretization */
private Object[] abstractChildValues;
/** The mappings to be used when concretizing */
private int[][] mappings;
private int highestParameterNumber;
private int[] mappingFor(CChild child) {
if (child.type == TypeChild.function)
return null;
if (child.type == TypeChild.condition)
return new int[] { ((ConditionPar1) child.value).par1.number };
if (child.value.getClass().isArray())
return IntStream.range(0, Array.getLength(child.value)).map(i -> ((XParameter) Array.get(child.value, i)).number).toArray();
// XUtility.control(((XParameter) child.value).number != -1, "%... forbidden when a single value is expected.");
return new int[] { ((XParameter) child.value).number };
}
public XAbstraction(CChild... abstractChilds) {
this.abstractChilds = abstractChilds;
this.abstractChildValues = Stream.of(abstractChilds).map(child -> child.value).toArray();
mappings = Stream.of(abstractChilds).map(child -> mappingFor(child)).toArray(int[][]::new);
highestParameterNumber = Math.max(-1,
IntStream.range(0, abstractChilds.length)
.map(i -> abstractChilds[i].type == TypeChild.function ? ((XNode>) abstractChilds[i].value).maxParameterNumber()
: IntStream.of(mappings[i]).max().getAsInt())
.max().getAsInt());
}
private Object concreteValueFor(CChild child, Object abstractChildValue, Object[] args, int[] mapping) {
if (child.type == TypeChild.function)
return ((XNodeParent>) abstractChildValue).concretization(args);
if (child.type == TypeChild.condition)
return ((ConditionPar1) abstractChildValue).concretizeWith(args[mapping[0]]);
if (child.value.getClass().isArray()) {
List