001    /*
002    Copyright (c) 1996-2011, Damon Hart-Davis
003    All rights reserved.
004    
005    Redistribution and use in source and binary forms, with or without
006    modification, are permitted provided that the following conditions are
007    met:
008    
009      * Redistributions of source code must retain the above copyright
010        notice, this list of conditions and the following disclaimer.
011    
012      * Redistributions in binary form must reproduce the above copyright
013        notice, this list of conditions and the following disclaimer in the
014        documentation and/or other materials provided with the
015        distribution.
016    
017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
018    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
019    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
020    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
024    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
025    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
027    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028    */
029    
030    package org.hd.d.pg2k.ai.scorer.fixed;
031    
032    import java.io.IOException;
033    import java.util.Collections;
034    import java.util.List;
035    import java.util.Map;
036    
037    import org.hd.d.pg2k.ai.scorer.AbstractScorer;
038    import org.hd.d.pg2k.ai.scorer.BadScorer;
039    import org.hd.d.pg2k.ai.scorer.ScoreAndConf;
040    import org.hd.d.pg2k.ai.scorer.ScorerIF;
041    import org.hd.d.pg2k.ai.scorer.ScorerParam;
042    import org.hd.d.pg2k.ai.scorer.ScorerParamInteger;
043    import org.hd.d.pg2k.svrCore.Name;
044    import org.hd.d.pg2k.svrCore.Tuple.Pair;
045    import org.hd.d.pg2k.svrCore.datasource.SimpleExhibitPipelineIF;
046    
047    /**Fixed score given by its single parameter.
048     * This "fake" Scorer should represent the highest "goodness"
049     * that a fake/dumb implementation (that does not look at its input) can reach;
050     * a real Scorer must do better than this to be credible.
051     *
052     * @author dhd
053     */
054    public final class FixedScore extends AbstractScorer implements BadScorer
055        {
056        /**Create simple non-parameterised instance. */
057        public FixedScore()
058            {
059            // All parameters get default values.
060            resultScoreParam = resultScoreParamBounds;
061            }
062    
063        /**Create parameterised version. */
064        public FixedScore(final String nameAndParameters)
065            {
066            super(nameAndParameters);
067    
068            final Pair<String, Map<String, String>> nap = parseNameAndParameters(nameAndParameters);
069            final Map<String, String> paramValueMap = nap.second; // Capture the parameters.
070    
071            // Assign parameters from the captured map where possible, using default values otherwise.
072            resultScoreParam = (ScorerParamInteger) resultScoreParamBounds.parse(paramValueMap.get(resultScoreParamBounds.name));
073            }
074    
075        /**Create parameterised version. */
076        public FixedScore(final String baseName, final List<ScorerParam> parameters)
077            {
078            super(baseName, parameters);
079    
080            final Map<String, ScorerParam> paramValueMap = paramListAsMap(parameters);
081    
082            // Assign parameters from the captured map where possible, using default values otherwise.
083            resultScoreParam = (ScorerParamInteger) resultScoreParamBounds.extract(paramValueMap.get(resultScoreParamBounds.name));
084            }
085    
086        /**Simple non-static factory for the parameterised case. */
087        public ScorerIF createVariant(final String nameAndParameters) throws IllegalArgumentException
088            { return(new FixedScore(nameAndParameters)); }
089    
090        /**Simple non-static factory for the parameterised case. */
091        public ScorerIF createVariant(final String baseName, final List<ScorerParam> parameters) throws IllegalArgumentException
092            { return(new FixedScore(baseName, parameters)); }
093    
094        /**Respond with the fixed score based on the parameter; never null. */
095        public ScoreAndConf computeScoreAndConfidence(final SimpleExhibitPipelineIF dataSource, final Name.ExhibitFull exhibitName) throws IOException
096            { return(new ScoreAndConf((((2*resultScoreParam.value)-(resultScoreParamBounds.max)) * ScoreAndConf.MAX) / resultScoreParamBounds.max, ScoreAndConf.MAX)); }
097    
098    
099        /**Min, default and max result score; never null. */
100        private static final ScorerParamInteger resultScoreParamBounds =
101            ScorerParamInteger.createScorerParamInteger(0, 50, 100, "resultScore");
102    
103        /**Fixed result score for this variant; never null. */
104        private final ScorerParamInteger resultScoreParam;
105    
106        /**Get parameter definitions and values (immutable) for this Scorer; never null. */
107        @Override
108        public List<ScorerParam> getParameterDefsAndValues()
109            { return(Collections.singletonList((ScorerParam) resultScoreParam)); }
110        }