Skip to content

Commit 7358d4c

Browse files
author
Quim
committed
add grammar check & validation
1 parent f43e122 commit 7358d4c

File tree

6 files changed

+106
-83
lines changed

6 files changed

+106
-83
lines changed

src/main/java/com/essi/Dependency/Application.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.essi.Dependency.Service.DependencyService;
1212
import com.essi.Dependency.Service.StorageProperties;
1313

14+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
1415
import springfox.documentation.builders.ApiInfoBuilder;
1516
import springfox.documentation.builders.PathSelectors;
1617
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -21,6 +22,7 @@
2122

2223
@SpringBootApplication
2324
// @PropertySource({"classpath:application.properties"})
25+
@EnableJpaRepositories("com.essi.Dependency.Repository")
2426
@EnableConfigurationProperties(StorageProperties.class)
2527
public class Application {
2628
public static void main(String[] args) throws IOException {

src/main/java/com/essi/Dependency/Controller/Controller.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public Controller(DependencyService depService) {
9696
public ResponseEntity<?> crossReferenceDetector(
9797
@ApiParam(value = "The file to upload (HTML format)",
9898
required = true) @RequestParam("file") MultipartFile file,
99+
@ApiParam(value = "Company") @RequestParam(required = false) String company,
99100
RedirectAttributes redirectAttributes) throws IOException, InterruptedException {
100101

101102
long startTime = System.currentTimeMillis();
@@ -118,7 +119,7 @@ public ResponseEntity<?> crossReferenceDetector(
118119

119120
// extract clauses and locations from the input file (Clause.class)
120121
ArrayList<Object> clauseList = depService.extractClauseList();
121-
dependencies = depService.getDependencies();
122+
dependencies = depService.getDependencies(company);
122123

123124
// Create the new JSON to be returned (Project, requirements, dependencies).
124125
JSONHandler jh = new JSONHandler();
@@ -290,6 +291,7 @@ public ResponseEntity<?> crossReferenceDetector(
290291
public ResponseEntity<?> crossReferenceDetector(
291292
@ApiParam(value = "The file to upload (HTML fromat)",
292293
required = true) @RequestParam("file") MultipartFile file,
294+
@ApiParam(value = "Company") @RequestParam(required = false) String company,
293295
RedirectAttributes redirectAttributes,
294296
@ApiParam(value = "First index of the clause list that will be analysed (included)",
295297
required = true) @PathVariable("n") String n,
@@ -320,7 +322,7 @@ public ResponseEntity<?> crossReferenceDetector(
320322

321323
// extract clauses and locations (Clause.class)
322324
ArrayList<Object> clauseList = depService.extractClauseList();
323-
dependencies = depService.getNMDependencies(n, m);
325+
dependencies = depService.getNMDependencies(company, n, m);
324326
depService.deleteAll();
325327

326328
// Create the new Json (project, requirements, dependencies)
@@ -395,7 +397,8 @@ public ResponseEntity<?> crossReferenceDetector(
395397
message = "Internal Server Error. For more information see ‘message’ in the Response Body.") })
396398
public ResponseEntity<?> crossReferenceDetectorJson(
397399
@ApiParam(value = "The json object to upload.", required = true, example = "") @RequestBody String json,
398-
RedirectAttributes redirectAttributes,
400+
@ApiParam(value = "Company") @RequestParam(required = false) String company,
401+
RedirectAttributes redirectAttributes,
399402
@ApiParam(value = "Id of the project where the requirements to analize are.",
400403
required = true) @PathVariable("projectId") String projectId,
401404
HttpServletRequest request)
@@ -425,7 +428,7 @@ public ResponseEntity<?> crossReferenceDetectorJson(
425428
}
426429

427430
// Exrtact the dependencies from the bug requirements
428-
ArrayList<Object> dependencies = depService.getDependencies();
431+
ArrayList<Object> dependencies = depService.getDependencies(company);
429432

430433
// Save the detected cross-reference dependencies within the intput JSON
431434
ObjectNode objN = depService.storeDependenciesJson(dependencies);
@@ -470,7 +473,8 @@ public ResponseEntity<?> crossReferenceDetectorJson(
470473
message = "Internal Server Error. For more information see ‘message’ in the Response Body.") })
471474
public ResponseEntity<?> crossReferenceDetectorJson(
472475
@ApiParam(value = "The json object to upload.", required = true) @RequestBody String json,
473-
RedirectAttributes redirectAttributes,
476+
@ApiParam(value = "Company") @RequestParam(required = false) String company,
477+
RedirectAttributes redirectAttributes,
474478
@ApiParam(value = "Id of the project where the requirements to analize are.",
475479
required = true) @PathVariable("projectId") String projectId,
476480
@ApiParam(value = "First index of the requirement list that will be analysed (included)",
@@ -508,7 +512,7 @@ public ResponseEntity<?> crossReferenceDetectorJson(
508512
}
509513

510514
// Get the dependencies from the bug requirements
511-
ArrayList<Object> dependencies = depService.getNMDependencies(n, m);
515+
ArrayList<Object> dependencies = depService.getNMDependencies(company, n, m);
512516

513517
// Save the dependencies into the input JSON
514518
ObjectNode objN = depService.storeDependenciesJson(dependencies);

src/main/java/com/essi/Dependency/Functionalities/CallableTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class CallableTask implements Callable<ArrayList<Object>> {
2525
* @param pattern
2626
* @param dep
2727
*/
28-
CallableTask(Object expression, ArrayList<Object> expressionList, Pattern pattern, ArrayList<Object> dep) {
28+
CallableTask(com.essi.Dependency.Components.Grammar grammarObj, Object expression, ArrayList<Object> expressionList, Pattern pattern, ArrayList<Object> dep) {
2929
this.expression = expression;
3030
this.expressionList = expressionList;
3131
this.pattern = pattern;
3232
this.dep = dep;
33-
grammar = new Grammar();
33+
grammar = new Grammar(grammarObj);
3434
}
3535

3636
/**

src/main/java/com/essi/Dependency/Functionalities/Grammar.java

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import com.essi.Dependency.Repository.GrammarRepository;
1414
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.beans.factory.annotation.Value;
1516
import org.springframework.stereotype.Repository;
1617

1718
import com.essi.Dependency.Components.Bug;
@@ -20,13 +21,10 @@
2021
import com.essi.Dependency.Components.DependencyType;
2122
import com.essi.Dependency.Components.ExternalDependency;
2223
import com.essi.Dependency.Components.Status;
24+
import org.springframework.stereotype.Service;
2325

24-
@Repository
2526
public class Grammar {
2627

27-
@Autowired
28-
private GrammarRepository grammarRepository;
29-
3028
// Gazetters
3129
private String linkTerm;
3230
private String implicitTerm;
@@ -57,63 +55,23 @@ public class Grammar {
5755
/**
5856
* Constructor
5957
*/
60-
public Grammar() {
58+
public Grammar(com.essi.Dependency.Components.Grammar grammar) {
6159
super();
6260
// Gazetteers
6361
this.linkTerm = "((\\s)?of(\\s)?|(\\s)?of the(\\s)?|(\\s)?of a(\\s)?)";
6462

6563
this.implicitTerm = "((\\s)?above(\\s)?|(\\s)?below(\\s)?|(\\s)?preceding(\\s)?|(\\s)?following(\\s)?|(\\s)?that follows(\\s)?|"
6664
+ "(\\s)?next(\\s)?|(\\s)?previous(\\s)?|(\\s)?this(\\s)?|(\\s)?same(\\s)?|(\\s)?current(\\s)?)";
67-
this.markerTerm = "((\\s+|\\(|\\|)(section(s)?(\\s)?|sect\\.(\\s)?|subsection(s)?(\\s)?|subsect\\.(\\s)?|paragraph(s)?(\\s)?|"
68-
+ "subparagraph(s)?(\\s)?|subitem(s)?(\\s)?|volume(s)?(\\s)?|lot('s)?(\\s)?|book(s)?(\\s)?"
69-
+ "|article(s)?(\\s)?|item(s)?(\\s)?|point(s)?(\\s)?" +
70-
"|" +
71-
"(\\s)?qt(-)?bug(s(:?))?(-)?" +
72-
"|" +
73-
"(\\s)?qt(-)?3ds(s(:?))?(-)?" +
74-
"|" +
75-
"(\\s)?qt(-)?jira(s(:?))?(-)?" +
76-
"|" +
77-
"(\\s)?qt(-)?creatorbug(s(:?))?(-)?" +
78-
"|" +
79-
"(\\s)?qt(-)?ifw(s(:?))?(-)?" +
80-
"|" +
81-
"(\\s)?qt(-)?mobility(s(:?))?(-)?" +
82-
"|" +
83-
"(\\s)?qt(-)?playground(s(:?))?(-)?" +
84-
"|" +
85-
"(\\s)?qt(-)?website(s(:?))?(-)?" +
86-
"|" +
87-
"(\\s)?qt(-)?qainfra(s(:?))?(-)?" +
88-
"|" +
89-
"(\\s)?qt(-)?components(s(:?))?(-)?" +
90-
"|" +
91-
"(\\s)?qt(-)?solbug(s(:?))?(-)?" +
92-
"|" +
93-
"(\\s)?qt(-)?vsaddinbug(s(:?))?(-)?" +
94-
"|" +
95-
"(\\s)?qt(-)?wb(s(:?))?(-)?" +
96-
"|" +
97-
"(\\s)?qt(-)?sysadm(s(:?))?(-)?" +
98-
"|" +
99-
"[?]id(=)?" +
100-
"|" +
101-
"(\\s)?bug(s(:?))?(\\s)?(#)?" +
102-
"|" +
103-
"(\\s)?qbs(s(:?))?(\\s)?(#)?" +
104-
"|" +
105-
"(\\s)?autosuite(s(:?))?(\\s)?(#)?" +
106-
"|" +
107-
"(\\s)?qds(s(:?))?(\\s)?(#)?" +
108-
"|" +
109-
"(\\s)?pyside(s(:?))?(\\s)?(#)?" +
110-
"|" +
111-
"(\\s)?qsr(s(:?))?(\\s)?(#)?" +
112-
"))";
65+
11366
this.sepTerm = "(,(\\s)?|(\\s)?-(\\s)?|(\\s)?and(\\s)?|(\\s)?or(\\s)?|(\\s)?to(\\s)?|(\\s)?in(\\s)?)";
11467
this.externalTerm = "((\\s)?contract(\\s)?|(\\s)?tender documentation(\\s)?|(\\s)?technical specifications(\\s)?"
11568
+ "|(\\s)?contractor proposal(\\s)?|(\\s)?tendering documentation(\\s)?)";
11669

70+
if (grammar == null)
71+
loadDefaultGrammar();
72+
else
73+
loadCustomGrammar(grammar);
74+
11775
// Expressions
11876

11977
this.numExp = "(([\\(]?((\\d)+|[a-zA-Z])[(\\.(\\d)+(\\.)?)]*[\\)]?)(?![a-zA-Z]).?|(\\(?(ix|iv|v?i{1,3}|x?i{1,3})\\)?)(?![a-zA-Z]).?)";
@@ -145,8 +103,25 @@ public Grammar() {
145103

146104
this.grammar = "(" + externalExp + "|" + complexExp + "|" + simpleExp + ")";
147105

106+
}
107+
108+
private void loadCustomGrammar(com.essi.Dependency.Components.Grammar grammar ) {
109+
this.bugPrefixs = new String[grammar.getPrefixes().size()];
110+
this.bugPrefixs = grammar.getPrefixes().toArray(this.bugPrefixs);
111+
this.markerTerm = "((\\s+|\\(|\\|)(section(s)?(\\s)?|sect\\.(\\s)?|subsection(s)?(\\s)?|subsect\\.(\\s)?|paragraph(s)?(\\s)?|"
112+
+ "subparagraph(s)?(\\s)?|subitem(s)?(\\s)?|volume(s)?(\\s)?|lot('s)?(\\s)?|book(s)?(\\s)?"
113+
+ "|article(s)?(\\s)?|item(s)?(\\s)?|point(s)?(\\s)?";
114+
115+
for (String prefix : grammar.getPrefixes()) {
116+
this.markerTerm += "|" + "(\\s)?" + prefix + "(s(:?))?(-)?";
117+
}
118+
this.markerTerm += "))";
119+
120+
}
121+
122+
private void loadDefaultGrammar() {
148123
this.bugPrefixs = new String[]{
149-
"QBS",
124+
"QBS",
150125
"QTBUG",
151126
"QT3DS" ,
152127
"AUTOSUITE" ,
@@ -164,7 +139,55 @@ public Grammar() {
164139
"QTSOLBUG" ,
165140
"QTVSADDINBUG" ,
166141
"QTWB" ,
167-
"QTSYSADM"};
142+
"QTSYSADM"
143+
};
144+
145+
this.markerTerm = "((\\s+|\\(|\\|)(section(s)?(\\s)?|sect\\.(\\s)?|subsection(s)?(\\s)?|subsect\\.(\\s)?|paragraph(s)?(\\s)?|"
146+
+ "subparagraph(s)?(\\s)?|subitem(s)?(\\s)?|volume(s)?(\\s)?|lot('s)?(\\s)?|book(s)?(\\s)?"
147+
+ "|article(s)?(\\s)?|item(s)?(\\s)?|point(s)?(\\s)?" +
148+
"|" +
149+
"(\\s)?qt(-)?bug(s(:?))?(-)?" +
150+
"|" +
151+
"(\\s)?qt(-)?3ds(s(:?))?(-)?" +
152+
"|" +
153+
"(\\s)?qt(-)?jira(s(:?))?(-)?" +
154+
"|" +
155+
"(\\s)?qt(-)?creatorbug(s(:?))?(-)?" +
156+
"|" +
157+
"(\\s)?qt(-)?ifw(s(:?))?(-)?" +
158+
"|" +
159+
"(\\s)?qt(-)?mobility(s(:?))?(-)?" +
160+
"|" +
161+
"(\\s)?qt(-)?playground(s(:?))?(-)?" +
162+
"|" +
163+
"(\\s)?qt(-)?website(s(:?))?(-)?" +
164+
"|" +
165+
"(\\s)?qt(-)?qainfra(s(:?))?(-)?" +
166+
"|" +
167+
"(\\s)?qt(-)?components(s(:?))?(-)?" +
168+
"|" +
169+
"(\\s)?qt(-)?solbug(s(:?))?(-)?" +
170+
"|" +
171+
"(\\s)?qt(-)?vsaddinbug(s(:?))?(-)?" +
172+
"|" +
173+
"(\\s)?qt(-)?wb(s(:?))?(-)?" +
174+
"|" +
175+
"(\\s)?qt(-)?sysadm(s(:?))?(-)?" +
176+
"|" +
177+
"[?]id(=)?" +
178+
"|" +
179+
"(\\s)?bug(s(:?))?(\\s)?(#)?" +
180+
"|" +
181+
"(\\s)?qbs(s(:?))?(\\s)?(#)?" +
182+
"|" +
183+
"(\\s)?autosuite(s(:?))?(\\s)?(#)?" +
184+
"|" +
185+
"(\\s)?qds(s(:?))?(\\s)?(#)?" +
186+
"|" +
187+
"(\\s)?pyside(s(:?))?(\\s)?(#)?" +
188+
"|" +
189+
"(\\s)?qsr(s(:?))?(\\s)?(#)?" +
190+
"))";
168191
}
169192

170193
/**
@@ -177,7 +200,7 @@ public Grammar() {
177200
* @return ArrayList
178201
* @throws InterruptedException
179202
*/
180-
public ArrayList<Object> applyGrammar(ArrayList<Object> expressionList, ArrayList<Object> expressionDB)
203+
public ArrayList<Object> applyGrammar(com.essi.Dependency.Components.Grammar grammarObj, ArrayList<Object> expressionList, ArrayList<Object> expressionDB)
181204
throws InterruptedException {
182205

183206
ArrayList<Object> dependencies = new ArrayList<>();
@@ -186,7 +209,7 @@ public ArrayList<Object> applyGrammar(ArrayList<Object> expressionList, ArrayLis
186209
Pattern pattern = Pattern.compile("(" + grammar + ")");
187210
for (Object expression : expressionList) {
188211

189-
FutureTask task = new FutureTask(new CallableTask(expression, expressionDB, pattern, dep));
212+
FutureTask task = new FutureTask(new CallableTask(grammarObj, expression, expressionDB, pattern, dep));
190213
ExecutorService executor = Executors.newSingleThreadExecutor();
191214
executor.submit(task);
192215

src/main/java/com/essi/Dependency/Service/DependencyService.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.sql.Statement;
1212
import java.util.ArrayList;
1313

14+
import com.essi.Dependency.Repository.GrammarRepository;
1415
import org.springframework.beans.factory.annotation.Autowired;
1516
//import org.springframework.jdbc.core.JdbcTemplate;
1617
import org.springframework.stereotype.Service;
@@ -28,6 +29,9 @@
2829
@Service
2930
public class DependencyService {
3031

32+
@Autowired
33+
private GrammarRepository grammarRepository;
34+
3135
private final Path rootLocation;
3236
private ClauseExtraction clauseExtraction;
3337
private Grammar grammar;
@@ -45,7 +49,7 @@ public class DependencyService {
4549
public DependencyService(StorageProperties properties) {
4650
this.rootLocation = Paths.get(properties.getLocation());
4751
this.clauseExtraction = new ClauseExtraction();
48-
this.grammar = new Grammar();
52+
this.grammar = new Grammar(null);
4953
}
5054

5155
/**
@@ -207,22 +211,9 @@ public ArrayList<Object> extractClauseList() throws IOException {
207211
* @throws IOException
208212
* @throws InterruptedException
209213
*/
210-
public ArrayList<Object> getDependencies() throws IOException, InterruptedException {
211-
return grammar.applyGrammar(this.clauseList, this.clauseList);
212-
}
213-
214-
/**
215-
* Service function to get the first N dependencies after applying the grammar
216-
* and analyze the expressions.
217-
*
218-
* @param n
219-
* @return
220-
* @throws IOException
221-
* @throws InterruptedException
222-
*/
223-
public ArrayList<Object> getNDependencies(String n) throws IOException, InterruptedException {
224-
return grammar.applyGrammar(new ArrayList<Object>(this.clauseList.subList(0, Integer.parseInt(n))),
225-
this.clauseList);
214+
public ArrayList<Object> getDependencies(String company) throws IOException, InterruptedException {
215+
com.essi.Dependency.Components.Grammar grammarObj = grammarRepository.findByCompany(company);
216+
return this.grammar.applyGrammar(grammarObj, this.clauseList, this.clauseList);
226217
}
227218

228219
/**
@@ -235,8 +226,10 @@ public ArrayList<Object> getNDependencies(String n) throws IOException, Interrup
235226
* @throws IOException
236227
* @throws InterruptedException
237228
*/
238-
public ArrayList<Object> getNMDependencies(String n, String m) throws IOException, InterruptedException {
239-
return grammar.applyGrammar(
229+
public ArrayList<Object> getNMDependencies(String company, String n, String m) throws IOException, InterruptedException {
230+
com.essi.Dependency.Components.Grammar grammarObj = grammarRepository.findByCompany(company);
231+
return this.grammar.applyGrammar(
232+
grammarObj,
240233
new ArrayList<Object>(this.clauseList.subList(Integer.parseInt(n), Integer.parseInt(m))),
241234
this.clauseList);
242235
}

src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ spring.jpa.hibernate.ddl-auto=update
77
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
88
spring.datasource.url=jdbc:mariadb://localhost/cross_reference_db
99
spring.datasource.username=root
10-
spring.datasource.password=Uoh45cmP
10+
spring.datasource.password=Uoh45cmP
11+
company=null

0 commit comments

Comments
 (0)