130242014013+杨俊杰+第3次实验
一、实验目的
1.理解不同体系结构风格的具体内涵。
2.学习体系结构风格的具体实践。
二、实验环境
硬件: (依据具体情况填写)
软件:Java或任何一种自己熟悉的语言
三、实验内容
“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。
尝试用不同的策略实现这个系统。选择2-3种体系结构风格来实现。
四、实验步骤:
KWIC 的管道过滤器体系结构风格示意图如下所示:
1 KWIC 基于管道过滤器风格的 JAVA 语言实现 2 3 //Main类 package kwic_pipe; import java.io.File; import java.util.Scanner; 4 public class Main { public static void main(String[] args) { File infile = new File("e:\\mykwic_in.txt"); 5 File outfile = new File("e:\\mykwic_out.txt"); 6 Scanner inputfile; Scanner outputfile; try{ inputfile = new Scanner(infile); outputfile = new Scanner(outfile); // 定义三个管道 7 Pipe pipe1 = new Pipe(); 8 Pipe pipe2 = new Pipe(); 9 Pipe pipe3 = new Pipe(); 10 // 定义四种过滤器 11 Input input = new Input(infile, pipe1); Shift shift = new Shift(pipe1, pipe2); 12 Output output = new Output(pipe3, outfile); 13 // 启动四种过滤器的线程 14 input.transform() shift. transform (); 15 output. transform (); 16 // 直接输出结果 17 System.out.println("----- infile -----"); String str = null; while (inputfile.hasNextLine()){ str = inputfile.nextLine(); 18 System.out.println(str); 19 } 20 System.out.println("input end"); 21 Thread.sleep(3000); 22 System.out.println("----- outfile -----"); while (outputfile.hasNextLine()){ System.out.println(str); 23 } inputfile.close(); outputfile.close(); 24 } catch (Exception e){ e.getMessage(); 25 } 26 } 27 } 28 29 //Filter类 package kwic_pipe; import java.io.IOException; public abstract class Filter { 30 //定义输入管道 31 protected Pipe input; //定义输出管道 protected Pipe output; private boolean isStart = false; 32 33 Filter(Pipe input, Pipe output){ this.input = input; this.output = output; 34 } 35 36 // 防止多次调用,调用之后线程开始执行 public void start(){ if(!isStart){ isStart = true; 37 Thread thread = new Thread(); thread.start(); 38 } 39 } 40 //线程的 run 方法 41 public void run(){ try{ this.transform(); } catch (IOException e){ e.getMessage(); 42 } 43 } 44 //将输入数据转换为所需数据并写入输出管道 45 //由子类实现抽象方法 46 protected abstract void transform()throws IOException; 47 } 48 Pipe类 49 package kwic_pipe; 50 import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; import java.io.PrintWriter; import java.util.Scanner; 51 52 public class Pipe { 53 //输入管道 54 private Scanner pipereader; 55 //输出管道 56 private PrintWriter pipewriter; public Pipe(){ 57 PipedWriter pw = new PipedWriter(); 58 PipedReader pr = new PipedReader(); 59 try{ 60 pw.connect(pr); 61 } catch (IOException e){ e.getMessage(); 62 } 63 pipewriter = new PrintWriter(pw); 64 pipereader = new Scanner(pr); 65 } 66 //读入一行数据到管道 67 //@return 读入的数据 68 public String readerLine() throws IOException{ return pipereader.nextLine(); 69 } 70 71 //从管道输出一行数据 72 public void writerLine(String strline) throws IOException{ pipewriter.println(strline); 73 } 74 //将读管道关闭,调用该方法后,不能再从管道中读数据 75 //如不能关闭则抛出异 76 public void closeReader() throws IOException{ pipereader.close(); 77 } 78 79 //先刷新数据,在将写管道关闭,调用该方法后,不能向管道中写数据 80 //如不能关闭则抛出异常 81 public void closeWriter() throws IOException{ pipewriter.flush(); pipewriter.close(); 82 } 83 } Alphabetizer类 package kwic_pipe; 84 import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Alphabetizer extends Filter{ 85 private ArrayList<String> al = new ArrayList<String>(); 86 87 Alphabetizer(Pipe input, Pipe output){ super(input, output); 88 } 89 90 //对读入的数据进行排序 91 protected void transform() throws IOException { 92 String templine = null; 93 94 //读入数据 95 while((templine = input.readerLine()) != null){ al.add(templine); 96 } 97 98 //按字母表排序 99 Collections.sort(al); 100 101 //对排序后的数据进行输出 102 for(int i = 0; i < al.size(); i++){ output.writerLine(al.get(i)); 103 } input.closeReader(); output.closeWriter(); 104 } 105 } Shift类 package kwic_pipe; 106 107 import java.io.IOException; import java.util.ArrayList; public class Shift extends Filter{ 108 //单词的列表 109 private ArrayList<String> wordlist = new ArrayList<String>(); 110 //重组后的行的列表 111 private ArrayList<String> linelist = new ArrayList<String>(); 112 113 Shift(Pipe input, Pipe output){ super(input, output); 114 } 115 116 @Override 117 protected void transform() throws IOException { 118 String templine = ""; 119 //读数据 120 while((templine = input.readerLine()) != null){ 121 //将数据拆分为不同单词 122 this.lineSplitWord(templine); 123 //将单词重组为句子 124 this.recombination(); //输出重组结果 125 for(int i = 0; i < linelist.size(); i++){ output.writerLine(linelist.get(i)); } 126 //清空wordlist、linelist和templine 127 wordlist.clear(); linelist.clear(); templine = ""; 128 } input.closeReader(); output.closeWriter(); 129 } 130 //从一行中提取单词存入单词表中 131 private void lineSplitWord(String line){ 132 String word = ""; int i = 0; while(i < line.length()){ if(line.charAt(i) != ' '){ word += line.charAt(i); 133 } else{ wordlist.add(word); 134 } i++; 135 } 136 } 137 private void recombination(){ 138 for(int j = 0; j < wordlist.size(); j++){ String templine = ""; for (int k = wordlist.size() - 1 - j; k < wordlist.size(); k++){ templine += wordlist.get(k) + " "; 139 } for (int m = 0; m < wordlist.size() - 1 - j; m++){ if(m != wordlist.size() - j - 2){ templine += wordlist.get(m) + " "; 140 } else{ templine += wordlist.get(m); 141 } 142 } 143 linelist.add(templine); 144 } 145 } 146 } 147 Input类 package kwic_pipe; 148 import java.io.File; import java.io.IOException; import java.util.Scanner; 149 150 public class Input extends Filter{ 151 152 //输入文件的文件名 153 private File infile; Input(File file, Pipe output){ super(null, output); this.infile = file; 154 } 155 156 @Override //读取数据 157 protected void transform() throws IOException { 158 Scanner sc = new Scanner(infile); String templine = ""; while((templine = sc.nextLine()) != null){ output.writerLine(templine); 159 } output.closeWriter(); sc.close(); 160 } 161 } 162 Output类 package kwic_pipe; import java.io.File; import java.io.IOException; import java.io.PrintWriter; 163 public class Output extends Filter{ 164 //输出文件的文件名 165 private File file; Output(Pipe input, File file){ super(input, null); this.file = file; 166 } 167 //输出数据 168 protected void transform() throws IOException { 169 PrintWriter pw = new PrintWriter(file); String templine = ""; 170 while((templine = input.readerLine()) != null){ pw.write(templine); pw.write("\n"); 171 } pw.flush(); pw.close(); input.closeReader(); 172 } 173 }
实验结果:输入文件:
输出文件:
采用面向对象体系架构风格实现KWIC关键词索引系统:
1 import java.io.BufferedReader; 2 import java.util.ArrayList; 3 public class Kwic { private Input input; 4 private Output output; 5 private Alphabetize alphabetize; private Shift shift; 6 7 private ArrayList<String> kwicList= new ArrayList<String>(); 8 private ArrayList<String> kwicList2 = new ArrayList<String>(); 9 private BufferedReader buffer = null; 10 11 12 13 private String inputPath; 14 private String outputPath; 15 private LineStorage lineStorage; 16 public Kwic( Input input,Output output,Alphabetize alphabetize,Shift shift,LineStorage lineStorage) { 17 this.input=input; this.output = output; 18 this.alphabetize = alphabetize; this.shift = shift; 19 this.lineStorage=lineStorage; 20 } 21 22 public void input(){ 23 input.readString(); } public void lineStorage(){ 24 lineStorage.addLines(kwicList); } public void output(){ output.export(kwicList2); 25 } 26 public void alphabetize(){ alphabetize.SortKwic(kwicList2); } 27 public void shift() { shift.action(kwicList,kwicList2); } 28 public static void main(String[] args) { Input inputer=new Input(); LineStorage lineStorager=new LineStorage(); 29 Output outputor = new Output(); 30 Alphabetize alphabetizer = new Alphabetize(); Shift shiftor = new Shift(); 31 32 Kwic myKwic = new Kwic(inputer,outputor,alphabetizer,shiftor,lineStorager); 34 myKwic.lineStorage();
myKwic.shift(); myKwic.alphabetize(); myKwic.output(); } }