1   package eu.fbk.knowledgestore.client;
2   
3   import java.util.Set;
4   
5   import com.google.common.base.Joiner;
6   import com.google.common.base.Throwables;
7   import com.google.common.collect.ImmutableList;
8   import com.google.common.collect.Ordering;
9   import com.google.common.collect.Sets;
10  
11  import org.junit.Ignore;
12  import org.junit.Test;
13  import org.openrdf.model.URI;
14  import org.openrdf.model.impl.URIImpl;
15  import org.openrdf.model.vocabulary.DCTERMS;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  import eu.fbk.knowledgestore.KnowledgeStore;
20  import eu.fbk.knowledgestore.Session;
21  import eu.fbk.knowledgestore.data.Criteria;
22  import eu.fbk.knowledgestore.data.Data;
23  import eu.fbk.knowledgestore.data.Record;
24  import eu.fbk.knowledgestore.data.Representation;
25  import eu.fbk.knowledgestore.data.Stream;
26  import eu.fbk.knowledgestore.internal.Util;
27  import eu.fbk.knowledgestore.vocabulary.KS;
28  
29  public class KnowledgeStoreClientTest {
30  
31      private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
32  
33      // TODO: intercept client-side connection close
34  
35      @Ignore
36      @Test
37      public void test2() throws Throwable {
38          final Set<String> namespaces = Sets.newHashSet();
39          final String query = "" //
40                  + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
41                  + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" //
42                  + "select distinct ?t\n" //
43                  + "where {\n" //
44                  + "  { ?s a ?t } union { ?t a rdfs:Class } union { ?t a owl:Class }\n" //
45                  + "  union { ?t rdfs:subClassOf ?x } union { ?y rdfs:subClassOf ?t }\n" //
46                  + "  filter (isIRI(?t))" + "}";
47  
48          final KnowledgeStore store = Client.builder("http://localhost:8080/")
49                  .maxConnections(2).validateServer(false).build();
50          try {
51              final Session session = store.newSession();
52              final Stream<URI> uris = session.sparql(query).execTuples()
53                      .transform(URI.class, true, "t");
54              int index = 0;
55              for (final URI uri : uris) {
56                  namespaces.add(uri.getNamespace());
57                  ++index;
58                  if (index % 10000 == 0) {
59                      System.out.println(index + " URIs found");
60                  }
61              }
62              System.out.println(Joiner.on('\n').join(Ordering.natural().sortedCopy(namespaces)));
63              uris.close();
64  
65          } finally {
66              store.close();
67          }
68      }
69  
70      @Test
71      public void test() throws Throwable {
72          final KnowledgeStore store = Client.builder("http://localhost:8080/")
73                  .maxConnections(2).validateServer(false).build();
74          try {
75              final URI exampleID = new URIImpl("ex:test");
76              final URI resourceID = new URIImpl(
77                      "http://www.newsreader-project.eu/2013/4/30/589F-S2Y1-JD34-V1MC.xml");
78  
79              final Session session = store.newSession("ks", "kspass");
80  
81              System.out.println(serialize(session.retrieve(KS.RESOURCE)
82                      .condition("not(dct:creator = 'Francesco')").exec()));
83  
84              session.count(KS.RESOURCE).condition("not(dct:creator = 'Francesco')").exec();
85  
86              // System.err.println(session.upload(exampleID).representation(null).exec());
87  
88              session.delete(KS.RESOURCE).ids(exampleID).exec();
89  
90              final Record record = Record.create(exampleID, KS.RESOURCE).set(DCTERMS.CREATOR,
91                      "Francesco");
92              session.merge(KS.RESOURCE).records(record).criteria(Criteria.overwrite()).exec();
93  
94              System.out.println(session.sparql("select (count(*) as ?n) where { ?s ?p ?o }")
95                      .execTuples().transform(Integer.class, true, "n").getUnique());
96              System.out.println(session
97                      .sparql("describe <http://dbpedia.org/resource/Michael_Schumacher>")
98                      .execTriples().count());
99  
100             session.merge(KS.RESOURCE).records(record).criteria(Criteria.overwrite()).exec();
101 
102             System.out.println(session.sparql(
103                     "ask { <http://dbpedia.org/resource/David_Beckam> ?p ?o }").execBoolean());
104 
105             final Representation representation = session.download(resourceID).exec();
106             System.out.println(representation.writeToString());
107             System.out.println(serialize(ImmutableList.of(representation.getMetadata())));
108 
109             session.download(new URIImpl("ex:does-not-exist")).exec();
110 
111             System.out.println(session
112                     .upload(exampleID)
113                     .representation(
114                             Representation.create("Nel bel mezzo del cammin di nostra vita"))
115                     .exec());
116         } catch (final Throwable ex) {
117             LOGGER.error("Test failed", ex);
118             Throwables.propagate(ex);
119 
120         } finally {
121             store.close();
122         }
123     }
124 
125     private String serialize(final Iterable<Record> records) {
126         final StringBuilder builder = new StringBuilder();
127         try {
128             int index = 0;
129             for (final Record record : records) {
130                 builder.append("(").append(++index)
131                         .append(") -----------------------------------------------------\n");
132                 builder.append(record.toString(Data.getNamespaceMap(), true)).append("\n\n");
133             }
134             return builder.toString();
135         } finally {
136             Util.closeQuietly(records);
137         }
138 
139         // final ByteArrayOutputStream out = new ByteArrayOutputStream();
140         // RDFUtil.writeRDF(RDFFormat.TURTLE, out, Data.getNamespaceMap(),
141         // Record.encode(Stream.create(records), ImmutableSet.<URI>of()));
142         // return new String(out.toByteArray());
143     }
144 }