1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.story.misc;
14 import java.io.BufferedInputStream;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.Arrays;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.WriteRequest;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.service.LifecycleSupport;
30 import org.simantics.db.testing.annotation.Fails;
31 import org.simantics.db.testing.common.Tests;
32 import org.simantics.db.tests.common.Configuration;
33 import org.simantics.layer0.Layer0;
35 public class TextIndexingTest {
37 static String host = Configuration.get().host;
38 static int port = Configuration.get().port;
39 static String dataFileName = Configuration.get().textIndexingFile;
43 Resource[] relations = new Resource[128];
46 public void setUp() throws Exception {
47 // SessionFactory factory = new SessionFactory(host, port);
48 // session = factory.create();
49 session = Tests.getTestHandler().getSession();
53 public void tearDown() throws Exception {
54 LifecycleSupport support = session.getService(LifecycleSupport.class);
58 class SetUp extends WriteRequest {
61 public void perform(WriteGraph g) throws DatabaseException {
62 root = g.newResource();
63 Layer0 l0 = Layer0.getInstance(g);
64 for(int i=0;i<128;++i) {
65 relations[i] = g.newResource();
66 Resource inv = g.newResource();
67 g.claim(relations[i], l0.InverseOf, inv);
68 g.claim(relations[i], l0.SubrelationOf, l0.IsRelatedTo);
69 g.claim(inv, l0.SubrelationOf, l0.IsWeaklyRelatedTo);
75 boolean finished = false;
77 class WriteTrie extends WriteRequest {
83 public void perform(WriteGraph g) throws DatabaseException {
87 //System.out.println("T");
89 Layer0 b = Layer0.getInstance(g);
90 long startTime = System.nanoTime();
91 while( (word = text.getWord()) != null ) {
97 for(int i=0;i<word.length;++i) {
98 Resource relation = relations[word[i]];
99 Resource temp = g.getPossibleObject(cur, relation);
101 temp = g.newResource();
102 g.claim(cur, relation, temp);
107 Resource name = g.getPossibleObject(cur, b.HasName);
109 name = g.newResource();
110 g.claimValue(name, new String(word));
111 g.claim(cur, b.HasName, name);
116 if((count % 2000) == 0)
117 System.out.println(count + ", " + newWords);
119 long endTime = System.nanoTime();
120 System.out.println("End");
121 System.out.println(count + " words, " + newWords + " unique words");
122 System.out.println((endTime-startTime)*1e-6/count + " ms / word");
126 } catch (IOException e) {
138 text = new Text(new BufferedInputStream(new FileInputStream(dataFileName)));
139 session.syncRequest(new SetUp());
140 System.out.println("Start");
141 WriteTrie wt = new WriteTrie();
143 session.syncRequest(wt);
144 System.out.println("Ok");
145 } catch (FileNotFoundException e) {
146 // TODO Auto-generated catch block
148 } catch (DatabaseException e) {
153 final static byte[] PEND = new byte[0];
157 byte[] temp = new byte[1024];
159 public Text(InputStream s) {
163 byte[] getWord() throws IOException {
165 while( !Character.isLetter(b = s.read()) && b != -1 && b != '\n');
171 while( Character.isLetter(b) ) {
172 temp[i] = (byte)Character.toLowerCase(b);
176 return Arrays.copyOf(temp, i);