Search in sources :

Example 1 with IDescriptorValueCondition

use of ambit2.rules.conditions.IDescriptorValueCondition in project ambit-mirror by ideaconsult.

the class GenericReaction method getReactionFromJsonNode.

public static GenericReaction getReactionFromJsonNode(JsonNode node) throws Exception {
    GenericReaction reaction = new GenericReaction();
    Boolean use = JSONParsingUtils.extractBooleanKeyword(node, "USE", false);
    if (use != null)
        reaction.setFlagUse(use);
    reaction.name = JSONParsingUtils.extractStringKeyword(node, "NAME", true);
    reaction.smirks = JSONParsingUtils.extractStringKeyword(node, "SMIRKS", true);
    reaction.reactionClass = JSONParsingUtils.extractStringKeyword(node, "CLASS", false);
    Object[] obj = JSONParsingUtils.extractArrayKeyword(node, "USE_CONDITIONS", false, true);
    if (obj != null) {
        List<ICondition> conditions = new ArrayList<ICondition>();
        for (int i = 0; i < obj.length; i++) {
            if (obj[i] == null)
                continue;
            if (obj[i] instanceof String) {
                ICondition condition = ReactionParser.getConditionFromString((String) obj[i]);
                conditions.add(condition);
            } else {
                if (obj[i] instanceof JsonNode) {
                    IDescriptorValueCondition dvc = ConditionJsonParser.getDescriptorValueCondition((JsonNode) obj[i]);
                    conditions.add(dvc);
                } else
                    throw new Exception("Incorrect USE_CONDITIONS[" + (i + 1) + "]. It is not a string! " + obj[i]);
            }
        }
        reaction.setConditions(conditions);
    }
    return reaction;
}
Also used : IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ICondition(ambit2.rules.conditions.ICondition) EmptyMoleculeException(ambit2.base.exceptions.EmptyMoleculeException)

Example 2 with IDescriptorValueCondition

use of ambit2.rules.conditions.IDescriptorValueCondition in project ambit-mirror by ideaconsult.

the class ConditionJsonParser method getDescriptorValueCondition.

public static IDescriptorValueCondition getDescriptorValueCondition(JsonNode node) throws Exception {
    StringBuffer errors = new StringBuffer();
    String descrName = JSONParsingUtils.extractStringKeyword(node, "DESCRIPTOR", true);
    String descrRelationStr = JSONParsingUtils.extractStringKeyword(node, "RELATION", true);
    IValue.Relation relation = Relation.getRelationFromString(descrRelationStr);
    Double descrValue = JSONParsingUtils.extractDoubleKeyword(node, "VALUE", true);
    if (errors.toString().isEmpty()) {
        IValue val = new Value();
        val.setValue(descrValue);
        val.setRelation(relation);
        IDescriptorValueCondition dvc = new DescriptorValueCondition(val, null, descrName);
        return dvc;
    } else
        throw new Exception("Errors in DescriptorValueCondition: " + errors.toString());
}
Also used : IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition) Relation(ambit2.rules.conditions.value.IValue.Relation) IValue(ambit2.rules.conditions.value.IValue) DescriptorValueCondition(ambit2.rules.conditions.DescriptorValueCondition) IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition) Value(ambit2.rules.conditions.value.Value) IValue(ambit2.rules.conditions.value.IValue)

Example 3 with IDescriptorValueCondition

use of ambit2.rules.conditions.IDescriptorValueCondition in project ambit-mirror by ideaconsult.

the class ConditionParsingUtils method getDescriptorValueConditionFromToken.

/**
 * Parses token in the format: <NEGATION> <DESCR_NAME> <RELATION> <VALUE>
 *
 * @param token
 * @return
 * @throws Exception
 */
