All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.timefold.solver.jackson.api.domain.solution.AbstractConstraintWeightOverridesDeserializer Maven / Gradle / Ivy

Go to download

Timefold solves planning problems. This lightweight, embeddable planning engine implements powerful and scalable algorithms to optimize business resource scheduling and planning. This module contains the Jackson integration.

The newest version!
package ai.timefold.solver.jackson.api.domain.solution;

import java.io.IOException;
import java.util.LinkedHashMap;

import ai.timefold.solver.core.api.domain.solution.ConstraintWeightOverrides;
import ai.timefold.solver.core.api.score.Score;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * Extend this to implement {@link ConstraintWeightOverrides} deserialization specific for your domain.
 *
 * @param 
 */
public abstract class AbstractConstraintWeightOverridesDeserializer>
        extends JsonDeserializer> {

    @Override
    public final ConstraintWeightOverrides deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        var resultMap = new LinkedHashMap();
        JsonNode node = p.readValueAsTree();
        node.fields().forEachRemaining(entry -> {
            var constraintName = entry.getKey();
            var weight = parseScore(entry.getValue().asText());
            resultMap.put(constraintName, weight);
        });
        return ConstraintWeightOverrides.of(resultMap);
    }

    /**
     * The domain is based on a single {@link Score} subtype.
     * This method is responsible for parsing the score string into that subtype.
     *
     * @param scoreString never null
     * @return never null
     */
    protected abstract Score_ parseScore(String scoreString);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy