streamlet-examples/Streamlet.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 75 ecbf6504915c
permissions -rw-r--r--
portable order of (i++) parameters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
     1
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
/**
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Relational pipes
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * This program is free software: you can redistribute it and/or modify
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * the Free Software Foundation, version 3 of the License.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    18
import java.io.ByteArrayOutputStream;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    19
import java.io.IOException;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    20
import java.io.PrintWriter;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    21
import java.io.StringWriter;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    22
import java.util.ArrayList;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    23
import java.util.Arrays;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    24
import java.util.LinkedList;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    25
import java.util.List;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    26
import java.util.Objects;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    27
import java.util.regex.Matcher;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    28
import java.util.regex.Pattern;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
/**
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    31
 * Unlike the protocol and the message format, these helper classes are not part of the public API. Thus when writing
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    32
 * custom streamlets, it is better to copy this file and review its changes while upgrading to new upstream version.
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 */
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    34
public abstract class Streamlet {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    36
	// TODO: use generated constants
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    37
	private static final int VERSION_SUPPORTED = 100;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    38
	private static final int WAITING_FOR_VERSION = 101;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    39
	private static final int VERSION_ACCEPTED = 102;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    40
	private static final int RELATION_START = 103;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    41
	private static final int INPUT_ATTRIBUTE_METADATA = 104;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    42
	private static final int OUTPUT_ATTRIBUTE_ALIAS = 105;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    43
	private static final int OPTION = 106;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    44
	private static final int COMPLETION_REQUEST = 107;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    45
	private static final int COMPLETION = 108;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    46
	private static final int COMPLETION_END = 109;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    47
	private static final int WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA = 110;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    48
	private static final int OUTPUT_ATTRIBUTE_METADATA = 111;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    49
	private static final int WAITING_FOR_INPUT_ATTRIBUTES = 112;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    50
	private static final int INPUT_ATTRIBUTE = 113;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    51
	private static final int WAITING_FOR_OUTPUT_ATTRIBUTES = 114;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    52
	private static final int OUTPUT_ATTRIBUTE = 115;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    53
	private static final int EXECUTOR_ERROR = 116;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    54
	private static final int STREAMLET_ERROR = 117;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    55
	private static final int STREAMLET_WARNING = 118;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    56
	private static final int RELATION_END = 120;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    58
	protected static class Message {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    60
		public int code;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    61
		public List<String> parameters;
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    63
		public Message() {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    66
		public Message(int code, String... parameters) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    67
			this.code = code;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    68
			this.parameters = Arrays.asList(parameters);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    71
	}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    73
	private static final char SEPARATOR = '\0';
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    75
	private int readInt() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    76
		return Integer.valueOf(readString());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    77
	}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
    78
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    79
	private String readString() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    80
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    81
		for (int b; (b = System.in.read()) > 0;) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    82
			buffer.write(b);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    83
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    84
		return new String(buffer.toByteArray());
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    87
	private void writeString(String s) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    88
		System.out.print(s);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    89
		System.out.print(SEPARATOR);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    92
	private void writeInt(int i) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    93
		writeString(String.valueOf(i));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    96
	private void flush() {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
    97
		System.out.flush();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   100
	private Message read() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   101
		Message m = new Message();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
		m.code = readInt();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		int count = readInt();
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   104
		m.parameters = new ArrayList<>(count);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   105
		for (int i = 0; i < count; i++) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   106
			m.parameters.add(readString());
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   107
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		return m;
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   111
	private void copyMatches(Matcher source, List<String> destination) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   112
		for (int i = 1; i < source.groupCount(); i++) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   113
			destination.add(source.group(i));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   117
	private void processMessages() throws IOException {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   118
		OUTER:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   119
		while (true) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   120
			Message m = read();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   121
			switch (m.code) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   122
				case VERSION_SUPPORTED:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   123
					processVersionSupported(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   124
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   125
				case WAITING_FOR_VERSION:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   126
					processWaitingForVersion(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   127
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   128
				case RELATION_START:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   129
					processRelationStart(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   130
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   131
				case INPUT_ATTRIBUTE_METADATA:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   132
					processInputAttributeMetadata(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   133
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   134
				case OUTPUT_ATTRIBUTE_ALIAS:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   135
					processOutputAttributeAlias(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   136
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   137
				case OPTION:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   138
					processOption(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   139
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   140
				case INPUT_ATTRIBUTE:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   141
					processInputAttribute(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   142
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   143
				case WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   144
					processWaitingForOutputAttributesMetadata(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   145
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   146
				case WAITING_FOR_OUTPUT_ATTRIBUTES:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   147
					processWaitingForOutputAttributes(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   148
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   149
				case RELATION_END:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   150
					break OUTER;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   151
				default:
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   152
					processUnsupportedMessage(m);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   153
					break;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   154
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   155
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   156
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   158
	protected static class AttributeMetadata {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   160
		public String name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   161
		public Type type;
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   162
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   163
		public AttributeMetadata(String name, Type type) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   164
			this.name = name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   165
			this.type = type;
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   166
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	};
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   169
	protected static class Option {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   170
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   171
		public String name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   172
		public String value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   173
		public List<String> nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   174
		public List<String> valueMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   175
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   176
		Option(String name, String value) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   177
			this.name = name;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   178
			this.value = value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   179
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   180
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
75
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   182
	private List<String> versionsSupported = new LinkedList<>();
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   183
	private List<AttributeMetadata> inputAttributes = new ArrayList<>();
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   184
	private List<String> outputAttributeAliases = new ArrayList<>();
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   185
	private List<Option> options = new LinkedList<>();
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   186
	private String currentRelation;
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   187
	private String currentFile;
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   188
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   189
	/**
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   190
	 * @return n.b. generic streamlet (later in relpipe-tr-streamler) will not have currentFile
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   191
	 */
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   192
	public String getCurrentFile() {
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   193
		return currentFile;
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   194
	}
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   195
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   196
	public String getCurrentRelation() {
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   197
		return currentRelation;
ecbf6504915c streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents: 72
diff changeset
   198
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   200
	protected static enum Type {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   201
		BOOLEAN,
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   202
		INTEGER,
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   203
		STRING
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   204
	}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   206
	protected void write(Message m) {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
		writeInt(m.code);
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
		writeInt(m.parameters.size());
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   209
		for (String p : m.parameters) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   210
			writeString(p);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   211
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
		flush();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   215
	protected void processVersionSupported(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   216
		versionsSupported.add(m.parameters.get(0));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   219
	protected void processWaitingForVersion(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   220
		for (String v : versionsSupported) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   221
			if ("1".equals(v)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   222
				write(new Message(VERSION_ACCEPTED, "1"));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   223
				return;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   224
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   225
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   226
		write(new Message(STREAMLET_ERROR, "INCOMPATIBLE_VERSION", "Only version 1 is supported by this streamlet."));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   229
	protected void processRelationStart(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   230
		currentRelation = m.parameters.get(0);
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   233
	protected void processInputAttributeMetadata(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   234
		inputAttributes.add(new AttributeMetadata(m.parameters.get(0), Type.valueOf(m.parameters.get(1).toUpperCase())));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   235
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   236
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   237
	protected void processOutputAttributeAlias(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   238
		outputAttributeAliases.add(m.parameters.get(0));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   241
	protected void processOption(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   242
		options.add(new Option(m.parameters.get(0), m.parameters.get(1)));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   243
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   245
	protected void processInputAttribute(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   246
		int index = Integer.valueOf(m.parameters.get(0));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   247
		String value = m.parameters.get(1);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   248
		boolean isNull = "true".equals(m.parameters.get(2));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   249
		if ("path".equals(inputAttributes.get(index).name)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   250
			currentFile = value;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   251
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   252
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   253
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   254
	protected void processWaitingForOutputAttributesMetadata(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   255
		for (AttributeMetadata am : getOutputAttributesMetadata()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   256
			write(new Message(OUTPUT_ATTRIBUTE_METADATA, am.name, am.type.name().toLowerCase()));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   257
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   258
		write(new Message(WAITING_FOR_INPUT_ATTRIBUTES));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   259
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   260
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   261
	protected void processWaitingForOutputAttributes(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   262
		for (Object oa : getOutputAttributes()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   263
			write(new Message(OUTPUT_ATTRIBUTE, String.valueOf(oa), oa == null ? "true" : "false"));
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   264
		}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   265
		write(new Message(WAITING_FOR_INPUT_ATTRIBUTES));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   266
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   267
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   268
	protected void processUnsupportedMessage(Message m) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   269
		write(new Message(STREAMLET_ERROR, "UNSUPPORTED_MESSAGE"));
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   270
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   271
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   272
	protected String getAlias(int index, String defaultValue) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   273
		if (outputAttributeAliases.size() > index) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   274
			return outputAttributeAliases.get(index);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   275
		} else {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   276
			return defaultValue;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   277
		}
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   278
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   279
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   280
	protected List<Option> getOptions(String name) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   281
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   282
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   283
			if (Objects.equals(o.name, name)) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   284
				result.add(o);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   285
			}
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   286
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   287
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   288
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   289
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   290
	protected List<Option> getOptions(Pattern namePattern) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   291
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   292
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   293
			Matcher nameMatcher = namePattern.matcher(o.name);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   294
			if (nameMatcher.matches()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   295
				o.nameMatch = o.nameMatch == null ? new ArrayList<>() : o.nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   296
				copyMatches(nameMatcher, o.nameMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   297
				result.add(o);
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   298
			}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   299
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   300
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   301
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   302
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   303
	protected List<Option> getOptions(Pattern namePattern, Pattern valuePattern) {
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   304
		// TODO: support multiple modes: 
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   305
		//   a) throw an exception if valuePattern does not match
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   306
		//   b) return option even if valuePattern does not match (valueMatch will be empty)
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   307
		//   c) skip options with value not matching (current behavior)
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   308
		List<Option> result = new ArrayList<>();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   309
		for (Option o : options) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   310
			Matcher nameMatcher = namePattern.matcher(o.name);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   311
			Matcher valueMatcher = valuePattern.matcher(o.value);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   312
			if (nameMatcher.matches() && valueMatcher.matches()) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   313
				o.nameMatch = o.nameMatch == null ? new ArrayList<>() : o.nameMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   314
				o.valueMatch = o.valueMatch == null ? new ArrayList<>() : o.valueMatch;
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   315
				copyMatches(nameMatcher, o.nameMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   316
				copyMatches(valueMatcher, o.valueMatch);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   317
				result.add(o);
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   318
			}
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   319
		}
67
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   320
		return result;
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   321
	}
0766d298eb1c streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents: 66
diff changeset
   322
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   323
	protected abstract List<AttributeMetadata> getOutputAttributesMetadata();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   324
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   325
	protected abstract List<Object> getOutputAttributes();
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   327
	public int run() {
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
		try {
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
			processMessages();
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
			return 0;
72
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   331
		} catch (Exception e) {
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   332
			StringWriter stackTraceStringWriter = new StringWriter();
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   333
			PrintWriter stackTracePrintWriter = new PrintWriter(stackTraceStringWriter);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   334
			e.printStackTrace(stackTracePrintWriter);
f7b9db6fc32b streamlet examples: dirty implementation of Java helper classes + demo code
František Kučera <franta-hg@frantovo.cz>
parents: 69
diff changeset
   335
			write(new Message(STREAMLET_ERROR, "xxxx", "Exception in streamlet: " + e.getClass().getName() + ": " + e.getLocalizedMessage() + "\n" + stackTraceStringWriter.toString())); // FIXME: correct error codes
63
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   336
			return 1;
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   337
		}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   338
	}
8c6885543e2c streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   339
}