public static IDescriptorValueCondition getDescriptorValueConditionFromToken(String token) throws Exception {
    if (token == null)
        throw new Exception("null token");
    String s = token.trim();
    if (s.isEmpty())
        throw new Exception("Empty token");
    DescriptorValueCondition dvc = new DescriptorValueCondition(null, null, null);
    int pos = 0;
    int n = s.length();
    // Handle negation in the beginning of the token
    boolean FlagNegation = false;
    while (pos < n) {
        if (s.charAt(pos) == '!') {
            pos++;
            FlagNegation = !FlagNegation;
        } else {
            break;
        }
    }
    dvc.setIsNegated(FlagNegation);
    // Omit empty spaces
    while (pos < n) {
        if (s.charAt(pos) == ' ')
            pos++;
        else
            break;
    }
    if (pos >= n)
        throw new Exception("Incorrect token " + token);
    // Handle descriptor name
    int pos0 = pos;
    if (Character.isLetter(s.charAt(pos)) || s.charAt(pos) == '_')
        pos++;
    while (pos < n) {
        if (Character.isLetterOrDigit(s.charAt(pos)) || s.charAt(pos) == '_')
            pos++;
        else
            break;
    }
    String descrName = s.substring(pos0, pos);
    dvc.setDescriptorName(descrName);
    // Omit empty spaces
    while (pos < n) {
        if (s.charAt(pos) == ' ')
            pos++;
        else
            break;
    }
    if (pos >= n)
        throw new Exception("Incorrect token " + token);
    // Handling relation
    Relation relation = null;
    char c = s.charAt(pos);
    switch(c) {
        case '=':
            pos++;
            if (pos < n) {
                if (s.charAt(pos) == '=') {
                    relation = Relation.EQUALS;
                    pos++;
                } else
                    relation = Relation.EQUALS;
            }
            break;
        case '!':
            pos++;
            if (pos < n)
                if (s.charAt(pos) == '=') {
                    relation = Relation.DIFFERENT;
                    pos++;
                }
            // This is an incorrect relation;
            break;
        case '>':
            pos++;
            if (pos < n) {
                if (s.charAt(pos) == '=') {
                    relation = Relation.GREATER_THAN_OR_EQUALS;
                    pos++;
                } else
                    relation = Relation.GREATER_THAN;
            } else
                relation = Relation.GREATER_THAN;
            break;
        case '<':
            pos++;
            if (pos < n) {
                if (s.charAt(pos) == '=') {
                    relation = Relation.LESS_THAN_OR_EQUALS;
                    pos++;
                } else if (s.charAt(pos) == '>') {
                    relation = Relation.DIFFERENT;
                    pos++;
                } else
                    relation = Relation.LESS_THAN;
            } else
                relation = Relation.LESS_THAN;
            break;
    }
    // Omit empty spaces
    while (pos < n) {
        if (s.charAt(pos) == ' ')
            pos++;
        else
            break;
    }
    if (relation == null)
        throw new Exception("Incorrect token " + token);
    if (pos >= n)
        throw new Exception("Incorrect token " + token);
    // Handle value
    Double d = null;
    String vStr = s.substring(pos);
    try {
        d = Double.parseDouble(vStr);
    } catch (Exception x) {
        throw new Exception("Incorrect token " + token);
    }
    Value value = new Value(d, relation);
    dvc.setValue(value);
    return dvc;
}
Also used : Relation(ambit2.rules.conditions.value.IValue.Relation) DescriptorValueCondition(ambit2.rules.conditions.DescriptorValueCondition) IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition) Value(ambit2.rules.conditions.value.Value)

Example 4 with IDescriptorValueCondition

use of ambit2.rules.conditions.IDescriptorValueCondition in project ambit-mirror by ideaconsult.

the class ConditionsTests method checkDescriptorValueCondition.

void checkDescriptorValueCondition(String descrCondString, DescriptorSolver0 solver0, boolean expectedResult) throws Exception {
    IDescriptorValueCondition dvc = ConditionParsingUtils.getDescriptorValueConditionFromToken(descrCondString);
    dvc.setDescriptorSolver(solver0);
    System.out.println(dvc.toString());
    // The target object is
    boolean res = dvc.isTrue(new String("S000"));
    // not taken into
    // account by solver0
    Assert.assertEquals("condition: " + descrCondString, expectedResult, res);
}
Also used : IDescriptorValueCondition(ambit2.rules.conditions.IDescriptorValueCondition)

Aggregations

IDescriptorValueCondition (ambit2.rules.conditions.IDescriptorValueCondition)4 DescriptorValueCondition (ambit2.rules.conditions.DescriptorValueCondition)2 Relation (ambit2.rules.conditions.value.IValue.Relation)2 Value (ambit2.rules.conditions.value.Value)2 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)1 ICondition (ambit2.rules.conditions.ICondition)1 IValue (ambit2.rules.conditions.value.IValue)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1