Search in sources :

Example 1 with ISLNDictionaryObject

use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.

the class SLNParser method parseMacroAtomDictionaryObject.

public ISLNDictionaryObject parseMacroAtomDictionaryObject(String dictObjectString, boolean parseName) {
    if (dictObjectString.isEmpty())
        return null;
    int pos = 0;
    int n = dictObjectString.length();
    String dictObjName = null;
    if (parseName) {
        if (Character.isUpperCase(dictObjectString.charAt(0)))
            pos++;
        else {
            newError("Incorrect dictionary object: " + dictObjectString, pos, "");
            return null;
        }
        while ((pos < n) && (Character.isLowerCase(dictObjectString.charAt(pos)) || Character.isDigit(dictObjectString.charAt(pos)) || dictObjectString.charAt(pos) == '_')) {
            pos++;
        }
        if (pos == n) {
            newError("Incorrecr dictionary object: " + dictObjectString, -1, "");
            return null;
        }
        dictObjName = dictObjectString.substring(0, pos);
        if (dictObjectString.charAt(pos) != ':') {
            newError("Incorrecr dictionary object: " + dictObjectString, -1, "");
            return null;
        } else
            pos++;
        if (pos == n) {
            newError("Incorrect dictionary object: " + dictObjectString, -1, "");
            return null;
        }
    }
    ParserState state = getState();
    errorContextPrefix = "Parsing macro/markush atom: " + dictObjectString + ": ";
    // Parsing a macro atom
    sln = dictObjectString.substring(pos);
    container = new SLNContainer(SilentChemObjectBuilder.getInstance());
    init();
    parse();
    ISLNDictionaryObject dictObj = null;
    if (errors.isEmpty()) {
        if (FlagUseSimpleMacroAtomsInDictionary && container.getAtomCount() == 1)
            dictObj = new AtomDictionaryObject(dictObjName, dictObjectString, container);
        else
            dictObj = new MacroAtomDictionaryObject(dictObjName, dictObjectString, container);
    }
    restoreState(state);
    errorContextPrefix = "";
    return dictObj;
}
Also used : MarkushAtomDictionaryObject(ambit2.sln.dictionary.MarkushAtomDictionaryObject) MacroAtomDictionaryObject(ambit2.sln.dictionary.MacroAtomDictionaryObject) AtomDictionaryObject(ambit2.sln.dictionary.AtomDictionaryObject) ISLNDictionaryObject(ambit2.sln.dictionary.ISLNDictionaryObject) MacroAtomDictionaryObject(ambit2.sln.dictionary.MacroAtomDictionaryObject)

Example 2 with ISLNDictionaryObject

use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.

the class SLNParser method parseLocalDictionaryObjects.

SLNDictionary parseLocalDictionaryObjects() {
    if (!errors.isEmpty())
        return null;
    if (localDictionaryObjectBeginPos.isEmpty())
        return null;
    SLNDictionary dict = new SLNDictionary();
    for (int i = 0; i < localDictionaryObjectBeginPos.size(); i++) {
        int beginPos = localDictionaryObjectBeginPos.get(i) + 1;
        int endPos = localDictionaryObjectEndPos.get(i);
        String locDictObjStr = sln.substring(beginPos, endPos);
        // System.out.println(locDictObjStr);
        ISLNDictionaryObject dictObj = parseDictionaryObject(locDictObjStr);
        if (dictObj != null)
            dict.addDictionaryObject(dictObj);
    }
    dict.checkDictionary();
    if (!dict.getCheckErrors().isEmpty()) {
        for (int i = 0; i < dict.getCheckErrors().size(); i++) newError(dict.getCheckErrors().get(i), 0, "");
    }
    return dict;
}
Also used : SLNDictionary(ambit2.sln.dictionary.SLNDictionary) PredefinedSLNDictionary(ambit2.sln.dictionary.PredefinedSLNDictionary) ISLNDictionaryObject(ambit2.sln.dictionary.ISLNDictionaryObject)

Example 3 with ISLNDictionaryObject

use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.

the class SLNDictionary method getDictionary.

