Skip to content

Commit

Permalink
Queries Smart Data Models for smarter code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
computate committed Jan 19, 2024
1 parent 754aa37 commit 8c211ce
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 59 deletions.
111 changes: 57 additions & 54 deletions src/main/java/org/computate/frFR/java/EcrireGenClasse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6359,47 +6359,49 @@ public void ecrireClasseCommentaire(JsonObject langueConfig, String siteNom, Str
if(classeSmartDataModel != null && !classeFiware) {
File smartDataModelSpecFile = new File(System.getenv("HOME"), String.format(".local/src/smart-data-models/%s/dataModel.%s/%s/model.yaml", classeSmartDataDomain, classeSmartDataSubModule, classeSmartDataModel));
if(smartDataModelSpecFile.exists()) {
ToutEcrivain wSmartDataModel = ToutEcrivain.create();
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(FileUtils.readFileToString(smartDataModelSpecFile, StandardCharsets.UTF_8));
JsonObject spec = new JsonObject(map);
JsonObject properties = spec.getJsonObject(classeSmartDataModel).getJsonObject("properties");
l();
tl(0, " * <h1>FIWARE SmartDataModel fields:</h1>");
wSmartDataModel.l();
wSmartDataModel.tl(0, "FIWARE SmartDataModel fields:");
wSmartDataModel.l();

l(" * {@literal /}{@literal **}<br>");
l(" * {@literal * SmartDataModel: ", classeSmartDataModel, " - ", classeSmartDataSubModule, " - ", classeSmartDataDomain, "}<br>");
l(" * {@literal * Fiware: true}<br>");
l(" *<br>");
l(" * {@literal * Model: true}<br>");
l(" * {@literal * SqlOrder: 1}<br>");
l(" * {@literal * Api: true}<br>");
l(" * {@literal * Page: true}<br>");
l(" * {@literal * SuperPage.enUS: BaseModelPage}<br>");
l(" * {@literal * Indexed: true}<br>");
l(" * {@literal * Order: 1}<br>");
l(" * {@literal * Description: }<br>");
l(" * {@literal * ApiTag.enUS: ", classeNomSimple, "}<br>");
l(" * {@literal * ApiUri.enUS: /api/", classeNomSimple, "}<br>");
l(" * <br>");
l(" * {@literal * ApiMethod.enUS: Search}<br>");
l(" * {@literal * ApiMethod: GET}<br>");
l(" * {@literal * ApiMethod: PATCH}<br>");
l(" * {@literal * ApiMethod: POST}<br>");
l(" * {@literal * ApiMethod: PUTImport}<br>");
l(" * <br>");
l(" * {@literal * ApiMethod.enUS: SearchPage}<br>");
l(" * {@literal * Page.SearchPage.enUS: ", classeNomSimple, "Page}<br>");
l(" * {@literal * ApiUri.SearchPage.enUS: /", classeNomSimple, "}<br>");
l(" * <br>");
l(" * {@literal * Role.enUS: SiteAdmin}<br>");
l(" * <br>");
l(" * {@literal * AName.enUS: a ", classeNomSimple, "}<br>");
l(" * {@literal * Color: 2017-shaded-spruce}<br>");
l(" * {@literal * IconGroup: duotone}<br>");
l(" * {@literal * IconName: map-location-dot}<br>");
l(" * {@literal * Rows: 100}<br>");
l(" * {@literal **}{@literal /}<br>");
l(" * {@literal public class ", classeNomSimple, " extends ", classeNomSimple, "Gen<BaseModel>} {<br>");
wSmartDataModel.l("/**");
wSmartDataModel.l(" * SmartDataModel: ", classeSmartDataModel, " - ", classeSmartDataSubModule, " - ", classeSmartDataDomain);
wSmartDataModel.l(" * Fiware: true");
wSmartDataModel.l(" *");
wSmartDataModel.l(" * Model: true");
wSmartDataModel.l(" * SqlOrder: 1");
wSmartDataModel.l(" * Api: true");
wSmartDataModel.l(" * Page: true");
wSmartDataModel.l(" * SuperPage.enUS: BaseModelPage");
wSmartDataModel.l(" * Indexed: true");
wSmartDataModel.l(" * Order: 1");
wSmartDataModel.l(" * Description: ");
wSmartDataModel.l(" * ApiTag.enUS: ", classeNomSimple);
wSmartDataModel.l(" * ApiUri.enUS: /api/", classeNomSimple);
wSmartDataModel.l(" *");
wSmartDataModel.l(" * ApiMethod.enUS: Search");
wSmartDataModel.l(" * ApiMethod: GET");
wSmartDataModel.l(" * ApiMethod: PATCH");
wSmartDataModel.l(" * ApiMethod: POST");
wSmartDataModel.l(" * ApiMethod: PUTImport");
wSmartDataModel.l(" *");
wSmartDataModel.l(" * ApiMethod.enUS: SearchPage");
wSmartDataModel.l(" * Page.SearchPage.enUS: ", classeNomSimple, "Page");
wSmartDataModel.l(" * ApiUri.SearchPage.enUS: /", classeNomSimple);
wSmartDataModel.l(" *");
wSmartDataModel.l(" * Role.enUS: SiteAdmin");
wSmartDataModel.l(" *");
wSmartDataModel.l(" * AName.enUS: a ", classeNomSimple);
wSmartDataModel.l(" * Color: 2017-shaded-spruce");
wSmartDataModel.l(" * IconGroup: duotone");
wSmartDataModel.l(" * IconName: map-location-dot");
wSmartDataModel.l(" * Rows: 100");
wSmartDataModel.l(" **/");
wSmartDataModel.l("public class ", classeNomSimple, " extends ", classeNomSimple, "Gen<BaseModel> {");

Integer row = 3;
Integer cell = 1;
Expand All @@ -6423,36 +6425,37 @@ else if("number".equals(jsonType))
javaType = "BigDecimal";
else if("location".equals(fieldName))
javaType = "Path";
l(" * <br>");
l(" * {@literal /**}<br>");
l(" * {@literal * {@inheritDoc}}<br>");
wSmartDataModel.l();
wSmartDataModel.l(" /**");
wSmartDataModel.l(" * {@inheritDoc}");
if("areaServed".equals(fieldName)) {
l(" * {@literal * FiwareType: geo:linestring}<br>");
l(" * {@literal * Location: true}<br>");
wSmartDataModel.l(" * FiwareType: geo:linestring");
wSmartDataModel.l(" * Location: true");
}
if("location".equals(fieldName)) {
l(" * {@literal * FiwareType: geo:point}<br>");
wSmartDataModel.l(" * FiwareType: geo:point");
}
l(" * {@literal * DocValues: true}<br>");
l(" * {@literal * Persist: true}<br>");
l(" * {@literal * DisplayName: ", StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(fieldName), " ").toLowerCase(), "}<br>");
wSmartDataModel.l(" * DocValues: true");
wSmartDataModel.l(" * Persist: true");
wSmartDataModel.l(" * DisplayName: ", StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(fieldName), " ").toLowerCase(), "");
if(description != null)
l(" * {@literal * Description: ", description.replace("\r\n", " ").replace("\n", " "), "}<br>");
l(" * {@literal * HtmRow: ", row, "}<br>");
l(" * {@literal * HtmCell: ", cell, "}<br>");
l(" * {@literal * Facet: true}<br>");
l(" * {@literal **}{@literal /}<br>");
l(" * {@literal protected void _", fieldName, "(Wrap<", javaType, "> w)} {}<br>");
wSmartDataModel.l(" * Description: ", description.replace("\r\n", " ").replace("\n", " "), "");
wSmartDataModel.l(" * HtmRow: ", row, "");
wSmartDataModel.l(" * HtmCell: ", cell, "");
wSmartDataModel.l(" * Facet: true");
wSmartDataModel.l(" **/");
wSmartDataModel.l(" protected void _", fieldName, "(Wrap<", javaType, "> w) {}");
cell++;
if(cell > 3) {
row++;
cell = 1;
}
wSmartDataModel.l();
}
}
l(" * }<br>");

