Skip to content

Commit

Permalink
Merge pull request #47 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Apr 23, 2024
2 parents 94683fb + f3af948 commit acf6463
Show file tree
Hide file tree
Showing 17 changed files with 738 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand All @@ -20,13 +20,67 @@

import org.drools.model.functions.Operator;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public enum MatchesOperator implements Operator.SingleValue<String, String> {

INSTANCE;

// not final due to unit tests
private static int MAX_SIZE_CACHE = getMaxSizeCache();

// store Pattern for regular expressions using the regular expression as the key up to MAX_SIZE_CACHE entries.
private static final Map<String, Pattern> patternMap = Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Pattern> eldest) {
return size() > (MAX_SIZE_CACHE);
}
});

// 0 default disables the Pattern map
private static int getMaxSizeCache() {
final String CACHE_MATCHES_COMPILED_MAX_PROPERTY = "drools.matches.compiled.cache.count";
return Integer.parseInt(System.getProperty(CACHE_MATCHES_COMPILED_MAX_PROPERTY, "0"));
}

// package-private for unit testing
void forceCacheSize(int size) {
MAX_SIZE_CACHE = size;
patternMap.clear();
}

// package-private for unit testing
void reInitialize() {
forceCacheSize(getMaxSizeCache());
}

// package-private for unit testing
int mapSize() {
return patternMap.size();
}

@Override
public boolean eval( String s1, String s2 ) {
return s1 != null && s1.matches( s2 );
public boolean eval(String input, String regex) {
if (input == null) {
return false;
} else if (MAX_SIZE_CACHE == 0) {
return input.matches(regex);
} else {
Pattern pattern = patternMap.get(regex);
if (pattern == null) {
// Cache miss on regex, compile it, store it.
// Storing in patternMap may remove the oldest entry per MAX_SIZE_CACHE.
pattern = Pattern.compile(regex);
patternMap.put(regex, pattern);
}
Matcher matcher = pattern.matcher(input);
return matcher.matches();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.model.operators;

import org.junit.After;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;


public class MatchesOperatorTest {


@Test
public void testMatchesOperatorCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(100);

// input maybe null
assertThat(instance.eval(null,"anything")).isFalse();
assertThat(instance.mapSize()).isEqualTo(0); // not added to cache with null input
// cache enabled
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.mapSize()).isEqualTo(1);
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.mapSize()).isEqualTo(2);
assertThat(instance.eval("a","a")).isTrue(); // regular expression "a" in map.
assertThat(instance.eval("b","b")).isTrue(); // regular expression "b" in map.
assertThat(instance.eval("c","a")).isFalse(); // regular expression "a" in map.
assertThat(instance.eval("c","b")).isFalse(); // regular expression "b" in map.
assertThat(instance.mapSize()).isEqualTo(2);
}

@Test
public void testMatchesOperatorNoCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.forceCacheSize(0);
// input maybe null
assertThat(instance.eval(null,"anything")).isFalse();
assertThat(instance.eval("a","a")).isTrue();
assertThat(instance.eval("a","b")).isFalse();
assertThat(instance.eval("b","a")).isFalse();
assertThat(instance.eval("b","b")).isTrue();
assertThat(instance.mapSize()).isEqualTo(0);
}

@After
public void resetCache() {
MatchesOperator instance = MatchesOperator.INSTANCE;
instance.reInitialize();
}

}
9 changes: 9 additions & 0 deletions drools-model/drools-codegen-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
</properties>

<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-util</artifactId>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
Expand All @@ -58,5 +62,10 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,63 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;

