001: package org.ontoware.rdf2go.util;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import org.junit.Assert;
007: import org.junit.Test;
008: import org.ontoware.rdf2go.RDF2Go;
009: import org.ontoware.rdf2go.model.Model;
010: import org.ontoware.rdf2go.model.node.URI;
011: import org.ontoware.rdf2go.model.node.Variable;
012: import org.ontoware.rdf2go.model.node.impl.URIImpl;
013: import org.ontoware.rdf2go.util.transform.NamespaceSearchReplaceRule;
014: import org.ontoware.rdf2go.util.transform.SearchRemoveAddRule;
015: import org.ontoware.rdf2go.util.transform.Transformer;
016: import org.ontoware.rdf2go.util.transform.URISearchReplaceRule;
017: import org.ontoware.rdf2go.vocabulary.RDF;
018: import org.ontoware.rdf2go.vocabulary.RDFS;
019: import org.slf4j.Logger;
020: import org.slf4j.LoggerFactory;
021:
022: public class TransformerTest {
023:
024: private static Logger log = LoggerFactory
025: .getLogger(TransformerTest.class);
026:
027: @Test
028: public void testToSparqlConstruct() {
029: Map<String, URI> nsMap = new HashMap<String, URI>();
030: nsMap.put("skos", new URIImpl(
031: "http://www.w3.org/2004/02/skos/core#"));
032: nsMap.put("", new URIImpl("http://purl.org/net/schemarama#"));
033: String construct = "[] a :Error; :message 'Resource [1] has more than one preferred lexical label [2][3] in a given language.'; :implicated ( ?x ?l ?m ); .";
034: String where = "?x skos:prefLabel ?l; skos:prefLabel ?m. FILTER ( str(?l) != str(?m) && lang(?l) = lang(?m) )";
035: String query = Transformer.toSparqlConstruct(nsMap, construct,
036: where);
037: log.debug("query = " + query);
038: }
039:
040: @Test
041: public void testApplyRule() {
042: Model m = RDF2Go.getModelFactory().createModel();
043: m.open();
044: Map<String, URI> nsMap = new HashMap<String, URI>();
045: nsMap.put("rdf", new URIImpl(
046: "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
047: String LOCAL = "http://example.com#";
048: nsMap.put("", new URIImpl(LOCAL));
049: URI a = new URIImpl(LOCAL + "a");
050: URI b = new URIImpl(LOCAL + "b");
051: URI c = new URIImpl(LOCAL + "c");
052:
053: m.addStatement(a, b, c);
054: m.addStatement(a, RDF.type, c);
055: log.debug("Before");
056: m.dump();
057:
058: String constructRemove = "?x a " + c.toSPARQL();
059: String constructAdd = "?x a " + b.toSPARQL();
060: String where = constructRemove;
061:
062: SearchRemoveAddRule.searchAndReplace(m, nsMap, where,
063: constructRemove, constructAdd);
064: log.debug("After");
065: m.dump();
066: }
067:
068: @Test
069: public void testUriRename() {
070: Model m = RDF2Go.getModelFactory().createModel();
071: m.open();
072:
073: URI a = new URIImpl("urn:test:a");
074: URI b = new URIImpl("urn:test:b");
075: URI c = new URIImpl("urn:test:c");
076:
077: URI super Rel = new URIImpl(
078: "http://www.semanticdesktop.org/ontologies/2007/09/cds/hasSuperRelation");
079: m.addStatement(super Rel, b, c);
080: m.addStatement(a, super Rel, c);
081: m.addStatement(a, b, super Rel);
082: Map<String, URI> nsMap = new HashMap<String, URI>();
083: URISearchReplaceRule.searchAndReplace(m, nsMap, super Rel,
084: RDFS.subPropertyOf);
085:
086: m.dump();
087: Assert.assertFalse(m.contains(super Rel, Variable.ANY,
088: Variable.ANY));
089: }
090:
091: @Test
092: public void testUriPrefixRename() {
093: Model m = RDF2Go.getModelFactory().createModel();
094: m.open();
095:
096: URI a = new URIImpl("urn:test:a");
097: URI b = new URIImpl("urn:test:b");
098: URI c = new URIImpl("urn:test:c");
099:
100: URI super Rel = new URIImpl(
101: "http://www.semanticdesktop.org/ontologies/2007/09/cds/hasSuperRelation");
102: m.addStatement(super Rel, b, c);
103: m.addStatement(a, super Rel, c);
104: m.addStatement(a, b, super Rel);
105: NamespaceSearchReplaceRule.searchAndReplace(m, "urn:test:",
106: "http://example.com#");
107:
108: m.dump();
109: Assert.assertFalse(m.contains(a, Variable.ANY, Variable.ANY));
110: }
111: }
|