Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#…
Browse files Browse the repository at this point in the history
…1473
  • Loading branch information
Gabriele-Cardosi committed Sep 25, 2024
2 parents 63c1f3c + 011e17a commit 89d35de
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ private static void buildKieProject( BuildContext buildContext,
kieRepository.addKieModule( kDep );
}
}

clearBuilderCache();
}

private static void clearBuilderCache() {
DecisionTableFactory.clearCompilerCache();
}

private void addKBasesFilesToTrg() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.drools.decisiontable.parser.xls.PropertiesSheetListener;
import org.drools.drl.extensions.DecisionTableProvider;
Expand All @@ -42,16 +43,16 @@ public class DecisionTableProviderImpl
implements
DecisionTableProvider {

private static final transient Logger logger = LoggerFactory.getLogger( DecisionTableProviderImpl.class );
private static final Logger logger = LoggerFactory.getLogger( DecisionTableProviderImpl.class );

private Map<String, String> compiledDtablesCache = new ConcurrentHashMap<>();

@Override
public String loadFromResource(Resource resource,
DecisionTableConfiguration configuration) {

try {
return compileResource( resource, configuration );
} catch (IOException e) {
throw new UncheckedIOException( e );
} catch (Exception e) {
throw new DecisionTableParseException(resource, e);
}
Expand All @@ -78,29 +79,46 @@ public List<String> loadFromInputStreamWithTemplates(Resource resource,
return drls;
}

private String compileResource(Resource resource,
DecisionTableConfiguration configuration) throws IOException {
private String compileResource(Resource resource, DecisionTableConfiguration configuration) {
if (resource.getSourcePath() == null) {
return internalCompileResource(resource, configuration);
}
String resourceKey = resource.getSourcePath() + "?trimCell=" + configuration.isTrimCell() + "&worksheetName=" + configuration.getWorksheetName();
return compiledDtablesCache.computeIfAbsent(resourceKey, path -> internalCompileResource(resource, configuration));
}

private String internalCompileResource(Resource resource, DecisionTableConfiguration configuration) throws UncheckedIOException {
SpreadsheetCompiler compiler = new SpreadsheetCompiler(configuration.isTrimCell());

switch ( configuration.getInputType() ) {
case XLS :
case XLSX :
if ( StringUtils.isEmpty( configuration.getWorksheetName() ) ) {
return compiler.compile( resource,
InputType.XLS );
return compiler.compile( resource, InputType.XLS );
} else {
return compiler.compile( resource.getInputStream(),
configuration.getWorksheetName() );
try {
return compiler.compile( resource.getInputStream(), configuration.getWorksheetName() );
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
case CSV : {
return compiler.compile( resource.getInputStream(),
InputType.CSV );
try {
return compiler.compile( resource.getInputStream(), InputType.CSV );
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

return null;
}

@Override
public void clearCompilerCache() {
compiledDtablesCache.clear();
}

@Override
public Map<String, List<String[]>> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration) {
switch (configuration.getInputType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public static List<String> loadFromInputStreamWithTemplates(Resource resource, D
return getDecisionTableProvider().loadFromInputStreamWithTemplates( resource, configuration );
}

public static void clearCompilerCache() {
if (provider != null) {
provider.clearCompilerCache();
}
}

public static synchronized void setDecisionTableProvider(DecisionTableProvider provider) {
DecisionTableFactory.provider = provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

public interface DecisionTableProvider extends KieService {

String loadFromResource(Resource resource,
DecisionTableConfiguration configuration);
String loadFromResource(Resource resource, DecisionTableConfiguration configuration);

List<String> loadFromInputStreamWithTemplates(Resource resource,
DecisionTableConfiguration configuration);
List<String> loadFromInputStreamWithTemplates(Resource resource, DecisionTableConfiguration configuration);

Map<String,List<String[]>> loadPropertiesFromFile(File file, DecisionTableConfiguration configuration);

Map<String,List<String[]>> loadPropertiesFromInputStream(InputStream inputStream, DecisionTableConfiguration configuration);

void clearCompilerCache();
}

0 comments on commit 89d35de

Please sign in to comment.