Skip to content

Commit 2aa5116

Browse files
Merge pull request #24 from griddigit-ci/devel
Fixed datatype resolving issues when input data is missing. Hotfix will be published.
2 parents db17ac0 + de27807 commit 2aa5116

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

CimPal-Main/src/main/java/eu/griddigit/cimpal/main/core/ModelManipulationFactory.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static Set<Resource> LoadRDFEnum(String xmlBase, List<String> profileURIs
211211
}
212212

213213
//Model transformation method
214-
public static void modelTransformation(List<File> fileOrigModelList, List<File> fileSHACLTransList) throws IOException {
214+
public static void modelTransformation(List<File> fileOrigModelList, List<File> fileSHACLTransList) throws IOException {
215215

216216
// Load your data and shapes (SHACL rules)
217217

@@ -269,7 +269,7 @@ public static void modelTransformation(List<File> fileOrigModelList, List<File>
269269

270270
Model model1single = null;
271271
String baseIRI = "http://iec.ch/TC57/CIM100";
272-
Map<String,Model> dataModelMap = new HashMap<>();
272+
Map<String, Model> dataModelMap = new HashMap<>();
273273
Map<String, String> prefixMap = new HashMap<>();
274274

275275
for (File item : fileOrigModelList) {
@@ -283,8 +283,8 @@ public static void modelTransformation(List<File> fileOrigModelList, List<File>
283283
}
284284
prefixMap.putAll(model1single.getNsPrefixMap());
285285
//get header ID
286-
String headerID = model1single.listStatements(null,RDF.type,ResourceFactory.createProperty("http://iec.ch/TC57/61970-552/ModelDescription/1#FullModel")).next().getSubject().getLocalName();
287-
dataModelMap.put(headerID,model1single);
286+
String headerID = model1single.listStatements(null, RDF.type, ResourceFactory.createProperty("http://iec.ch/TC57/61970-552/ModelDescription/1#FullModel")).next().getSubject().getLocalName();
287+
dataModelMap.put(headerID, model1single);
288288
}
289289

290290

@@ -314,7 +314,8 @@ public static void modelTransformation(List<File> fileOrigModelList, List<File>
314314
//Model rulesModel = eu.griddigit.cimpal.util.ModelFactory.modelLoad(fileSHACLTransList, null, rdfSourceFormat1, false);
315315

316316
// Execute the rules and store inferences
317-
Model inferredModel = ModelFactory.createDefaultModel();;
317+
Model inferredModel = ModelFactory.createDefaultModel();
318+
;
318319
List<Map.Entry<String, Model>> entries = new ArrayList<>(dataModelMap.entrySet());
319320

320321
// Process first entry separately if needed
@@ -347,8 +348,6 @@ public static void modelTransformation(List<File> fileOrigModelList, List<File>
347348
inferredModel.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
348349

349350

350-
351-
352351
// save file
353352
Map<String, Object> saveProperties = new HashMap<>();
354353
boolean sortRDF = true;
@@ -413,10 +412,9 @@ public static void modelTransformation(List<File> fileOrigModelList, List<File>
413412
saveProperties.put("useFileDialog", false);
414413

415414

416-
417415
}
418416

419-
public static void generateDataFromXls(String xmlBase, Map<String, Object> saveProperties, IOutputHandler outputHandler) throws IOException {
417+
public static void generateDataFromXls(String xmlBase, Map<String, Object> saveProperties, IOutputHandler outputHandler) throws IOException {
420418

421419
//this is to load profile data - this is needed for the export
422420
//Map<String, Map> loadDataMap = ModelManipulationFactory.loadDataForIGMMulDateTime(xmlBase, profileModelUnionFlag, instanceModelUnionFlag, inputData, shaclModelUnionFlag);
@@ -634,7 +632,7 @@ public static void generateDataFromXlsV2(String xmlBase, File xmlfile, Map<Strin
634632
throw new Exception("Couldn't find the sheet for class: " + className);
635633
}
636634
continue;
637-
} else if (((LinkedList<?>) o).size() <=4)
635+
} else if (((LinkedList<?>) o).size() <= 4)
638636
continue;
639637

640638
// getting classes to print
@@ -653,8 +651,7 @@ public static void generateDataFromXlsV2(String xmlBase, File xmlfile, Map<Strin
653651
if (headerClassName.isEmpty()) {
654652
try {
655653
headerClassName = ((LinkedList<?>) o).get(5).toString();
656-
}
657-
catch (IndexOutOfBoundsException e) {
654+
} catch (IndexOutOfBoundsException e) {
658655
throw new NoSuchElementException("Missing header class name from config tab.");
659656
}
660657
if (!headerClassName.isEmpty()) {
@@ -776,9 +773,8 @@ public static void generateDataFromXlsV2(String xmlBase, File xmlfile, Map<Strin
776773
case "Literal" -> { //add literal
777774
String datatype;
778775
try {
779-
datatype = ((LinkedList<?>) headerXlsData.get(3)).get(j).toString();
780-
}
781-
catch (IndexOutOfBoundsException e) {
776+
datatype = resolveDatatype(((LinkedList<?>) headerXlsData.get(3)).get(j), object);
777+
} catch (Exception e) {
782778
datatype = "string";
783779
}
784780
if (datatype.equalsIgnoreCase("float"))
@@ -866,7 +862,7 @@ else if (datatype.equalsIgnoreCase("integer"))
866862
Resource rdfidRes = ResourceFactory.createResource(rdfid);
867863

868864

869-
if (((LinkedList<?>) classXlsData.getFirst()).get(3).toString().equals("true")){
865+
if (((LinkedList<?>) classXlsData.getFirst()).get(3).toString().equals("true")) {
870866
rdfAboutList.add(ResourceFactory.createResource(classWNS));
871867
}
872868
for (int j = 0; j < cols; j++) {
@@ -910,16 +906,15 @@ else if (datatype.equalsIgnoreCase("integer"))
910906
String datatype;
911907
try {
912908
datatype = ((LinkedList<?>) classXlsData.get(3)).get(j).toString();
913-
}
914-
catch (Exception e){
909+
} catch (Exception e) {
915910
datatype = "";
916911
}
917912
if (datatype.equalsIgnoreCase("float"))
918913
model.add(ResourceFactory.createStatement(rdfidRes, propertyURIProp, ResourceFactory.createPlainLiteral(String.valueOf(Float.parseFloat(object)))));
919914
else if (datatype.equalsIgnoreCase("integer"))
920915
model.add(ResourceFactory.createStatement(rdfidRes, propertyURIProp, ResourceFactory.createPlainLiteral(String.valueOf(Math.round(Float.parseFloat(object))))));
921916
else
922-
model.add(ResourceFactory.createStatement(rdfidRes, propertyURIProp, ResourceFactory.createPlainLiteral(object)));
917+
model.add(ResourceFactory.createStatement(rdfidRes, propertyURIProp, ResourceFactory.createPlainLiteral(object)));
923918
}
924919
case "LiteralLangEN" -> {
925920
model.add(ResourceFactory.createStatement(rdfidRes, propertyURIProp, ResourceFactory.createLangLiteral(object, "en")));
@@ -969,7 +964,7 @@ else if (datatype.equalsIgnoreCase("integer"))
969964
}
970965
}
971966
}
972-
if (model.listStatements(rdfidRes, null, (RDFNode)null).hasNext()) {
967+
if (model.listStatements(rdfidRes, null, (RDFNode) null).hasNext()) {
973968
// If it has properties then add the primary class type
974969
model.add(ResourceFactory.createStatement(rdfidRes, RDF.type, ResourceFactory.createProperty(classWNS)));
975970
}
@@ -1302,5 +1297,31 @@ public static void generateCommonData(List<File> file) throws IOException {
13021297
saveProperties.replace("filename", "ConvertedCommonData.xml");
13031298
InstanceDataFactory.saveInstanceData(modelComData, saveProperties);
13041299
}
1300+
1301+
private static String resolveDatatype(Object datatypeCell, String value) {
1302+
String datatype = "string";
1303+
if (datatypeCell != null) {
1304+
String dt = datatypeCell.toString().trim();
1305+
if (!dt.isEmpty()) return dt;
1306+
}
1307+
1308+
if (value == null) return datatype;
1309+
String s = value.trim();
1310+
if (s.isEmpty()) return datatype;
1311+
1312+
// try to resolve from the value
1313+
try {
1314+
if (!s.contains(".") && !s.contains(",") && !s.toLowerCase(java.util.Locale.ROOT).contains("e")) {
1315+
Long.parseLong(s);
1316+
return "integer";
1317+
}
1318+
1319+
Double.parseDouble(s.replace(',', '.'));
1320+
return "float";
1321+
} catch (NumberFormatException e1) {
1322+
return datatype;
1323+
}
1324+
1325+
}
13051326
}
13061327

0 commit comments

Comments
 (0)