管道风格kwiclxyWord格式.docx
《管道风格kwiclxyWord格式.docx》由会员分享,可在线阅读,更多相关《管道风格kwiclxyWord格式.docx(15页珍藏版)》请在冰点文库上搜索。
2.3PipelineinKWICsystem................................................................................5
2.3KWIC系统中的传输途径................................................................................5
3Students’Assignment.............................................................................................6
3学生作业.............................................................................................6
3.1ImplementShiftFilterMechanism.................................................................6
3.1循环移位机制实现.................................................................6
3.2ImplementLineandShiftTransformMechanism..........................................6
3.2行与循环移位变换机制的实现..........................................6
3.3Answerunderstandingquestions....................................................................7
3.3回答理解问题....................................................................7
KWICImplementedwithPipeFilterArchitecturalStyle
1PipeFilterSystemsinGeneral
1一般而言的管道过滤器系统
Inapipeandfilterstyleeachcomponenthasasetofinputstreamsandasetofoutputstreams.Acomponentreadsstreamsofdataonitsinputstreams,processesthedataandwritestheresultingdataonitsoutputstreams.Hencecomponentsaretermedfilters.Theconnectorsofthisstylemergethestreamstogether,i.e.,theytransmitoutputsofonefiltertoinputsofanotherfilter.Hencetheconnectorsaretermedpipes.
在管道和过滤器风格,每个组件有一个输入流和输出流。
一个组件从输入流读取数据流,然后处理数据,并向输出流写入结果数据。
因此组件被称为过滤器。
这种风格的连接器把流合并一起,即,他们发送一个过滤器的输出到另一过滤器的输入。
因连接器此被称为管道。
Figure1:
Pipe-filterstopologyinageneralpipe-filtersystem
Amongtheimportantinvariantsofthestyleistheconditionthatfiltersmustbeindependententities:
inparticular,theyshouldnotsharestatewithotherfilters.Anotherimportantinvariantisthatfiltersdonotknowtheidentityoftheirupstreamanddownstreamfilters.Thus,filtersmaynotidentifythecomponentsontheendsoftheirpipes.
图1:
管道-过滤器系统的一般管道-过滤器拓扑结构
在这种风格中最重要的不变量就是过滤器必须是独立的实体:
特别是,他们不应该与其他过滤器共享状态。
另一个重要的不变量是过滤器不知道上游和下游的过滤器的识别方式。
因此,过滤器可能无法识别的管道两端的组件。
Commonspecializationsofthisstyleincludepipelines(seefigurebelow),whichrestrictthetopologiestolinearsequencesoffilters;
boundedpipes,whichrestricttheamountofdatathatcanresideonapipe;
andtypedpipes,whichrequirethatthedatapassedbetweentwofiltershaveawell-definedtype.
这种风格常见的特点包括管道(见下图),这限制了过滤器线性序列的拓扑;
有界管道,这限制了可以驻留在一个管的数据的数量;
和输入管,这要求通过两过滤器之间的数据有一个明确的类型。
Figure2:
Pipelinetopologyinaspecificpipe-filtersystem
Thebest-knownexamplesofpipeandfilterarchitecturesareprogramswrittenintheUNIXshell.UNIXsupportsthisstylebyprovidinganotationforconnectingcomponents(representedasUNIXprocesses)andbyprovidingrun-timemechanismsforimplementingpipes.
图2:
一个具体管道-过滤器系统的管道拓扑
最有名管道和过滤器的结构的例子是用UNIX操作系统编写的程序。
UNIX支持这种风格提供了一个连接组件的符号(代表操作过程)并为实现管道提供运行时机制。
2Architecture
PipeandFilterKWICsystemiscomposedofthefollowingcomponents:
•Inputfilter,whichreadsthecontentofaKWICinputfile,parsesit,andwritestheparsed
linestoitsoutputstream.
2架构
管道和过滤器的关键词检索系统由以下部件组成:
•输入过滤器,它读取关键词索引输入文件的内容,分析它,并将分析到的内容写到其输出流。
•CircularShifterfilterconnectedwithapipetotheoutputstreamofInputfilter.Thus,the
linesproducedbyInputfilterserveasinputforCircularShifterfilter.CircularShifterprocessestheinputlinesproducingcircularshiftsofthoselines.TheproducedshiftsarewrittentotheCircularShifter'
soutputstream.
•circularshifter过滤器通过管道连接输出流到输入流过滤器。
因此,这些行通过为circularshifter过滤器输入服务的输入流产生。
Circularshifter对这些行的处理就产生了这些行的循环移位行。
所产生的循环移位行是写入到输出circularshifter。
•AlphabetizerfilterconnectedwithapipetotheoutputstreamofCircularShifterfilter.Thus,circularshiftsproducedbyCircularShifterserveasinputforAlphabetizer.Alphabetizersortstheseshiftsalphabeticallyandwritesthemtoitsoutputstream.
Alphabetizerfilter(按字母顺序排列过滤器)通过管道连接到circularshifter过滤器的输出流。
因此由circularshifter产生的循环移位行作为Alphabetizerfilter的输入。
Alphabetizerfilter把这些循环移位行按字母顺序排序并写入到输出流
•OutputfilterconnectedwithapipetotheoutputstreamofAlphabetizer.Thus,Outputreadssortedshiftsfromitsinputstreamandwritesthemtothestandardoutput.
输出过滤器通过管道连接到Alphabetizer流的输出。
因此,Output从输入流读取排好序的行并写入到标准输出。
•MasterControl,whichmanagesfilterandpipemechanism,bycreatingapipelineofthe
abovedescribedfilters.
•主控制,通过建立以上所述的过滤器的管道序列,管理过滤器和管道机制。
Figure3:
PipelineinKWICsystem
2.1PipesinKWICsystem
PipesintheKWICsystemarerepresentedasinstancesofthePipeclass.AninstanceofthePipe
classisacompositionoftwostreams:
aninputandanoutputstream.Pipeclassconnectsthese
twostreamsinthefollowingway.Thedatathatiswrittentotheinputstreamistransmittedtotheoutputstream.Inthiswaythesedatabecomeavailableforreadingfromtheoutputstream.
图3:
关键词检索系统中的管道
2.1个管道关键词检索系统
管道在关键词检索系统的表示为管道类的实例。
管道的实例类是一个两个流的组合:
一个输入和输出流。
管类连接这些流的方式如下。
写入到输入流的数据被传输到输出流。
这样,这些数据可用于被从输出流读取。
ThePipeclassencapsulatestheinputandoutputstreamsasitsprivateinstancevariablesandprovidejustasimplepublicinterfaceforwritingandreadingdatatoapipeobject.Thus,clientssimplywritesomedatatoapipeobjectbycallingitswritemethod.Thisdatabecomesthenavailableforotherclients,whichcancallthereadmethodofthepipeobjecttoretrievethedata.
管道类封装输入、输出流是通过作为它的私有的实例变量并且仅仅提供一个向管道读取数据的一个公共接口,因此,用户在向一个管道对象写数据的时候只需要通过调用它的写方法,然后这些数据变得可以被其他客户可用,这些客户可以通过调用管道对象的读方法来检索数据。
Figure4:
Pipeclassdiagram
Therearefewimportantpropertiesofpipeobjects:
•Pipeobjectslimittheamountofdatatheycanhold.However,wheneverclientsreadthedatafromapipe,thisdataisconsidered"
consumed"
andthespacethatwasoccupiedbytheretrieveddataismadefreeagain.
图4:
管类图
管道对象有几个重要的性能:
•管对象限制可以承受的数据量。
然而,当客户端从一个管读取数据,这一数据被认为是“消费的”和被检索到的数据所占去的空间就被释放了。
•Pipeobjectsaresynchronized.Thishastwomajorconsequences.Firstly,therecanbeonlyonethreadworkingwithapipeobjectataspecificmoment,i.e,therecanbeonlyonethreadcurrentlywritingorreadingthedatafromapipeobject.Secondly,threadsreadingfromanemptypipeorwritingtoafullpipeareblockedaslongastherearenotsomedatatoreadfromthepipe,orthereisnofreespaceinthepipetowritethedata.Thus,apipeobjectmaybeseenasatypicalsharedsynchronizeddatastructureforproducer/consumermulti-threadscenarios.Thus,apipeobjectisalwayssharedbetweenaproducerandaconsumerthread,wheretheproducerthreadwritessomedatatothepipe,andtheconsumerthread"
consumes"
thatdatafromthepipe.
•管对象是同步的。
这两家主要的后果。
首先,只能有一个线程在一个特定的时刻通过一个管道对象工作,即,只能有一个线程正在从一个管道对象读或写数据。
其次,从一个空的管道读或向一个满的管道写就会受阻,只要管道没有数据可读或没有空间可写,就会阻塞。
因此,一管对象可被视为一个典型的共享数据结构的生产者/消费者多线程情况。
因此,一管对象始终是被生产者和消费者线程共享,在这里在生产者线程写入一些数据到管道,消费者线程从数据管“消费”数据。
2.2FiltersinKWICSystem
FiltersintheKWICsystemarerepresentedbyanabstractclassnamedFilter.AninstanceofFilterclassiscomposedof:
2.2关键词检索系统的过滤器
关键词检索系统的过滤器是由一个抽象类命名的过滤器。
过滤器类的实例组成是:
•Inputpipeobject,fromwhichthefilterobjectreadsthedata
•Outputpipeobject,towhichthefilterobjectwritestheprocesseddata
•Threadofcontrol.Eachfilterobjectrunsinitsownthreadandisindependentofanyotherfiltersinthesystem,i.e.,itdoesnotsharestatewithotherfiltersinthesystem.
•输入管对象,从该过滤器对象读取数据
•输出管对象,该过滤器对象写入处理过的数据
•线程控制。
每个过滤器对象运行在它自己的线程,独立于系统中任何其他过滤器,即,在系统中它与其他过滤器不共享状态。
Figure5:
Filterclassdiagram
ThecurrentimplementationoftheFilterclassprovidesalreadythebasicfilterfunctionality.Thus,clientsstarttheexecutionofthefilterobjectbyinvokingitsstartmethod.Inturn,thismethodstartsthecontrolthread,whichconsecutivelyinvokesthefilter'
stransformmethod.
图5:
过滤类图
当前实现的过滤器类已经提供了基本过滤器功能。
因此,客户通过调用其start方法开始执行的过滤器对象。
反过来,这种方法启动了控制线程,(控制线程)连续调用过滤器的转换方法。
Inthecurrentimplementationthetransformmethodisanabstractmethod,whichmeansthatsubclassesofFilterclassshouldprovideaparticularimplementationforthatspecificfilterobject.Forexample,CircularShifterfilterimplementstransformmethodinthefollowingway.Thelinesretrievedfromtheinputpipeareprocessedandcircularshiftsofthoselinesaremade.Thereaftertheproducedshiftsarewrittentotheoutputpipe.Speakingmoregenerally,subclassesofFilterclassimplementtheirtransformmethodsinthefollowingway:
thedataisretrievedfromtheinputpipe,transformed(processed)andwrittenouttotheoutputpipe.
在当前的实现中transformmethod(转换方法)是一个抽象方法,即过滤器