Search in sources :

Example 6 with KeyValueMap

use of android.filterfw.core.KeyValueMap in project platform_frameworks_base by android.

the class TextGraphReader method parseString.

private void parseString(String graphString) throws GraphIOException {
    final Pattern commandPattern = Pattern.compile("@[a-zA-Z]+");
    final Pattern curlyClosePattern = Pattern.compile("\\}");
    final Pattern curlyOpenPattern = Pattern.compile("\\{");
    final Pattern ignorePattern = Pattern.compile("(\\s+|//[^\\n]*\\n)+");
    final Pattern packageNamePattern = Pattern.compile("[a-zA-Z\\.]+");
    final Pattern libraryNamePattern = Pattern.compile("[a-zA-Z\\./:]+");
    final Pattern portPattern = Pattern.compile("\\[[a-zA-Z0-9\\-_]+\\]");
    final Pattern rightArrowPattern = Pattern.compile("=>");
    final Pattern semicolonPattern = Pattern.compile(";");
    final Pattern wordPattern = Pattern.compile("[a-zA-Z0-9\\-_]+");
    final int STATE_COMMAND = 0;
    final int STATE_IMPORT_PKG = 1;
    final int STATE_ADD_LIBRARY = 2;
    final int STATE_FILTER_CLASS = 3;
    final int STATE_FILTER_NAME = 4;
    final int STATE_CURLY_OPEN = 5;
    final int STATE_PARAMETERS = 6;
    final int STATE_CURLY_CLOSE = 7;
    final int STATE_SOURCE_FILTERNAME = 8;
    final int STATE_SOURCE_PORT = 9;
    final int STATE_RIGHT_ARROW = 10;
    final int STATE_TARGET_FILTERNAME = 11;
    final int STATE_TARGET_PORT = 12;
    final int STATE_ASSIGNMENT = 13;
    final int STATE_EXTERNAL = 14;
    final int STATE_SETTING = 15;
    final int STATE_SEMICOLON = 16;
    int state = STATE_COMMAND;
    PatternScanner scanner = new PatternScanner(graphString, ignorePattern);
    String curClassName = null;
    String curSourceFilterName = null;
    String curSourcePortName = null;
    String curTargetFilterName = null;
    String curTargetPortName = null;
    // State machine main loop
    while (!scanner.atEnd()) {
        switch(state) {
            case STATE_COMMAND:
                {
                    String curCommand = scanner.eat(commandPattern, "<command>");
                    if (curCommand.equals("@import")) {
                        state = STATE_IMPORT_PKG;
                    } else if (curCommand.equals("@library")) {
                        state = STATE_ADD_LIBRARY;
                    } else if (curCommand.equals("@filter")) {
                        state = STATE_FILTER_CLASS;
                    } else if (curCommand.equals("@connect")) {
                        state = STATE_SOURCE_FILTERNAME;
                    } else if (curCommand.equals("@set")) {
                        state = STATE_ASSIGNMENT;
                    } else if (curCommand.equals("@external")) {
                        state = STATE_EXTERNAL;
                    } else if (curCommand.equals("@setting")) {
                        state = STATE_SETTING;
                    } else {
                        throw new GraphIOException("Unknown command '" + curCommand + "'!");
                    }
                    break;
                }
            case STATE_IMPORT_PKG:
                {
                    String packageName = scanner.eat(packageNamePattern, "<package-name>");
                    mCommands.add(new ImportPackageCommand(packageName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_ADD_LIBRARY:
                {
                    String libraryName = scanner.eat(libraryNamePattern, "<library-name>");
                    mCommands.add(new AddLibraryCommand(libraryName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_FILTER_CLASS:
                curClassName = scanner.eat(wordPattern, "<class-name>");
                state = STATE_FILTER_NAME;
                break;
            case STATE_FILTER_NAME:
                {
                    String curFilterName = scanner.eat(wordPattern, "<filter-name>");
                    mCommands.add(new AllocateFilterCommand(curClassName, curFilterName));
                    state = STATE_CURLY_OPEN;
                    break;
                }
            case STATE_CURLY_OPEN:
                scanner.eat(curlyOpenPattern, "{");
                state = STATE_PARAMETERS;
                break;
            case STATE_PARAMETERS:
                {
                    KeyValueMap params = readKeyValueAssignments(scanner, curlyClosePattern);
                    mCommands.add(new InitFilterCommand(params));
                    state = STATE_CURLY_CLOSE;
                    break;
                }
            case STATE_CURLY_CLOSE:
                scanner.eat(curlyClosePattern, "}");
                state = STATE_COMMAND;
                break;
            case STATE_SOURCE_FILTERNAME:
                curSourceFilterName = scanner.eat(wordPattern, "<source-filter-name>");
                state = STATE_SOURCE_PORT;
                break;
            case STATE_SOURCE_PORT:
                {
                    String portString = scanner.eat(portPattern, "[<source-port-name>]");
                    curSourcePortName = portString.substring(1, portString.length() - 1);
                    state = STATE_RIGHT_ARROW;
                    break;
                }
            case STATE_RIGHT_ARROW:
                scanner.eat(rightArrowPattern, "=>");
                state = STATE_TARGET_FILTERNAME;
                break;
            case STATE_TARGET_FILTERNAME:
                curTargetFilterName = scanner.eat(wordPattern, "<target-filter-name>");
                state = STATE_TARGET_PORT;
                break;
            case STATE_TARGET_PORT:
                {
                    String portString = scanner.eat(portPattern, "[<target-port-name>]");
                    curTargetPortName = portString.substring(1, portString.length() - 1);
                    mCommands.add(new ConnectCommand(curSourceFilterName, curSourcePortName, curTargetFilterName, curTargetPortName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_ASSIGNMENT:
                {
                    KeyValueMap assignment = readKeyValueAssignments(scanner, semicolonPattern);
                    mBoundReferences.putAll(assignment);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_EXTERNAL:
                {
                    String externalName = scanner.eat(wordPattern, "<external-identifier>");
                    bindExternal(externalName);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_SETTING:
                {
                    KeyValueMap setting = readKeyValueAssignments(scanner, semicolonPattern);
                    mSettings.putAll(setting);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_SEMICOLON:
                scanner.eat(semicolonPattern, ";");
                state = STATE_COMMAND;
                break;
        }
    }
    // Make sure end of input was expected
    if (state != STATE_SEMICOLON && state != STATE_COMMAND) {
        throw new GraphIOException("Unexpected end of input!");
    }
}
Also used : Pattern(java.util.regex.Pattern) KeyValueMap(android.filterfw.core.KeyValueMap) PatternScanner(android.filterfw.io.PatternScanner) String(java.lang.String) GraphIOException(android.filterfw.io.GraphIOException)

Example 7 with KeyValueMap

use of android.filterfw.core.KeyValueMap in project platform_frameworks_base by android.

the class Filter method initWithAssignmentString.

public final void initWithAssignmentString(String assignments) {
    try {
        KeyValueMap valueMap = new TextGraphReader().readKeyValueAssignments(assignments);
        initWithValueMap(valueMap);
    } catch (GraphIOException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
}
Also used : KeyValueMap(android.filterfw.core.KeyValueMap) TextGraphReader(android.filterfw.io.TextGraphReader) GraphIOException(android.filterfw.io.GraphIOException)

Example 8 with KeyValueMap

use of android.filterfw.core.KeyValueMap in project platform_frameworks_base by android.

the class FilterGraph method connectPorts.

private void connectPorts() {
    int branchId = 1;
    for (Entry<OutputPort, LinkedList<InputPort>> connection : mPreconnections.entrySet()) {
        OutputPort outputPort = connection.getKey();
        LinkedList<InputPort> inputPorts = connection.getValue();
        if (inputPorts.size() == 1) {
            outputPort.connectTo(inputPorts.get(0));
        } else if (mAutoBranchMode == AUTOBRANCH_OFF) {
            throw new RuntimeException("Attempting to connect " + outputPort + " to multiple " + "filter ports! Enable auto-branching to allow this.");
        } else {
            if (mLogVerbose)
                Log.v(TAG, "Creating branch for " + outputPort + "!");
            FrameBranch branch = null;
            if (mAutoBranchMode == AUTOBRANCH_SYNCED) {
                branch = new FrameBranch("branch" + branchId++);
            } else {
                throw new RuntimeException("TODO: Unsynced branches not implemented yet!");
            }
            KeyValueMap branchParams = new KeyValueMap();
            branch.initWithAssignmentList("outputs", inputPorts.size());
            addFilter(branch);
            outputPort.connectTo(branch.getInputPort("in"));
            Iterator<InputPort> inputPortIter = inputPorts.iterator();
            for (OutputPort branchOutPort : ((Filter) branch).getOutputPorts()) {
                branchOutPort.connectTo(inputPortIter.next());
            }
        }
    }
    mPreconnections.clear();
}
Also used : KeyValueMap(android.filterfw.core.KeyValueMap) Iterator(java.util.Iterator) FrameBranch(android.filterpacks.base.FrameBranch) LinkedList(java.util.LinkedList)

Example 9 with KeyValueMap

use of android.filterfw.core.KeyValueMap in project android_frameworks_base by DirtyUnicorns.

the class TextGraphReader method reset.

private void reset() {
    mCurrentGraph = null;
    mCurrentFilter = null;
    mCommands.clear();
    mBoundReferences = new KeyValueMap();
    mSettings = new KeyValueMap();
    mFactory = new FilterFactory();
}
Also used : KeyValueMap(android.filterfw.core.KeyValueMap) FilterFactory(android.filterfw.core.FilterFactory)

Example 10 with KeyValueMap

use of android.filterfw.core.KeyValueMap in project android_frameworks_base by DirtyUnicorns.

the class TextGraphReader method parseString.

private void parseString(String graphString) throws GraphIOException {
    final Pattern commandPattern = Pattern.compile("@[a-zA-Z]+");
    final Pattern curlyClosePattern = Pattern.compile("\\}");
    final Pattern curlyOpenPattern = Pattern.compile("\\{");
    final Pattern ignorePattern = Pattern.compile("(\\s+|//[^\\n]*\\n)+");
    final Pattern packageNamePattern = Pattern.compile("[a-zA-Z\\.]+");
    final Pattern libraryNamePattern = Pattern.compile("[a-zA-Z\\./:]+");
    final Pattern portPattern = Pattern.compile("\\[[a-zA-Z0-9\\-_]+\\]");
    final Pattern rightArrowPattern = Pattern.compile("=>");
    final Pattern semicolonPattern = Pattern.compile(";");
    final Pattern wordPattern = Pattern.compile("[a-zA-Z0-9\\-_]+");
    final int STATE_COMMAND = 0;
    final int STATE_IMPORT_PKG = 1;
    final int STATE_ADD_LIBRARY = 2;
    final int STATE_FILTER_CLASS = 3;
    final int STATE_FILTER_NAME = 4;
    final int STATE_CURLY_OPEN = 5;
    final int STATE_PARAMETERS = 6;
    final int STATE_CURLY_CLOSE = 7;
    final int STATE_SOURCE_FILTERNAME = 8;
    final int STATE_SOURCE_PORT = 9;
    final int STATE_RIGHT_ARROW = 10;
    final int STATE_TARGET_FILTERNAME = 11;
    final int STATE_TARGET_PORT = 12;
    final int STATE_ASSIGNMENT = 13;
    final int STATE_EXTERNAL = 14;
    final int STATE_SETTING = 15;
    final int STATE_SEMICOLON = 16;
    int state = STATE_COMMAND;
    PatternScanner scanner = new PatternScanner(graphString, ignorePattern);
    String curClassName = null;
    String curSourceFilterName = null;
    String curSourcePortName = null;
    String curTargetFilterName = null;
    String curTargetPortName = null;
    // State machine main loop
    while (!scanner.atEnd()) {
        switch(state) {
            case STATE_COMMAND:
                {
                    String curCommand = scanner.eat(commandPattern, "<command>");
                    if (curCommand.equals("@import")) {
                        state = STATE_IMPORT_PKG;
                    } else if (curCommand.equals("@library")) {
                        state = STATE_ADD_LIBRARY;
                    } else if (curCommand.equals("@filter")) {
                        state = STATE_FILTER_CLASS;
                    } else if (curCommand.equals("@connect")) {
                        state = STATE_SOURCE_FILTERNAME;
                    } else if (curCommand.equals("@set")) {
                        state = STATE_ASSIGNMENT;
                    } else if (curCommand.equals("@external")) {
                        state = STATE_EXTERNAL;
                    } else if (curCommand.equals("@setting")) {
                        state = STATE_SETTING;
                    } else {
                        throw new GraphIOException("Unknown command '" + curCommand + "'!");
                    }
                    break;
                }
            case STATE_IMPORT_PKG:
                {
                    String packageName = scanner.eat(packageNamePattern, "<package-name>");
                    mCommands.add(new ImportPackageCommand(packageName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_ADD_LIBRARY:
                {
                    String libraryName = scanner.eat(libraryNamePattern, "<library-name>");
                    mCommands.add(new AddLibraryCommand(libraryName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_FILTER_CLASS:
                curClassName = scanner.eat(wordPattern, "<class-name>");
                state = STATE_FILTER_NAME;
                break;
            case STATE_FILTER_NAME:
                {
                    String curFilterName = scanner.eat(wordPattern, "<filter-name>");
                    mCommands.add(new AllocateFilterCommand(curClassName, curFilterName));
                    state = STATE_CURLY_OPEN;
                    break;
                }
            case STATE_CURLY_OPEN:
                scanner.eat(curlyOpenPattern, "{");
                state = STATE_PARAMETERS;
                break;
            case STATE_PARAMETERS:
                {
                    KeyValueMap params = readKeyValueAssignments(scanner, curlyClosePattern);
                    mCommands.add(new InitFilterCommand(params));
                    state = STATE_CURLY_CLOSE;
                    break;
                }
            case STATE_CURLY_CLOSE:
                scanner.eat(curlyClosePattern, "}");
                state = STATE_COMMAND;
                break;
            case STATE_SOURCE_FILTERNAME:
                curSourceFilterName = scanner.eat(wordPattern, "<source-filter-name>");
                state = STATE_SOURCE_PORT;
                break;
            case STATE_SOURCE_PORT:
                {
                    String portString = scanner.eat(portPattern, "[<source-port-name>]");
                    curSourcePortName = portString.substring(1, portString.length() - 1);
                    state = STATE_RIGHT_ARROW;
                    break;
                }
            case STATE_RIGHT_ARROW:
                scanner.eat(rightArrowPattern, "=>");
                state = STATE_TARGET_FILTERNAME;
                break;
            case STATE_TARGET_FILTERNAME:
                curTargetFilterName = scanner.eat(wordPattern, "<target-filter-name>");
                state = STATE_TARGET_PORT;
                break;
            case STATE_TARGET_PORT:
                {
                    String portString = scanner.eat(portPattern, "[<target-port-name>]");
                    curTargetPortName = portString.substring(1, portString.length() - 1);
                    mCommands.add(new ConnectCommand(curSourceFilterName, curSourcePortName, curTargetFilterName, curTargetPortName));
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_ASSIGNMENT:
                {
                    KeyValueMap assignment = readKeyValueAssignments(scanner, semicolonPattern);
                    mBoundReferences.putAll(assignment);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_EXTERNAL:
                {
                    String externalName = scanner.eat(wordPattern, "<external-identifier>");
                    bindExternal(externalName);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_SETTING:
                {
                    KeyValueMap setting = readKeyValueAssignments(scanner, semicolonPattern);
                    mSettings.putAll(setting);
                    state = STATE_SEMICOLON;
                    break;
                }
            case STATE_SEMICOLON:
                scanner.eat(semicolonPattern, ";");
                state = STATE_COMMAND;
                break;
        }
    }
    // Make sure end of input was expected
    if (state != STATE_SEMICOLON && state != STATE_COMMAND) {
        throw new GraphIOException("Unexpected end of input!");
    }
}
Also used : Pattern(java.util.regex.Pattern) KeyValueMap(android.filterfw.core.KeyValueMap) PatternScanner(android.filterfw.io.PatternScanner) String(java.lang.String) GraphIOException(android.filterfw.io.GraphIOException)

Aggregations

KeyValueMap (android.filterfw.core.KeyValueMap)48 GraphIOException (android.filterfw.io.GraphIOException)18 String (java.lang.String)12 Pattern (java.util.regex.Pattern)12 FilterFactory (android.filterfw.core.FilterFactory)6 MutableFrameFormat (android.filterfw.core.MutableFrameFormat)6 PatternScanner (android.filterfw.io.PatternScanner)6 TextGraphReader (android.filterfw.io.TextGraphReader)6 FrameBranch (android.filterpacks.base.FrameBranch)6 Iterator (java.util.Iterator)6 LinkedList (java.util.LinkedList)6