l();
wSmartDataModel.l("}");
wSmartDataModel.l();
System.out.println(wSmartDataModel);
}
}
} catch(Exception ex) {
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/org/computate/frFR/java/IndexerClasse.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.Normalizer;
import java.text.NumberFormat;
Expand Down Expand Up @@ -52,6 +54,7 @@

import javax.imageio.ImageIO;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.configuration2.YAMLConfiguration;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
Expand Down Expand Up @@ -91,6 +94,7 @@
import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

/**
* NomCanonique.enUS: org.computate.enUS.java.IndexClass
Expand Down Expand Up @@ -5385,11 +5389,28 @@ else if(langueConfig.getString(ConfigCles.var_PUTFusion).equals(classeApiMethode

String classeSmartDataModelStr = regex("^SmartDataModel:\\s(.*)", classeCommentaire);
if(classeSmartDataModelStr != null) {
Matcher m = Pattern.compile("([\\w ]+) - ([\\w ]+) - ([\\w ]+)").matcher(classeCommentaire);
if (m.find()) {
indexerStockerSolr(classeDoc, "classeSmartDataDomain", m.group(3).replace(" ", "").trim());
indexerStockerSolr(classeDoc, "classeSmartDataSubModule", m.group(2).trim());
indexerStockerSolr(classeDoc, "classeSmartDataModel", m.group(1).trim());
String encodedStr = "\"" + Arrays.asList(classeSmartDataModelStr.replaceAll("([a-z])([A-Z])", "$1 $2").split(" +")).stream().map(s -> encodeUrl(s)).collect(Collectors.joining("\" OR \"")) + "\"";
System.out.println(encodedStr);
SolrQuery rechercheSolr = new SolrQuery();
rechercheSolr.setQuery("domainName_suggested:" + encodedStr + "^2 OR domainName_text_enUS:" + encodedStr + "^5 OR submoduleShortName_suggested:" + encodedStr + "^3 OR submoduleShortName_text_enUS:" + encodedStr + "^10 OR modelName_suggested:" + encodedStr + "^3 OR modelName_text_enUS:" + encodedStr + "^20&fl=*,score&sort=score desc");
rechercheSolr.setRows(10);
rechercheSolr.addFilterQuery("computateFiwareSmartDataModel_docvalues_boolean:true");
rechercheSolr.setIncludeScore(true);
QueryResponse reponseRecherche = clientSolrComputate.query(rechercheSolr);
SolrDocumentList listeRecherche = reponseRecherche.getResults();
if(listeRecherche.size() > 0) {
SolrDocument doc = listeRecherche.get(0);
indexerStockerSolr(classeDoc, "classeSmartDataDomain", doc.getFieldValue("domainName_docvalues_string").toString().replace(" ", "").trim());
indexerStockerSolr(classeDoc, "classeSmartDataSubModule", doc.getFieldValue("submoduleShortName_docvalues_string").toString().trim());
indexerStockerSolr(classeDoc, "classeSmartDataModel", doc.getFieldValue("modelName_docvalues_string").toString().trim());
System.out.println(String.format("Top %s Smart Data Model results were: ", listeRecherche.size()));
System.out.println(String.format("Model - Submodule - Domain - score", listeRecherche.size()));
System.out.println(String.format("--------------------------", listeRecherche.size()));
for(Integer i = 0; i < listeRecherche.size(); i++) {
SolrDocument model = listeRecherche.get(i);
System.out.println(String.format("%s. %s %s %s %s", String.format("%02d", (i + 1)), model.getFieldValue("modelName_docvalues_string"), model.getFieldValue("submoduleShortName_docvalues_string"), model.getFieldValue("domainName_docvalues_string"), model.getFieldValue("score")));
}
System.out.println(" ");
}
}

Expand Down Expand Up @@ -5760,4 +5781,12 @@ else if("enUS".equals(langueNom)) {
clientSolrComputate.commit(false, false, true);
return classeDoc;
}

public String encodeUrl(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
}

0 comments on commit 8c211ce

Please sign in to comment.