Author: reto
Date: Tue Jul 20 07:19:25 2010
New Revision: 965740

URL: http://svn.apache.org/viewvc?rev=965740&view=rev
Log:
CLEREZZA-218: added test and fixed for CityWeatherEnricher

Modified:
    
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/pom.xml
    
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/main/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriples.java
    
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/CityWeatherEnricher.java
    
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriplesTest.java

Modified: 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/pom.xml?rev=965740&r1=965739&r2=965740&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/pom.xml
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/pom.xml
 Tue Jul 20 07:19:25 2010
@@ -39,6 +39,11 @@
                        <groupId>org.apache.clerezza</groupId>
                        
<artifactId>org.apache.clerezza.rdf.ontologies</artifactId>
                </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.utils</artifactId>
+                       <scope>test</scope>
+               </dependency>
        </dependencies>
 </project>
 

Modified: 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/main/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriples.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/main/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriples.java?rev=965740&r1=965739&r2=965740&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/main/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriples.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/main/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriples.java
 Tue Jul 20 07:19:25 2010
@@ -54,9 +54,9 @@ public class EnrichmentTriples extends A
                                new 
ArrayList<Iterator<Triple>>(enrichers.size());
 
                for (Enricher enricher : enrichers) {
-                       if (enricher.getSubjectFilter().accept(subject, base) &&
-                               enricher.getPropertyFilter().accept(predicate, 
base) &&
-                               enricher.getObjectFilter().accept(object, 
base)) {
+                       if (((subject == null) || 
enricher.getSubjectFilter().accept(subject, base)) &&
+                               ((predicate == null) || 
enricher.getPropertyFilter().accept(predicate, base)) &&
+                               ((object == null) || 
enricher.getObjectFilter().accept(object, base))) {
                                iteratorCollection.add(enricher.filter(subject, 
predicate, object, base));
 
                        }

Modified: 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/CityWeatherEnricher.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/CityWeatherEnricher.java?rev=965740&r1=965739&r2=965740&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/CityWeatherEnricher.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/CityWeatherEnricher.java
 Tue Jul 20 07:19:25 2010
@@ -19,6 +19,8 @@
 package org.apache.clerezza.rdf.enrichment;
 
 import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -37,17 +39,17 @@ import org.apache.clerezza.utils.Iterato
 
 /**
  * add a a weather property pointing to the weather expressed a a bnode with
- * current temperature and humidity (fake values based on time and city name)
+ * current temperature and humidity (fake values)
  *
  * @author reto
  */
 public class CityWeatherEnricher extends Enricher {
 
        //ontology part
-       UriRef classCity = new UriRef("http://example.org/ontology#City";);
-       UriRef weatherProperty = new 
UriRef("http://example.org/ontology#weather";);
-       UriRef temperatureProperty = new 
UriRef("http://example.org/ontology#temperature";);
-       UriRef humidityProperty = new 
UriRef("http://example.org/ontology#humidity";);
+       static UriRef classCity = new 
UriRef("http://example.org/ontology#City";);
+       static UriRef weatherProperty = new 
UriRef("http://example.org/ontology#weather";);
+       static UriRef temperatureProperty = new 
UriRef("http://example.org/ontology#temperature";);
+       static UriRef humidityProperty = new 
UriRef("http://example.org/ontology#humidity";);
        final private static UriRef xsdDouble =
                        new UriRef("http://www.w3.org/2001/XMLSchema#double";);
        private final ResourceFilter classCityFilter = 
getFilterForSubjectsWith(RDF.type, classCity);
@@ -120,10 +122,24 @@ public class CityWeatherEnricher extends
                                resultSet.add(new TripleImpl(subject, 
predicate, weatherNode));
                        }
                } else {
-                       //TODO get the real wather of that city
+                       NonLiteral city = null;
+                       Collection<NonLiteral> obsoleteCities = new 
ArrayList<NonLiteral>();
+                       for (Map.Entry<NonLiteral, WeakReference<BNode>> entry 
: cityWeatherMap.entrySet()) {
+                               BNode node = entry.getValue().get();
+                               if (node == null) {
+                                       obsoleteCities.add(entry.getKey());
+                               }
+                               if (node.equals(subject)) {
+                                       city = entry.getKey();
+                                       break;
+                               }
+                       }
+                       for (NonLiteral nonLiteral : obsoleteCities) {
+                               cityWeatherMap.remove(nonLiteral);
+                       }
                        if (predicate.equals(temperatureProperty) || (predicate 
== null)) {
                                resultSet.add(new TripleImpl(subject,
-                                               temperatureProperty, 
LiteralFactory.getInstance().createTypedLiteral(21.4)));
+                                               temperatureProperty, 
LiteralFactory.getInstance().createTypedLiteral((double)city.toString().length())));
                        }
                        if (predicate.equals(humidityProperty) || (predicate == 
null)) {
                                resultSet.add(new TripleImpl(subject,

Modified: 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriplesTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriplesTest.java?rev=965740&r1=965739&r2=965740&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriplesTest.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-218/org.apache.clerezza.rdf.enrichment/org.apache.clerezza.rdf.enrichment.core/src/test/java/org/apache/clerezza/rdf/enrichment/EnrichmentTriplesTest.java
 Tue Jul 20 07:19:25 2010
@@ -27,6 +27,8 @@ import org.apache.clerezza.rdf.core.Trip
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.ontologies.*;
+import org.apache.clerezza.rdf.utils.GraphNode;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -58,6 +60,26 @@ public class EnrichmentTriplesTest {
                Assert.assertFalse(enrichmentTriples.filter(uncle, 
SiblingEnricher.siblingProperty, sister).hasNext());
        }
 
+       @Test
+       public void cityWeather() {
+               MGraph base = new SimpleMGraph();
+               Enricher cityEnricher = new CityWeatherEnricher();
+               TripleCollection enrichmentTriples = new EnrichmentTriples(base,
+                               Collections.singleton(cityEnricher));
+               NonLiteral london = new UriRef("http://example.org/london";);
+               NonLiteral me = new UriRef("http://example.org/me";);
+               base.add(new TripleImpl(me, RDF.type, FOAF.Agent));
+               base.add(new TripleImpl(london, RDF.type, 
CityWeatherEnricher.classCity));
+               Assert.assertTrue(enrichmentTriples.filter(london, 
CityWeatherEnricher.weatherProperty, null).hasNext());
+               GraphNode node = new GraphNode(london, enrichmentTriples);
+               Assert.assertEquals("0.51",
+                               
node.getObjectNodes(CityWeatherEnricher.weatherProperty).next()
+                               
.getLiterals(CityWeatherEnricher.humidityProperty).next().getLexicalForm());
+               Assert.assertEquals(Double.toString(london.toString().length()),
+                               
node.getObjectNodes(CityWeatherEnricher.weatherProperty).next()
+                               
.getLiterals(CityWeatherEnricher.temperatureProperty).next().getLexicalForm());
+       }
+
 
 
 }


Reply via email to