Android Open Source - sana Criterion






From Project

Back to project page sana.

License

The source code is released under:

Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...

If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.moca.procedure.branching;
//www.  ja va2 s  .  c  o  m
import org.moca.procedure.MultiSelectElement;
import org.moca.procedure.ProcedureElement;
import org.moca.procedure.ProcedureParseException;
import org.moca.procedure.ProcedureElement.ElementType;

import android.util.Log;

/**
 * The Criterion class is a data representation of the XML 
 * <Criteria type="" elementId="" value=""/>
 * It holds a reference to the element elementId refers to so that it can 
 * determine user responses. The three Criterion Types (EQUALS, GREATER, LESS) are 
 * logically-complete, since a Criterion object will always sit inside a Criteria 
 * object, which can perform arbitrary boolean logic on it.
 */
public class Criterion {
    public static enum CriterionType {
        EQUALS, GREATER, LESS
    }
    private static final String TAG = "Criterion";
    private CriterionType criterionType;
    private ProcedureElement element;
    private String value;
    
    public Criterion(CriterionType critType, ProcedureElement elmt, String val) throws ProcedureParseException {
        this.criterionType = critType;
        this.element = elmt;
        this.value = val;
        if (elmt == null)
            throw new ProcedureParseException("Null element");
        if ((critType == CriterionType.GREATER) || (critType == CriterionType.LESS)) {
            try {
                Integer.parseInt(val);
            } catch (NumberFormatException e) {
                throw new ProcedureParseException("Cannot compare non-integer value. " +
                        "Cannot create criterion for element " + elmt.getId());
            }
        }
    }
    
    /**
     * Checks if the given Criterion is met, given user responses.
     * 
     * For a Criterion based on a Multi-Select element, if any of the choices
     * evaluates as true, then the Criterion is met.
     * 
     * For blank (unanswered, blank default) elements, criterionMet evaluates as true.
     */
    public boolean criterionMet() {
        // lookup what the user selected
        String userVal = "";
        try {
            userVal = element.getAnswer();
        } catch (NullPointerException e) {
            // play it safe and show the page
            return true;
        }           
        // check if it is empty
        if ((userVal == "") || (userVal== null)) {
            // empty user response, lets play it safe and show the page
            return true;
        }
        // special case MULTI-SELECT
        if (element.getType() == ElementType.MULTI_SELECT) {
            // We (arbitrarily) handle MultiSelect by seeing if 
            // ANY selection in the "answer" matches "val" 
            // if so, then it evaluates as true
            String[] vals = userVal.split(MultiSelectElement.TOKEN_DELIMITER);
            for (String s : vals) {
                if (criterionMetHelper(s))
                    return true;
            }
            return false;
        } else {
            // test the user's answer against the criterion
            return criterionMetHelper(userVal);
        }
    }
    
    private boolean criterionMetHelper(String userVal) {
        switch(criterionType) {
        case EQUALS:
            if (value.equals(userVal)) {
                return true;
            }
            break;
        case GREATER:
            try {
                if (Integer.parseInt(userVal) > Integer.parseInt(value))
                    return true;
            // show the page if we can't parse
            } catch (NumberFormatException e) {return true;}
            break;
        case LESS:
            try {
                if (Integer.parseInt(userVal) < Integer.parseInt(value))
                    return true;
            // show the page if we can't parse
            } catch (NumberFormatException e) {return true;}
            break;
        }
        return false;
    }
}




Java Source Code List

.Moca.java
org.moca.Constants.java
org.moca.ImagePreviewDialog.java
org.moca.ScalingImageAdapter.java
org.moca.SelectableImageView.java
org.moca.activity.NotificationList.java
org.moca.activity.NotificationViewer.java
org.moca.activity.PatientInfoDialog.java
org.moca.activity.ProcedureRunner.java
org.moca.activity.ProceduresList.java
org.moca.activity.SavedProcedureList.java
org.moca.activity.Settings.java
org.moca.db.EncounterDAO.java
org.moca.db.EventDAO.java
org.moca.db.EventProvider.java
org.moca.db.Event.java
org.moca.db.ImageProvider.java
org.moca.db.MocaDB.java
org.moca.db.NotificationMessage.java
org.moca.db.NotificationProvider.java
org.moca.db.PatientInfo.java
org.moca.db.PatientProvider.java
org.moca.db.PatientValidator.java
org.moca.db.ProcedureDAO.java
org.moca.db.ProcedureProvider.java
org.moca.db.SavedProcedureProvider.java
org.moca.db.SoundProvider.java
org.moca.media.AudioPlayer.java
org.moca.net.MDSCode.java
org.moca.net.MDSInterface.java
org.moca.net.MDSNotification.java
org.moca.net.MDSResult.java
org.moca.net.SMSReceive.java
org.moca.procedure.BinaryUploadElement.java
org.moca.procedure.DateElement.java
org.moca.procedure.GpsElement.java
org.moca.procedure.MultiSelectElement.java
org.moca.procedure.PatientIdElement.java
org.moca.procedure.PictureElement.java
org.moca.procedure.ProcedureElement.java
org.moca.procedure.ProcedurePage.java
org.moca.procedure.ProcedureParseException.java
org.moca.procedure.Procedure.java
org.moca.procedure.RadioElement.java
org.moca.procedure.SelectElement.java
org.moca.procedure.SoundElement.java
org.moca.procedure.TextElement.java
org.moca.procedure.TextEntryElement.java
org.moca.procedure.ValidationError.java
org.moca.procedure.branching.Criteria.java
org.moca.procedure.branching.Criterion.java
org.moca.procedure.branching.LogicAnd.java
org.moca.procedure.branching.LogicBase.java
org.moca.procedure.branching.LogicNot.java
org.moca.procedure.branching.LogicOr.java
org.moca.service.BackgroundUploader.java
org.moca.service.QueueManager.java
org.moca.service.ServiceConnector.java
org.moca.service.ServiceListener.java
org.moca.task.CheckCredentialsTask.java
org.moca.task.ImageProcessingTaskRequest.java
org.moca.task.ImageProcessingTask.java
org.moca.task.MDSSyncTask.java
org.moca.task.PatientLookupListener.java
org.moca.task.PatientLookupTask.java
org.moca.task.ResetDatabaseTask.java
org.moca.task.ValidationListener.java
org.moca.util.MocaUtil.java
org.moca.util.UserDatabase.java