View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jpl7
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    * 
10   * 1. Redistributions of source code must retain the above copyright notice,
11   *    this list of conditions and the following disclaimer.
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * #L%
28   */
29  package io.github.prolobjectlink.prolog.jpl7;
30  
31  import static io.github.prolobjectlink.prolog.PrologLogger.FILE_NOT_FOUND;
32  import static io.github.prolobjectlink.prolog.PrologLogger.IO;
33  
34  import java.io.BufferedReader;
35  import java.io.File;
36  import java.io.FileNotFoundException;
37  import java.io.FileReader;
38  import java.io.IOException;
39  import java.io.Reader;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  import org.jpl7.Atom;
44  import org.jpl7.Compound;
45  import org.jpl7.Term;
46  import org.jpl7.Util;
47  
48  /**
49   * 
50   * @author Jose Zalacain
51   * @since 1.0
52   */
53  final class JplParser {
54  
55  	public Term parseTerm(String term) {
56  		return Util.textToTerm(term);
57  	}
58  
59  	public Term[] parseTerms(Term term) {
60  		return parseTerms("" + term + "");
61  	}
62  
63  	public Term[] parseTerms(String stringTerms) {
64  		Term[] a = new Term[0];
65  		Term ptr = Util.textToTerm(stringTerms);
66  		List<Term> terms = new ArrayList<Term>();
67  		while (ptr.isCompound() && ptr.hasFunctor(",", 2)) {
68  			terms.add(ptr.arg(1));
69  			ptr = ptr.arg(2);
70  		}
71  		terms.add(ptr);
72  		return terms.toArray(a);
73  	}
74  
75  	public JplProgram parseProgram(String file) {
76  		return parseProgram(new File(file));
77  	}
78  
79  	public JplProgram parseProgram(File in) {
80  
81  		FileReader reader = null;
82  		BufferedReader buffer = null;
83  		JplProgram program = new JplProgram();
84  
85  		try {
86  			reader = new FileReader(in);
87  			buffer = new BufferedReader(reader);
88  			String line = buffer.readLine();
89  			StringBuilder b = new StringBuilder();
90  			while (line != null) {
91  				if (!line.isEmpty() && line.lastIndexOf('.') == line.length() - 1) {
92  					b.append(line.substring(0, line.length() - 1));
93  					Term clauseTerm = Util.textToTerm("" + b + "");
94  					if (clauseTerm.hasFunctor(":-", 1)) {
95  						String absoluteString = "";
96  						Term arg = clauseTerm.arg(1);
97  						if (arg.hasFunctor("consult", 1)) {
98  							Term relative = arg.arg(1);
99  							String path = relative.name();
100 							String[] array = path.split("\\.\\./");
101 							if (array.length > 1) {
102 								String ok = array[array.length - 1];
103 								File currentPtr = in.getCanonicalFile();
104 								for (int i = 0; i < array.length; i++) {
105 									currentPtr = currentPtr.getParentFile();
106 								}
107 								String ptr = currentPtr.getCanonicalPath();
108 								File abs = new File(ptr + File.separator + ok);
109 								absoluteString = abs.getCanonicalPath();
110 							}
111 							Atom absolute = new Atom(absoluteString.toLowerCase().replace(File.separatorChar, '/'));
112 							Compound c = new Compound("consult", new Term[] { absolute });
113 							program.addDirective(c);
114 						} else {
115 							program.addDirective(clauseTerm);
116 						}
117 
118 					} else {
119 						program.add(clauseTerm);
120 					}
121 					b = new StringBuilder();
122 				} else {
123 					b.append(line);
124 				}
125 				line = buffer.readLine();
126 			}
127 		} catch (FileNotFoundException e) {
128 			JplProvider.logger.error(getClass(), FILE_NOT_FOUND, e);
129 		} catch (IOException e) {
130 			JplProvider.logger.error(getClass(), IO, e);
131 		} finally {
132 			if (reader != null) {
133 				try {
134 					reader.close();
135 				} catch (IOException e) {
136 					JplProvider.logger.error(getClass(), IO, e);
137 				}
138 			}
139 			if (buffer != null) {
140 				try {
141 					buffer.close();
142 				} catch (IOException e) {
143 					JplProvider.logger.error(getClass(), IO, e);
144 				}
145 			}
146 		}
147 
148 		return program;
149 	}
150 
151 	public JplProgram parseProgram(Reader in) {
152 
153 		BufferedReader buffer = null;
154 		JplProgram program = new JplProgram();
155 
156 		try {
157 			buffer = new BufferedReader(in);
158 			String line = buffer.readLine();
159 			StringBuilder b = new StringBuilder();
160 			while (line != null) {
161 				if (!line.isEmpty() && line.lastIndexOf('.') == line.length() - 1) {
162 					b.append(line.substring(0, line.length() - 1));
163 					Term clauseTerm = Util.textToTerm("" + b + "");
164 					program.add(clauseTerm);
165 					b = new StringBuilder();
166 				} else {
167 					b.append(line);
168 				}
169 				line = buffer.readLine();
170 			}
171 		} catch (IOException e) {
172 			JplProvider.logger.error(getClass(), IO, e);
173 		} finally {
174 			if (buffer != null) {
175 				try {
176 					buffer.close();
177 				} catch (IOException e) {
178 					JplProvider.logger.error(getClass(), IO, e);
179 				}
180 			}
181 		}
182 
183 		return program;
184 	}
185 
186 }