public static SLNDictionary getDictionary(String[] dictionaryObjects, SLNParser parser) {
    if (dictionaryObjects == null)
        return null;
    if (parser == null)
        return null;
    parser.getErrors().clear();
    SLNDictionary dict = new SLNDictionary();
    for (int i = 0; i < dictionaryObjects.length; i++) {
        String s = dictionaryObjects[i];
        if (s.isEmpty() || !s.startsWith("{") || !s.endsWith("}")) {
            parser.getErrors().add(new SLNParserError(s, "Missing open or close brackets {}", 0, ""));
            continue;
        }
        String dictObjStr = s.substring(1, s.length() - 1);
        ISLNDictionaryObject dictObj = parser.parseDictionaryObject(dictObjStr);
        if (dictObj != null)
            dict.addDictionaryObject(dictObj);
    }
    if (!parser.getErrors().isEmpty()) {
        // Store errors inside the dictionary
        dict.parserErrors.addAll(parser.getErrors());
        parser.getErrors().clear();
    }
    return dict;
}
Also used : SLNParserError(ambit2.sln.SLNParserError)

Example 4 with ISLNDictionaryObject

use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.

the class SLNDictionary method getDictionary.

public static SLNDictionary getDictionary(String fileName, SLNParser parser) throws Exception {
    if (fileName == null)
        return null;
    if (parser == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(fileName));
    } catch (FileNotFoundException e) {
        throw e;
    }
    parser.getErrors().clear();
    SLNDictionary dict = new SLNDictionary();
    try {
        int nLine = 0;
        String line;
        while ((line = br.readLine()) != null) {
            nLine++;
            String s = line.trim();
            if (s.isEmpty()) {
                // System.out.println("Empty SLN string in line #" + nLine);
                continue;
            }
            if (s.isEmpty() || !s.startsWith("{") || !s.endsWith("}")) {
                parser.getErrors().add(new SLNParserError("Line #" + nLine + "  " + s, "Missing open or close brackets {}", 0, ""));
                continue;
            }
            String dictObjStr = s.substring(1, s.length() - 1);
            ISLNDictionaryObject dictObj = parser.parseDictionaryObject(dictObjStr);
            if (dictObj != null)
                dict.addDictionaryObject(dictObj);
        }
        br.close();
    } catch (IOException e) {
        throw e;
    }
    if (!parser.getErrors().isEmpty()) {
        // Store errors inside the dictionary
        dict.parserErrors.addAll(parser.getErrors());
        parser.getErrors().clear();
    }
    return dict;
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) SLNParserError(ambit2.sln.SLNParserError)

Example 5 with ISLNDictionaryObject

use of ambit2.sln.dictionary.ISLNDictionaryObject in project ambit-mirror by ideaconsult.

the class Expander method expandDictionaryAtom.

public List<IAtom> expandDictionaryAtom(SLNAtom at) {
    // Handle a dictionary object
    if (at.dictObj instanceof AtomDictionaryObject) {
        AtomDictionaryObject aDO = (AtomDictionaryObject) at.dictObj;
        SLNAtom newAt = aDO.atom.clone();
        expContainer.addAtom(newAt);
        List<IAtom> list = new ArrayList<IAtom>();
        list.add(newAt);
        return list;
    } else if (at.dictObj instanceof MacroAtomDictionaryObject) {
        return expandMacroAtomDictionaryObject((MacroAtomDictionaryObject) at.dictObj);
    } else if (at.dictObj instanceof MarkushAtomDictionaryObject) {
        // Get the proper macro/atom component from the Markush atom
        // according to the markushPos[...] value
        // maObj is either AtomDictionaryObject or MacroAtomDictionaryObject
        ISLNDictionaryObject maObj = getProperDictionaryObject(at);
        if (maObj instanceof AtomDictionaryObject) {
            AtomDictionaryObject aDO = (AtomDictionaryObject) maObj;
            SLNAtom newAt = aDO.atom.clone();
            expContainer.addAtom(newAt);
            List<IAtom> list = new ArrayList<IAtom>();
            list.add(newAt);
            return list;
        } else {
            return expandMacroAtomDictionaryObject((MacroAtomDictionaryObject) maObj);
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) SLNAtom(ambit2.sln.SLNAtom) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

ISLNDictionaryObject (ambit2.sln.dictionary.ISLNDictionaryObject)4 SLNParserError (ambit2.sln.SLNParserError)2 MarkushAtomDictionaryObject (ambit2.sln.dictionary.MarkushAtomDictionaryObject)2 ArrayList (java.util.ArrayList)2 SLNAtom (ambit2.sln.SLNAtom)1 AtomDictionaryObject (ambit2.sln.dictionary.AtomDictionaryObject)1 MacroAtomDictionaryObject (ambit2.sln.dictionary.MacroAtomDictionaryObject)1 PredefinedSLNDictionary (ambit2.sln.dictionary.PredefinedSLNDictionary)1 SLNDictionary (ambit2.sln.dictionary.SLNDictionary)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 IAtom (org.openscience.cdk.interfaces.IAtom)1