Author: reto
Date: Mon Jan 17 13:03:48 2011
New Revision: 1059908

URL: http://svn.apache.org/viewvc?rev=1059908&view=rev
Log:
CLEREZZA-388: first lucene based implementation

Added:
    incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/   
(with props)
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/pom.xml
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/serviceComponents.xml
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/Condition.scala
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/IndexService.scala
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/WilcardCondition.scala
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/
    
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/IndexServiceTest.scala

Propchange: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Jan 17 13:03:48 2011
@@ -0,0 +1 @@
+target

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/pom.xml?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/pom.xml
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/pom.xml
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?><project 
xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <groupId>org.apache.clerezza</groupId>
+               <artifactId>org.apache.clerezza.rdf.cris</artifactId>
+               <version>0.1-incubating-SNAPSHOT</version>
+       </parent>
+       <groupId>org.apache.clerezza</groupId>
+       <artifactId>org.apache.clerezza.rdf.cris.core</artifactId>
+       <version>0.1-incubating-SNAPSHOT</version>
+       <packaging>bundle</packaging>
+       <name>Clerezza - RDF Composite Resource Index Service Core</name>
+       <dependencies>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       
<artifactId>org.apache.clerezza.rdf.cris.ontologies</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.core</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.utils</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       
<artifactId>org.apache.clerezza.rdf.scala.utils</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.scala-lang</groupId>
+                       <artifactId>scala-library</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.lucene</groupId>
+                       <artifactId>lucene-highlighter</artifactId>
+                       <version>3.0.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.lucene</groupId>
+                       <artifactId>lucene-analyzers</artifactId>
+                       <version>3.0.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.lucene</groupId>
+                       <artifactId>lucene-memory</artifactId>
+                       <version>3.0.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.lucene</groupId>
+                       <artifactId>lucene-queries</artifactId>
+                       <version>3.0.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.lucene</groupId>
+                       <artifactId>lucene-core</artifactId>
+                       <version>3.0.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.wymiwyg</groupId>
+                       <artifactId>wymiwyg-commons-core</artifactId>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <scope>test</scope>
+               </dependency>
+       </dependencies>
+       <build>
+               <sourceDirectory>src/main/scala</sourceDirectory>
+               <testSourceDirectory>src/test/scala</testSourceDirectory>
+               <plugins>
+                       <plugin>
+                               <groupId>org.scala-tools</groupId>
+                               <artifactId>maven-scala-plugin</artifactId>
+                               <executions>
+                                       <execution>
+                                               <goals>
+                                                       <goal>compile</goal>
+                                                       <goal>testCompile</goal>
+                                               </goals>
+                                       </execution>
+                               </executions>
+                       </plugin>
+                       <plugin>
+                               <groupId>org.apache.felix</groupId>
+                               <artifactId>maven-bundle-plugin</artifactId>
+                               <configuration>
+                                       <instructions>
+                                               
<Service-Component>OSGI-INF/serviceComponents.xml</Service-Component>
+                                               
<Export-Package>org.apache.clerezza.rdf.cris.*</Export-Package>
+                                               
<Bundle-SymbolicName>org.apache.clerezza.rdf.cris</Bundle-SymbolicName>
+                                       </instructions>
+                               </configuration>
+                       </plugin>
+
+               </plugins>
+       </build>
+</project>

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/serviceComponents.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/serviceComponents.xml?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/serviceComponents.xml
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/resources/OSGI-INF/serviceComponents.xml
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0";>
+       <scr:component enabled="true" immediate="true" 
name="org.apache.clerezza.rdf.cris.IndexService">
+               <implementation 
class="org.apache.clerezza.rdf.cris.IndexService"/>
+               <service servicefactory="false">
+                       <provide 
interface="org.apache.clerezza.rdf.cris.IndexService"/>
+               </service>
+       </scr:component>
+</components>

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/Condition.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/Condition.scala?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/Condition.scala
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/Condition.scala
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.rdf.cris
+
+import org.apache.lucene.search.Query
+
+abstract class Condition {
+
+       def query: Query
+
+}

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/IndexService.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/IndexService.scala?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/IndexService.scala
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/IndexService.scala
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.rdf.cris;
+
+import org.apache.clerezza.rdf.cris.ontologies.CRIS
+import org.apache.clerezza.rdf.core.event._
+import org.apache.clerezza.rdf.core.TripleCollection
+import org.apache.clerezza.rdf.core.Resource
+import org.apache.clerezza.rdf.core.UriRef
+import org.apache.clerezza.rdf.core.Triple
+import org.apache.clerezza.rdf.utils._
+import org.apache.clerezza.rdf.scala.utils._
+import org.apache.clerezza.rdf.ontologies._
+import org.apache.lucene.analysis.standard.StandardAnalyzer
+import org.apache.lucene.document._
+import org.apache.lucene.store._
+import org.apache.lucene.index._
+import org.apache.lucene.search._
+import org.apache.lucene.util.Version
+import org.slf4j._
+import java.util.ArrayList
+import java.util.List
+import scala.actors.DaemonActor
+import scala.actors.TIMEOUT
+
+/**
+ *
+ * Provides access to indexed resources
+ *
+ * @author reto
+ */
+//while implemented in scala we don't expose any scala-library classes in the
+//public interface
+class IndexService(definitions: TripleCollection, baseGraph: TripleCollection) 
{
+
+       private val URI_FIELD_NAME = "resource-uri"
+       private val logger = LoggerFactory.getLogger(classOf[IndexService])
+       private val definitionsPreamble = new Preamble(definitions)
+       import definitionsPreamble._
+       val indexDefinitions = CRIS.IndexDefinition/-RDF.`type`
+       val indexedTypes = for (d <- indexDefinitions) yield d/CRIS.indexedType!
+       val indexedProperties = for (p <- (for (d <- indexDefinitions)
+               yield (d/CRIS.indexedProperty)).flatten) yield 
(p!).asInstanceOf[UriRef]
+       val index: Directory = new RAMDirectory
+       val analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
+
+       baseGraph.addGraphListener(new GraphListener() {
+               def graphChanged(events: List[GraphEvent]) {
+                       import scala.collection.JavaConversions._
+                       for (e <- events) {
+                               val triple = e.getTriple
+                               logger.info("processing addition of type 
"+triple.getObject)
+                               if (indexedTypes.contains(triple.getObject)) {
+                                       scheduleForReindex(triple.getSubject)
+                               }
+                       }
+               }
+       }, new FilterTriple(null, RDF.`type`, null))
+
+       baseGraph.addGraphListener(new GraphListener() {
+               def graphChanged(events: List[GraphEvent]) {
+                       import scala.collection.JavaConversions._
+                       for (e <- events) {
+                               val triple = e.getTriple
+                               val resource = triple.getSubject
+                               val types = for (tn <- resource/RDF.`type`) 
yield tn! ;
+                               if (types.exists(t => 
indexedTypes.contains(t))) {
+                                       scheduleForReindex(resource)
+                               }
+                       }
+               }
+       }, new FilterTriple(null, null, null) {
+               override def `match`(triple: Triple) = {
+                       val predicate = triple.getPredicate
+                       indexedProperties.contains(predicate)
+               }
+       })
+   
+       def scheduleForReindex(r: Resource) {
+               logger.info("Scheduling for reindex: "+r)
+               reindexActor ! r
+       }
+
+       val reindexActor = new DaemonActor {
+
+               val resourcesToProcess = new 
scala.collection.mutable.HashSet[Resource]
+               def act() {
+                       while(true) {
+                               receive {
+                                       case r: Resource => {
+                                               resourcesToProcess.add(r)
+                                               receiveSubsequentResources()
+                                               val writer = new 
IndexWriter(index, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
+                                               for (instance <- 
resourcesToProcess) indexResource(instance!, writer)
+                                               writer.close
+                                               resourcesToProcess.clear()
+                                       }
+                               }
+                       }
+               }
+
+               def     receiveSubsequentResources() {
+                       receiveWithin(100) {
+                               case r: Resource => {
+                                       resourcesToProcess.add(r)
+                                       receiveSubsequentResources()
+                               }
+                               case TIMEOUT => {}//do nothing, return
+                       }
+               }
+       }
+       reindexActor.start()
+
+       def indexResource(resource: Resource, writer: IndexWriter) {
+               val basePreamble = new Preamble(baseGraph)
+               import basePreamble._
+               def resourceToDocument(resource: Resource) = {
+                       val doc = new Document
+                       for (property <- indexedProperties) {
+                               logger.info("indexing "+property+" with values 
"+(resource/property).length)
+                               for (propertyValue <- resource/property) {
+                                       logger.info("indexing "+property+" with 
value "+(propertyValue*))
+                                       doc.add(new 
Field(property.getUnicodeString,
+                                                                         
propertyValue*,
+                                                                         
Field.Store.YES,
+                                                                         
Field.Index.NOT_ANALYZED))
+                               }
+                       }
+                       doc
+               }
+               def indexNamedResource(uriRef: UriRef) {
+                       val doc = resourceToDocument(uriRef)
+                       doc.add(new Field(URI_FIELD_NAME, 
uriRef.getUnicodeString, Field.Store.YES, Field.Index.ANALYZED))
+                       writer.addDocument(doc)
+               }
+               def indexAnonymousResource(resource: Resource) {
+                       logger.warn("Currently only indexing named resources is 
supported")
+                       /*val doc = resourceToDocument(resource)
+                       doc.add(new Field(URI_FIELD_NAME, 
getIdentifier(resource), Field.Store.YES, Field.Index.ANALYZED))
+                       writer.addDocument(doc)*/
+               }
+               resource match {
+                       case u: UriRef => indexNamedResource(u)
+                       case r => indexAnonymousResource(r)
+               }
+       }
+
+       def indexExistingResources() {
+               val basePreamble = new Preamble(baseGraph)
+               import basePreamble._
+               val instances = (for (indexedType <- indexedTypes) yield 
(indexedType)/-RDF.`type`).flatten
+               logger.info("instances "+instances.length)
+               val writer = new IndexWriter(index, analyzer, true, 
IndexWriter.MaxFieldLength.UNLIMITED);
+               for (instance <- instances) indexResource(instance!, writer)
+               writer.close
+
+       }
+
+       indexExistingResources()
+
+       def findResources(conditions: Condition*) = {
+               val booleanQuery = new BooleanQuery()
+               for (c <- conditions) {
+                       booleanQuery.add(c.query, BooleanClause.Occur.MUST)
+               }
+               val searcher = new IndexSearcher(index, true);
+               val hitsPerPage = 10
+               val collector = TopScoreDocCollector.create(hitsPerPage, true);
+               searcher.search(booleanQuery, collector)
+               val hits = collector.topDocs().scoreDocs;
+               //logger.info("Found " + hits.length + " hits for "+pattern);
+               def getResource(d: Document): Resource = {
+                       new UriRef(d.get(URI_FIELD_NAME))
+               }
+               val result = new ArrayList[Resource]()
+               for(hit <- hits) {
+                       val docId = hit.doc;
+                       val d = searcher.doc(docId);
+                       result.add(getResource(d))
+               }
+               result
+       }
+
+       def findResources(property: UriRef, pattern: String): List[Resource] = {
+               
+               /*val query: Query = new TermQuery(new Term(
+                               FOAF.firstName.getUnicodeString, something))*/
+               val query: Query = new WildcardQuery(new Term(
+                               property.getUnicodeString, pattern))
+               /*val query: Query = new FuzzyQuery(new Term(
+                               FOAF.firstName.getUnicodeString, something))*/
+               findResources(WildcardCondition(property, pattern))
+       }
+}

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/WilcardCondition.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/WilcardCondition.scala?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/WilcardCondition.scala
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/main/scala/org/apache/clerezza/rdf/cris/WilcardCondition.scala
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.rdf.cris
+
+import org.apache.clerezza.rdf.core.UriRef
+import org.apache.lucene.search.Query
+import org.apache.lucene.search.WildcardQuery
+import org.apache.lucene.index.Term
+
+case class WildcardCondition(property: UriRef, pattern: String) extends 
Condition {
+
+       def query: Query = new WildcardQuery(new Term(
+                               property.getUnicodeString, pattern))
+
+}

Added: 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/IndexServiceTest.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/IndexServiceTest.scala?rev=1059908&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/IndexServiceTest.scala
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-388/org.apache.clerezza.rdf.cris/core/src/test/scala/org/apache/clerezza/rdf/cris/IndexServiceTest.scala
 Mon Jan 17 13:03:48 2011
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.clerezza.rdf.cris;
+
+import org.apache.clerezza.rdf.cris.ontologies.CRIS
+import org.apache.clerezza.rdf.core._
+import org.apache.clerezza.rdf.core.impl._
+import org.apache.clerezza.rdf.utils._
+import org.apache.clerezza.rdf.scala.utils._
+import org.apache.clerezza.rdf.ontologies._
+import org.apache.clerezza.rdf.ontologies.FOAF
+import org.apache.lucene.analysis.standard.StandardAnalyzer
+import org.apache.lucene.document._
+import org.apache.lucene.store._
+import org.apache.lucene.index.IndexWriter
+import org.apache.lucene.util.Version
+import org.wymiwyg.commons.util.Util
+import java.util.List
+import org.junit._
+
+class IndexServiceTest {
+
+       val definitions = new SimpleMGraph
+       val dataGraph = new SimpleMGraph
+       var service: IndexService = null
+
+       def createPerson(firstName: String, lastName: String) {
+               val node = new GraphNode(new UriRef(Util.createURN5), dataGraph)
+               node.addProperty(RDF.`type`, FOAF.Person)
+               node.addPropertyValue(FOAF.firstName, firstName)
+               node.addPropertyValue(FOAF.lastName, lastName)
+       }
+
+       @Before
+       def prepare() {
+               def createDefinition(rdfType: UriRef, properties: UriRef*) {
+                       val node = new GraphNode(new BNode, definitions)
+                       node.addProperty(RDF.`type`, CRIS.IndexDefinition)
+                       node.addProperty(CRIS.indexedType, rdfType)
+                       for (p <- properties) {
+                               node.addProperty(CRIS.indexedProperty, p)
+                       }
+               }
+               createDefinition(FOAF.Person, FOAF.firstName, FOAF.lastName)
+               
+               createPerson("John", "Doe")
+               createPerson("Jane", "Doe")
+               createPerson("Frank", "Capra")
+               createPerson("Joe", "Bloggs")
+               createPerson("Jane", "Bloggs")
+               createPerson("Harry", "Wotsit")
+               createPerson("Harry Joe", "Wotsit-Bloggs")
+               //a person with two first-names
+               val node = new GraphNode(new UriRef(Util.createURN5), dataGraph)
+               node.addProperty(RDF.`type`, FOAF.Person)
+               node.addPropertyValue(FOAF.firstName, "John")
+               node.addPropertyValue(FOAF.firstName, "William")
+               node.addPropertyValue(FOAF.lastName, "Smith")
+               service = new IndexService(definitions, dataGraph)
+       }
+
+
+       @Test
+       def findResources() {
+               import scala.collection.JavaConversions._
+               val results = service.findResources(FOAF.firstName, "*Joe*")
+               Assert.assertEquals(2, results.length)
+       }
+
+       @Test
+       def findMultiProperties() {
+               import scala.collection.JavaConversions._
+               val results = 
service.findResources(WildcardCondition(FOAF.firstName, "*Joe*"),
+                                                                               
        WildcardCondition(FOAF.lastName, "*Wotsit*"))
+               /*val basePreamble = new Preamble(dataGraph)
+               import basePreamble._
+               for (r <- results) {
+                       println(r/FOAF.firstName)
+                       println(r/FOAF.lastName)
+               }*/
+               Assert.assertEquals(1, results.length)
+       }
+
+       @Test
+       def findMultiProperties2() {
+               import scala.collection.JavaConversions._
+               val results = 
service.findResources(WildcardCondition(FOAF.firstName, "*Jo*"),
+                                                                               
        WildcardCondition(FOAF.firstName, "*Wil*"))
+               /*val basePreamble = new Preamble(dataGraph)
+               import basePreamble._
+               for (r <- results) {
+                       println(r/FOAF.firstName)
+                       println(r/FOAF.lastName)
+               }*/
+               Assert.assertEquals(1, results.length)
+       }
+
+       @Test
+       def lateAddition() {
+               import scala.collection.JavaConversions._
+               createPerson("Another Joe", "Simpsons")
+               Thread.sleep(1000)
+               val results = service.findResources(FOAF.firstName, "*Joe*")
+               Assert.assertEquals(3, results.length)
+       }
+}


Reply via email to