Skip to content

Commit

Permalink
[LO extension] new cache mechanisms and reset of check for N paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKruse committed Jul 17, 2018
1 parent 2ca9652 commit 9b41c40
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class Configuration {

static final int DEFAULT_SERVER_PORT = 8081; // should be HTTPServerConfig.DEFAULT_PORT but we don't have that dependency
static final int DEFAULT_NUM_CHECK_PARAS = 0; // default number of parameters to be checked by TextLevelRules in LO/OO
static final int DEFAULT_NUM_CHECK_PARAS = 5; // default number of parameters to be checked by TextLevelRules in LO/OO
static final int FONT_STYLE_INVALID = -1;
static final int FONT_SIZE_INVALID = -1;

Expand Down Expand Up @@ -100,7 +100,7 @@ public class Configuration {
private int fontSize = FONT_SIZE_INVALID;
private int serverPort = DEFAULT_SERVER_PORT;
private int numParasToCheck = DEFAULT_NUM_CHECK_PARAS;
private boolean doResetCheck = false;
private boolean doResetCheck = true;
private String externalRuleDirectory;
private String lookAndFeelName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,43 @@ public List<int[]> getFootnotePositions() {
}
}

/**
* Returns Number of all FlatParagraphs of Document

This comment has been minimized.

Copy link
@danielnaber

danielnaber Jul 17, 2018

Member

This seems misleading, the method doesn't return anything?

This comment has been minimized.

Copy link
@FredKruse

FredKruse Jul 17, 2018

Author Contributor

Sorry, that's a copy/paste error

* Returns negative value if it fails
*/
public void markFlatParasAsChecked(int from, int to) {
try {
if(xFlatParaIter == null || xFlatPara == null) {
return;
}
XFlatParagraph tmpFlatPara = xFlatPara;
XFlatParagraph startFlatPara = xFlatPara;
while (tmpFlatPara != null) {
startFlatPara = tmpFlatPara;
tmpFlatPara = xFlatParaIter.getParaBefore(tmpFlatPara);
}
tmpFlatPara = startFlatPara;
int num = 0;
while (tmpFlatPara != null && num < from) {
tmpFlatPara.setChecked(TextMarkupType.PROOFREADING, true);
tmpFlatPara = xFlatParaIter.getParaAfter(tmpFlatPara);
num++;
}
while (tmpFlatPara != null && num < to) {
tmpFlatPara = xFlatParaIter.getParaAfter(tmpFlatPara);
num++;
}
while (tmpFlatPara != null) {
tmpFlatPara.setChecked(TextMarkupType.PROOFREADING, true);
tmpFlatPara = xFlatParaIter.getParaAfter(tmpFlatPara);
num++;
}
} catch (Throwable t) {
printException(t); // all Exceptions thrown by UnoRuntime.queryInterface are caught
}
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public class Main extends WeakBase implements XJobExecutor,
private static final String MANUAL_LINEBREAK = "\r"; // to distinguish from paragraph separator
private static final String ZERO_WIDTH_SPACE = "\u200B"; // Used to mark footnotes
private static final String logLineBreak = System.getProperty("line.separator"); // LineBreak in Log-File (MS-Windows compatible)

private static final int PARA_CHECK_FACTOR = 10; // Factor for parameter checked at once at iteration (no text change)

private static final boolean debugMode = false; // should be false except for testing

private final List<XLinguServiceEventListener> xEventListeners;
Expand All @@ -135,6 +136,7 @@ public class Main extends WeakBase implements XJobExecutor,
private XComponentContext xContext;

private int numParasToCheck = 5; // will be overwritten by config
private int defaultParaCheck = 100; // will be overwritten by config
private boolean doResetCheck = true; // will be overwritten by config

private List<List<String>> allParas = null; // List of paragraphs (only readable by parallel thread)
Expand All @@ -148,13 +150,15 @@ public class Main extends WeakBase implements XJobExecutor,
private List<Integer> numLastFlPara; // Save position of FlatParagraph for the single documents
private String lastDocID = null; // docID of last checked document
private boolean textIsChecked = false; // false: check full text again (ignored by parallel thread)
private boolean textIsChanged = false; // false: check number of paragraphs again (ignored by parallel thread)
private boolean doFullTextCheck = true; // true: run full text check (not single paragraphs) (unchanged by parallel thread)
private int divNum; // difference between number of paragraphs from cursor and from flatParagraph (unchanged by parallel thread)
private int numLastVCPara1; // last paragraph for parallel thread (View Cursor)
private int numLastFlPara1; // last paragraph for parallel thread (FlatParagraph)
private XComponent goneContext = null; // save component of closed document
private int lastParaPos[] = {0, 0};
private int lastPara[] = {0, 0};
private int lastDocNum = 0;
private int lastParaNum = -1;

private boolean proofIsRunning = false;

Expand All @@ -168,6 +172,7 @@ private void prepareConfig(Language lang) {
try {
config = new Configuration(getHomeDir(), CONFIG_FILE, lang);
numParasToCheck = config.getNumParasToCheck();
defaultParaCheck = numParasToCheck * PARA_CHECK_FACTOR;
doResetCheck = config.isResetCheck();
disabledRules = config.getDisabledRuleIds();
if (disabledRules == null) {
Expand Down Expand Up @@ -430,24 +435,6 @@ private AnnotatedText getAnnotatedText(String text, int[] footnotePos, int start
return annotations.build();
}

// Old Implementation could be deleted if the new one is tested
private AnnotatedText getAnnotatedText(String sentence, int[] footnotePos, ProofreadingResult paRes) {
Set<Integer> correctedPos = new HashSet<>();
for (int pos : footnotePos) {
correctedPos.add(pos - paRes.nStartOfSentencePosition);
}
AnnotatedTextBuilder annotations = new AnnotatedTextBuilder();
// not very efficient but simple implementation:
for (int i = 0; i < sentence.length(); i++) {
if (correctedPos.contains(i)) {
annotations.addMarkup("ZERO_WIDTH_SPACE");
} else {
annotations.addText(String.valueOf(sentence.charAt(i)));
}
}
return annotations.build();
}

private void initLanguageTool() {
try {
prepareConfig(docLanguage);
Expand Down Expand Up @@ -540,52 +527,62 @@ private SingleProofreadingError[] checkParaRules(
}
numCurDoc = getNumDocID(docID);
numCurPara = getParaPos(paraText, numThread, numCurDoc);
paraPos = getStartOfParagraph(numCurPara, numCurDoc);
if (numThread > 0 || (numThread == 0
&& (startPos == 0 || paraPos != lastParaPos[numThread] || !sameDocID))) {
if (paraPos < 0) { // Position not found; check only Paragraph context
&& (startPos == 0 || numCurPara != lastPara[numThread] || !sameDocID))) {
if (numCurPara < 0) { // Position not found; check only Paragraph context
paragraphMatches = langTool.check(fixLinebreak(paraText), true,
JLanguageTool.ParagraphHandling.ONLYPARA);
if (numThread == 0) {
paragraphMatchesFirst = paragraphMatches;
} else {
paragraphMatchesSecond = paragraphMatches;
}
} else if (numParasToCheck > 0 && !doFullTextCheck) { // Check numParasToCheck paragraphs while text is changed
paragraphMatches = langTool.check(getDocAsString(numCurPara, numCurDoc), true,
} else if (numParasToCheck > 0 && !doFullTextCheck &&
(!textIsChecked || lastParaNum < 0 || numThread > 0
|| numCurPara < lastParaNum
|| numCurPara > lastParaNum + defaultParaCheck
|| numCurPara > allParas.get(numCurDoc).size() - numParasToCheck)) { // Check numParasToCheck paragraphs
paragraphMatches = langTool.check(getDocAsString(numCurPara, numCurDoc, numThread), true,
JLanguageTool.ParagraphHandling.ONLYPARA);
if (numThread == 0) {
paragraphMatchesFirst = paragraphMatches;
} else {
paragraphMatchesSecond = paragraphMatches;
}
if(textIsChanged) {
textIsChanged = false;
} else {
textIsChecked = true;
}
lastParaNum = numCurPara;
}
else if (!textIsChecked) { // Check Full Text only if not already checked
else if (doFullTextCheck && !textIsChecked) { // Check Full Text only if not already checked
if (debugMode) {
printToLogFile("check Text again");
}
fullTextMatches.set(numCurDoc, langTool.check(getDocAsString(numCurPara, numCurDoc), true,
fullTextMatches.set(numCurDoc, langTool.check(getDocAsString(numCurPara, numCurDoc, numThread), true,
JLanguageTool.ParagraphHandling.ONLYPARA));
textIsChecked = true; // mark Text as checked till next change; use saved fullTextMatches
}
lastParaPos[numThread] = paraPos;
lastPara[numThread] = numCurPara;
lastDocNum = numCurDoc;
if (paraPos < 0 || (numParasToCheck > 0 && !doFullTextCheck)) {
if (numCurPara < 0 || (numParasToCheck > 0 && !doFullTextCheck)) {
if (numThread == 0) {
paragraphMatches = paragraphMatchesFirst;
} else {
paragraphMatches = paragraphMatchesSecond;
}
}
} else {
if (paraPos < 0 || (numParasToCheck > 0 && !doFullTextCheck)) {
if (numCurPara < 0 || (numParasToCheck > 0 && !doFullTextCheck)) {
if (numThread == 0) {
paragraphMatches = paragraphMatchesFirst;
} else {
paragraphMatches = paragraphMatchesSecond;
}
}
}
paraPos = getStartOfParagraph(numCurPara, numCurDoc);
if ((paraPos < 0 || (numParasToCheck > 0 && !doFullTextCheck))
&& paragraphMatches != null && !paragraphMatches.isEmpty()) {
List<SingleProofreadingError> errorList = new ArrayList<>();
Expand Down Expand Up @@ -737,6 +734,7 @@ public final void runOptionsDialog() {
ConfigThread configThread = new ConfigThread(lang, config, this);
configThread.start();
numParasToCheck = config.getNumParasToCheck();
defaultParaCheck = numParasToCheck * PARA_CHECK_FACTOR;
doResetCheck = config.isResetCheck();
}

Expand Down Expand Up @@ -1061,14 +1059,19 @@ private int returnOneParaCheck() {
return -1;
}

private int returnContinueCheck(boolean isReset, int numCurPara, int numThread) {
private int returnContinueCheck(boolean isReset, boolean isChanged, int numCurPara, int numThread) {
if (numParasToCheck > 0) {
doFullTextCheck = false;
} else {
doFullTextCheck = true;
}
if (isReset && numThread == 0) {
textIsChecked = false;
if (numThread == 0) {
if(isReset) {
textIsChecked = false;
}
if(isChanged) {
textIsChanged = true;
}
}
return numCurPara;
}
Expand All @@ -1081,6 +1084,7 @@ private int returnNParaCheck(int numCurPara, int numThread) {
}
if (numThread == 0) {
textIsChecked = false;
textIsChanged = true;
}
return numCurPara;
}
Expand Down Expand Up @@ -1117,7 +1121,7 @@ private boolean resetAllParas(LOCursor loCursor, LOFlatParagraph loFlaPa, int do
/**
* Gives Back the full Text as String
*/
private String getDocAsString(int numCurPara, int docNum) {
private String getDocAsString(int numCurPara, int docNum, int thread) {
if (numCurPara < 0 || allParas.get(docNum) == null || allParas.get(docNum).size() < numCurPara - 1) {
return "";
}
Expand All @@ -1130,6 +1134,9 @@ private String getDocAsString(int numCurPara, int docNum) {
startPos = numCurPara - numParasToCheck;
if (startPos < 0) startPos = 0;
endPos = numCurPara + numParasToCheck;
if(!textIsChanged && thread == 0) {
endPos += defaultParaCheck;
}
if (endPos > allParas.get(docNum).size()) endPos = allParas.get(docNum).size();
}
String docText = fixLinebreak(allParas.get(docNum).get(startPos));
Expand All @@ -1148,7 +1155,7 @@ private int getStartOfParagraph(int nPara, int docNum) {
if (numParasToCheck < 1 || doFullTextCheck) {
startPos = 0;
} else {
startPos = nPara - numParasToCheck;
startPos = lastParaNum - numParasToCheck;
if (startPos < 0) startPos = 0;
}
int pos = 0;
Expand Down Expand Up @@ -1201,6 +1208,7 @@ private int getParaPos(String chPara, int numThread, int docNum) {
}
int nParas;
boolean isReset = false;
boolean isChanged = false;

if (allParas.get(docNum) == null || allParas.get(docNum).size() < 1) {
if (numThread > 0) { // if numThread > 0: Thread may only read allParas
Expand Down Expand Up @@ -1232,13 +1240,27 @@ private int getParaPos(String chPara, int numThread, int docNum) {
if (loFlaPa == null) {
loFlaPa = new LOFlatParagraph(xContext);
}
List<String> oldParas = allParas.get(docNum);
if (!resetAllParas(loCursor.get(docNum), loFlaPa, docNum)) {
return returnOneParaCheck();
}
if(doResetCheck) {
resetCheck();
if(numParasToCheck > 0) {
int from;
for(from = 0; from < allParas.get(docNum).size()
&& allParas.get(docNum).get(from).equals(oldParas.get(from)); from++);
from -= 1 + numParasToCheck;
int to;
for(to = 1; to <= allParas.get(docNum).size() && to <= oldParas.size()
&& allParas.get(docNum).get(allParas.get(docNum).size() - to).equals(
oldParas.get(oldParas.size() - to)); to++);
to = allParas.get(docNum).size() + numParasToCheck - to;
loFlaPa.markFlatParasAsChecked(from, to);
}
}
isReset = true;
isChanged = true;
}

// try to get next position from last ViewCursorPosition (proof per dialog box)
Expand All @@ -1255,7 +1277,7 @@ private int getParaPos(String chPara, int numThread, int docNum) {
} else {
numLastVCPara.set(docNum, nParas);
}
return returnContinueCheck(isReset, nParas, numThread);
return returnContinueCheck(isReset, isChanged, nParas, numThread);
}

// try to get next position from last Position of FlatParagraph (automatic Iteration without Text change)
Expand All @@ -1271,7 +1293,7 @@ private int getParaPos(String chPara, int numThread, int docNum) {
} else {
numLastFlPara.set(docNum, nParas);
}
return returnContinueCheck(isReset, nParas, numThread);
return returnContinueCheck(isReset, isChanged, nParas, numThread);
}

// try to get ViewCursor position (proof initiated by mouse click)
Expand All @@ -1282,7 +1304,7 @@ private int getParaPos(String chPara, int numThread, int docNum) {
} else {
numLastVCPara.set(docNum, nParas);
}
return returnContinueCheck(isReset, nParas, numThread);
return returnContinueCheck(isReset, isChanged, nParas, numThread);
}

// try to get paragraph position from automatic iteration
Expand Down Expand Up @@ -1325,14 +1347,17 @@ private int getParaPos(String chPara, int numThread, int docNum) {
allParas.get(docNum).set(nParas, chPara);
if(doResetCheck) {
resetCheck();
if(numParasToCheck > 0) {
loFlaPa.markFlatParasAsChecked(nParas - numParasToCheck, nParas + numParasToCheck);
}
}
return returnNParaCheck(nParas, numThread);
}
}
if (debugMode) {
printToLogFile("nParas from FlatParagraph: " + nParas);
}
return returnContinueCheck(isReset, nParas, numThread);
return returnContinueCheck(isReset, isChanged, nParas, numThread);
} catch (Throwable t) {
showError(t);
return returnOneParaCheck();
Expand Down

0 comments on commit 9b41c40

Please sign in to comment.