1605797820
In this video, we are going to explain what is gradient boosting.
We will discuss the following in this video:
ð (0:00:06) Introduction
ð (0:01:02) Boosting
ð (0:03:40) Gradient Descent
ð (0:07:57) Gradient Boosting
ð (0:17:49) Implementation
#data-science #artificial-intelligence #developer
1653475560
msgpack.php
A pure PHP implementation of the MessagePack serialization format.
The recommended way to install the library is through Composer:
composer require rybakit/msgpack
To pack values you can either use an instance of a Packer
:
$packer = new Packer();
$packed = $packer->pack($value);
or call a static method on the MessagePack
class:
$packed = MessagePack::pack($value);
In the examples above, the method pack
automatically packs a value depending on its type. However, not all PHP types can be uniquely translated to MessagePack types. For example, the MessagePack format defines map
and array
types, which are represented by a single array
type in PHP. By default, the packer will pack a PHP array as a MessagePack array if it has sequential numeric keys, starting from 0
and as a MessagePack map otherwise:
$mpArr1 = $packer->pack([1, 2]); // MP array [1, 2]
$mpArr2 = $packer->pack([0 => 1, 1 => 2]); // MP array [1, 2]
$mpMap1 = $packer->pack([0 => 1, 2 => 3]); // MP map {0: 1, 2: 3}
$mpMap2 = $packer->pack([1 => 2, 2 => 3]); // MP map {1: 2, 2: 3}
$mpMap3 = $packer->pack(['a' => 1, 'b' => 2]); // MP map {a: 1, b: 2}
However, sometimes you need to pack a sequential array as a MessagePack map. To do this, use the packMap
method:
$mpMap = $packer->packMap([1, 2]); // {0: 1, 1: 2}
Here is a list of type-specific packing methods:
$packer->packNil(); // MP nil
$packer->packBool(true); // MP bool
$packer->packInt(42); // MP int
$packer->packFloat(M_PI); // MP float (32 or 64)
$packer->packFloat32(M_PI); // MP float 32
$packer->packFloat64(M_PI); // MP float 64
$packer->packStr('foo'); // MP str
$packer->packBin("\x80"); // MP bin
$packer->packArray([1, 2]); // MP array
$packer->packMap(['a' => 1]); // MP map
$packer->packExt(1, "\xaa"); // MP ext
Check the "Custom types" section below on how to pack custom types.
The Packer
object supports a number of bitmask-based options for fine-tuning the packing process (defaults are in bold):
Name | Description |
---|---|
FORCE_STR | Forces PHP strings to be packed as MessagePack UTF-8 strings |
FORCE_BIN | Forces PHP strings to be packed as MessagePack binary data |
DETECT_STR_BIN | Detects MessagePack str/bin type automatically |
FORCE_ARR | Forces PHP arrays to be packed as MessagePack arrays |
FORCE_MAP | Forces PHP arrays to be packed as MessagePack maps |
DETECT_ARR_MAP | Detects MessagePack array/map type automatically |
FORCE_FLOAT32 | Forces PHP floats to be packed as 32-bits MessagePack floats |
FORCE_FLOAT64 | Forces PHP floats to be packed as 64-bits MessagePack floats |
The type detection mode (
DETECT_STR_BIN
/DETECT_ARR_MAP
) adds some overhead which can be noticed when you pack large (16- and 32-bit) arrays or strings. However, if you know the value type in advance (for example, you only work with UTF-8 strings or/and associative arrays), you can eliminate this overhead by forcing the packer to use the appropriate type, which will save it from running the auto-detection routine. Another option is to explicitly specify the value type. The library provides 2 auxiliary classes for this,Map
andBin
. Check the "Custom types" section below for details.
Examples:
// detect str/bin type and pack PHP 64-bit floats (doubles) to MP 32-bit floats
$packer = new Packer(PackOptions::DETECT_STR_BIN | PackOptions::FORCE_FLOAT32);
// these will throw MessagePack\Exception\InvalidOptionException
$packer = new Packer(PackOptions::FORCE_STR | PackOptions::FORCE_BIN);
$packer = new Packer(PackOptions::FORCE_FLOAT32 | PackOptions::FORCE_FLOAT64);
To unpack data you can either use an instance of a BufferUnpacker
:
$unpacker = new BufferUnpacker();
$unpacker->reset($packed);
$value = $unpacker->unpack();
or call a static method on the MessagePack
class:
$value = MessagePack::unpack($packed);
If the packed data is received in chunks (e.g. when reading from a stream), use the tryUnpack
method, which attempts to unpack data and returns an array of unpacked messages (if any) instead of throwing an InsufficientDataException
:
while ($chunk = ...) {
$unpacker->append($chunk);
if ($messages = $unpacker->tryUnpack()) {
return $messages;
}
}
If you want to unpack from a specific position in a buffer, use seek
:
$unpacker->seek(42); // set position equal to 42 bytes
$unpacker->seek(-8); // set position to 8 bytes before the end of the buffer
To skip bytes from the current position, use skip
:
$unpacker->skip(10); // set position to 10 bytes ahead of the current position
To get the number of remaining (unread) bytes in the buffer:
$unreadBytesCount = $unpacker->getRemainingCount();
To check whether the buffer has unread data:
$hasUnreadBytes = $unpacker->hasRemaining();
If needed, you can remove already read data from the buffer by calling:
$releasedBytesCount = $unpacker->release();
With the read
method you can read raw (packed) data:
$packedData = $unpacker->read(2); // read 2 bytes
Besides the above methods BufferUnpacker
provides type-specific unpacking methods, namely:
$unpacker->unpackNil(); // PHP null
$unpacker->unpackBool(); // PHP bool
$unpacker->unpackInt(); // PHP int
$unpacker->unpackFloat(); // PHP float
$unpacker->unpackStr(); // PHP UTF-8 string
$unpacker->unpackBin(); // PHP binary string
$unpacker->unpackArray(); // PHP sequential array
$unpacker->unpackMap(); // PHP associative array
$unpacker->unpackExt(); // PHP MessagePack\Type\Ext object
The BufferUnpacker
object supports a number of bitmask-based options for fine-tuning the unpacking process (defaults are in bold):
Name | Description |
---|---|
BIGINT_AS_STR | Converts overflowed integers to strings [1] |
BIGINT_AS_GMP | Converts overflowed integers to GMP objects [2] |
BIGINT_AS_DEC | Converts overflowed integers to Decimal\Decimal objects [3] |
1. The binary MessagePack format has unsigned 64-bit as its largest integer data type, but PHP does not support such integers, which means that an overflow can occur during unpacking.
2. Make sure the GMP extension is enabled.
3. Make sure the Decimal extension is enabled.
Examples:
$packedUint64 = "\xcf"."\xff\xff\xff\xff"."\xff\xff\xff\xff";
$unpacker = new BufferUnpacker($packedUint64);
var_dump($unpacker->unpack()); // string(20) "18446744073709551615"
$unpacker = new BufferUnpacker($packedUint64, UnpackOptions::BIGINT_AS_GMP);
var_dump($unpacker->unpack()); // object(GMP) {...}
$unpacker = new BufferUnpacker($packedUint64, UnpackOptions::BIGINT_AS_DEC);
var_dump($unpacker->unpack()); // object(Decimal\Decimal) {...}
In addition to the basic types, the library provides functionality to serialize and deserialize arbitrary types. This can be done in several ways, depending on your use case. Let's take a look at them.
If you need to serialize an instance of one of your classes into one of the basic MessagePack types, the best way to do this is to implement the CanBePacked interface in the class. A good example of such a class is the Map
type class that comes with the library. This type is useful when you want to explicitly specify that a given PHP array should be packed as a MessagePack map without triggering an automatic type detection routine:
$packer = new Packer();
$packedMap = $packer->pack(new Map([1, 2, 3]));
$packedArray = $packer->pack([1, 2, 3]);
More type examples can be found in the src/Type directory.
As with type objects, type transformers are only responsible for serializing values. They should be used when you need to serialize a value that does not implement the CanBePacked interface. Examples of such values could be instances of built-in or third-party classes that you don't own, or non-objects such as resources.
A transformer class must implement the CanPack interface. To use a transformer, it must first be registered in the packer. Here is an example of how to serialize PHP streams into the MessagePack bin
format type using one of the supplied transformers, StreamTransformer
:
$packer = new Packer(null, [new StreamTransformer()]);
$packedBin = $packer->pack(fopen('/path/to/file', 'r+'));
More type transformer examples can be found in the src/TypeTransformer directory.
In contrast to the cases described above, extensions are intended to handle extension types and are responsible for both serialization and deserialization of values (types).
An extension class must implement the Extension interface. To use an extension, it must first be registered in the packer and the unpacker.
The MessagePack specification divides extension types into two groups: predefined and application-specific. Currently, there is only one predefined type in the specification, Timestamp.
Timestamp
The Timestamp extension type is a predefined type. Support for this type in the library is done through the TimestampExtension
class. This class is responsible for handling Timestamp
objects, which represent the number of seconds and optional adjustment in nanoseconds:
$timestampExtension = new TimestampExtension();
$packer = new Packer();
$packer = $packer->extendWith($timestampExtension);
$unpacker = new BufferUnpacker();
$unpacker = $unpacker->extendWith($timestampExtension);
$packedTimestamp = $packer->pack(Timestamp::now());
$timestamp = $unpacker->reset($packedTimestamp)->unpack();
$seconds = $timestamp->getSeconds();
$nanoseconds = $timestamp->getNanoseconds();
When using the MessagePack
class, the Timestamp extension is already registered:
$packedTimestamp = MessagePack::pack(Timestamp::now());
$timestamp = MessagePack::unpack($packedTimestamp);
Application-specific extensions
In addition, the format can be extended with your own types. For example, to make the built-in PHP DateTime
objects first-class citizens in your code, you can create a corresponding extension, as shown in the example. Please note, that custom extensions have to be registered with a unique extension ID (an integer from 0
to 127
).
More extension examples can be found in the examples/MessagePack directory.
To learn more about how extension types can be useful, check out this article.
If an error occurs during packing/unpacking, a PackingFailedException
or an UnpackingFailedException
will be thrown, respectively. In addition, an InsufficientDataException
can be thrown during unpacking.
An InvalidOptionException
will be thrown in case an invalid option (or a combination of mutually exclusive options) is used.
Run tests as follows:
vendor/bin/phpunit
Also, if you already have Docker installed, you can run the tests in a docker container. First, create a container:
./dockerfile.sh | docker build -t msgpack -
The command above will create a container named msgpack
with PHP 8.1 runtime. You may change the default runtime by defining the PHP_IMAGE
environment variable:
PHP_IMAGE='php:8.0-cli' ./dockerfile.sh | docker build -t msgpack -
See a list of various images here.
Then run the unit tests:
docker run --rm -v $PWD:/msgpack -w /msgpack msgpack
To ensure that the unpacking works correctly with malformed/semi-malformed data, you can use a testing technique called Fuzzing. The library ships with a help file (target) for PHP-Fuzzer and can be used as follows:
php-fuzzer fuzz tests/fuzz_buffer_unpacker.php
To check performance, run:
php -n -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
=============================================
Test/Target Packer BufferUnpacker
---------------------------------------------
nil .................. 0.0030 ........ 0.0139
false ................ 0.0037 ........ 0.0144
true ................. 0.0040 ........ 0.0137
7-bit uint #1 ........ 0.0052 ........ 0.0120
7-bit uint #2 ........ 0.0059 ........ 0.0114
7-bit uint #3 ........ 0.0061 ........ 0.0119
5-bit sint #1 ........ 0.0067 ........ 0.0126
5-bit sint #2 ........ 0.0064 ........ 0.0132
5-bit sint #3 ........ 0.0066 ........ 0.0135
8-bit uint #1 ........ 0.0078 ........ 0.0200
8-bit uint #2 ........ 0.0077 ........ 0.0212
8-bit uint #3 ........ 0.0086 ........ 0.0203
16-bit uint #1 ....... 0.0111 ........ 0.0271
16-bit uint #2 ....... 0.0115 ........ 0.0260
16-bit uint #3 ....... 0.0103 ........ 0.0273
32-bit uint #1 ....... 0.0116 ........ 0.0326
32-bit uint #2 ....... 0.0118 ........ 0.0332
32-bit uint #3 ....... 0.0127 ........ 0.0325
64-bit uint #1 ....... 0.0140 ........ 0.0277
64-bit uint #2 ....... 0.0134 ........ 0.0294
64-bit uint #3 ....... 0.0134 ........ 0.0281
8-bit int #1 ......... 0.0086 ........ 0.0241
8-bit int #2 ......... 0.0089 ........ 0.0225
8-bit int #3 ......... 0.0085 ........ 0.0229
16-bit int #1 ........ 0.0118 ........ 0.0280
16-bit int #2 ........ 0.0121 ........ 0.0270
16-bit int #3 ........ 0.0109 ........ 0.0274
32-bit int #1 ........ 0.0128 ........ 0.0346
32-bit int #2 ........ 0.0118 ........ 0.0339
32-bit int #3 ........ 0.0135 ........ 0.0368
64-bit int #1 ........ 0.0138 ........ 0.0276
64-bit int #2 ........ 0.0132 ........ 0.0286
64-bit int #3 ........ 0.0137 ........ 0.0274
64-bit int #4 ........ 0.0180 ........ 0.0285
64-bit float #1 ...... 0.0134 ........ 0.0284
64-bit float #2 ...... 0.0125 ........ 0.0275
64-bit float #3 ...... 0.0126 ........ 0.0283
fix string #1 ........ 0.0035 ........ 0.0133
fix string #2 ........ 0.0094 ........ 0.0216
fix string #3 ........ 0.0094 ........ 0.0222
fix string #4 ........ 0.0091 ........ 0.0241
8-bit string #1 ...... 0.0122 ........ 0.0301
8-bit string #2 ...... 0.0118 ........ 0.0304
8-bit string #3 ...... 0.0119 ........ 0.0315
16-bit string #1 ..... 0.0150 ........ 0.0388
16-bit string #2 ..... 0.1545 ........ 0.1665
32-bit string ........ 0.1570 ........ 0.1756
wide char string #1 .. 0.0091 ........ 0.0236
wide char string #2 .. 0.0122 ........ 0.0313
8-bit binary #1 ...... 0.0100 ........ 0.0302
8-bit binary #2 ...... 0.0123 ........ 0.0324
8-bit binary #3 ...... 0.0126 ........ 0.0327
16-bit binary ........ 0.0168 ........ 0.0372
32-bit binary ........ 0.1588 ........ 0.1754
fix array #1 ......... 0.0042 ........ 0.0131
fix array #2 ......... 0.0294 ........ 0.0367
fix array #3 ......... 0.0412 ........ 0.0472
16-bit array #1 ...... 0.1378 ........ 0.1596
16-bit array #2 ........... S ............. S
32-bit array .............. S ............. S
complex array ........ 0.1865 ........ 0.2283
fix map #1 ........... 0.0725 ........ 0.1048
fix map #2 ........... 0.0319 ........ 0.0405
fix map #3 ........... 0.0356 ........ 0.0665
fix map #4 ........... 0.0465 ........ 0.0497
16-bit map #1 ........ 0.2540 ........ 0.3028
16-bit map #2 ............. S ............. S
32-bit map ................ S ............. S
complex map .......... 0.2372 ........ 0.2710
fixext 1 ............. 0.0283 ........ 0.0358
fixext 2 ............. 0.0291 ........ 0.0371
fixext 4 ............. 0.0302 ........ 0.0355
fixext 8 ............. 0.0288 ........ 0.0384
fixext 16 ............ 0.0293 ........ 0.0359
8-bit ext ............ 0.0302 ........ 0.0439
16-bit ext ........... 0.0334 ........ 0.0499
32-bit ext ........... 0.1845 ........ 0.1888
32-bit timestamp #1 .. 0.0337 ........ 0.0547
32-bit timestamp #2 .. 0.0335 ........ 0.0560
64-bit timestamp #1 .. 0.0371 ........ 0.0575
64-bit timestamp #2 .. 0.0374 ........ 0.0542
64-bit timestamp #3 .. 0.0356 ........ 0.0533
96-bit timestamp #1 .. 0.0362 ........ 0.0699
96-bit timestamp #2 .. 0.0381 ........ 0.0701
96-bit timestamp #3 .. 0.0367 ........ 0.0687
=============================================
Total 2.7618 4.0820
Skipped 4 4
Failed 0 0
Ignored 0 0
With JIT:
php -n -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.jit_buffer_size=64M -dopcache.jit=tracing -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
=============================================
Test/Target Packer BufferUnpacker
---------------------------------------------
nil .................. 0.0005 ........ 0.0054
false ................ 0.0004 ........ 0.0059
true ................. 0.0004 ........ 0.0059
7-bit uint #1 ........ 0.0010 ........ 0.0047
7-bit uint #2 ........ 0.0010 ........ 0.0046
7-bit uint #3 ........ 0.0010 ........ 0.0046
5-bit sint #1 ........ 0.0025 ........ 0.0046
5-bit sint #2 ........ 0.0023 ........ 0.0046
5-bit sint #3 ........ 0.0024 ........ 0.0045
8-bit uint #1 ........ 0.0043 ........ 0.0081
8-bit uint #2 ........ 0.0043 ........ 0.0079
8-bit uint #3 ........ 0.0041 ........ 0.0080
16-bit uint #1 ....... 0.0064 ........ 0.0095
16-bit uint #2 ....... 0.0064 ........ 0.0091
16-bit uint #3 ....... 0.0064 ........ 0.0094
32-bit uint #1 ....... 0.0085 ........ 0.0114
32-bit uint #2 ....... 0.0077 ........ 0.0122
32-bit uint #3 ....... 0.0077 ........ 0.0120
64-bit uint #1 ....... 0.0085 ........ 0.0159
64-bit uint #2 ....... 0.0086 ........ 0.0157
64-bit uint #3 ....... 0.0086 ........ 0.0158
8-bit int #1 ......... 0.0042 ........ 0.0080
8-bit int #2 ......... 0.0042 ........ 0.0080
8-bit int #3 ......... 0.0042 ........ 0.0081
16-bit int #1 ........ 0.0065 ........ 0.0095
16-bit int #2 ........ 0.0065 ........ 0.0090
16-bit int #3 ........ 0.0056 ........ 0.0085
32-bit int #1 ........ 0.0067 ........ 0.0107
32-bit int #2 ........ 0.0066 ........ 0.0106
32-bit int #3 ........ 0.0063 ........ 0.0104
64-bit int #1 ........ 0.0072 ........ 0.0162
64-bit int #2 ........ 0.0073 ........ 0.0174
64-bit int #3 ........ 0.0072 ........ 0.0164
64-bit int #4 ........ 0.0077 ........ 0.0161
64-bit float #1 ...... 0.0053 ........ 0.0135
64-bit float #2 ...... 0.0053 ........ 0.0135
64-bit float #3 ...... 0.0052 ........ 0.0135
fix string #1 ....... -0.0002 ........ 0.0044
fix string #2 ........ 0.0035 ........ 0.0067
fix string #3 ........ 0.0035 ........ 0.0077
fix string #4 ........ 0.0033 ........ 0.0078
8-bit string #1 ...... 0.0059 ........ 0.0110
8-bit string #2 ...... 0.0063 ........ 0.0121
8-bit string #3 ...... 0.0064 ........ 0.0124
16-bit string #1 ..... 0.0099 ........ 0.0146
16-bit string #2 ..... 0.1522 ........ 0.1474
32-bit string ........ 0.1511 ........ 0.1483
wide char string #1 .. 0.0039 ........ 0.0084
wide char string #2 .. 0.0073 ........ 0.0123
8-bit binary #1 ...... 0.0040 ........ 0.0112
8-bit binary #2 ...... 0.0075 ........ 0.0123
8-bit binary #3 ...... 0.0077 ........ 0.0129
16-bit binary ........ 0.0096 ........ 0.0145
32-bit binary ........ 0.1535 ........ 0.1479
fix array #1 ......... 0.0008 ........ 0.0061
fix array #2 ......... 0.0121 ........ 0.0165
fix array #3 ......... 0.0193 ........ 0.0222
16-bit array #1 ...... 0.0607 ........ 0.0479
16-bit array #2 ........... S ............. S
32-bit array .............. S ............. S
complex array ........ 0.0749 ........ 0.0824
fix map #1 ........... 0.0329 ........ 0.0431
fix map #2 ........... 0.0161 ........ 0.0189
fix map #3 ........... 0.0205 ........ 0.0262
fix map #4 ........... 0.0252 ........ 0.0205
16-bit map #1 ........ 0.1016 ........ 0.0927
16-bit map #2 ............. S ............. S
32-bit map ................ S ............. S
complex map .......... 0.1096 ........ 0.1030
fixext 1 ............. 0.0157 ........ 0.0161
fixext 2 ............. 0.0175 ........ 0.0183
fixext 4 ............. 0.0156 ........ 0.0185
fixext 8 ............. 0.0163 ........ 0.0184
fixext 16 ............ 0.0164 ........ 0.0182
8-bit ext ............ 0.0158 ........ 0.0207
16-bit ext ........... 0.0203 ........ 0.0219
32-bit ext ........... 0.1614 ........ 0.1539
32-bit timestamp #1 .. 0.0195 ........ 0.0249
32-bit timestamp #2 .. 0.0188 ........ 0.0260
64-bit timestamp #1 .. 0.0207 ........ 0.0281
64-bit timestamp #2 .. 0.0212 ........ 0.0291
64-bit timestamp #3 .. 0.0207 ........ 0.0295
96-bit timestamp #1 .. 0.0222 ........ 0.0358
96-bit timestamp #2 .. 0.0228 ........ 0.0353
96-bit timestamp #3 .. 0.0210 ........ 0.0319
=============================================
Total 1.6432 1.9674
Skipped 4 4
Failed 0 0
Ignored 0 0
You may change default benchmark settings by defining the following environment variables:
Name | Default |
---|---|
MP_BENCH_TARGETS | pure_p,pure_u , see a list of available targets |
MP_BENCH_ITERATIONS | 100_000 |
MP_BENCH_DURATION | not set |
MP_BENCH_ROUNDS | 3 |
MP_BENCH_TESTS | -@slow , see a list of available tests |
For example:
export MP_BENCH_TARGETS=pure_p
export MP_BENCH_ITERATIONS=1000000
export MP_BENCH_ROUNDS=5
# a comma separated list of test names
export MP_BENCH_TESTS='complex array, complex map'
# or a group name
# export MP_BENCH_TESTS='-@slow' // @pecl_comp
# or a regexp
# export MP_BENCH_TESTS='/complex (array|map)/'
Another example, benchmarking both the library and the PECL extension:
MP_BENCH_TARGETS=pure_p,pure_u,pecl_p,pecl_u \
php -n -dextension=msgpack.so -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
===========================================================================
Test/Target Packer BufferUnpacker msgpack_pack msgpack_unpack
---------------------------------------------------------------------------
nil .................. 0.0031 ........ 0.0141 ...... 0.0055 ........ 0.0064
false ................ 0.0039 ........ 0.0154 ...... 0.0056 ........ 0.0053
true ................. 0.0038 ........ 0.0139 ...... 0.0056 ........ 0.0044
7-bit uint #1 ........ 0.0061 ........ 0.0110 ...... 0.0059 ........ 0.0046
7-bit uint #2 ........ 0.0065 ........ 0.0119 ...... 0.0042 ........ 0.0029
7-bit uint #3 ........ 0.0054 ........ 0.0117 ...... 0.0045 ........ 0.0025
5-bit sint #1 ........ 0.0047 ........ 0.0103 ...... 0.0038 ........ 0.0022
5-bit sint #2 ........ 0.0048 ........ 0.0117 ...... 0.0038 ........ 0.0022
5-bit sint #3 ........ 0.0046 ........ 0.0102 ...... 0.0038 ........ 0.0023
8-bit uint #1 ........ 0.0063 ........ 0.0174 ...... 0.0039 ........ 0.0031
8-bit uint #2 ........ 0.0063 ........ 0.0167 ...... 0.0040 ........ 0.0029
8-bit uint #3 ........ 0.0063 ........ 0.0168 ...... 0.0039 ........ 0.0030
16-bit uint #1 ....... 0.0092 ........ 0.0222 ...... 0.0049 ........ 0.0030
16-bit uint #2 ....... 0.0096 ........ 0.0227 ...... 0.0042 ........ 0.0046
16-bit uint #3 ....... 0.0123 ........ 0.0274 ...... 0.0059 ........ 0.0051
32-bit uint #1 ....... 0.0136 ........ 0.0331 ...... 0.0060 ........ 0.0048
32-bit uint #2 ....... 0.0130 ........ 0.0336 ...... 0.0070 ........ 0.0048
32-bit uint #3 ....... 0.0127 ........ 0.0329 ...... 0.0051 ........ 0.0048
64-bit uint #1 ....... 0.0126 ........ 0.0268 ...... 0.0055 ........ 0.0049
64-bit uint #2 ....... 0.0135 ........ 0.0281 ...... 0.0052 ........ 0.0046
64-bit uint #3 ....... 0.0131 ........ 0.0274 ...... 0.0069 ........ 0.0044
8-bit int #1 ......... 0.0077 ........ 0.0236 ...... 0.0058 ........ 0.0044
8-bit int #2 ......... 0.0087 ........ 0.0244 ...... 0.0058 ........ 0.0048
8-bit int #3 ......... 0.0084 ........ 0.0241 ...... 0.0055 ........ 0.0049
16-bit int #1 ........ 0.0112 ........ 0.0271 ...... 0.0048 ........ 0.0045
16-bit int #2 ........ 0.0124 ........ 0.0292 ...... 0.0057 ........ 0.0049
16-bit int #3 ........ 0.0118 ........ 0.0270 ...... 0.0058 ........ 0.0050
32-bit int #1 ........ 0.0137 ........ 0.0366 ...... 0.0058 ........ 0.0051
32-bit int #2 ........ 0.0133 ........ 0.0366 ...... 0.0056 ........ 0.0049
32-bit int #3 ........ 0.0129 ........ 0.0350 ...... 0.0052 ........ 0.0048
64-bit int #1 ........ 0.0145 ........ 0.0254 ...... 0.0034 ........ 0.0025
64-bit int #2 ........ 0.0097 ........ 0.0214 ...... 0.0034 ........ 0.0025
64-bit int #3 ........ 0.0096 ........ 0.0287 ...... 0.0059 ........ 0.0050
64-bit int #4 ........ 0.0143 ........ 0.0277 ...... 0.0059 ........ 0.0046
64-bit float #1 ...... 0.0134 ........ 0.0281 ...... 0.0057 ........ 0.0052
64-bit float #2 ...... 0.0141 ........ 0.0281 ...... 0.0057 ........ 0.0050
64-bit float #3 ...... 0.0144 ........ 0.0282 ...... 0.0057 ........ 0.0050
fix string #1 ........ 0.0036 ........ 0.0143 ...... 0.0066 ........ 0.0053
fix string #2 ........ 0.0107 ........ 0.0222 ...... 0.0065 ........ 0.0068
fix string #3 ........ 0.0116 ........ 0.0245 ...... 0.0063 ........ 0.0069
fix string #4 ........ 0.0105 ........ 0.0253 ...... 0.0083 ........ 0.0077
8-bit string #1 ...... 0.0126 ........ 0.0318 ...... 0.0075 ........ 0.0088
8-bit string #2 ...... 0.0121 ........ 0.0295 ...... 0.0076 ........ 0.0086
8-bit string #3 ...... 0.0125 ........ 0.0293 ...... 0.0130 ........ 0.0093
16-bit string #1 ..... 0.0159 ........ 0.0368 ...... 0.0117 ........ 0.0086
16-bit string #2 ..... 0.1547 ........ 0.1686 ...... 0.1516 ........ 0.1373
32-bit string ........ 0.1558 ........ 0.1729 ...... 0.1511 ........ 0.1396
wide char string #1 .. 0.0098 ........ 0.0237 ...... 0.0066 ........ 0.0065
wide char string #2 .. 0.0128 ........ 0.0291 ...... 0.0061 ........ 0.0082
8-bit binary #1 ........... I ............. I ........... F ............. I
8-bit binary #2 ........... I ............. I ........... F ............. I
8-bit binary #3 ........... I ............. I ........... F ............. I
16-bit binary ............. I ............. I ........... F ............. I
32-bit binary ............. I ............. I ........... F ............. I
fix array #1 ......... 0.0040 ........ 0.0129 ...... 0.0120 ........ 0.0058
fix array #2 ......... 0.0279 ........ 0.0390 ...... 0.0143 ........ 0.0165
fix array #3 ......... 0.0415 ........ 0.0463 ...... 0.0162 ........ 0.0187
16-bit array #1 ...... 0.1349 ........ 0.1628 ...... 0.0334 ........ 0.0341
16-bit array #2 ........... S ............. S ........... S ............. S
32-bit array .............. S ............. S ........... S ............. S
complex array ............. I ............. I ........... F ............. F
fix map #1 ................ I ............. I ........... F ............. I
fix map #2 ........... 0.0345 ........ 0.0391 ...... 0.0143 ........ 0.0168
fix map #3 ................ I ............. I ........... F ............. I
fix map #4 ........... 0.0459 ........ 0.0473 ...... 0.0151 ........ 0.0163
16-bit map #1 ........ 0.2518 ........ 0.2962 ...... 0.0400 ........ 0.0490
16-bit map #2 ............. S ............. S ........... S ............. S
32-bit map ................ S ............. S ........... S ............. S
complex map .......... 0.2380 ........ 0.2682 ...... 0.0545 ........ 0.0579
fixext 1 .................. I ............. I ........... F ............. F
fixext 2 .................. I ............. I ........... F ............. F
fixext 4 .................. I ............. I ........... F ............. F
fixext 8 .................. I ............. I ........... F ............. F
fixext 16 ................. I ............. I ........... F ............. F
8-bit ext ................. I ............. I ........... F ............. F
16-bit ext ................ I ............. I ........... F ............. F
32-bit ext ................ I ............. I ........... F ............. F
32-bit timestamp #1 ....... I ............. I ........... F ............. F
32-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #1 ....... I ............. I ........... F ............. F
64-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #3 ....... I ............. I ........... F ............. F
96-bit timestamp #1 ....... I ............. I ........... F ............. F
96-bit timestamp #2 ....... I ............. I ........... F ............. F
96-bit timestamp #3 ....... I ............. I ........... F ............. F
===========================================================================
Total 1.5625 2.3866 0.7735 0.7243
Skipped 4 4 4 4
Failed 0 0 24 17
Ignored 24 24 0 7
With JIT:
MP_BENCH_TARGETS=pure_p,pure_u,pecl_p,pecl_u \
php -n -dextension=msgpack.so -dzend_extension=opcache.so \
-dpcre.jit=1 -dopcache.jit_buffer_size=64M -dopcache.jit=tracing -dopcache.enable=1 -dopcache.enable_cli=1 \
tests/bench.php
Example output
Filter: MessagePack\Tests\Perf\Filter\ListFilter
Rounds: 3
Iterations: 100000
===========================================================================
Test/Target Packer BufferUnpacker msgpack_pack msgpack_unpack
---------------------------------------------------------------------------
nil .................. 0.0001 ........ 0.0052 ...... 0.0053 ........ 0.0042
false ................ 0.0007 ........ 0.0060 ...... 0.0057 ........ 0.0043
true ................. 0.0008 ........ 0.0060 ...... 0.0056 ........ 0.0041
7-bit uint #1 ........ 0.0031 ........ 0.0046 ...... 0.0062 ........ 0.0041
7-bit uint #2 ........ 0.0021 ........ 0.0043 ...... 0.0062 ........ 0.0041
7-bit uint #3 ........ 0.0022 ........ 0.0044 ...... 0.0061 ........ 0.0040
5-bit sint #1 ........ 0.0030 ........ 0.0048 ...... 0.0062 ........ 0.0040
5-bit sint #2 ........ 0.0032 ........ 0.0046 ...... 0.0062 ........ 0.0040
5-bit sint #3 ........ 0.0031 ........ 0.0046 ...... 0.0062 ........ 0.0040
8-bit uint #1 ........ 0.0054 ........ 0.0079 ...... 0.0062 ........ 0.0050
8-bit uint #2 ........ 0.0051 ........ 0.0079 ...... 0.0064 ........ 0.0044
8-bit uint #3 ........ 0.0051 ........ 0.0082 ...... 0.0062 ........ 0.0044
16-bit uint #1 ....... 0.0077 ........ 0.0094 ...... 0.0065 ........ 0.0045
16-bit uint #2 ....... 0.0077 ........ 0.0094 ...... 0.0063 ........ 0.0045
16-bit uint #3 ....... 0.0077 ........ 0.0095 ...... 0.0064 ........ 0.0047
32-bit uint #1 ....... 0.0088 ........ 0.0119 ...... 0.0063 ........ 0.0043
32-bit uint #2 ....... 0.0089 ........ 0.0117 ...... 0.0062 ........ 0.0039
32-bit uint #3 ....... 0.0089 ........ 0.0118 ...... 0.0063 ........ 0.0044
64-bit uint #1 ....... 0.0097 ........ 0.0155 ...... 0.0063 ........ 0.0045
64-bit uint #2 ....... 0.0095 ........ 0.0153 ...... 0.0061 ........ 0.0045
64-bit uint #3 ....... 0.0096 ........ 0.0156 ...... 0.0063 ........ 0.0047
8-bit int #1 ......... 0.0053 ........ 0.0083 ...... 0.0062 ........ 0.0044
8-bit int #2 ......... 0.0052 ........ 0.0080 ...... 0.0062 ........ 0.0044
8-bit int #3 ......... 0.0052 ........ 0.0080 ...... 0.0062 ........ 0.0043
16-bit int #1 ........ 0.0089 ........ 0.0097 ...... 0.0069 ........ 0.0046
16-bit int #2 ........ 0.0075 ........ 0.0093 ...... 0.0063 ........ 0.0043
16-bit int #3 ........ 0.0075 ........ 0.0094 ...... 0.0062 ........ 0.0046
32-bit int #1 ........ 0.0086 ........ 0.0122 ...... 0.0063 ........ 0.0044
32-bit int #2 ........ 0.0087 ........ 0.0120 ...... 0.0066 ........ 0.0046
32-bit int #3 ........ 0.0086 ........ 0.0121 ...... 0.0060 ........ 0.0044
64-bit int #1 ........ 0.0096 ........ 0.0149 ...... 0.0060 ........ 0.0045
64-bit int #2 ........ 0.0096 ........ 0.0157 ...... 0.0062 ........ 0.0044
64-bit int #3 ........ 0.0096 ........ 0.0160 ...... 0.0063 ........ 0.0046
64-bit int #4 ........ 0.0097 ........ 0.0157 ...... 0.0061 ........ 0.0044
64-bit float #1 ...... 0.0079 ........ 0.0153 ...... 0.0056 ........ 0.0044
64-bit float #2 ...... 0.0079 ........ 0.0152 ...... 0.0057 ........ 0.0045
64-bit float #3 ...... 0.0079 ........ 0.0155 ...... 0.0057 ........ 0.0044
fix string #1 ........ 0.0010 ........ 0.0045 ...... 0.0071 ........ 0.0044
fix string #2 ........ 0.0048 ........ 0.0075 ...... 0.0070 ........ 0.0060
fix string #3 ........ 0.0048 ........ 0.0086 ...... 0.0068 ........ 0.0060
fix string #4 ........ 0.0050 ........ 0.0088 ...... 0.0070 ........ 0.0059
8-bit string #1 ...... 0.0081 ........ 0.0129 ...... 0.0069 ........ 0.0062
8-bit string #2 ...... 0.0086 ........ 0.0128 ...... 0.0069 ........ 0.0065
8-bit string #3 ...... 0.0086 ........ 0.0126 ...... 0.0115 ........ 0.0065
16-bit string #1 ..... 0.0105 ........ 0.0137 ...... 0.0128 ........ 0.0068
16-bit string #2 ..... 0.1510 ........ 0.1486 ...... 0.1526 ........ 0.1391
32-bit string ........ 0.1517 ........ 0.1475 ...... 0.1504 ........ 0.1370
wide char string #1 .. 0.0044 ........ 0.0085 ...... 0.0067 ........ 0.0057
wide char string #2 .. 0.0081 ........ 0.0125 ...... 0.0069 ........ 0.0063
8-bit binary #1 ........... I ............. I ........... F ............. I
8-bit binary #2 ........... I ............. I ........... F ............. I
8-bit binary #3 ........... I ............. I ........... F ............. I
16-bit binary ............. I ............. I ........... F ............. I
32-bit binary ............. I ............. I ........... F ............. I
fix array #1 ......... 0.0014 ........ 0.0059 ...... 0.0132 ........ 0.0055
fix array #2 ......... 0.0146 ........ 0.0156 ...... 0.0155 ........ 0.0148
fix array #3 ......... 0.0211 ........ 0.0229 ...... 0.0179 ........ 0.0180
16-bit array #1 ...... 0.0673 ........ 0.0498 ...... 0.0343 ........ 0.0388
16-bit array #2 ........... S ............. S ........... S ............. S
32-bit array .............. S ............. S ........... S ............. S
complex array ............. I ............. I ........... F ............. F
fix map #1 ................ I ............. I ........... F ............. I
fix map #2 ........... 0.0148 ........ 0.0180 ...... 0.0156 ........ 0.0179
fix map #3 ................ I ............. I ........... F ............. I
fix map #4 ........... 0.0252 ........ 0.0201 ...... 0.0214 ........ 0.0167
16-bit map #1 ........ 0.1027 ........ 0.0836 ...... 0.0388 ........ 0.0510
16-bit map #2 ............. S ............. S ........... S ............. S
32-bit map ................ S ............. S ........... S ............. S
complex map .......... 0.1104 ........ 0.1010 ...... 0.0556 ........ 0.0602
fixext 1 .................. I ............. I ........... F ............. F
fixext 2 .................. I ............. I ........... F ............. F
fixext 4 .................. I ............. I ........... F ............. F
fixext 8 .................. I ............. I ........... F ............. F
fixext 16 ................. I ............. I ........... F ............. F
8-bit ext ................. I ............. I ........... F ............. F
16-bit ext ................ I ............. I ........... F ............. F
32-bit ext ................ I ............. I ........... F ............. F
32-bit timestamp #1 ....... I ............. I ........... F ............. F
32-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #1 ....... I ............. I ........... F ............. F
64-bit timestamp #2 ....... I ............. I ........... F ............. F
64-bit timestamp #3 ....... I ............. I ........... F ............. F
96-bit timestamp #1 ....... I ............. I ........... F ............. F
96-bit timestamp #2 ....... I ............. I ........... F ............. F
96-bit timestamp #3 ....... I ............. I ........... F ............. F
===========================================================================
Total 0.9642 1.0909 0.8224 0.7213
Skipped 4 4 4 4
Failed 0 0 24 17
Ignored 24 24 0 7
Note that the msgpack extension (v2.1.2) doesn't support ext, bin and UTF-8 str types.
The library is released under the MIT License. See the bundled LICENSE file for details.
Author: rybakit
Source Code: https://github.com/rybakit/msgpack.php
License: MIT License
1648641360
A symbolic natural language parsing library for Rust, inspired by HDPSG.
This is a library for parsing natural or constructed languages into syntax trees and feature structures. There's no machine learning or probabilistic models, everything is hand-crafted and deterministic.
You can find out more about the motivations of this project in this blog post.
I'm using this to parse a constructed language for my upcoming xenolinguistics game, Themengi.
Using a simple 80-line grammar, introduced in the tutorial below, we can parse a simple subset of English, checking reflexive pronoun binding, case, and number agreement.
$ cargo run --bin cli examples/reflexives.fgr
> she likes himself
Parsed 0 trees
> her likes herself
Parsed 0 trees
> she like herself
Parsed 0 trees
> she likes herself
Parsed 1 tree
(0..3: S
(0..1: N (0..1: she))
(1..2: TV (1..2: likes))
(2..3: N (2..3: herself)))
[
child-2: [
case: acc
pron: ref
needs_pron: #0 she
num: sg
child-0: [ word: herself ]
]
child-1: [
tense: nonpast
child-0: [ word: likes ]
num: #1 sg
]
child-0: [
child-0: [ word: she ]
case: nom
pron: #0
num: #1
]
]
Low resource language? Low problem! No need to train on gigabytes of text, just write a grammar using your brain. Let's hypothesize that in American Sign Language, topicalized nouns (expressed with raised eyebrows) must appear first in the sentence. We can write a small grammar (18 lines), and plug in some sentences:
$ cargo run --bin cli examples/asl-wordorder.fgr -n
> boy sit
Parsed 1 tree
(0..2: S
(0..1: NP ((0..1: N (0..1: boy))))
(1..2: IV (1..2: sit)))
> boy throw ball
Parsed 1 tree
(0..3: S
(0..1: NP ((0..1: N (0..1: boy))))
(1..2: TV (1..2: throw))
(2..3: NP ((2..3: N (2..3: ball)))))
> ball nm-raised-eyebrows boy throw
Parsed 1 tree
(0..4: S
(0..2: NP
(0..1: N (0..1: ball))
(1..2: Topic (1..2: nm-raised-eyebrows)))
(2..3: NP ((2..3: N (2..3: boy))))
(3..4: TV (3..4: throw)))
> boy throw ball nm-raised-eyebrows
Parsed 0 trees
As an example, let's say we want to build a parser for English reflexive pronouns (himself, herself, themselves, themself, itself). We'll also support number ("He likes X" v.s. "They like X") and simple embedded clauses ("He said that they like X").
Grammar files are written in a custom language, similar to BNF, called Feature GRammar (.fgr). There's a VSCode syntax highlighting extension for these files available as fgr-syntax
.
We'll start by defining our lexicon. The lexicon is the set of terminal symbols (symbols in the actual input) that the grammar will match. Terminal symbols must start with a lowercase letter, and non-terminal symbols must start with an uppercase letter.
// pronouns
N -> he
N -> him
N -> himself
N -> she
N -> her
N -> herself
N -> they
N -> them
N -> themselves
N -> themself
// names, lowercase as they are terminals
N -> mary
N -> sue
N -> takeshi
N -> robert
// complementizer
Comp -> that
// verbs -- intransitive, transitive, and clausal
IV -> falls
IV -> fall
IV -> fell
TV -> likes
TV -> like
TV -> liked
CV -> says
CV -> say
CV -> said
Next, we can add our sentence rules (they must be added at the top, as the first rule in the file is assumed to be the top-level rule):
// sentence rules
S -> N IV
S -> N TV N
S -> N CV Comp S
// ... previous lexicon ...
Assuming this file is saved as examples/no-features.fgr
(which it is :wink:), we can test this file with the built-in CLI:
$ cargo run --bin cli examples/no-features.fgr
> he falls
Parsed 1 tree
(0..2: S
(0..1: N (0..1: he))
(1..2: IV (1..2: falls)))
[
child-1: [ child-0: [ word: falls ] ]
child-0: [ child-0: [ word: he ] ]
]
> he falls her
Parsed 0 trees
> he likes her
Parsed 1 tree
(0..3: S
(0..1: N (0..1: he))
(1..2: TV (1..2: likes))
(2..3: N (2..3: her)))
[
child-2: [ child-0: [ word: her ] ]
child-1: [ child-0: [ word: likes ] ]
child-0: [ child-0: [ word: he ] ]
]
> he likes
Parsed 0 trees
> he said that he likes her
Parsed 1 tree
(0..6: S
(0..1: N (0..1: he))
(1..2: CV (1..2: said))
(2..3: Comp (2..3: that))
(3..6: S
(3..4: N (3..4: he))
(4..5: TV (4..5: likes))
(5..6: N (5..6: her))))
[
child-0: [ child-0: [ word: he ] ]
child-2: [ child-0: [ word: that ] ]
child-1: [ child-0: [ word: said ] ]
child-3: [
child-2: [ child-0: [ word: her ] ]
child-1: [ child-0: [ word: likes ] ]
child-0: [ child-0: [ word: he ] ]
]
]
> he said that he
Parsed 0 trees
This grammar already parses some correct sentences, and blocks some trivially incorrect ones. However, it doesn't care about number, case, or reflexives right now:
> she likes himself // unbound reflexive pronoun
Parsed 1 tree
(0..3: S
(0..1: N (0..1: she))
(1..2: TV (1..2: likes))
(2..3: N (2..3: himself)))
[
child-0: [ child-0: [ word: she ] ]
child-2: [ child-0: [ word: himself ] ]
child-1: [ child-0: [ word: likes ] ]
]
> him like her // incorrect case on the subject pronoun, should be nominative
// (he) instead of accusative (him)
Parsed 1 tree
(0..3: S
(0..1: N (0..1: him))
(1..2: TV (1..2: like))
(2..3: N (2..3: her)))
[
child-0: [ child-0: [ word: him ] ]
child-1: [ child-0: [ word: like ] ]
child-2: [ child-0: [ word: her ] ]
]
> he like her // incorrect verb number agreement
Parsed 1 tree
(0..3: S
(0..1: N (0..1: he))
(1..2: TV (1..2: like))
(2..3: N (2..3: her)))
[
child-2: [ child-0: [ word: her ] ]
child-1: [ child-0: [ word: like ] ]
child-0: [ child-0: [ word: he ] ]
]
To fix this, we need to add features to our lexicon, and restrict the sentence rules based on features.
Features are added with square brackets, and are key: value pairs separated by commas. **top**
is a special feature value, which basically means "unspecified" -- we'll come back to it later. Features that are unspecified are also assumed to have a **top**
value, but sometimes explicitly stating top is more clear.
/// Pronouns
// The added features are:
// * num: sg or pl, whether this noun wants a singular verb (likes) or
// a plural verb (like). note this is grammatical number, so for example
// singular they takes plural agreement ("they like X", not *"they likes X")
// * case: nom or acc, whether this noun is nominative or accusative case.
// nominative case goes in the subject, and accusative in the object.
// e.g., "he fell" and "she likes him", not *"him fell" and *"her likes he"
// * pron: he, she, they, or ref -- what type of pronoun this is
// * needs_pron: whether this is a reflexive that needs to bind to another
// pronoun.
N[ num: sg, case: nom, pron: he ] -> he
N[ num: sg, case: acc, pron: he ] -> him
N[ num: sg, case: acc, pron: ref, needs_pron: he ] -> himself
N[ num: sg, case: nom, pron: she ] -> she
N[ num: sg, case: acc, pron: she ] -> her
N[ num: sg, case: acc, pron: ref, needs_pron: she] -> herself
N[ num: pl, case: nom, pron: they ] -> they
N[ num: pl, case: acc, pron: they ] -> them
N[ num: pl, case: acc, pron: ref, needs_pron: they ] -> themselves
N[ num: sg, case: acc, pron: ref, needs_pron: they ] -> themself
// Names
// The added features are:
// * num: sg, as people are singular ("mary likes her" / *"mary like her")
// * case: **top**, as names can be both subjects and objects
// ("mary likes her" / "she likes mary")
// * pron: whichever pronoun the person uses for reflexive agreement
// mary pron: she => mary likes herself
// sue pron: they => sue likes themself
// takeshi pron: he => takeshi likes himself
N[ num: sg, case: **top**, pron: she ] -> mary
N[ num: sg, case: **top**, pron: they ] -> sue
N[ num: sg, case: **top**, pron: he ] -> takeshi
N[ num: sg, case: **top**, pron: he ] -> robert
// Complementizer doesn't need features
Comp -> that
// Verbs -- intransitive, transitive, and clausal
// The added features are:
// * num: sg, pl, or **top** -- to match the noun numbers.
// **top** will match either sg or pl, as past-tense verbs in English
// don't agree in number: "he fell" and "they fell" are both fine
// * tense: past or nonpast -- this won't be used for agreement, but will be
// copied into the final feature structure, and the client code could do
// something with it
IV[ num: sg, tense: nonpast ] -> falls
IV[ num: pl, tense: nonpast ] -> fall
IV[ num: **top**, tense: past ] -> fell
TV[ num: sg, tense: nonpast ] -> likes
TV[ num: pl, tense: nonpast ] -> like
TV[ num: **top**, tense: past ] -> liked
CV[ num: sg, tense: nonpast ] -> says
CV[ num: pl, tense: nonpast ] -> say
CV[ num: **top**, tense: past ] -> said
Now that our lexicon is updated with features, we can update our sentence rules to constrain parsing based on those features. This uses two new features, tags and unification. Tags allow features to be associated between nodes in a rule, and unification controls how those features are compatible. The rules for unification are:
If unification fails anywhere, the parse is aborted and the tree is discarded. This allows the programmer to discard trees if features don't match.
// Sentence rules
// Intransitive verb:
// * Subject must be nominative case
// * Subject and verb must agree in number (copied through #1)
S -> N[ case: nom, num: #1 ] IV[ num: #1 ]
// Transitive verb:
// * Subject must be nominative case
// * Subject and verb must agree in number (copied through #2)
// * If there's a reflexive in the object position, make sure its `needs_pron`
// feature matches the subject's `pron` feature. If the object isn't a
// reflexive, then its `needs_pron` feature will implicitly be `**top**`, so
// will unify with anything.
S -> N[ case: nom, pron: #1, num: #2 ] TV[ num: #2 ] N[ case: acc, needs_pron: #1 ]
// Clausal verb:
// * Subject must be nominative case
// * Subject and verb must agree in number (copied through #1)
// * Reflexives can't cross clause boundaries (*"He said that she likes himself"),
// so we can ignore reflexives and delegate to inner clause rule
S -> N[ case: nom, num: #1 ] CV[ num: #1 ] Comp S
Now that we have this augmented grammar (available as examples/reflexives.fgr
), we can try it out and see that it rejects illicit sentences that were previously accepted, while still accepting valid ones:
> he fell
Parsed 1 tree
(0..2: S
(0..1: N (0..1: he))
(1..2: IV (1..2: fell)))
[
child-1: [
child-0: [ word: fell ]
num: #0 sg
tense: past
]
child-0: [
pron: he
case: nom
num: #0
child-0: [ word: he ]
]
]
> he like him
Parsed 0 trees
> he likes himself
Parsed 1 tree
(0..3: S
(0..1: N (0..1: he))
(1..2: TV (1..2: likes))
(2..3: N (2..3: himself)))
[
child-1: [
num: #0 sg
child-0: [ word: likes ]
tense: nonpast
]
child-2: [
needs_pron: #1 he
num: sg
child-0: [ word: himself ]
pron: ref
case: acc
]
child-0: [
child-0: [ word: he ]
pron: #1
num: #0
case: nom
]
]
> he likes herself
Parsed 0 trees
> mary likes herself
Parsed 1 tree
(0..3: S
(0..1: N (0..1: mary))
(1..2: TV (1..2: likes))
(2..3: N (2..3: herself)))
[
child-0: [
pron: #0 she
num: #1 sg
case: nom
child-0: [ word: mary ]
]
child-1: [
tense: nonpast
child-0: [ word: likes ]
num: #1
]
child-2: [
child-0: [ word: herself ]
num: sg
pron: ref
case: acc
needs_pron: #0
]
]
> mary likes themself
Parsed 0 trees
> sue likes themself
Parsed 1 tree
(0..3: S
(0..1: N (0..1: sue))
(1..2: TV (1..2: likes))
(2..3: N (2..3: themself)))
[
child-0: [
pron: #0 they
child-0: [ word: sue ]
case: nom
num: #1 sg
]
child-1: [
tense: nonpast
num: #1
child-0: [ word: likes ]
]
child-2: [
needs_pron: #0
case: acc
pron: ref
child-0: [ word: themself ]
num: sg
]
]
> sue likes himself
Parsed 0 trees
If this is interesting to you and you want to learn more, you can check out my blog series, the excellent textbook Syntactic Theory: A Formal Introduction (2nd ed.), and the DELPH-IN project, whose work on the LKB inspired this simplified version.
I need to write this section in more detail, but if you're comfortable with Rust, I suggest looking through the codebase. It's not perfect, it started as one of my first Rust projects (after migrating through F# -> TypeScript -> C in search of the right performance/ergonomics tradeoff), and it could use more tests, but overall it's not too bad.
Basically, the processing pipeline is:
Grammar
structGrammar
is defined in rules.rs
.Grammar
is Grammar::parse_from_file
, which is mostly a hand-written recusive descent parser in parse_grammar.rs
. Yes, I recognize the irony here.Grammar::parse
, which does everything for you, or Grammar::parse_chart
, which just does the chart)earley.rs
forest.rs
, using an algorithm I found in a very useful blog series I forget the URL for, because the algorithms in the academic literature for this are... weird.The most interesting thing you can do via code and not via the CLI is probably getting at the raw feature DAG, as that would let you do things like pronoun coreference. The DAG code is in featurestructure.rs
, and should be fairly approachable -- there's a lot of Rust ceremony around Rc<RefCell<...>>
because using an arena allocation crate seemed too harlike overkill, but that is somewhat mitigated by the NodeRef
type alias. Hit me up at https://vgel.me/contact if you need help with anything here!
Download Details:
Author: vgel
Source Code: https://github.com/vgel/treebender
License: MIT License
1642390128
íìŽì¬ ë¬Žë£ ê°ì (íì©íž6 - ìŽë¯žì§ ì²ëЬ)ì
ëë€.
OpenCV 륌 ìŽì©í ë€ìí ìŽë¯žì§ ì²ëЬ êž°ë²ê³Œ ì¬ë¯žìë íë¡ì ížë¥Œ ì§íí©ëë€.
ë구ë 볌 ì ìëë¡ ìœê³ ì¬ë¯žìê² ì ìíììµëë€. ^^
[ìê°]
(0:00:00) 0.Intro
(0:00:31) 1.ìê°
(0:02:18) 2.íì©íž 6 ìŽë¯žì§ ì²ëЬ ìê°
[OpenCV ì ë°ì ]
(0:04:36) 3.í겜ì€ì
(0:08:41) 4.ìŽë¯žì§ ì¶ë ¥
(0:21:51) 5.ëìì ì¶ë ¥ #1 íìŒ
(0:29:58) 6.ëìì ì¶ë ¥ #2 칎ë©ëŒ
(0:34:23) 7.ëí 귞늬Ʞ #1 ë¹ ì€ìŒì¹ë¶
(0:39:49) 8.ëí 귞늬Ʞ #2 ìì ìì¹
(0:42:26) 9.ëí 귞늬Ʞ #3 ì§ì
(0:51:23) 10.ëí 귞늬Ʞ #4 ì
(0:55:09) 11.ëí 귞늬Ʞ #5 ì¬ê°í
(0:58:32) 12.ëí 귞늬Ʞ #6 ë€ê°í
(1:09:23) 13.í
ì€íž #1 Ʞ볞
(1:17:45) 14.í
ì€íž #2 íêž ì°í
(1:24:14) 15.íìŒ ì ì¥ #1 ìŽë¯žì§
(1:29:27) 16.íìŒ ì ì¥ #2 ëìì
(1:39:29) 17.í¬êž° ì¡°ì
(1:50:16) 18.ìŽë¯žì§ ì륎Ʞ
(1:57:03) 19.ìŽë¯žì§ ëì¹
(2:01:46) 20.ìŽë¯žì§ íì
(2:06:07) 21.ìŽë¯žì§ ë³í - íë°±
(2:11:25) 22.ìŽë¯žì§ ë³í - í늌
(2:18:03) 23.ìŽë¯žì§ ë³í - ìê·Œ #1
(2:27:45) 24.ìŽë¯žì§ ë³í - ìê·Œ #2
[ë°ìë 묞ì ì€ìºë íë¡ì íž]
(2:32:50) 25.믞ë íë¡ì íž 1 - #1 ë§ì°ì€ ìŽë²€íž ë±ë¡
(2:42:06) 26.믞ë íë¡ì íž 1 - #2 Ʞ볞 ìœë ìì±
(2:49:54) 27.믞ë íë¡ì íž 1 - #3 ì§ì ì êžêž°
(2:55:24) 28.믞ë íë¡ì íž 1 - #4 ì€ìê° ì êžêž°
[OpenCV íë°ì ]
(3:01:52) 29.ìŽë¯žì§ ë³í - ìŽì§í #1 Trackbar
(3:14:37) 30.ìŽë¯žì§ ë³í - ìŽì§í #2 ìê³ê°
(3:20:26) 31.ìŽë¯žì§ ë³í - ìŽì§í #3 Adaptive Threshold
(3:28:34) 32.ìŽë¯žì§ ë³í - ìŽì§í #4 ì€ìž ìê³ ëŠ¬ìŠ
(3:32:22) 33.ìŽë¯žì§ ë³í - íœì°œ
(3:41:10) 34.ìŽë¯žì§ ë³í - 칚ì
(3:45:56) 35.ìŽë¯žì§ ë³í - ìŽëŠŒ & ë«í
(3:54:10) 36.ìŽë¯žì§ ê²ì¶ - 겜ê³ì
(4:05:08) 37.ìŽë¯žì§ ê²ì¶ - ì€ê³œì #1 Ʞ볞
(4:15:26) 38.ìŽë¯žì§ ê²ì¶ - ì€ê³œì #2 ì°Ÿêž° 몚ë
(4:20:46) 39.ìŽë¯žì§ ê²ì¶ - ì€ê³œì #3 멎ì
[칎ë ê²ì¶ & ë¶ë¥êž° íë¡ì íž]
(4:27:42) 40.믞ëíë¡ì íž 2
[íŽìŠ]
(4:31:57) 41.íŽìŠ
[ìŒêµŽìžì íë¡ì íž]
(4:41:25) 42.í겜ì€ì ë° êž°ë³ž ìœë ì 늬
(4:54:48) 43.ë곌 ìœ ìžìíì¬ ëí 귞늬Ʞ
(5:10:42) 44.귞늌í ìŽë¯žì§ ìì°êž°
(5:20:52) 45.ìºëŠí° ìŽë¯žì§ ìì°êž°
(5:33:10) 46.볎충ì€ëª
(5:40:53) 47.ë§ì¹ë©° (íìµ ì°žê³ ìë£)
(5:42:18) 48.Outro
[íìµìë£]
ìì
ì íìí ìŽë¯žì§, ëìì ìë£ ë§í¬ì
ëë€.
ê³ ììŽ ìŽë¯žì§ : https://pixabay.com/images/id-2083492/
í¬êž° : 640 x 390
íìŒëª
: img.jpg
ê³ ììŽ ëìì : https://www.pexels.com/video/7515833/
í¬êž° : SD (360 x 640)
íìŒëª
: video.mp4
ì 묞 ìŽë¯žì§ : https://pixabay.com/images/id-350376/
í¬êž° : 1280 x 853
íìŒëª
: newspaper.jpg
칎ë ìŽë¯žì§ 1 : https://pixabay.com/images/id-682332/
í¬êž° : 1280 x 1019
íìŒëª
: poker.jpg
ì±
ìŽë¯žì§ : https://www.pexels.com/photo/1029807/
í¬êž° : Small (640 x 853)
íìŒëª
: book.jpg
ëì¬ë ìŽë¯žì§ : https://pixabay.com/images/id-1300089/
í¬êž° : 1280 x 904
íìŒëª
: snowman.png
칎ë ìŽë¯žì§ 2 : https://pixabay.com/images/id-161404/
í¬êž° : 640 x 408
íìŒëª
: card.png
íŽìŠì© ëìì : https://www.pexels.com/video/3121459/
í¬êž° : HD (1280 x 720)
íìŒëª
: city.mp4
íë¡ì ížì© ëìì : https://www.pexels.com/video/3256542/
í¬êž° : Full HD (1920 x 1080)
íìŒëª
: face_video.mp4
íë¡ì ížì© ìºëŠí° ìŽë¯žì§ : https://www.freepik.com/free-vector/cute-animal-masks-video-chat-application-effect-filters-set_6380101.htm
íìŒëª
: right_eye.png (100 x 100), left_eye.png (100 x 100), nose.png (300 x 100)
ë¬Žë£ ìŽë¯žì§ ížì§ ë구 : https://pixlr.com/kr/
(Pixlr E -Advanced Editor)
#python #opencv
1630743562
FHIR_DB
This is really just a wrapper around Sembast_SQFLite - so all of the heavy lifting was done by Alex Tekartik. I highly recommend that if you have any questions about working with this package that you take a look at Sembast. He's also just a super nice guy, and even answered a question for me when I was deciding which sembast version to use. As usual, ResoCoder also has a good tutorial.
I have an interest in low-resource settings and thus a specific reason to be able to store data offline. To encourage this use, there are a number of other packages I have created based around the data format FHIR. FHIR® is the registered trademark of HL7 and is used with the permission of HL7. Use of the FHIR trademark does not constitute endorsement of this product by HL7.
So, while not absolutely necessary, I highly recommend that you use some sort of interface class. This adds the benefit of more easily handling errors, plus if you change to a different database in the future, you don't have to change the rest of your app, just the interface.
I've used something like this in my projects:
class IFhirDb {
IFhirDb();
final ResourceDao resourceDao = ResourceDao();
Future<Either<DbFailure, Resource>> save(Resource resource) async {
Resource resultResource;
try {
resultResource = await resourceDao.save(resource);
} catch (error) {
return left(DbFailure.unableToSave(error: error.toString()));
}
return right(resultResource);
}
Future<Either<DbFailure, List<Resource>>> returnListOfSingleResourceType(
String resourceType) async {
List<Resource> resultList;
try {
resultList =
await resourceDao.getAllSortedById(resourceType: resourceType);
} catch (error) {
return left(DbFailure.unableToObtainList(error: error.toString()));
}
return right(resultList);
}
Future<Either<DbFailure, List<Resource>>> searchFunction(
String resourceType, String searchString, String reference) async {
List<Resource> resultList;
try {
resultList =
await resourceDao.searchFor(resourceType, searchString, reference);
} catch (error) {
return left(DbFailure.unableToObtainList(error: error.toString()));
}
return right(resultList);
}
}
I like this because in case there's an i/o error or something, it won't crash your app. Then, you can call this interface in your app like the following:
final patient = Patient(
resourceType: 'Patient',
name: [HumanName(text: 'New Patient Name')],
birthDate: Date(DateTime.now()),
);
final saveResult = await IFhirDb().save(patient);
This will save your newly created patient to the locally embedded database.
IMPORTANT: this database will expect that all previously created resources have an id. When you save a resource, it will check to see if that resource type has already been stored. (Each resource type is saved in it's own store in the database). It will then check if there is an ID. If there's no ID, it will create a new one for that resource (along with metadata on version number and creation time). It will save it, and return the resource. If it already has an ID, it will copy the the old version of the resource into a _history store. It will then update the metadata of the new resource and save that version into the appropriate store for that resource. If, for instance, we have a previously created patient:
{
"resourceType": "Patient",
"id": "fhirfli-294057507-6811107",
"meta": {
"versionId": "1",
"lastUpdated": "2020-10-16T19:41:28.054369Z"
},
"name": [
{
"given": ["New"],
"family": "Patient"
}
],
"birthDate": "2020-10-16"
}
And we update the last name to 'Provider'. The above version of the patient will be kept in _history, while in the 'Patient' store in the db, we will have the updated version:
{
"resourceType": "Patient",
"id": "fhirfli-294057507-6811107",
"meta": {
"versionId": "2",
"lastUpdated": "2020-10-16T19:45:07.316698Z"
},
"name": [
{
"given": ["New"],
"family": "Provider"
}
],
"birthDate": "2020-10-16"
}
This way we can keep track of all previous version of all resources (which is obviously important in medicine).
For most of the interactions (saving, deleting, etc), they work the way you'd expect. The only difference is search. Because Sembast is NoSQL, we can search on any of the fields in a resource. If in our interface class, we have the following function:
Future<Either<DbFailure, List<Resource>>> searchFunction(
String resourceType, String searchString, String reference) async {
List<Resource> resultList;
try {
resultList =
await resourceDao.searchFor(resourceType, searchString, reference);
} catch (error) {
return left(DbFailure.unableToObtainList(error: error.toString()));
}
return right(resultList);
}
You can search for all immunizations of a certain patient:
searchFunction(
'Immunization', 'patient.reference', 'Patient/$patientId');
This function will search through all entries in the 'Immunization' store. It will look at all 'patient.reference' fields, and return any that match 'Patient/$patientId'.
The last thing I'll mention is that this is a password protected db, using AES-256 encryption (although it can also use Salsa20). Anytime you use the db, you have the option of using a password for encryption/decryption. Remember, if you setup the database using encryption, you will only be able to access it using that same password. When you're ready to change the password, you will need to call the update password function. If we again assume we created a change password method in our interface, it might look something like this:
class IFhirDb {
IFhirDb();
final ResourceDao resourceDao = ResourceDao();
...
Future<Either<DbFailure, Unit>> updatePassword(String oldPassword, String newPassword) async {
try {
await resourceDao.updatePw(oldPassword, newPassword);
} catch (error) {
return left(DbFailure.unableToUpdatePassword(error: error.toString()));
}
return right(Unit);
}
You don't have to use a password, and in that case, it will save the db file as plain text. If you want to add a password later, it will encrypt it at that time.
After using this for a while in an app, I've realized that it needs to be able to store data apart from just FHIR resources, at least on occasion. For this, I've added a second class for all versions of the database called GeneralDao. This is similar to the ResourceDao, but fewer options. So, in order to save something, it would look like this:
await GeneralDao().save('password', {'new':'map'});
await GeneralDao().save('password', {'new':'map'}, 'key');
The difference between these two options is that the first one will generate a key for the map being stored, while the second will store the map using the key provided. Both will return the key after successfully storing the map.
Other functions available include:
// deletes everything in the general store
await GeneralDao().deleteAllGeneral('password');
// delete specific entry
await GeneralDao().delete('password','key');
// returns map with that key
await GeneralDao().find('password', 'key');
FHIR® is a registered trademark of Health Level Seven International (HL7) and its use does not constitute an endorsement of products by HL7®
Run this command:
With Flutter:
$ flutter pub add fhir_db
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):
dependencies:
fhir_db: ^0.4.3
Alternatively, your editor might support or flutter pub get. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:fhir_db/dstu2.dart';
import 'package:fhir_db/dstu2/fhir_db.dart';
import 'package:fhir_db/dstu2/general_dao.dart';
import 'package:fhir_db/dstu2/resource_dao.dart';
import 'package:fhir_db/encrypt/aes.dart';
import 'package:fhir_db/encrypt/salsa.dart';
import 'package:fhir_db/r4.dart';
import 'package:fhir_db/r4/fhir_db.dart';
import 'package:fhir_db/r4/general_dao.dart';
import 'package:fhir_db/r4/resource_dao.dart';
import 'package:fhir_db/r5.dart';
import 'package:fhir_db/r5/fhir_db.dart';
import 'package:fhir_db/r5/general_dao.dart';
import 'package:fhir_db/r5/resource_dao.dart';
import 'package:fhir_db/stu3.dart';
import 'package:fhir_db/stu3/fhir_db.dart';
import 'package:fhir_db/stu3/general_dao.dart';
import 'package:fhir_db/stu3/resource_dao.dart';
import 'package:fhir/r4.dart';
import 'package:fhir_db/r4.dart';
import 'package:flutter/material.dart';
import 'package:test/test.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final resourceDao = ResourceDao();
// await resourceDao.updatePw('newPw', null);
await resourceDao.deleteAllResources(null);
group('Playing with passwords', () {
test('Playing with Passwords', () async {
final patient = Patient(id: Id('1'));
final saved = await resourceDao.save(null, patient);
await resourceDao.updatePw(null, 'newPw');
final search1 = await resourceDao.find('newPw',
resourceType: R4ResourceType.Patient, id: Id('1'));
expect(saved, search1[0]);
await resourceDao.updatePw('newPw', 'newerPw');
final search2 = await resourceDao.find('newerPw',
resourceType: R4ResourceType.Patient, id: Id('1'));
expect(saved, search2[0]);
await resourceDao.updatePw('newerPw', null);
final search3 = await resourceDao.find(null,
resourceType: R4ResourceType.Patient, id: Id('1'));
expect(saved, search3[0]);
await resourceDao.deleteAllResources(null);
});
});
final id = Id('12345');
group('Saving Things:', () {
test('Save Patient', () async {
final humanName = HumanName(family: 'Atreides', given: ['Duke']);
final patient = Patient(id: id, name: [humanName]);
final saved = await resourceDao.save(null, patient);
expect(saved.id, id);
expect((saved as Patient).name?[0], humanName);
});
test('Save Organization', () async {
final organization = Organization(id: id, name: 'FhirFli');
final saved = await resourceDao.save(null, organization);
expect(saved.id, id);
expect((saved as Organization).name, 'FhirFli');
});
test('Save Observation1', () async {
final observation1 = Observation(
id: Id('obs1'),
code: CodeableConcept(text: 'Observation #1'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save(null, observation1);
expect(saved.id, Id('obs1'));
expect((saved as Observation).code.text, 'Observation #1');
});
test('Save Observation1 Again', () async {
final observation1 = Observation(
id: Id('obs1'),
code: CodeableConcept(text: 'Observation #1 - Updated'));
final saved = await resourceDao.save(null, observation1);
expect(saved.id, Id('obs1'));
expect((saved as Observation).code.text, 'Observation #1 - Updated');
expect(saved.meta?.versionId, Id('2'));
});
test('Save Observation2', () async {
final observation2 = Observation(
id: Id('obs2'),
code: CodeableConcept(text: 'Observation #2'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save(null, observation2);
expect(saved.id, Id('obs2'));
expect((saved as Observation).code.text, 'Observation #2');
});
test('Save Observation3', () async {
final observation3 = Observation(
id: Id('obs3'),
code: CodeableConcept(text: 'Observation #3'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save(null, observation3);
expect(saved.id, Id('obs3'));
expect((saved as Observation).code.text, 'Observation #3');
});
});
group('Finding Things:', () {
test('Find 1st Patient', () async {
final search = await resourceDao.find(null,
resourceType: R4ResourceType.Patient, id: id);
final humanName = HumanName(family: 'Atreides', given: ['Duke']);
expect(search.length, 1);
expect((search[0] as Patient).name?[0], humanName);
});
test('Find 3rd Observation', () async {
final search = await resourceDao.find(null,
resourceType: R4ResourceType.Observation, id: Id('obs3'));
expect(search.length, 1);
expect(search[0].id, Id('obs3'));
expect((search[0] as Observation).code.text, 'Observation #3');
});
test('Find All Observations', () async {
final search = await resourceDao.getResourceType(
null,
resourceTypes: [R4ResourceType.Observation],
);
expect(search.length, 3);
final idList = [];
for (final obs in search) {
idList.add(obs.id.toString());
}
expect(idList.contains('obs1'), true);
expect(idList.contains('obs2'), true);
expect(idList.contains('obs3'), true);
});
test('Find All (non-historical) Resources', () async {
final search = await resourceDao.getAll(null);
expect(search.length, 5);
final patList = search.toList();
final orgList = search.toList();
final obsList = search.toList();
patList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Patient);
orgList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Organization);
obsList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Observation);
expect(patList.length, 1);
expect(orgList.length, 1);
expect(obsList.length, 3);
});
});
group('Deleting Things:', () {
test('Delete 2nd Observation', () async {
await resourceDao.delete(
null, null, R4ResourceType.Observation, Id('obs2'), null, null);
final search = await resourceDao.getResourceType(
null,
resourceTypes: [R4ResourceType.Observation],
);
expect(search.length, 2);
final idList = [];
for (final obs in search) {
idList.add(obs.id.toString());
}
expect(idList.contains('obs1'), true);
expect(idList.contains('obs2'), false);
expect(idList.contains('obs3'), true);
});
test('Delete All Observations', () async {
await resourceDao.deleteSingleType(null,
resourceType: R4ResourceType.Observation);
final search = await resourceDao.getAll(null);
expect(search.length, 2);
final patList = search.toList();
final orgList = search.toList();
patList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Patient);
orgList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Organization);
expect(patList.length, 1);
expect(patList.length, 1);
});
test('Delete All Resources', () async {
await resourceDao.deleteAllResources(null);
final search = await resourceDao.getAll(null);
expect(search.length, 0);
});
});
group('Password - Saving Things:', () {
test('Save Patient', () async {
await resourceDao.updatePw(null, 'newPw');
final humanName = HumanName(family: 'Atreides', given: ['Duke']);
final patient = Patient(id: id, name: [humanName]);
final saved = await resourceDao.save('newPw', patient);
expect(saved.id, id);
expect((saved as Patient).name?[0], humanName);
});
test('Save Organization', () async {
final organization = Organization(id: id, name: 'FhirFli');
final saved = await resourceDao.save('newPw', organization);
expect(saved.id, id);
expect((saved as Organization).name, 'FhirFli');
});
test('Save Observation1', () async {
final observation1 = Observation(
id: Id('obs1'),
code: CodeableConcept(text: 'Observation #1'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save('newPw', observation1);
expect(saved.id, Id('obs1'));
expect((saved as Observation).code.text, 'Observation #1');
});
test('Save Observation1 Again', () async {
final observation1 = Observation(
id: Id('obs1'),
code: CodeableConcept(text: 'Observation #1 - Updated'));
final saved = await resourceDao.save('newPw', observation1);
expect(saved.id, Id('obs1'));
expect((saved as Observation).code.text, 'Observation #1 - Updated');
expect(saved.meta?.versionId, Id('2'));
});
test('Save Observation2', () async {
final observation2 = Observation(
id: Id('obs2'),
code: CodeableConcept(text: 'Observation #2'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save('newPw', observation2);
expect(saved.id, Id('obs2'));
expect((saved as Observation).code.text, 'Observation #2');
});
test('Save Observation3', () async {
final observation3 = Observation(
id: Id('obs3'),
code: CodeableConcept(text: 'Observation #3'),
effectiveDateTime: FhirDateTime(DateTime(1981, 09, 18)),
);
final saved = await resourceDao.save('newPw', observation3);
expect(saved.id, Id('obs3'));
expect((saved as Observation).code.text, 'Observation #3');
});
});
group('Password - Finding Things:', () {
test('Find 1st Patient', () async {
final search = await resourceDao.find('newPw',
resourceType: R4ResourceType.Patient, id: id);
final humanName = HumanName(family: 'Atreides', given: ['Duke']);
expect(search.length, 1);
expect((search[0] as Patient).name?[0], humanName);
});
test('Find 3rd Observation', () async {
final search = await resourceDao.find('newPw',
resourceType: R4ResourceType.Observation, id: Id('obs3'));
expect(search.length, 1);
expect(search[0].id, Id('obs3'));
expect((search[0] as Observation).code.text, 'Observation #3');
});
test('Find All Observations', () async {
final search = await resourceDao.getResourceType(
'newPw',
resourceTypes: [R4ResourceType.Observation],
);
expect(search.length, 3);
final idList = [];
for (final obs in search) {
idList.add(obs.id.toString());
}
expect(idList.contains('obs1'), true);
expect(idList.contains('obs2'), true);
expect(idList.contains('obs3'), true);
});
test('Find All (non-historical) Resources', () async {
final search = await resourceDao.getAll('newPw');
expect(search.length, 5);
final patList = search.toList();
final orgList = search.toList();
final obsList = search.toList();
patList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Patient);
orgList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Organization);
obsList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Observation);
expect(patList.length, 1);
expect(orgList.length, 1);
expect(obsList.length, 3);
});
});
group('Password - Deleting Things:', () {
test('Delete 2nd Observation', () async {
await resourceDao.delete(
'newPw', null, R4ResourceType.Observation, Id('obs2'), null, null);
final search = await resourceDao.getResourceType(
'newPw',
resourceTypes: [R4ResourceType.Observation],
);
expect(search.length, 2);
final idList = [];
for (final obs in search) {
idList.add(obs.id.toString());
}
expect(idList.contains('obs1'), true);
expect(idList.contains('obs2'), false);
expect(idList.contains('obs3'), true);
});
test('Delete All Observations', () async {
await resourceDao.deleteSingleType('newPw',
resourceType: R4ResourceType.Observation);
final search = await resourceDao.getAll('newPw');
expect(search.length, 2);
final patList = search.toList();
final orgList = search.toList();
patList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Patient);
orgList.retainWhere(
(resource) => resource.resourceType == R4ResourceType.Organization);
expect(patList.length, 1);
expect(patList.length, 1);
});
test('Delete All Resources', () async {
await resourceDao.deleteAllResources('newPw');
final search = await resourceDao.getAll('newPw');
expect(search.length, 0);
await resourceDao.updatePw('newPw', null);
});
});
}
Download Details:
Author: MayJuun
Source Code: https://github.com/MayJuun/fhir/tree/main/fhir_db
1633693200
ãã¥ãŒãšãŒã¯ã§åãããŠã©ãŒã«è¡äžã®ããã°ã©ããŒãšè©±ãããŠãããšãã»ãšãã©ã®ãªã¢ã«ã¿ã€ã ããã°ã©ãã³ã°ã¢ããªã±ãŒã·ã§ã³ã§æåŸ ãããå ±éã®ç¥èã®ç³žã«æ°ã¥ããŸããããã®ç¥èã¯ãã«ãã¹ã¬ãããšããŠç¥ãããŠããŸããç§ã¯ããã°ã©ãã³ã°ã®äžçãç§»åããæœåšçãªããã°ã©ãã³ã°åè£è ã«ã€ã³ã¿ãã¥ãŒãè¡ã£ãã®ã§ããã«ãã¹ã¬ããã«ã€ããŠã»ãšãã©ç¥ãããŠããªãããšããã¹ã¬ãããé©çšãããçç±ãæ¹æ³ã«é©ããããããšã¯æ±ºããŠãããŸãããVance Morrisonã«ãã£ãŠæžãããäžé£ã®åªããèšäºã§ãMSDNã¯ãã®åé¡ã«å¯ŸåŠããããšããŸããïŒïŒMSDNã®8æå·ããã¹ãŠã®éçºè ããã«ãã¹ã¬ããã¢ããªã«ã€ããŠç¥ã£ãŠããã¹ãããšãããã³10æå·ã¯ãã«ãã¹ã¬ããã§ã®ããŒããã¯æè¡ã®åœ±é¿ãçè§£ãããåç §ããŠãã ããïŒãã¢ããªã
ãã®èšäºã§ã¯ãã¹ã¬ããåãã¹ã¬ããåã䜿çšãããçç±ãããã³.NETã§ã®ã¹ã¬ããåã®äœ¿ç𿹿³ã«ã€ããŠç޹ä»ããŸãããã«ãã¹ã¬ããã®èåŸã«ããè¬ãå®å šã«æããã«ããããã説æããéã«ãã³ãŒãå ã®æœåšçãªã¹ã¬ããé害ãåé¿ããã®ã«åœ¹ç«ã€ããšãé¡ã£ãŠããŸãã
ãã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã¯ãå°ãªããšã1ã€ã®ã¹ã¬ããã§å®è¡ãããŸããã§ã¯ãã¹ã¬ãããšã¯äœã§ããïŒã¹ã¬ããã¯ããã»ã¹ã«ãããŸãããç§ã®æšæž¬ã§ã¯ã糞ãšããèšèã¯ãç¹æ©ã§ç³žãç¹ãäžããè¶ èªç¶çãªãã¥ãŒãºã®ã®ãªã·ã£ç¥è©±ã«ç±æ¥ããŠãããšæããŸããå糞ã¯ã誰ãã®äººçã®æéã®éã衚ããŠããŸããããªãããã®ç³žãããããšãããªãã¯äººçã®æ§é ãä¹±ãããã人çã®ããã»ã¹ãå€ãããããŸããã³ã³ãã¥ãŒã¿ãŒã§ã¯ãã¹ã¬ããã¯æéã®çµéãšãšãã«ç§»åããããã»ã¹ã§ããããã»ã¹ã¯äžé£ã®é 次ã¹ããããå®è¡ããåã¹ãããã¯ã³ãŒãè¡ãå®è¡ããŸããã¹ãããã¯é£ç¶ããŠãããããåã¹ãããã«ã¯äžå®ã®æéãããããŸããäžé£ã®ã¹ããããå®äºããã®ã«ãããæéã¯ãåããã°ã©ãã³ã°ã¹ãããã®å®è¡ã«ãããæéã®åèšã§ãã
é·ãéãã»ãšãã©ã®ããã°ã©ãã³ã°ã¢ããªã±ãŒã·ã§ã³ïŒçµã¿èŸŒã¿ã·ã¹ãã ããã°ã©ã ãé€ãïŒã¯ã·ã³ã°ã«ã¹ã¬ããã§ãããããã¯ãã¢ããªã±ãŒã·ã§ã³å šäœã§ã¹ã¬ããã1ã€ãããªãããšãæå³ããŸããèšç®Bãå®äºãããŸã§ãèšç®Aãå®è¡ããããšã¯ã§ããŸãããããã°ã©ã ã¯ã¹ããã1ããå§ãŸããæåŸã®ã¹ãããïŒã¹ããã10ãšåŒã³ãŸãïŒã«å°éãããŸã§é 次ç¶è¡ããŸãïŒã¹ããã2ãã¹ããã3ãã¹ããã4ïŒããã«ãã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ã䜿çšãããšãè€æ°ã®ã¹ã¬ãããå®è¡ã§ããŸããåã¹ã¬ããã¯ç¬èªã®ããã»ã¹ã§å®è¡ãããŸãããããã£ãŠãçè«çã«ã¯ãããã¹ã¬ããã§ã¹ããã1ãå®è¡ããåæã«å¥ã®ã¹ã¬ããã§ã¹ããã2ãå®è¡ã§ããŸããåæã«ãã¹ããã3ãç¬èªã®ã¹ã¬ããã§å®è¡ããã¹ããã4ãç¬èªã®ã¹ã¬ããã§å®è¡ããããšãã§ããŸãããããã£ãŠãã¹ããã1ãã¹ããã2ãã¹ããã3ãããã³ã¹ããã4ã¯åæã«å®è¡ãããŸããçè«çã«ã¯ã4ã€ã®ã¹ããããã¹ãŠãã»ãŒåæã«ããã£ãå Žåãã·ã³ã°ã«ã¹ã¬ããã®å®è¡ã«ãããæéã®4åã®1ã§ããã°ã©ã ãçµäºã§ããŸãïŒ4ããã»ããµãã·ã³ã䜿çšããŠãããšä»®å®ïŒãã§ã¯ããªããã¹ãŠã®ããã°ã©ã ããã«ãã¹ã¬ããåãããŠããªãã®ã§ãããããã¹ããŒããšãšãã«ãããªãã¯è€éãã«çŽé¢ããããã§ããã¹ããã1ãã¹ããã2ã®æ å ±ã«äœããã®åœ¢ã§äŸåããŠããå Žåãæ³åããŠã¿ãŠãã ãããã¹ããã1ãã¹ããã2ã®åã«èšç®ãçµäºããå ŽåããŸãã¯ãã®éã®å Žåãããã°ã©ã ãæ£ããå®è¡ãããªãå¯èœæ§ããããŸãã
ãã«ãã¹ã¬ãããèããå¥ã®æ¹æ³ã¯ã人äœãèæ ®ããããšã§ããäœã®ååšå®ïŒå¿èãèºãèèãè³ïŒã¯ãã¹ãŠããã»ã¹ã«é¢äžããŠããŸããåããã»ã¹ã¯åæã«å®è¡ãããŠããŸããåèåšãããã»ã¹ã®ã¹ããããšããŠå®è¡ãããå Žåãæ³åããŠã¿ãŠãã ãããæåã«å¿èãæ¬¡ã«è³ã次ã«èèãæ¬¡ã«èºã§ããç§ãã¡ã¯ããããæ»ãã§ããŸãã§ããããã€ãŸãã人äœã¯1ã€ã®å€§ããªãã«ãã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ã®ãããªãã®ã§ãããã¹ãŠã®èåšã¯åæã«å®è¡ãããããã»ã¹ã§ããããããã®ããã»ã¹ã¯ãã¹ãŠçžäºã«äŸåããŠããŸãããããã®ããã»ã¹ã¯ãã¹ãŠãç¥çµä¿¡å·ãè¡æµãååŠçããªã¬ãŒãä»ããŠéä¿¡ããŸãããã¹ãŠã®ãã«ãã¹ã¬ããã¢ããªã±ãŒã·ã§ã³ãšåæ§ã«ã人äœã¯éåžžã«è€éã§ããäžéšã®ããã»ã¹ãä»ã®ããã»ã¹ããæ å ±ãååŸããªãå ŽåããŸãã¯ç¹å®ã®ããã»ã¹ãé ããªã£ããéããªã£ãããããšãå»åŠçãªåé¡ãçºçããŸããããã'
ãã«ãã¹ã¬ããã¯ãããã°ã©ã ãããå¹ççã«å®è¡ãããç¶æ³ã§æããã䜿çšãããŸããããšãã°ããŠã£ã³ããŠãã©ãŒã ããã°ã©ã ã®äžã«ãå®è¡ã«1ç§ä»¥äžããããç¹°ãè¿ãå®è¡ããå¿ èŠã®ããã¡ãœããïŒmethod_AãšåŒã³ãŸãïŒãå«ãŸããŠãããšããŸããããã°ã©ã å šäœãåäžã®ã¹ã¬ããã§å®è¡ãããå Žåããã¿ã³ã®æŒäžãæ£ããæ©èœããªãã£ãããå ¥åãå°ãé ããªã£ããããããšããããŸããmethod_Aã®èšç®éãå€ããããšããŠã£ã³ããŠãã©ãŒã ã®ç¹å®ã®éšåããŸã£ããæ©èœããªãããšã«æ°ä»ããããããŸããããã®èš±å®¹ã§ããªãããã°ã©ã ã®åäœã¯ãããã°ã©ã ã«ãã«ãã¹ã¬ãããå¿ èŠã§ããããšã瀺ããŠããŸããã¹ã¬ããåãå¿ èŠã«ãªããã1ã€ã®äžè¬çãªã·ããªãªã¯ãã¡ãã»ãŒãžã³ã°ã·ã¹ãã ã§ããã¢ããªã±ãŒã·ã§ã³ã«å€æ°ã®ã¡ãã»ãŒãžãéä¿¡ãããŠããå Žåã¯ãã¡ã€ã³ã®åŠçããã°ã©ã ã®å®è¡ãšåæã«ãããããã£ããã£ããé©åã«é åžããå¿ èŠããããŸããéãåŠçãå®è¡ããŠãããšãã«äžé£ã®ã¡ãã»ãŒãžãå¹ççã«ãã£ããã£ããããšã¯ã§ããŸãããããããªããšãã¡ãã»ãŒãžãèŠéãå¯èœæ§ããããŸããè€æ°ã®ã¹ã¬ããã¯ãè€æ°ã®ããã»ã¹ãåæã«å®è¡ãããçµç«ã©ã€ã³æ¹åŒã§äœ¿çšããããšãã§ããŸããããšãã°ãããã»ã¹ãã¹ã¬ããã§ããŒã¿ãåéãããšã1ã€ã®ããã»ã¹ãããŒã¿ããã£ã«ã¿ãªã³ã°ãã1ã€ã®ããã»ã¹ãããŒã¿ãããŒã¿ããŒã¹ãšç §åããŸãããããã®åã·ããªãªã¯ãã«ãã¹ã¬ããã®äžè¬çãªäœ¿çšæ³ã§ãããã·ã³ã°ã«ã¹ã¬ããã§å®è¡ãããŠããåæ§ã®ã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹ãå€§å¹ ã«åäžãããŸããããããªããšãã¡ãã»ãŒãžãèŠéãå¯èœæ§ãããããã§ããè€æ°ã®ã¹ã¬ããã¯ãè€æ°ã®ããã»ã¹ãåæã«å®è¡ãããçµç«ã©ã€ã³æ¹åŒã§äœ¿çšããããšãã§ããŸããããšãã°ãããã»ã¹ãã¹ã¬ããã§ããŒã¿ãåéãããšã1ã€ã®ããã»ã¹ãããŒã¿ããã£ã«ã¿ãªã³ã°ãã1ã€ã®ããã»ã¹ãããŒã¿ãããŒã¿ããŒã¹ãšç §åããŸãããããã®åã·ããªãªã¯ãã«ãã¹ã¬ããã®äžè¬çãªäœ¿çšæ³ã§ãããã·ã³ã°ã«ã¹ã¬ããã§å®è¡ãããŠããåæ§ã®ã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹ãå€§å¹ ã«åäžãããŸããããããªããšãã¡ãã»ãŒãžãèŠéãå¯èœæ§ãããããã§ããè€æ°ã®ã¹ã¬ããã¯ãè€æ°ã®ããã»ã¹ãåæã«å®è¡ãããçµç«ã©ã€ã³æ¹åŒã§äœ¿çšããããšãã§ããŸããããšãã°ãããã»ã¹ãã¹ã¬ããã§ããŒã¿ãåéãããšã1ã€ã®ããã»ã¹ãããŒã¿ããã£ã«ã¿ãªã³ã°ãã1ã€ã®ããã»ã¹ãããŒã¿ãããŒã¿ããŒã¹ãšç §åããŸãããããã®åã·ããªãªã¯ãã«ãã¹ã¬ããã®äžè¬çãªäœ¿çšæ³ã§ãããã·ã³ã°ã«ã¹ã¬ããã§å®è¡ãããŠããåæ§ã®ã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹ãå€§å¹ ã«åäžãããŸãã
åå¿è ã®ããã°ã©ããŒãæåã«ã¹ã¬ããåãåŠã¶ãšãã圌ãã¯ããã°ã©ã ã§ã¹ã¬ããåã䜿çšããå¯èœæ§ã«é äºãããå¯èœæ§ããããŸãã圌ãã¯å®éã«ã¹ã¬ããããããŒã«ãªããããããŸããã 詳ãã説æãããŠãã ããã
1æ¥ç®ïŒããã°ã©ããŒã¯ãã¹ã¬ãããçæã§ããããšãåŠã³ãããã°ã©ã ã§1ã€ã®æ°ããã¹ã¬ããCoolïŒã®äœæãéå§ããŸã ã
2æ¥ç®ïŒããã°ã©ããŒã¯ããããã°ã©ã ã®äžéšã§ä»ã®ã¹ã¬ãããçæããããšã§ããããããã«å¹ççã«ããããšãã§ããŸãïŒããšèšããŸãã
3æ¥ç®ïŒPïŒããããã¹ã¬ããå ã§ã¹ã¬ããããã©ãŒã¯ããããšãã§ããæ¬åœã«å¹çãåäžããŸã!!ã
4æ¥ç®ïŒPïŒãå¥åŠãªçµæãåºãŠããããã§ãããããã¯åé¡ãããŸãããä»ã¯ç¡èŠããŸããã
5æ¥ç®ïŒãããŒããwidgetX倿°ã«å€ãããå ŽåããããŸããããŸã£ããèšå®ãããŠããªãããã«èŠããå ŽåããããŸããã³ã³ãã¥ãŒã¿ãŒãæ©èœããŠããªãããããããã¬ãŒãå®è¡ããã ãã§ããã
9æ¥ç®ïŒããã®ããã£ããïŒãã匷ãèšèªïŒããã°ã©ã ã¯ãã¡ãã¡ã§ãžã£ã³ãããŠããŸã!!äœãèµ·ãã£ãŠããã®ãçè§£ã§ããŸããïŒã
2é±ç®ïŒæã ãããã°ã©ã ã¯ãã ããã«åº§ã£ãŠããŸã£ããäœãããŸããïŒãã«ãïŒïŒïŒïŒïŒ
ããªãã¿ã§ããïŒãã«ãã¹ã¬ããããã°ã©ã ãåããŠèšèšããããšããã»ãšãã©ã®äººã¯ãã¹ã¬ããã®èšèšç¥èãè±å¯ã§ãã£ãŠãããããããããã®æ¯æ¥ã®ç®æ¡æžãã®å°ãªããšã1ã€ãŸãã¯2ã€ãçµéšããããšããããŸããã¹ã¬ããåãæªãããšã ãšã»ã®ããããŠããããã§ã¯ãããŸãããããã°ã©ã ã§ã¹ã¬ããåã®å¹çãäžããããã»ã¹ã§ã¯ãéåžžã«æ³šæããŠãã ããã ã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ãšã¯ç°ãªããåæã«å€ãã®ããã»ã¹ãåŠçããŠãããããè€æ°ã®åŸå±å€æ°ãæã€è€æ°ã®ããã»ã¹ã远跡ããã®ã¯éåžžã«é£ããå ŽåããããŸãããžã£ã°ãªã³ã°ãšåãããã«ãã«ãã¹ã¬ãããèããŠãã ãããæã§1ã€ã®ããŒã«ããžã£ã°ãªã³ã°ããã®ã¯ïŒéå±ã§ã¯ãããŸããïŒããªãç°¡åã§ãããã ãããããã®ããŒã«ã®ãã¡2ã€ã空äžã«çœ®ãããã«ææŠãããå Žåããã®äœæ¥ã¯å°ãé£ãããªããŸãã3ã4ãããã³5ã®å ŽåãããŒã«ã¯æ¬¡ç¬¬ã«é£ãããªããŸããããŒã«ã®æ°ãå¢ãããšãå®éã«ããŒã«ãèœãšãå¯èœæ§ãé«ããªããŸãã äžåºŠã«ããããã®ããŒã«ããžã£ã°ãªã³ã°ããã«ã¯ãç¥èãã¹ãã«ãæ£ç¢ºãªã¿ã€ãã³ã°ãå¿ èŠã§ãããã«ãã¹ã¬ãããããã§ãã
å³1-ãã«ãã¹ã¬ããã¯ãžã£ã°ãªã³ã°ããã»ã¹ã®ãããªãã®ã§ã
ããã°ã©ã å ã®ãã¹ãŠã®ããã»ã¹ãçžäºã«æä»çã§ããå Žåãã€ãŸããããã»ã¹ãä»ã®ããã»ã¹ã«ãŸã£ããäŸåããŠããªãå Žåãè€æ°ã®ã¹ã¬ããåã¯éåžžã«ç°¡åã§ãåé¡ã¯ã»ãšãã©çºçããŸãããåããã»ã¹ã¯ãä»ã®ããã»ã¹ã«ç ©ããããããšãªããç¬èªã®ããããŒã³ãŒã¹ã§å®è¡ãããŸãããã ããè€æ°ã®ããã»ã¹ãä»ã®ããã»ã¹ã«ãã£ãŠäœ¿çšãããŠããã¡ã¢ãªã®èªã¿åããŸãã¯æžã蟌ã¿ãè¡ãå¿ èŠãããå Žåãåé¡ãçºçããå¯èœæ§ããããŸããããšãã°ãããã»ã¹ïŒ1ãšããã»ã¹ïŒ2ã®2ã€ã®ããã»ã¹ããããšããŸããäž¡æ¹ã®ããã»ã¹ã倿°Xãå ±æããŸããã¹ã¬ããããã»ã¹ïŒ1ãæåã«å€5ã®å€æ°Xãæžã蟌ã¿ãã¹ã¬ããããã»ã¹ïŒ2ãæ¬¡ã«å€-3ã®å€æ°Xãæžã蟌ãå ŽåãXã®æçµå€ã¯-3ã§ãããã ããããã»ã¹ïŒ2ãæåã«å€-3ã®å€æ°Xãæžã蟌ã¿ã次ã«ããã»ã¹ïŒ1ãå€5ã®å€æ°Xãæžã蟌ãå ŽåãXã®æçµå€ã¯5ã§ããXãèšå®ã§ããããã»ã¹ãããã»ã¹ïŒ1ãŸãã¯ããã»ã¹ïŒ2ã®ç¥èãæã£ãŠããªãå ŽåãXã¯ãæåã«Xã«å°éããã¹ã¬ããã«å¿ããŠç°ãªãæçµå€ã«ãªãå¯èœæ§ããããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ããã¹ãŠãé çªã«ç¶ãããããããçºçããå¯èœæ§ã¯ãããŸãããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ã䞊åã«å®è¡ãããŠããããã»ã¹ããªããããXã¯åžžã«æåã«ã¡ãœããïŒ1ã«ãã£ãŠèšå®ããïŒæåã«åŒã³åºãããå ŽåïŒã次ã«ã¡ãœããïŒ2ã«ãã£ãŠèšå®ãããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ããXã¯ãã©ã®ã¹ã¬ãããæåã«Xã«å°éãããã«ãã£ãŠãæçµçã«ç°ãªãæçµå€ã«ãªãå¯èœæ§ããããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ããã¹ãŠãé çªã«ç¶ãããããããçºçããå¯èœæ§ã¯ãããŸãããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ã䞊åã«å®è¡ãããŠããããã»ã¹ããªããããXã¯åžžã«æåã«ã¡ãœããïŒ1ã«ãã£ãŠèšå®ããïŒæåã«åŒã³åºãããå ŽåïŒã次ã«ã¡ãœããïŒ2ã«ãã£ãŠèšå®ãããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ããXã¯ãã©ã®ã¹ã¬ãããæåã«Xã«å°éãããã«ãã£ãŠãæçµçã«ç°ãªãæçµå€ã«ãªãå¯èœæ§ããããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ããã¹ãŠãé çªã«ç¶ãããããããçºçããå¯èœæ§ã¯ãããŸãããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ã䞊åã«å®è¡ãããŠããããã»ã¹ããªããããXã¯åžžã«æåã«ã¡ãœããïŒ1ã«ãã£ãŠèšå®ããïŒæåã«åŒã³åºãããå ŽåïŒã次ã«ã¡ãœããïŒ2ã«ãã£ãŠèšå®ãããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ãããã¹ãŠãé çªã«ç¶ãããããããçºçããå¯èœæ§ã¯ãããŸãããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ã䞊åã«å®è¡ãããŠããããã»ã¹ããªããããXã¯åžžã«æåã«ã¡ãœããïŒ1ã«ãã£ãŠèšå®ããïŒæåã«åŒã³åºãããå ŽåïŒã次ã«ã¡ãœããïŒ2ã«ãã£ãŠèšå®ãããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ãããã¹ãŠãé çªã«ç¶ãããããããçºçããå¯èœæ§ã¯ãããŸãããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã§ã¯ã䞊åã«å®è¡ãããŠããããã»ã¹ããªããããXã¯åžžã«æåã«ã¡ãœããïŒ1ã«ãã£ãŠèšå®ããïŒæåã«åŒã³åºãããå ŽåïŒã次ã«ã¡ãœããïŒ2ã«ãã£ãŠèšå®ãããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ããïŒæåã«åŒã³åºãããå ŽåïŒæ¬¡ã«ãã¡ãœããïŒ2ã§èšå®ããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ããïŒæåã«åŒã³åºãããå ŽåïŒæ¬¡ã«ãã¡ãœããïŒ2ã§èšå®ããŸããã·ã³ã°ã«ã¹ã¬ããããã°ã©ã ã«ã¯é©ãã¯ãããŸãããããã¯ã¹ããããã€ã¹ãããã§ãããã«ãã¹ã¬ããããã°ã©ã ã䜿çšãããšã2ã€ã®ã¹ã¬ãããåæã«ã³ãŒããå ¥åããçµæã«å€§æ··ä¹±ãããããå¯èœæ§ããããŸããã¹ã¬ããã®åé¡ã¯ãåæã«å®è¡ãããŠããå¥ã®ã¹ã¬ãããåãã³ãŒããå ¥åããŠå ±æããŒã¿ãæäœã§ããããã«ããªãããå ±æã¡ã¢ãªã«ã¢ã¯ã»ã¹ãã1ã€ã®ã¹ã¬ãããå¶åŸ¡ããäœããã®æ¹æ³ãå¿ èŠãªããšã§ãã
3ã€ã®ããŒã«ããžã£ã°ãªã³ã°ãããã³ã«ã空äžã®ããŒã«ããèªç¶ã®ç°åžžã«ãã£ãŠããã§ã«å³æã«åº§ã£ãŠããããŒã«ãæãããããŸã§ã峿ã«å°éããããšã決ããŠèš±ãããªãã£ããšæ³åããŠã¿ãŠãã ãããå°å¹Žããžã£ã°ãªã³ã°ã¯ãã£ãšç°¡åã§ãããïŒãããã¹ã¬ããã»ãŒãã®ãã¹ãŠã§ããç§ãã¡ã®ããã°ã©ã ã§ã¯ãããäžæ¹ã®ã¹ã¬ãããããžãã¹ãçµäºããŠããéãäžæ¹ã®ã¹ã¬ãããã³ãŒããããã¯å ã§åŸ æ©ãããŸããã¹ã¬ããã®ãããã¯ãŸãã¯ã¹ã¬ããã®åæãšåŒã°ãããã®ã¢ã¯ãã£ããã£ã«ãããããã°ã©ã å ã§å®è¡ãããåæã¹ã¬ããã®ã¿ã€ãã³ã°ãå¶åŸ¡ã§ããŸããCïŒã§ã¯ãã¡ã¢ãªã®ç¹å®ã®éšåïŒéåžžã¯ãªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ïŒãããã¯ãããªããžã§ã¯ãã䜿çšããŠå¥ã®ã¹ã¬ãããå®äºãããŸã§ãã¹ã¬ããããã®ãªããžã§ã¯ãã®ã¡ã¢ãªã®ã³ãŒããå ¥åã§ããªãããã«ããŸããä»ã§ã¯ããããã³ãŒãäŸãæžæããŠããã®ã§ãããã«è¡ããŸãã
2ã¹ã¬ããã®ã·ããªãªãèŠãŠã¿ãŸãããããã®äŸã§ã¯ãCïŒã§ã¹ã¬ãã1ãšã¹ã¬ãã2ã®2ã€ã®ã¹ã¬ãããäœæããŸããã©ã¡ãããç¬èªã®whileã«ãŒãã§å®è¡ãããŸããã¹ã¬ããã¯äœã®åœ¹ã«ãç«ã¡ãŸãããã©ã®ã¹ã¬ããã«å±ããŠãããã瀺ãã¡ãã»ãŒãžãåºåããã ãã§ãã_threadOutputãšåŒã°ããå ±æã¡ã¢ãªã¯ã©ã¹ã¡ã³ããŒãå©çšããŸãã_threadOutputã«ã¯ãå®è¡äžã®ã¹ã¬ããã«åºã¥ããŠã¡ãã»ãŒãžãå²ãåœãŠãããŸãããªã¹ãïŒ1ã¯ãããããDisplayThread1ãšDisplayThread2ã«å«ãŸãã2ã€ã®ã¹ã¬ããã瀺ããŠããŸãã
ãªã¹ã1-ã¡ã¢ãªå ã§å ±éã®å€æ°ãå ±æãã2ã€ã®ã¹ã¬ãããäœæãã
// shared memory variable between the two threads
// used to indicate which thread we are in
private string _threadOutput = "";
/// <summary>
/// Thread 1: Loop continuously,
/// Thread 1: Displays that we are in thread 1
/// </summary>
void DisplayThread1()
{
while (_stopThreads == false)
{
Console.WriteLine("Display Thread 1");
// Assign the shared memory to a message about thread #1
_threadOutput = "Hello Thread1";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user what thread we are in thread #1, and display shared memory
Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
}
}
/// <summary>
/// Thread 2: Loop continuously,
/// Thread 2: Displays that we are in thread 2
/// </summary>
void DisplayThread2()
{
while (_stopThreads == false)
{
Console.WriteLine("Display Thread 2");
// Assign the shared memory to a message about thread #2
_threadOutput = "Hello Thread2";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user we are in thread #2
Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
}
}
Class1()
{
// construct two threads for our demonstration;
Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
Thread thread2 = new Thread(new ThreadStart(DisplayThread2));
// start them
thread1.Start();
thread2.Start();
}
ãã®ã³ãŒãã®çµæãå³2ã«ç€ºããŸããçµæãæ³šææ·±ãèŠãŠãã ãããããã°ã©ã ãé©ãã¹ãåºåãæäŸããããšã«æ°ä»ãã§ãããïŒãããã·ã³ã°ã«ã¹ã¬ããã®èãæ¹ããèŠãå ŽåïŒã_threadOutputãããããå±ããã¹ã¬ããã«å¯Ÿå¿ããçªå·ã®æååã«æç¢ºã«å²ãåœãŠãŸããããã³ã³ãœãŒã«ã§ã¯æ¬¡ã®ããã«è¡šç€ºãããŸãã
å³2-2ã¹ã¬ããã®äŸããã®ç°åžžãªåºåã
ç§ãã¡ã®ã³ãŒãããæ¬¡ã®ããšãæåŸ ãããŸãã
ã¹ã¬ãã1ã®åºå->ãããŒã¹ã¬ãã1ãšã¹ã¬ãã2ã®åºå->ãããŒã¹ã¬ãã2ã§ãããã»ãšãã©ã®å Žåãçµæã¯å®å šã«äºæž¬ã§ããŸããã
ã¹ã¬ãã2ã®åºå->ãããŒã¹ã¬ãã1ãšã¹ã¬ãã1ã®åºå->ãããŒã¹ã¬ãã2ã衚瀺ãããããšããããŸããã¹ã¬ããã®åºåãã³ãŒããšäžèŽããŸãããã³ãŒããèŠãŠããããç®ã§è¿œã£ãŠããŸããã_threadOutput = "Hello Thread 2"ãSleepãWrite "Thread 2-> Hello Thread 2"ã§ããããã®ã·ãŒã±ã³ã¹ã§å¿ ãããæçµçµæãåŸããããšã¯éããŸããã
ãã®ãããªãã«ãã¹ã¬ããããã°ã©ã ã§ã¯ãçè«çã«ã¯ã³ãŒãã2ã€ã®ã¡ãœããDisplayThread1ãšDisplayThread2ãåæã«å®è¡ããŠããããã§ããåã¡ãœããã¯å€æ°_threadOutputãå ±æããŸãããããã£ãŠã_threadOutputã«ã¯ã¹ã¬ããïŒ1ã§å€ "Hello Thread1"ãå²ãåœãŠããã2è¡åŸã«ã³ã³ãœãŒã«ã«_threadOutputã衚瀺ãããŸãããã¹ã¬ããïŒ1ããããå²ãåœãŠãŠè¡šç€ºããæéã®éã®ã©ããã§ãã¹ã¬ããïŒ2ã_threadOutputãå²ãåœãŠãå¯èœæ§ããããŸããå€ãHelloThread2ãããããã®å¥åŠãªçµæãçºçããå¯èœæ§ãããã ãã§ãªããå³2ã«ç€ºãåºåã«èŠãããããã«ãéåžžã«é »ç¹ã«çºçããŸãããã®çã¿ã䌎ãã¹ã¬ããã®åé¡ã¯ãç«¶åç¶æ ãšããŠç¥ãããã¹ã¬ããããã°ã©ãã³ã°ã§éåžžã«äžè¬çãªãã°ã§ãã ãã®äŸã¯ãããç¥ãããŠããã¹ã¬ããã®åé¡ã®éåžžã«åçŽãªäŸã§ãããã®åé¡ã¯ãåç §ãããŠãã倿°ãã¹ã¬ããã»ãŒãã§ãªã倿°ãæãã³ã¬ã¯ã·ã§ã³ãªã©ãä»ããŠãããã°ã©ããŒããã¯ããã«éæ¥çã«é ãããŠããå¯èœæ§ããããŸããå³2ã§ã¯çç¶ã¯é²éªšã§ãããç«¶åç¶æ ã¯éåžžã«ãŸãã«ããçŸããã1åã«1åã1æéã«1åããŸãã¯3æ¥åŸã«æç¶çã«çŸããå¯èœæ§ããããŸããã¬ãŒã¹ã¯ããã®é »åºŠãäœããåçŸãéåžžã«é£ãããããããããããã°ã©ããŒã«ãšã£ãŠææªã®æªå€¢ã§ãã
ç«¶åç¶æ ãåé¿ããæåã®æ¹æ³ã¯ãã¹ã¬ããã»ãŒããªã³ãŒããäœæããããšã§ããã³ãŒããã¹ã¬ããã»ãŒãã§ããå Žåãããã€ãã®åä»ãªã¹ã¬ããã®åé¡ãçºçããã®ãé²ãããšãã§ããŸããã¹ã¬ããã»ãŒããªã³ãŒããæžãããã®ããã€ãã®é²åŸ¡çããããŸãã1ã€ã¯ãã¡ã¢ãªã®å ±æãã§ããã ãå°ãªãããããšã§ããã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããããã1ã€ã®ã¹ã¬ããã§å®è¡ãããæ¬¡ã«åãã¯ã©ã¹ã®å¥ã®ã€ã³ã¹ã¿ã³ã¹ãäœæãããããå¥ã®ã¹ã¬ããã§å®è¡ãããå Žåãéç倿°ãå«ãŸããŠããªãéããã¯ã©ã¹ã¯ã¹ã¬ããã»ãŒãã§ãã ã2ã€ã®ã¯ã©ã¹ã¯ãããããç¬èªã®ãã£ãŒã«ãçšã«ç¬èªã®ã¡ã¢ãªãäœæãããããå ±æã¡ã¢ãªã¯ãããŸãããã¯ã©ã¹ã«éç倿°ãããå ŽåããŸãã¯ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãä»ã®è€æ°ã®ã¹ã¬ããã«ãã£ãŠå ±æãããŠããå Žåã¯ãä»ã®ã¯ã©ã¹ããã®å€æ°ã®äœ¿çšãå®äºãããŸã§ãäžæ¹ã®ã¹ã¬ããããã®å€æ°ã®ã¡ã¢ãªã䜿çšã§ããªãããã«ããæ¹æ³ãèŠã€ããå¿ èŠããããŸããããã¯ã CïŒã䜿çšãããšãMonitorã¯ã©ã¹ãŸãã¯lock {}æ§é ã®ããããã䜿çšããŠã³ãŒããããã¯ã§ããŸããïŒlockæ§é ã¯ãå®éã«ã¯try-finallyãããã¯ãä»ããŠMonitorã¯ã©ã¹ãå éšçã«å®è£ ããŸãããããã°ã©ããŒãããããã®è©³çްãé ããŸãïŒããªã¹ã1ã®äŸã§ã¯ãå ±æ_threadOutput倿°ãèšå®ããæç¹ãããã³ã³ãœãŒã«ãžã®å®éã®åºåãŸã§ãã³ãŒãã®ã»ã¯ã·ã§ã³ãããã¯ã§ããŸããã³ãŒãã®ã¯ãªãã£ã«ã«ã»ã¯ã·ã§ã³ãäž¡æ¹ã®ã¹ã¬ããã§ããã¯ããŠãã©ã¡ããäžæ¹ã«ç«¶åãçºçããªãããã«ããŸããã¡ãœããå ãããã¯ããæãéããŠæ±ãæ¹æ³ã¯ããã®ãã€ã³ã¿ãŒãããã¯ããããšã§ãããã®ãã€ã³ã¿ãããã¯ãããšãã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹å šäœãããã¯ããããããããã¯å ã§ã¯ã©ã¹ã®ãã£ãŒã«ãã倿Žããããšããã¹ã¬ããã¯ãã¹ãŠãããã¯ãããŸãããããããã³ã°ãšã¯ã倿°ã倿ŽããããšããŠããã¹ã¬ããããããã¯ãããã¹ã¬ããã§ããã¯ãè§£é€ããããŸã§åŸ æ©ããããšãæå³ããŸããã¹ã¬ããã¯ãlock {}æ§é ã®æåŸã®ãã©ã±ããã«å°éãããšãããã¯ããè§£æŸãããŸãã
ãªã¹ã2-2ã€ã®ã¹ã¬ãããããã¯ããŠåæãã
/// <summary>
/// Thread 1, Displays that we are in thread 1 (locked)
/// </summary>
void DisplayThread1()
{
while (_stopThreads == false)
{
// lock on the current instance of the class for thread #1
lock (this)
{
Console.WriteLine("Display Thread 1");
_threadOutput = "Hello Thread1";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user what thread we are in thread #1
Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
}// lock released for thread #1 here
}
}
/// <summary>
/// Thread 1, Displays that we are in thread 1 (locked)
/// </summary>
void DisplayThread2()
{
while (_stopThreads == false)
{
// lock on the current instance of the class for thread #2
lock (this)
{
Console.WriteLine("Display Thread 2");
_threadOutput = "Hello Thread2";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user what thread we are in thread #1
Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
} // lock released for thread #2 here
}
}
2ã€ã®ã¹ã¬ãããããã¯ããçµæãå³3ã«ç€ºããŸãããã¹ãŠã®ã¹ã¬ããåºåãé©åã«åæãããŠããããšã«æ³šæããŠãã ãããã¹ã¬ãã1ã®åºå->ãããŒã¹ã¬ãã1ãšã¹ã¬ãã2ã®åºå->ãããŒã¹ã¬ãã2ãšããçµæãåžžã«è¡šç€ºãããŸãããã ããã¹ã¬ããã®ããã¯ã«ã¯ã³ã¹ãããããããšã«æ³šæããŠãã ãããã¹ã¬ãããããã¯ãããšãããã¯ãè§£é€ããããŸã§ä»ã®ã¹ã¬ããã匷å¶çã«åŸ æ©ãããŸããæ¬è³ªçã«ãä»ã®ã¹ã¬ãããå ±æã¡ã¢ãªã®äœ¿çšãåŸ æ©ããŠããéãæåã®ã¹ã¬ããã¯ããã°ã©ã ã§äœãããŠããªããããããã°ã©ã ã®é床ãäœäžããŸããããããã£ãŠãããã¯ã¯æ éã«äœ¿çšããå¿ èŠããããŸããå ±æã¡ã¢ãªã«é¢äžããŠããªãå Žåã¯ãã³ãŒãå ã«ãããã¹ãŠã®ã¡ãœãããããã¯ããã ãã§ã¯ãããŸããããŸããããã¯ã䜿çšãããšãã¯æ³šæããŠãã ãããã¹ã¬ããïŒ1ãã¹ã¬ããïŒ2ã«ãã£ãŠããã¯ãè§£æŸãããã®ãåŸ ã£ãŠããç¶æ³ã«é¥ããããªãããã§ããã¹ã¬ããïŒ2ã¯ãã¹ã¬ããïŒ1ã«ãã£ãŠããã¯ãè§£æŸãããã®ãåŸ ã£ãŠããŸãããã®ç¶æ³ãçºçãããšãäž¡æ¹ã®ã¹ã¬ããããããã¯ãããããã°ã©ã ãããªãŒãºããããã«èŠããŸãããã®ç¶æ³ã¯ãšããŠç¥ãããŠããŸããããããã¯ãçºçããããã°ã©ã å ã®äºæž¬ã§ããªãæç¶çãªæéã«ãçºçããå¯èœæ§ããããããç«¶åç¶æ ãšã»ãŒåããããæªãç¶æ³ã§ãã
å³3-ããã¯ã䜿çšãããã¥ã¢ã«ã¹ã¬ããããã°ã©ã ã®åæ
.NETã¯ãã¹ã¬ããã®å¶åŸ¡ã«åœ¹ç«ã€å€ãã®ã¡ã«ããºã ãæäŸããŸããå¥ã®ã¹ã¬ãããå ±æã¡ã¢ãªã®äžéšãåŠçããŠããéãã¹ã¬ããããããã¯ãããŸãŸã«ããå¥ã®æ¹æ³ã¯ãAutoResetEventã䜿çšããããšã§ããAutoResetEventã¯ã©ã¹ã«ã¯ãSetãšWaitOneã®2ã€ã®ã¡ãœããããããŸãããããã®2ã€ã®æ¹æ³ã¯ãã¹ã¬ããã®ãããã¯ãå¶åŸ¡ããããã«äžç·ã«äœ¿çšã§ããŸããAutoResetEventãfalseã§åæåããããšãããã°ã©ã ã¯ãAutoResetEventã§Setã¡ãœãããåŒã³åºããããŸã§ãWaitOneãåŒã³åºãã³ãŒãè¡ã§åæ¢ããŸããAutoResetEventã§Setã¡ãœãããå®è¡ããããšãã¹ã¬ããã®ãããã¯ãè§£é€ãããWaitOneãè¶ ããŠç¶è¡ã§ããããã«ãªããŸããæ¬¡åWaitOneãåŒã³åºããããšãèªåçã«ãªã»ããããããããããã°ã©ã ã¯ãWaitOneã¡ãœãããå®è¡ãããŠããã³ãŒãè¡ã§åã³åŸ æ©ïŒãããã¯ïŒããŸãããã®ã忢ãšããªã¬ãŒãã䜿çšã§ããŸã SetãåŒã³åºããŠãå¥ã®ã¹ã¬ããããããã¯ãããã¹ã¬ãããè§£æŸããæºåãã§ãããŸã§ãããã¹ã¬ããããããã¯ããã¡ã«ããºã ããªã¹ã3ã¯ãAutoResetEventã䜿çšããŠããããã¯ãããã¹ã¬ãããåŸ æ©ãããããã¯ãããŠããªãã¹ã¬ãããå®è¡ãããŠã³ã³ãœãŒã«ã«_threadOutputã衚瀺ããŠããéã«ãäºãã«ãããã¯ããåã2ã€ã®ã¹ã¬ããã瀺ããŠããŸããæåã«ã_blockThread1ã¯falseãéç¥ããããã«åæåããã_blockThread2ã¯trueãéç¥ããããã«åæåãããŸããããã¯ã_blockThread2ãDisplayThread_2ã®ã«ãŒããæåã«ééãããšãã«ãWaitOneåŒã³åºããç¶è¡ã§ããããã«ãªãäžæ¹ã§ã_blockThread1ã¯DisplayThread_1ã®WaitOneåŒã³åºãããããã¯ããããšãæå³ããŸãã_blockThread2ãã¹ã¬ãã2ã®ã«ãŒãã®çµããã«éãããšãã¹ã¬ãã1ããããã¯ããè§£æŸããããã«SetãåŒã³åºããŠ_blockThread1ã«ä¿¡å·ãéããŸããæ¬¡ã«ãã¹ã¬ãã2ã¯ãã¹ã¬ãã1ãã«ãŒãã®çµããã«å°éããŠ_blockThread2ã§SetãåŒã³åºããŸã§ãWaitOneåŒã³åºãã§åŸ æ©ããŸããã¹ã¬ãã1ã§åŒã³åºãããã»ããã¯ã¹ã¬ãã2ã®ãããã¯ãè§£æŸããããã»ã¹ãåéãããŸããäž¡æ¹ã®AutoResetEventsïŒ_blockThread1ãš_blockThread2ïŒãæåã«falseãéç¥ããããã«èšå®ããå Žåãäž¡æ¹ã®ã¹ã¬ãããäºãã«ããªã¬ãŒããæ©äŒãªãã«ã«ãŒãã®é²è¡ãåŸ æ©ãããããããã¯ã
ãªã¹ã3-ãããã¯ãAutoResetEventã§ã¹ã¬ããããããã¯ãã
AutoResetEvent _blockThread1 = new AutoResetEvent(false);
AutoResetEvent _blockThread2 = new AutoResetEvent(true);
/// <summary>
/// Thread 1, Displays that we are in thread 1
/// </summary>
void DisplayThread_1()
{
while (_stopThreads == false)
{
// block thread 1 while the thread 2 is executing
_blockThread1.WaitOne();
// Set was called to free the block on thread 1, continue executing the code
Console.WriteLine("Display Thread 1");
_threadOutput = "Hello Thread 1";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user what thread we are in thread #1
Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
// finished executing the code in thread 1, so unblock thread 2
_blockThread2.Set();
}
}
/// <summary>
/// Thread 2, Displays that we are in thread 2
/// </summary>
void DisplayThread_2()
{
while (_stopThreads == false)
{
// block thread 2 while thread 1 is executing
_blockThread2.WaitOne();
// Set was called to free the block on thread 2, continue executing the code
Console.WriteLine("Display Thread 2");
_threadOutput = "Hello Thread 2";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user we are in thread #2
Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
// finished executing the code in thread 2, so unblock thread 1
_blockThread1.Set();
}
}
ãªã¹ã3ã§çæãããåºåã¯ãå³3ã«ç€ºãããã¯ã³ãŒããšåãåºåã§ãããAutoResetEventã䜿çšãããšãçŸåšã®ã¹ã¬ãããåŠçãå®äºãããšãã«ãããã¹ã¬ãããå¥ã®ã¹ã¬ããã«éç¥ããæ¹æ³ãããåçã«å¶åŸ¡ã§ããŸãã
ãã€ã¯ãããã»ããµã®é床ã®çè«çéçãæŒãäžããŠããããããã¯ãããžã¯ãã³ã³ãã¥ãŒã¿ãã¯ãããžã®é床ãšããã©ãŒãã³ã¹ãæé©åã§ããæ°ããæ¹æ³ãèŠã€ããå¿ èŠããããŸãããã«ãããã»ããµãããã®çºæãšäžŠåããã°ã©ãã³ã°ãžã®äŸµå ¥ã«ããããã«ãã¹ã¬ãããçè§£ããããšã§ãã ãŒã¢ã®æ³åã«ææŠãç¶ããããã«å¿ èŠãªå©ç¹ããããããããã®ããæè¿ã®ãã¯ãããžãŒãåŠçããããã«å¿ èŠãªãã©ãã€ã ã«åããããšãã§ããŸããCïŒãš.NETã¯ããã«ãã¹ã¬ãããšäžŠååŠçããµããŒãããæ©èœãæäŸããŸãããããã®ããŒã«ãäžæã«æŽ»çšããæ¹æ³ãçè§£ããã°ãç§ãã¡èªèº«ã®æ¥ã ã®ããã°ã©ãã³ã°æŽ»åã«ãããŠãå°æ¥ã®ãããã®ããŒããŠã§ã¢ã®çŽæã«åããããšãã§ããŸããäžæ¹ãã·ã£ãŒããªããªããã§ããã®ã§ãã¹ã¬ããã®ããªãã®ç¥èãšã³.NETå¯èœæ§ãã
ãªã³ã¯ïŒhttps://www.c-sharpcorner.com/article/introduction-to-multithreading-in-C-Sharp/