public class AppPaths {

public enum BuildTool {
MAVEN,
GRADLE;
MAVEN("target",
Path.of("target","generated-sources"),
Path.of("target","generated-resources"),
Path.of("target","generated-test-resources"),
Path.of("target","classes"),
Path.of("target","test-classes")),
GRADLE("build",
Path.of("build", "generated", "sources"),
Path.of("build","generated", "resources"),
Path.of("build","generated", "test", "resources"),
Path.of("build","classes", "java", "main"),
Path.of("build","classes", "java", "test"));

public final String OUTPUT_DIRECTORY;
public final Path GENERATED_SOURCES_PATH;
public final Path GENERATED_RESOURCES_PATH;
public final Path GENERATED_TEST_RESOURCES_PATH;
public final Path CLASSES_PATH;
public final Path TEST_CLASSES_PATH;

BuildTool(String outputDirectory, Path generatedSourcesPath, Path generatedResourcesPath, Path generatedTestResourcesPath, Path classesPath, Path testClassesPath) {
this.OUTPUT_DIRECTORY = outputDirectory;
this.GENERATED_SOURCES_PATH = generatedSourcesPath;
this.GENERATED_RESOURCES_PATH = generatedResourcesPath;
this.GENERATED_TEST_RESOURCES_PATH = generatedTestResourcesPath;
this.CLASSES_PATH = classesPath;
this.TEST_CLASSES_PATH = testClassesPath;
}

public static AppPaths.BuildTool findBuildTool() {
return System.getProperty("org.gradle.appname") == null ? MAVEN : GRADLE;
}
}

public static final String TARGET_DIR = "target";
public static final String TARGET_DIR;
public static final String GENERATED_SOURCES_DIR;
public static final String GENERATED_RESOURCES_DIR;
public static final BuildTool BT;

static {
BT = BuildTool.findBuildTool();
TARGET_DIR = BT.OUTPUT_DIRECTORY;
GENERATED_SOURCES_DIR = BT.GENERATED_SOURCES_PATH.toString();
GENERATED_RESOURCES_DIR = BT.GENERATED_RESOURCES_PATH.toString();
}

public static final String SRC_DIR = "src";

public static final String RESOURCES_DIR = "resources";

public static final String GENERATED_RESOURCES_DIR = "generated-resources";

public static final String MAIN_DIR = "main";
public static final String TEST_DIR = "test";

Expand All @@ -66,8 +101,8 @@ public static AppPaths.BuildTool findBuildTool() {

private final Path[] sourcePaths;

public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) {
return new AppPaths(Collections.singleton(projectDir), Collections.emptyList(), false, BuildTool.findBuildTool(), MAIN_DIR, outputTarget);
public static AppPaths fromProjectDir(Path projectDir) {
return fromProjectDir(projectDir, BT);
}

/**
Expand All @@ -77,7 +112,27 @@ public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) {
* @return
*/
public static AppPaths fromTestDir(Path projectDir) {
return new AppPaths(Collections.singleton(projectDir), Collections.emptyList(), false, BuildTool.findBuildTool(), TEST_DIR, Paths.get(projectDir.toString(), TARGET_DIR));
return fromTestDir(projectDir, BT);
}

/**
* Default-access method for testing purpose
* @param projectDir
* @param bt
* @return
*/
static AppPaths fromProjectDir(Path projectDir, BuildTool bt) {
return new AppPaths(Collections.singletonList(projectDir), Collections.emptyList(), false, bt, MAIN_DIR, false);
}

/**
* Default-access method for testing purpose
* @param projectDir
* @param bt
* @return
*/
static AppPaths fromTestDir(Path projectDir, BuildTool bt) {
return new AppPaths(Collections.singletonList(projectDir), Collections.emptyList(), false, bt, TEST_DIR, true);
}

/**
Expand All @@ -87,14 +142,14 @@ public static AppPaths fromTestDir(Path projectDir) {
* @param bt
* @param resourcesBasePath "main" or "test"
*/
protected AppPaths(Set<Path> projectPaths, Collection<Path> classesPaths, boolean isJar, BuildTool bt,
String resourcesBasePath, Path outputTarget) {
protected AppPaths(List<Path> projectPaths, Collection<Path> classesPaths, boolean isJar, BuildTool bt,
String resourcesBasePath, boolean isTest) {
this.isJar = isJar;
this.projectPaths.addAll(projectPaths);
this.classesPaths.addAll(classesPaths);
this.outputTarget = outputTarget;
firstProjectPath = getFirstProjectPath(this.projectPaths, outputTarget, bt);
resourcePaths = getResourcePaths(this.projectPaths, resourcesBasePath, bt);
this.outputTarget = Paths.get(".", bt.OUTPUT_DIRECTORY);
firstProjectPath = projectPaths.get(0);
resourcePaths = getResourcePaths(this.projectPaths, resourcesBasePath, bt, isTest);
paths = isJar ? getJarPaths(isJar, this.classesPaths) : resourcePaths;
resourceFiles = getResourceFiles(resourcePaths);
sourcePaths = getSourcePaths(this.projectPaths);
Expand Down Expand Up @@ -137,12 +192,6 @@ public String toString() {
'}';
}

static Path getFirstProjectPath(Set<Path> innerProjectPaths, Path innerOutputTarget, BuildTool innerBt) {
return innerBt == BuildTool.MAVEN
? innerProjectPaths.iterator().next()
: innerOutputTarget;
}

static Path[] getJarPaths(boolean isInnerJar, Collection<Path> innerClassesPaths) {
if (!isInnerJar) {
throw new IllegalStateException("Not a jar");
Expand All @@ -151,20 +200,14 @@ static Path[] getJarPaths(boolean isInnerJar, Collection<Path> innerClassesPaths
}
}

static Path[] getResourcePaths(Set<Path> innerProjectPaths, String resourcesBasePath, BuildTool innerBt) {
Path[] toReturn;
if (innerBt == BuildTool.GRADLE) {
toReturn = transformPaths(innerProjectPaths, p -> p.resolve(Paths.get("")));
} else {
toReturn = transformPaths(innerProjectPaths, p -> p.resolve(Paths.get(SRC_DIR, resourcesBasePath,
RESOURCES_DIR)));
Path[] generatedResourcesPaths = transformPaths(innerProjectPaths, p -> p.resolve(Paths.get(TARGET_DIR,
GENERATED_RESOURCES_DIR)));
Path[] newToReturn = new Path[toReturn.length + generatedResourcesPaths.length];
System.arraycopy(toReturn, 0, newToReturn, 0, toReturn.length);
System.arraycopy(generatedResourcesPaths, 0, newToReturn, toReturn.length, generatedResourcesPaths.length);
toReturn = newToReturn;
}
static Path[] getResourcePaths(Set<Path> innerProjectPaths, String resourcesBasePath, BuildTool innerBt, boolean isTest) {
Path[] resourcesPaths = transformPaths(innerProjectPaths, p -> p.resolve(Paths.get(SRC_DIR, resourcesBasePath,
RESOURCES_DIR)));
Path generatedResourcesPath = isTest ? innerBt.GENERATED_TEST_RESOURCES_PATH : innerBt.GENERATED_RESOURCES_PATH;
Path[] generatedResourcesPaths = transformPaths(innerProjectPaths, p -> p.resolve(generatedResourcesPath));
Path[] toReturn = new Path[resourcesPaths.length + generatedResourcesPaths.length];
System.arraycopy(resourcesPaths, 0, toReturn, 0, resourcesPaths.length);
System.arraycopy(generatedResourcesPaths, 0, toReturn, resourcesPaths.length, generatedResourcesPaths.length);
return toReturn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum Category {
/**
* Represent a cp resource automatically generated during codegen, so after generate-resources maven phase.
* This means to add it to target/classes both for Quarkus or using kogito-maven-plugin (SB). For additional
* information see {@link org.kie.kogito.codegen.utils.GeneratedFileWriter#write(GeneratedFile)}
* information see {@link org.drools.codegen.common.GeneratedFileWriter#write(GeneratedFile)}
* For Quarkus it will be subject of GeneratedResourceBuildItem and NativeImageResourceBuildItem too
*/
INTERNAL_RESOURCE,
Expand Down
Loading

0 comments on commit acf6463

Please sign in to comment.