Skip to content

Commit

Permalink
Code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkioraskari committed Jan 4, 2024
1 parent 949aacc commit 087480f
Show file tree
Hide file tree
Showing 43 changed files with 627 additions and 691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ private void extractIFCtoLBD(File ifcFile, RDFFormat rdfformat) {

try {
File tempExcelFile = File.createTempFile("ifc2lbd-", ".xlsx");
FileOutputStream outputStream = new FileOutputStream(tempExcelFile);
workbook.write(outputStream);
try (FileOutputStream outputStream = new FileOutputStream(tempExcelFile)) {
workbook.write(outputStream);
}
workbook.close();
Resource res = new FileResource(tempExcelFile);
if (this.fileDownloader == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public WebFileHandler(String userId, String uploads) {
this.uploads = this.uploads + File.separator;
}

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
if ((filename == null) || filename.length() == 0) {
Notification n = new Notification("A file has to be selected", " ", Notification.Type.ERROR_MESSAGE);
Expand Down Expand Up @@ -62,6 +63,7 @@ public OutputStream receiveUpload(String filename, String mimeType) {
return fos;
}

@Override
public void uploadSucceeded(SucceededEvent event) {
File file = new File(this.uploads + event.getFilename());
if (!file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public CreateOntology() {
}


public static void main(String[] args) {
@SuppressWarnings("unused")
public static void main(String[] args) {
new CreateOntology();
}
}
13 changes: 7 additions & 6 deletions IFCtoLBD/src/main/java/examples/Example3.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public static void main(String[] args) {
+ "SELECT ?e WHERE {\r\n"
+ " ?e a bot:Element .\r\n"
+ "} ");
QueryExecution queryExecution = QueryExecutionFactory.create(query, m);
ResultSet rs = queryExecution.execSelect();
rs.forEachRemaining(qs -> {
System.out.println("BOT element: "+qs.get("e").asResource().getLocalName());

});
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, m)) {
ResultSet rs = queryExecution.execSelect();
rs.forEachRemaining(qs -> {
System.out.println("BOT element: "+qs.get("e").asResource().getLocalName());

});
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
19 changes: 10 additions & 9 deletions IFCtoLBD/src/main/java/examples/Example4.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ public static void main(String[] args) {
+ " ?g <http://www.opengis.net/ont/geosparql#asWKT> ?wkt .\r\n"
+ " ?g fog:asObj_v3.0-obj ?obj \r\n"
+ "} ");
QueryExecution queryExecution = QueryExecutionFactory.create(query, m);
ResultSet rs = queryExecution.execSelect();
rs.forEachRemaining(qs -> {
System.out.println("BOT element: "+qs.get("e").asResource().getLocalName());
System.out.println("BOT element WKT: "+qs.get("wkt"));
System.out.println("BOT element OBJ: "+qs.get("obj"));

});
//m.write(System.out, "TTL");
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, m)) {
ResultSet rs = queryExecution.execSelect();
rs.forEachRemaining(qs -> {
System.out.println("BOT element: "+qs.get("e").asResource().getLocalName());
System.out.println("BOT element WKT: "+qs.get("wkt"));
System.out.println("BOT element OBJ: "+qs.get("obj"));

});
//m.write(System.out, "TTL");
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Timer;
Expand Down Expand Up @@ -249,52 +248,41 @@ public Model convert(String ifc_filename) {
return this.lbd_general_output_model;
}

private String unzip(String ifcZipFile) {
ZipInputStream zis = null;
try {
private static String unzip(String ifcZipFile) {
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(ifcZipFile));){
byte[] buffer = new byte[1024];
zis = new ZipInputStream(new FileInputStream(ifcZipFile));

ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
System.out.println("entry: " + zipEntry);
//String name = zipEntry.getName().split("\\.")[0];
File newFile = File.createTempFile("ifc", ".ifc");

// write file content
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
// JO 2024
try (// write file content
FileOutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
fos.close();

zipEntry = zis.getNextEntry();
zis.close();
newFile.deleteOnExit();
return newFile.getAbsolutePath();
}

} catch (FileNotFoundException e) {
try {
if(zis!=null)
zis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
try {
if(zis!=null)
zis.close();
} catch (IOException e1) {
e1.printStackTrace();
}

e.printStackTrace();
}
return null;
}

public CompletableFuture<IFCGeometry> getgeom(String ifc_filename) throws InterruptedException {
public CompletableFuture<IFCGeometry> getgeom(String ifc_filename) {
CompletableFuture<IFCGeometry> completableFuture = new CompletableFuture<>();

Executors.newCachedThreadPool().submit(() -> {
Expand Down Expand Up @@ -417,11 +405,7 @@ public boolean convert_read_in_phase(String ifc_filename, String target_file, bo

CompletableFuture<IFCGeometry> future_ifc_geometry = null;
if (hasGeometry)
try {
future_ifc_geometry = getgeom(ifc_filename);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
future_ifc_geometry = getgeom(ifc_filename);

this.ifcowl_model = readAndConvertIFC2ifcOWL(ifc_filename, uriBase.get(), !exportIfcOWL, target_file,
hasPerformanceBoost); // Before:
Expand Down Expand Up @@ -495,6 +479,7 @@ public Model convert_LBD_phase(boolean hasBuildingElements,
}


@SuppressWarnings("unused")
public static void main(String[] args) {
JenaSystem.init();
if (args.length > 3) {
Expand All @@ -519,19 +504,20 @@ public static void main(String[] args) {
} else if (args.length == 1) {
// directory upload
final List<String> inputFiles;
final List<String> outputFiles;
//final List<String> outputFiles; //TODO Check this
inputFiles = FileUtils.listFiles(args[0]);
outputFiles = null;
//outputFiles = null;

for (int i = 0; i < inputFiles.size(); ++i) {
final String inputFile = inputFiles.get(i);
String outputFile;
if (inputFile.endsWith(".ifc")) {
if (outputFiles == null) {
//TODO Check this
//if (outputFiles == null) {
outputFile = inputFile.substring(0, inputFile.length() - 4) + ".ttl";
} else {
outputFile = outputFiles.get(i);
}
//} else {
// outputFile = outputFiles.get(i);
//}

outputFile = outputFile.replaceAll(args[0], args[0] + "\\___out\\");
String copyFile = inputFile.replaceAll(args[0], args[0] + "\\___done\\");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package org.linkedbuildingdata.ifc2lbd.core;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -784,43 +783,42 @@ private Resource addSingleElement(Resource ifcOWL_element) {
});
this.included_elements.add(ifcOWL_element);
return lbd_element;
} else { //TODO Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally
if (ifcowl_type.isPresent()) {
Resource lbd_element = LBD_RDF_Utils.createformattedURIRecource(ifcOWL_element,
this.lbd_general_output_model, "ifcOWL_" + ifcowl_type.get().getLocalName(), this.ifcOWL,
this.uriBase.get(), this.exportIfcOWL_setting);
String guid = IfcOWLUtils.getGUID(ifcOWL_element, this.ifcOWL);
String uncompressed_guid = GuidCompressor.uncompressGuidString(guid);
addGeometry(lbd_element, guid);
Resource lbd_property_object = this.lbd_product_output_model.createResource(lbd_element.getURI());
if (predefined_type.isPresent()) {
Resource product = this.lbd_product_output_model
.createResource(ifcowl_type.get().getURI() + "-" + predefined_type.get());
lbd_property_object.addProperty(RDF.type, product);
}
lbd_property_object.addProperty(RDF.type, ifcowl_type.get());
lbd_element.addProperty(RDF.type, BOT.element);
}
if (ifcowl_type.isPresent()) {
Resource lbd_element = LBD_RDF_Utils.createformattedURIRecource(ifcOWL_element,
this.lbd_general_output_model, "ifcOWL_" + ifcowl_type.get().getLocalName(), this.ifcOWL,
this.uriBase.get(), this.exportIfcOWL_setting);
String guid = IfcOWLUtils.getGUID(ifcOWL_element, this.ifcOWL);
String uncompressed_guid = GuidCompressor.uncompressGuidString(guid);
addGeometry(lbd_element, guid);
Resource lbd_property_object = this.lbd_product_output_model.createResource(lbd_element.getURI());
if (predefined_type.isPresent()) {
Resource product = this.lbd_product_output_model
.createResource(ifcowl_type.get().getURI() + "-" + predefined_type.get());
lbd_property_object.addProperty(RDF.type, product);
}
lbd_property_object.addProperty(RDF.type, ifcowl_type.get());
lbd_element.addProperty(RDF.type, BOT.element);

IfcOWLUtils.listPropertysets(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(propertyset -> {
PropertySet p_set = this.propertysets.get(propertyset.getURI());
if (p_set != null)
p_set.connect(lbd_element, uncompressed_guid);
});
addAttrributes(this.lbd_property_output_model, ifcOWL_element, lbd_element);
IfcOWLUtils.listPropertysets(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(propertyset -> {
PropertySet p_set = this.propertysets.get(propertyset.getURI());
if (p_set != null)
p_set.connect(lbd_element, uncompressed_guid);
});
addAttrributes(this.lbd_property_output_model, ifcOWL_element, lbd_element);

IfcOWLUtils.listHosted_Elements(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(ifc_element2 -> {
connectElement(lbd_element, BOT.hasSubElement, ifc_element2);
});
IfcOWLUtils.listHosted_Elements(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(ifc_element2 -> {
connectElement(lbd_element, BOT.hasSubElement, ifc_element2);
});

IfcOWLUtils.listAggregated_Elements(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(ifc_element2 -> {
connectElement(lbd_element, BOT.hasSubElement, ifc_element2);
});
this.included_elements.add(ifcOWL_element);
return lbd_element;
}
IfcOWLUtils.listAggregated_Elements(ifcOWL_element, this.ifcOWL).stream().map(rn -> rn.asResource())
.forEach(ifc_element2 -> {
connectElement(lbd_element, BOT.hasSubElement, ifc_element2);
});
this.included_elements.add(ifcOWL_element);
return lbd_element;
}
return null;
}
Expand Down Expand Up @@ -1085,36 +1083,32 @@ protected Model readAndConvertIFC2ifcOWL(String ifc_file, String uriBase, boolea
}

}
try {
Model m = ModelFactory.createDefaultModel();
this.eventBus.post(new IFCtoLBD_SystemStatusEvent("IFCtoRDF conversion"));
Model m = ModelFactory.createDefaultModel();
this.eventBus.post(new IFCtoLBD_SystemStatusEvent("IFCtoRDF conversion"));

if (hasPerformanceBoost) {
File pruned_file_ = IfcOWLUtils.filterIFC(new File(ifc_file));
this.ontURI = rj.convert_into_rdf(pruned_file_.getAbsolutePath(), outputFile.getAbsolutePath(),
uriBase, hasPerformanceBoost);
} else {
this.ontURI = rj.convert_into_rdf(ifc_file, outputFile.getAbsolutePath(), uriBase,
hasPerformanceBoost);
}
if (hasPerformanceBoost) {
File pruned_file_ = IfcOWLUtils.filterIFC(new File(ifc_file));
this.ontURI = rj.convert_into_rdf(pruned_file_.getAbsolutePath(), outputFile.getAbsolutePath(),
uriBase, hasPerformanceBoost);
} else {
this.ontURI = rj.convert_into_rdf(ifc_file, outputFile.getAbsolutePath(), uriBase,
hasPerformanceBoost);
}

this.eventBus.post(new IFCtoLBD_SystemStatusEvent("ifcOWL ready: reading in the model."));
this.eventBus.post(new IFCtoLBD_SystemStatusEvent("ifcOWL ready: reading in the model."));

// File t2 = IfcOWLUtils.filterContent(outputFile); // Performance!!
// if (t2 != null) {
// RDFDataMgr.read(m, t2.getAbsolutePath());
// } else
// RDFDataMgr.read(m, outputFile.getAbsolutePath());
File t2 = IfcOWLUtils.characterCoding(outputFile); // UTF-8 characters
if (t2 != null) {
RDFDataMgr.read(m, t2.getAbsolutePath());
} else
RDFDataMgr.read(m, outputFile.getAbsolutePath());
System.out.println("ifcOWL read in done");
return m;
} catch (IOException e) {
e.printStackTrace();
}
// File t2 = IfcOWLUtils.filterContent(outputFile); // Performance!!
// if (t2 != null) {
// RDFDataMgr.read(m, t2.getAbsolutePath());
// } else
// RDFDataMgr.read(m, outputFile.getAbsolutePath());
File t2 = IfcOWLUtils.characterCoding(outputFile); // UTF-8 characters
if (t2 != null) {
RDFDataMgr.read(m, t2.getAbsolutePath());
} else
RDFDataMgr.read(m, outputFile.getAbsolutePath());
System.out.println("ifcOWL read in done");
return m;

} catch (Exception e) {
this.eventBus.post(new IFCtoLBD_SystemErrorEvent(this.getClass().getSimpleName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class IFCtoRDF extends IfcSpfReader {



public Optional<String> convert_into_rdf(String ifcFile, String outputFile, String baseURI,boolean hasPerformanceBoost) throws IOException {
i = 0;
public Optional<String> convert_into_rdf(String ifcFile, String outputFile, String baseURI,boolean hasPerformanceBoost) {
this.i = 0;
PrintStream orgSystemOut = System.out;
PrintStream orgSystemError = System.err;

Expand All @@ -36,7 +36,7 @@ public Optional<String> convert_into_rdf(String ifcFile, String outputFile, Stri
timer.schedule(new TimerTask() {
@Override
public void run() {
eventBus.post(new IFCtoLBD_SystemStatusEvent("IFCtoRDF running " + i++));
IFCtoRDF.this.eventBus.post(new IFCtoLBD_SystemStatusEvent("IFCtoRDF running " + IFCtoRDF.this.i++));
}
}, 1000, 1000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
public class Test1 {


public static void main(String[] args) { try {
@SuppressWarnings("unused")
public static void main(String[] args) { try {
File ifc_file = new File("C:\\test\\bim4ren\\BIM4Ren_DUNANT_cleaned_IFC2x3.ifc");
File temp_file = File.createTempFile("ifc2lbd", "test.ttl");
new IFCtoLBDConverter(ifc_file.getAbsolutePath(), "https://dot.dc.rwth-aachen.de/IFCtoLBDset#", temp_file.getAbsolutePath(), 3, true, false, true, false, false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static private String unIFCUnicode(String txt)
{
switch(state)
{
default:
case 0:
if(ch=='\\' || ch=='/')
state=1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static List<String> getListofFiles(String dir, String extension) {
* @return List of files found
*/
public static List<String> listFiles(String dir) {
List<String> goodFiles = new ArrayList<String>();
List<String> goodFiles = new ArrayList<>();

File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
Expand Down
Loading

0 comments on commit 087480f

Please sign in to comment.