001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.reasoning;
021    
022    import java.io.BufferedReader;
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.InputStreamReader;
027    import java.io.OutputStream;
028    import java.io.OutputStreamWriter;
029    import java.net.HttpURLConnection;
030    import java.net.URI;
031    import java.net.URISyntaxException;
032    import java.net.URL;
033    
034    import org.apache.xmlbeans.XmlException;
035    import org.dllearner.utilities.Files;
036    import org.kr.dl.dig.v1_1.GetIdentifierDocument;
037    import org.kr.dl.dig.v1_1.IdRespType;
038    import org.kr.dl.dig.v1_1.IdentifierDocument;
039    import org.kr.dl.dig.v1_1.NewKBDocument;
040    import org.kr.dl.dig.v1_1.ReleaseKBDocument;
041    import org.kr.dl.dig.v1_1.ResponseDocument;
042    import org.kr.dl.dig.v1_1.ResponsesDocument;
043    import org.kr.dl.dig.v1_1.KbDocument.Kb;
044    
045    /**
046     * Methods for sending messages to a DIG-capable reasoner and receiving answers
047     * using Apache XML Beans.
048     * 
049     * @author Jens Lehmann
050     * 
051     */
052    public class DIGHTTPConnector {
053    
054            private URL url;
055            private File protocolFile;
056    
057            public DIGHTTPConnector(URL url) {
058                    this.url = url;
059            }
060     
061            public DIGHTTPConnector(URL url, File protocolFile) {
062                    this.url = url;
063                    this.protocolFile = protocolFile;
064            }
065            
066            public URI newKB() {
067                    NewKBDocument newKB = NewKBDocument.Factory.newInstance();
068                    newKB.addNewNewKB();
069                    String answer = "";
070                    try {
071                            answer = sendAndReceive(newKB.toString());
072                    } catch (IOException e1) {
073                            // TODO Auto-generated catch block
074                            e1.printStackTrace();
075                    }
076                    ResponseDocument rd = parse(answer);
077                    
078                    IdRespType rt = rd.getResponse();
079                    Kb kb = rt.getKb();
080                    String uriStr = kb.getUri();
081                    
082                    URI uri = null;
083                    try {
084                            uri = new URI(uriStr);
085                    } catch (URISyntaxException e) {
086                            System.out.println("Reasoner did not provide a valid URI.");
087                            e.printStackTrace();
088                            System.exit(0);
089                    }
090                    
091                    return uri;
092            }
093    
094            public boolean releaseKB(URI kbURI) {
095                    ReleaseKBDocument releaseKB = ReleaseKBDocument.Factory.newInstance();
096                    releaseKB.addNewReleaseKB().setUri(kbURI.toString());
097                    String answer = "";
098                    try {
099                            answer = sendAndReceive(releaseKB.toString());
100                    } catch (IOException e) {
101                            // TODO Auto-generated catch block
102                            e.printStackTrace();
103                    }
104                    ResponseDocument rd = parse(answer);
105                    return rd.getResponse().isSetOk();
106            }
107    
108            public String getIdentifier() throws IOException {
109                    GetIdentifierDocument gid = GetIdentifierDocument.Factory.newInstance();
110                    gid.addNewGetIdentifier();
111                    String answer = "";
112                    answer = sendAndReceive(gid.toString());
113                    IdentifierDocument id = null;
114                    try {                   
115                            id = IdentifierDocument.Factory.parse(answer);
116                    } catch (XmlException e) {
117                            System.out.println("DIG-Reasoner does not identify properly.");
118                            e.printStackTrace();
119                            System.exit(0);
120                    }
121                    
122                    return id.getIdentifier().getName() + " (version " + id.getIdentifier().getVersion() + ")";
123            }
124    
125            // tell-Anfrage als XML-String schicken
126            public ResponseDocument tells(String tells) throws IOException {
127                    return parse(sendAndReceive(tells));
128            }
129    
130            private int askCounter = 0;
131            
132            // asks-Anfrage als XML-String schicken
133            public ResponsesDocument asks(String asks) {
134                    askCounter++;
135                    
136                    String answer = "";
137                    try {
138                            answer = sendAndReceive(asks);
139                    } catch (IOException e1) {
140                            // TODO Auto-generated catch block
141                            e1.printStackTrace();
142                    }
143                    ResponsesDocument rd = null;
144                    try {
145                            rd = ResponsesDocument.Factory.parse(answer);
146                    } catch (XmlException e) {
147                            e.printStackTrace();
148                            System.err.println("Exception occured when receiving the following string:\n" + answer);
149                    }
150                    return rd;
151            }
152    
153            private ResponseDocument parse(String answer) {
154                    ResponseDocument rd = null;
155                    try {
156                            rd = ResponseDocument.Factory.parse(answer);
157                    } catch (XmlException e) {
158                            e.printStackTrace();
159                    }
160                    return rd;
161            }
162            
163            private String sendAndReceive(String send) throws IOException {
164                    StringBuilder answer = new StringBuilder();     
165                    
166                    // String an DIG-Reasoner schicken
167                    HttpURLConnection connection;
168                            
169    //              try {
170                            connection = (HttpURLConnection) url.openConnection();
171                            connection.setDoOutput(true);
172                            
173                            OutputStream os = connection.getOutputStream();
174                            OutputStreamWriter osw = new OutputStreamWriter(os);
175                            osw.write(send);
176                            osw.close();
177                            
178                            if(protocolFile != null)
179                                    Files.appendFile(protocolFile, "DIG code send to reasoner:\n\n"+send+"\n\n");
180                            
181                            // receive answer
182                            InputStream is = connection.getInputStream();
183                            InputStreamReader isr = new InputStreamReader(is);
184                            BufferedReader br = new BufferedReader(isr);
185                            
186                            String line;
187                            do {
188                                    line = br.readLine();
189                                    if(line!=null)
190                                            answer.append(line);
191                            } while (line != null);
192                            
193                            br.close();
194    
195    //              } catch (IOException e) {               
196    //                      System.out.println("Communication problem with DIG Reasoner. Please make sure there is a DIG reasoner running at " + url + " and try again.");
197    //                      System.exit(0);
198    //              }       
199                    
200                    if(protocolFile != null)
201                            Files.appendFile(protocolFile, "DIG code received from reasoner:\n\n"+answer+"\n\n");
202                    
203                    return answer.toString();
204            }
205            
206    }