Commit 53ece542 authored by Angel Borroy's avatar Angel Borroy

Using random sentences and words for tokenized fields.

parent c14edb50
......@@ -18,7 +18,7 @@ import org.springframework.util.StopWatch;
public class App implements CommandLineRunner
{
private static Logger log = LoggerFactory.getLogger(App.class);
private static Logger LOG = LoggerFactory.getLogger(App.class);
private static final int NUM_DOCS = 1000;
private static final boolean ADD_RELATIONSHIPS = false;
......@@ -37,11 +37,11 @@ public class App implements CommandLineRunner
StopWatch watch = new StopWatch("CMIS Operations");
watch.start("Document creation");
log.info("--Getting admin user ObjectId...");
LOG.info("--Getting admin user ObjectId...");
CmisObject adminUser = cmisService.getAdminUser();
log.info("... " + adminUser.getId());
LOG.info("... " + adminUser.getId());
log.info("--Creating documents...");
LOG.info("--Creating documents...");
Folder rootFolder = cmisService.getRootFolder();
Folder currentFolder = null;
......@@ -51,34 +51,34 @@ public class App implements CommandLineRunner
if (i % 100 == 0)
{
currentFolder = cmisService.createFolder(rootFolder, String.valueOf(i));
log.info("created folder " + i + " " + currentFolder.getId());
LOG.info("created folder " + i + " " + currentFolder.getId());
}
Document doc = cmisService.createDocumentSampleContent(currentFolder, "document-" + i, ".txt");
log.info("\tcreated document " + i + " "+ doc.getId());
LOG.info("\tcreated document " + i + " "+ doc.getId());
if (ADD_RELATIONSHIPS)
{
Item item = cmisService.createSampleItem(currentFolder, "document-" + i);
log.info("\t\tcreated item " + i + " "+ item.getId());
LOG.info("\t\tcreated item " + i + " "+ item.getId());
ObjectId relationship = cmisService.createRelationship(doc, item, "R:otp:documentBackOfficeDocumentForm");
log.info("\t\tcreated doc relationship " + relationship);
LOG.info("\t\tcreated doc relationship " + relationship);
cmisService.createRelationship(doc, adminUser, "R:otp:documentNew");
cmisService.createRelationship(doc, adminUser, "R:otp:documentApproval");
cmisService.createRelationship(doc, adminUser, "R:otp:documentAgreed");
cmisService.createRelationship(doc, adminUser, "R:otp:documentExecution");
cmisService.createRelationship(doc, adminUser, "R:otp:documentStarter");
log.info("\t\tcreated user relationships with " + adminUser.getId());
LOG.info("\t\tcreated user relationships with " + adminUser.getId());
}
}
log.info("... documents created---");
LOG.info("... documents created---");
watch.stop();
log.info(watch.prettyPrint());
LOG.info(watch.prettyPrint());
System.exit(0);
......
......@@ -9,6 +9,7 @@ import java.util.Map;
import javax.annotation.PostConstruct;
import org.alfresco.cmis.assoc.util.WordGenerator;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
......@@ -45,7 +46,6 @@ import org.springframework.stereotype.Service;
@Service
public class CmisService
{
// Set values from "application.properties" file
@Value("${alfresco.repository.url}")
String alfrescoUrl;
......@@ -59,6 +59,9 @@ public class CmisService
@Autowired
ResourceLoader resourceLoader;
@Autowired
WordGenerator wordGenerator;
@PostConstruct
public void init()
......@@ -114,7 +117,7 @@ public class CmisService
properties.put(PropertyIds.OBJECT_TYPE_ID, "D:otp:document");
properties.put(PropertyIds.NAME, documentName + documentExt);
Resource contentFile = resourceLoader.getResource("classpath:cryptonomicon.txt");
Resource contentFile = resourceLoader.getResource("classpath:in-the-beggining-command.txt");
ContentStream contentStream = new ContentStreamImpl(documentName + documentExt, null,
"text/plain", contentFile.getInputStream());
......@@ -123,24 +126,25 @@ public class CmisService
addAspect(doc, "P:cm:titled");
properties = new HashMap<>();
properties.put("cm:title", "Title");
properties.put("cm:description", "Description");
properties.put("otp:processId", "processId");
properties.put("otp:originalPresent", "originalPresent");
properties.put("otp:sourceDocId", "sourceDocId");
properties.put("otp:docType", "docType");
properties.put("otp:docBarcode", "docBarcode");
properties.put("otp:fileName", "fileName");
properties.put("otp:fileType", "fileType");
properties.put("otp:employeeFullName", "employeeFullName");
properties.put("otp:employeeLoginSs", "employeeLoginSs");
properties.put("otp:departmentIdSs", "departmentIdSs");
properties.put("otp:departmentIdSap", "departmentIdSap");
properties.put("otp:departmentIdPath", "departmentIdPath");
properties.put("otp:deleted", "deleted");
properties.put("otp:docInboxEmployeeId", "docInboxEmployeeId");
properties.put("otp:docInboxEmployeeNm", "docInboxEmployeeNm");
properties.put("otp:docMessageId", "docMessageId");
properties.put("cm:title", wordGenerator.getSentence());
properties.put("cm:description", wordGenerator.getSentence());
properties.put("otp:processId", wordGenerator.getWord());
properties.put("otp:originalPresent", wordGenerator.getWord());
properties.put("otp:sourceDocId", wordGenerator.getWord());
properties.put("otp:docType", wordGenerator.getWord());
properties.put("otp:docBarcode", wordGenerator.getWord());
properties.put("otp:fileName", wordGenerator.getWord());
properties.put("otp:fileType", wordGenerator.getWord());
properties.put("otp:employeeFullName", wordGenerator.getSentence());
properties.put("otp:employeeLoginSs", wordGenerator.getSentence());
properties.put("otp:departmentIdSs", wordGenerator.getSentence());
properties.put("otp:departmentIdSap", wordGenerator.getSentence());
properties.put("otp:departmentIdPath", wordGenerator.getSentence());
properties.put("otp:deleted", wordGenerator.getWord());
properties.put("otp:docInboxEmployeeId", wordGenerator.getWord());
properties.put("otp:docInboxEmployeeNm", wordGenerator.getSentence());
properties.put("otp:docMessageId", wordGenerator.getSentence());
updateProperties(doc, properties);
return doc;
......@@ -240,5 +244,5 @@ public class CmisService
}
session.delete(object);
}
}
package org.alfresco.cmis.assoc.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
@Service
public class WordGenerator
{
private Random r = new Random();
@Value("classpath:alice-oz.txt")
Resource contentFile;
String[] words;
private void initWords()
{
try
{
byte[] bytes = contentFile.getInputStream().readAllBytes();
words = new String(bytes).trim().split(" ");
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
public String markov(int keySize, int outputSize)
{
if (words == null) initWords();
if (keySize < 1) throw new IllegalArgumentException("Key size can't be less than 1");
if (outputSize < keySize || outputSize >= words.length) {
throw new IllegalArgumentException("Output size is out of range");
}
Map<String, List<String>> dict = new HashMap<>();
for (int i = 0; i < (words.length - keySize); ++i) {
StringBuilder key = new StringBuilder(words[i]);
for (int j = i + 1; j < i + keySize; ++j) {
key.append(' ').append(words[j]);
}
String value = (i + keySize < words.length) ? words[i + keySize] : "";
if (!dict.containsKey(key.toString())) {
ArrayList<String> list = new ArrayList<>();
list.add(value);
dict.put(key.toString(), list);
} else {
dict.get(key.toString()).add(value);
}
}
int n = 0;
int rn = r.nextInt(dict.size());
String prefix = (String) dict.keySet().toArray()[rn];
List<String> output = new ArrayList<>(Arrays.asList(prefix.split(" ")));
while (true) {
List<String> suffix = dict.get(prefix);
if (suffix == null)
{
return "random";
}
else
{
if (suffix.size() == 1) {
if (Objects.equals(suffix.get(0), "")) return output.stream().reduce("", (a, b) -> a + " " + b);
output.add(suffix.get(0));
} else {
rn = r.nextInt(suffix.size());
output.add(suffix.get(rn));
}
if (output.size() >= outputSize) return output.stream().limit(outputSize).reduce("", (a, b) -> a + " " + b);
n++;
prefix = output.stream().skip(n).limit(keySize).reduce("", (a, b) -> a + " " + b).trim();
}
}
}
public String getSentence()
{
String sentence = markov(4, 4).replaceAll("[^a-zA-Z ]", "").trim();
return sentence;
}
public String getWord()
{
String word = markov(1, 1).replaceAll("[^a-zA-Z ]", "").trim();
return word;
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment