Skip to content

Commit

Permalink
isolate prototypes in their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jan 3, 2024
1 parent 0fe5938 commit 6279ef5
Show file tree
Hide file tree
Showing 36 changed files with 304 additions and 128 deletions.
18 changes: 18 additions & 0 deletions bom/drools-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,24 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-prototype</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-prototype</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-prototype</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<!-- Quarkus Extension -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface Variable<T> extends Argument<T> {
default boolean isFact() {
return true;
}

default boolean isPrototype() {
return false;
}
}
4 changes: 4 additions & 0 deletions drools-model/drools-model-codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<groupId>org.drools</groupId>
<artifactId>drools-model-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-prototype</artifactId>
</dependency>

<dependency>
<groupId>com.github.javaparser</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.Type;
import org.drools.model.PrototypeFact;
import org.drools.model.prototype.PrototypeFact;

import static org.drools.model.codegen.execmodel.generator.DrlxParseUtil.toClassOrInterfaceType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import org.drools.model.PrototypeDSL;
import org.drools.model.prototype.PrototypeDSL;

public class DslMethodNames {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import org.drools.model.PrototypeVariable;
import org.drools.model.codegen.execmodel.PackageModel;
import org.drools.model.prototype.PrototypeVariable;

import static org.drools.model.codegen.execmodel.generator.DrlxParseUtil.toClassOrInterfaceType;
import static org.drools.model.codegen.execmodel.generator.DslMethodNames.PROTOTYPE_CALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
*/
package org.drools.model.codegen.execmodel;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

import org.drools.base.facttemplates.Event;
import org.drools.base.facttemplates.Fact;
import org.drools.base.facttemplates.FactTemplateObjectType;
Expand All @@ -30,17 +41,17 @@
import org.drools.model.DSL;
import org.drools.model.Index;
import org.drools.model.Model;
import org.drools.model.Prototype;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeFact;
import org.drools.model.PrototypeVariable;
import org.drools.model.Query;
import org.drools.model.Rule;
import org.drools.model.Variable;
import org.drools.model.codegen.execmodel.domain.Person;
import org.drools.model.codegen.execmodel.domain.Result;
import org.drools.model.functions.Function1;
import org.drools.model.impl.ModelImpl;
import org.drools.model.prototype.Prototype;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeFact;
import org.drools.model.prototype.PrototypeVariable;
import org.drools.modelcompiler.KieBaseBuilder;
import org.junit.Test;
import org.kie.api.KieBase;
Expand All @@ -54,17 +65,6 @@
import org.kie.api.runtime.rule.ViewChangedEventListener;
import org.kie.api.time.SessionPseudoClock;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.drools.model.DSL.accFunction;
Expand All @@ -80,16 +80,16 @@
import static org.drools.model.PatternDSL.query;
import static org.drools.model.PatternDSL.reactOn;
import static org.drools.model.PatternDSL.rule;
import static org.drools.model.PrototypeDSL.field;
import static org.drools.model.PrototypeDSL.protoPattern;
import static org.drools.model.PrototypeDSL.prototype;
import static org.drools.model.PrototypeDSL.variable;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.PrototypeExpression.prototypeField;
import static org.drools.model.PrototypeExpression.thisPrototype;
import static org.drools.model.codegen.execmodel.BaseModelTest.getObjectsIntoList;
import static org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedEvent;
import static org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact;
import static org.drools.model.prototype.PrototypeDSL.field;
import static org.drools.model.prototype.PrototypeDSL.protoPattern;
import static org.drools.model.prototype.PrototypeDSL.prototype;
import static org.drools.model.prototype.PrototypeDSL.variable;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.prototypeField;
import static org.drools.model.prototype.PrototypeExpression.thisPrototype;
import static org.drools.model.prototype.facttemplate.FactFactory.createMapBasedEvent;
import static org.drools.model.prototype.facttemplate.FactFactory.createMapBasedFact;

public class FactTemplateTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.drools.base.facttemplates.Fact;
import org.drools.model.Index;
import org.drools.model.Model;
import org.drools.model.Prototype;
import org.drools.model.PrototypeVariable;
import org.drools.model.prototype.Prototype;
import org.drools.model.prototype.PrototypeVariable;
import org.drools.model.Rule;
import org.drools.model.codegen.ExecutableModelProject;
import org.drools.model.impl.ModelImpl;
Expand All @@ -39,10 +39,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.model.DSL.on;
import static org.drools.model.PatternDSL.rule;
import static org.drools.model.PrototypeDSL.protoPattern;
import static org.drools.model.PrototypeDSL.prototype;
import static org.drools.model.PrototypeDSL.variable;
import static org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact;
import static org.drools.model.prototype.PrototypeDSL.protoPattern;
import static org.drools.model.prototype.PrototypeDSL.prototype;
import static org.drools.model.prototype.PrototypeDSL.variable;
import static org.drools.model.prototype.facttemplate.FactFactory.createMapBasedFact;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PrototypeDialectTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
package org.drools.model.codegen.execmodel;

import org.drools.model.Prototype;
import org.drools.model.PrototypeExpression;
import org.drools.model.PrototypeFact;
import org.drools.model.prototype.Prototype;
import org.drools.model.prototype.PrototypeExpression;
import org.drools.model.prototype.PrototypeFact;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.model.PrototypeDSL.prototype;
import static org.drools.model.PrototypeExpression.fixedValue;
import static org.drools.model.PrototypeExpression.prototypeField;
import static org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact;
import static org.drools.model.prototype.PrototypeDSL.prototype;
import static org.drools.model.prototype.PrototypeExpression.fixedValue;
import static org.drools.model.prototype.PrototypeExpression.prototypeField;
import static org.drools.model.prototype.facttemplate.FactFactory.createMapBasedFact;

public class SegmentPrototypeExpressionTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.drools.base.definitions.impl.KnowledgePackageImpl;
import org.drools.base.definitions.rule.impl.QueryImpl;
import org.drools.base.definitions.rule.impl.RuleImpl;
import org.drools.base.facttemplates.FactTemplateObjectType;
import org.drools.base.rule.Accumulate;
import org.drools.base.rule.AsyncReceive;
import org.drools.base.rule.AsyncSend;
Expand Down Expand Up @@ -100,8 +99,6 @@
import org.drools.model.GroupByPattern;
import org.drools.model.Index;
import org.drools.model.Model;
import org.drools.model.Prototype;
import org.drools.model.PrototypeVariable;
import org.drools.model.Query;
import org.drools.model.Rule;
import org.drools.model.SingleConstraint;
Expand Down Expand Up @@ -161,7 +158,6 @@
import static org.drools.model.bitmask.BitMaskUtil.calculatePatternMask;
import static org.drools.model.functions.FunctionUtils.toFunctionN;
import static org.drools.model.impl.NamesGenerator.generateName;
import static org.drools.modelcompiler.facttemplate.FactFactory.prototypeToFactTemplate;
import static org.drools.modelcompiler.util.EvaluationUtil.adaptBitMask;
import static org.drools.modelcompiler.util.TimerUtil.buildTimerExpression;
import static org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration;
Expand Down Expand Up @@ -1204,20 +1200,11 @@ Collection<InternalKnowledgePackage> getKiePackages() {
}

ObjectType getObjectType( Variable patternVariable ) {
return patternVariable instanceof PrototypeVariable ?
getPrototypeObjectType( (( PrototypeVariable ) patternVariable).getPrototype() ) :
return patternVariable.isPrototype() ?
PrototypeService.get().getPrototypeObjectType( objectTypeCache, packages, this::createKiePackage, patternVariable ) :
getClassObjectType( patternVariable.getType() );
}

private ObjectType getPrototypeObjectType( Prototype prototype ) {
return objectTypeCache.computeIfAbsent( prototype.getFullName(), name -> {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent( prototype.getPackage(), this::createKiePackage );
FactTemplateObjectType objectType = new FactTemplateObjectType(prototypeToFactTemplate( prototype, pkg ));
objectType.setEvent(prototype.isEvent());
return objectType;
} );
}

private ObjectType getClassObjectType( Class<?> patternClass ) {
return objectTypeCache.computeIfAbsent( patternClass.getCanonicalName(), name -> {
boolean isEvent = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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.modelcompiler;

import java.util.Map;
import java.util.function.Function;

import org.drools.base.base.ObjectType;
import org.drools.base.common.MissingDependencyException;
import org.drools.base.definitions.InternalKnowledgePackage;
import org.drools.model.Variable;
import org.kie.api.internal.utils.KieService;

public interface PrototypeService extends KieService {
String NO_PROTOTYPE = "You're trying to use the Prototypes without having imported it. Please add the module org.drools:drools-model-prototype to your classpath.";

ObjectType getPrototypeObjectType(Map<String, ObjectType> objectTypeCache, Map<String, InternalKnowledgePackage> packages, Function<String, InternalKnowledgePackage> packageCreator, Variable patternVariable );

class Holder {
private static final PrototypeService INSTANCE = KieService.load(PrototypeService.class);
}

static PrototypeService get() {
return present() ? PrototypeService.Holder.INSTANCE : throwExceptionForMissingPrototype();
}

static boolean present() {
return PrototypeService.Holder.INSTANCE != null;
}

static <T> T throwExceptionForMissingPrototype() {
throw new MissingDependencyException(NO_PROTOTYPE);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.drools.model.prototype.facttemplate.PrototypeFactFactoryImpl
50 changes: 50 additions & 0 deletions drools-model/drools-model-prototype/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.drools</groupId>
<artifactId>drools-model</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<name>Drools :: Canonical Model :: Prototype</name>
<artifactId>drools-model-prototype</artifactId>

<properties>
<java.module.name>org.drools.model.prototype</java.module.name>
</properties>

<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-base</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-canonical-model</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-compiler</artifactId>
</dependency>

<dependency><!-- For unit test logging: configure in src/test/resources/logback-test.xml -->
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.model;
package org.drools.model.prototype;

import java.util.Collection;
import java.util.function.Function;

import org.drools.model.NamedModelItem;

public interface Prototype extends NamedModelItem {

Object UNDEFINED_VALUE = UndefinedValue.INSTANCE;
Expand Down
Loading

0 comments on commit 6279ef5

Please sign in to comment.