Skip to content

Commit

Permalink
[incubator-kie-issues#1356] Working with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele-Cardosi committed Jul 26, 2024
1 parent 0d24efb commit e6bc83b
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 151 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 @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import javax.xml.namespace.QName;

Expand All @@ -45,17 +46,21 @@ public abstract class DMNBaseNode implements DMNNode {

private Map<String, QName> importAliases = new HashMap<>();

private final String id;

public DMNBaseNode() {
id = UUID.randomUUID().toString();
}

public DMNBaseNode(NamedElement source) {
this.source = source;
id = source != null && source.getId() != null ? source.getId() : UUID.randomUUID().toString();
}

public abstract DMNType getType();

public String getId() {
return source != null ? source.getId() : null;
return id;
}

public String getName() {
Expand Down Expand Up @@ -88,7 +93,7 @@ public String getModelName() {

public String getIdentifierString() {
String identifier = "[unnamed]";
if( source != null ) {
if (source != null) {
identifier = source.getName() != null ? source.getName() : source.getId();
}
return identifier;
Expand All @@ -107,21 +112,21 @@ public void setDependencies(Map<String, DMNNode> dependencies) {
}

public void addDependency(String name, DMNNode dependency) {
this.dependencies.put( name, dependency );
this.dependencies.put(name, dependency);
}

public List<InformationRequirement> getInformationRequirement() {
if ( source instanceof Decision ) {
if (source instanceof Decision) {
return ((Decision) source).getInformationRequirement();
} else {
return Collections.emptyList();
}
}

public List<KnowledgeRequirement> getKnowledgeRequirement() {
if ( source instanceof Decision ) {
if (source instanceof Decision) {
return ((Decision) source).getKnowledgeRequirement();
} else if( source instanceof BusinessKnowledgeModel ) {
} else if (source instanceof BusinessKnowledgeModel) {
return ((BusinessKnowledgeModel) source).getKnowledgeRequirement();
} else {
return Collections.emptyList();
Expand All @@ -148,5 +153,4 @@ public String toString() {
builder.append("]");
return builder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public void setItemDef(ItemDefinition itemDef) {
this.itemDef = itemDef;
}

public String getId() {
return itemDef.getId();
}

public String getName() {
return itemDef.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private void processItemDefinitions(DMNCompilerContext ctx, DMNModelImpl model,
Msg.DUPLICATED_ITEM_DEFINITION,
id.getName());
}
if (id.getItemComponent() != null && id.getItemComponent().size() > 0) {
if (id.getItemComponent() != null && !id.getItemComponent().isEmpty()) {
DMNCompilerHelper.checkVariableName(model, id, id.getName());
CompositeTypeImpl compType = new CompositeTypeImpl(model.getNamespace(), id.getName(), id.getId(), id.isIsCollection());
DMNType preregistered = model.getTypeRegistry().registerType(compType);
Expand All @@ -402,6 +402,9 @@ private void processItemDefinitions(DMNCompilerContext ctx, DMNModelImpl model,
private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Definitions dmndefs) {
for ( DRGElement e : dmndefs.getDrgElement() ) {
boolean foundIt = false;
if (e.getId() == null) {
e.setId(UUID.randomUUID().toString());
}
for( DRGElementCompiler dc : drgCompilers ) {
if ( dc.accept( e ) ) {
foundIt = true;
Expand Down Expand Up @@ -436,10 +439,8 @@ private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Defi
ds.setVariable(variable);
}
// continuing with normal compilation of Decision Service:
boolean foundIt = false;
for (DRGElementCompiler dc : drgCompilers) {
if (dc.accept(ds)) {
foundIt = true;
dc.compileNode(ds, this, model);
}
}
Expand Down Expand Up @@ -487,7 +488,7 @@ private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Defi
}

@FunctionalInterface
public static interface AfterProcessDrgElements {
public interface AfterProcessDrgElements {
void callback(DMNCompilerImpl compiler, DMNCompilerContext ctx, DMNModelImpl model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.BaseDMNTypeImpl;
import org.kie.dmn.core.impl.CompositeTypeImpl;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.feel.lang.Scope;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.types.BuiltInType;
Expand All @@ -42,12 +42,12 @@
public abstract class DMNTypeRegistryAbstract implements DMNTypeRegistry, FEELTypeRegistry {

protected Map<String, Map<String, DMNType>> types = new HashMap<>();
protected Map<DMNModelImpl.TupleIdentifier, QName> aliases;
protected Map<TupleIdentifier, QName> aliases;
protected ScopeImpl feelTypesScope = new ScopeImpl(); // no parent scope, intentional.
protected Map<DMNModelImpl.TupleIdentifier, ScopeImpl> feelTypesScopeChildLU = new HashMap<>();
protected Map<TupleIdentifier, ScopeImpl> feelTypesScopeChildLU = new HashMap<>();


public DMNTypeRegistryAbstract(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryAbstract(Map<TupleIdentifier, QName> aliases) {
this.aliases = aliases;
String feelNamespace = feelNS();
Map<String, DMNType> feelTypes = new HashMap<>( );
Expand Down Expand Up @@ -86,8 +86,8 @@ public Scope getItemDefScope(Scope parent) {
public Type resolveFEELType(List<String> qns) {
if (qns.size() == 1) {
return feelTypesScope.resolve(qns.get(0)).getType();
} else if (qns.size() == 2 && feelTypesScopeChildLU.containsKey(new DMNModelImpl.TupleIdentifier(null, qns.get(0)))) {
return feelTypesScopeChildLU.get(new DMNModelImpl.TupleIdentifier(null, qns.get(0))).resolve(qns.get(1)).getType();
} else if (qns.size() == 2 && feelTypesScopeChildLU.containsKey(new TupleIdentifier(null, qns.get(0)))) {
return feelTypesScopeChildLU.get(new TupleIdentifier(null, qns.get(0))).resolve(qns.get(1)).getType();
} else {
throw new IllegalStateException("Inconsistent state when resolving for qns: " + qns);
}
Expand All @@ -99,12 +99,12 @@ public Map<String, Map<String, DMNType>> getTypes() {
}

protected void registerAsFEELType(DMNType dmnType) {
Optional<DMNModelImpl.TupleIdentifier> optAliasKey = keyfromNS(dmnType.getNamespace());
Optional<TupleIdentifier> optAliasKey = keyfromNS(dmnType.getNamespace());
Type feelType = ((BaseDMNTypeImpl) dmnType).getFeelType();
if (optAliasKey.isEmpty()) {
feelTypesScope.define(new TypeSymbol(dmnType.getName(), feelType));
} else {
DMNModelImpl.TupleIdentifier aliasKey = optAliasKey.get();
TupleIdentifier aliasKey = optAliasKey.get();
feelTypesScopeChildLU.computeIfAbsent(aliasKey, k -> {
ScopeImpl importScope = new ScopeImpl(k.getName(), feelTypesScope);
feelTypesScope.define(new TypeSymbol(k.getName(), null));
Expand All @@ -113,7 +113,7 @@ protected void registerAsFEELType(DMNType dmnType) {
}
}

private Optional<DMNModelImpl.TupleIdentifier> keyfromNS(String ns) {
private Optional<TupleIdentifier> keyfromNS(String ns) {
return aliases == null ? Optional.empty() : aliases.entrySet().stream().filter(kv -> kv.getValue().getNamespaceURI().equals(ns)).map(kv -> kv.getKey()).findFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import javax.xml.namespace.QName;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase;

public class DMNTypeRegistryV11 extends DMNTypeRegistryAbstract {

public DMNTypeRegistryV11(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryV11(Map<TupleIdentifier, QName> aliases) {
super(aliases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import javax.xml.namespace.QName;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase;

Expand All @@ -39,7 +39,7 @@ public DMNTypeRegistryV12() {
super(Collections.emptyMap());
}

public DMNTypeRegistryV12(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryV12(Map<TupleIdentifier, QName> aliases) {
super(aliases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import javax.xml.namespace.QName;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase;

public class DMNTypeRegistryV13 extends DMNTypeRegistryAbstract {

private static final DMNType UNKNOWN = SimpleTypeImpl.UNKNOWN_DMNTYPE(KieDMNModelInstrumentedBase.URI_FEEL);

public DMNTypeRegistryV13(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryV13(Map<TupleIdentifier, QName> aliases) {
super(aliases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import javax.xml.namespace.QName;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase;

public class DMNTypeRegistryV14 extends DMNTypeRegistryAbstract {

private static final DMNType UNKNOWN = SimpleTypeImpl.UNKNOWN_DMNTYPE(KieDMNModelInstrumentedBase.URI_FEEL);

public DMNTypeRegistryV14(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryV14(Map<TupleIdentifier, QName> aliases) {
super(aliases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.kie.dmn.core.compiler;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.DMNModelImpl;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.core.impl.TupleIdentifier;
import org.kie.dmn.model.v1_5.KieDMNModelInstrumentedBase;

import javax.xml.namespace.QName;
Expand All @@ -31,7 +31,7 @@ public class DMNTypeRegistryV15 extends DMNTypeRegistryAbstract {
private static final DMNType UNKNOWN = SimpleTypeImpl.UNKNOWN_DMNTYPE(KieDMNModelInstrumentedBase.URI_FEEL);


public DMNTypeRegistryV15(Map<DMNModelImpl.TupleIdentifier, QName> aliases) {
public DMNTypeRegistryV15(Map<TupleIdentifier, QName> aliases) {
super(aliases);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
import org.kie.dmn.model.api.Definitions;

import static org.kie.dmn.core.compiler.UnnamedImportUtils.isInUnnamedImport;
import static org.kie.dmn.core.impl.TupleIdentifier.createTupleIdentifier;
import static org.kie.dmn.core.impl.TupleIdentifier.createTupleIdentifierById;
import static org.kie.dmn.core.impl.TupleIdentifier.createTupleIdentifierByName;

public class DMNModelImpl
implements DMNModel, DMNMessageManager, Externalizable {
Expand All @@ -81,8 +84,6 @@ private enum SerializationFormat {
DMN_XML
}

static final String AUTO_GENERATED_ID_PREFIX = "auto-generated-id";

private SerializationFormat serializedAs = SerializationFormat.DMN_XML;
private Resource resource;
private Definitions definitions;
Expand Down Expand Up @@ -461,13 +462,13 @@ public void setImportAliasForNS(String iAlias, String iNS, String iModelName) {

public Map<String, QName> getImportAliasesForNS() {
return importAliases.entrySet()
.stream().collect(Collectors.toMap(tupleIdentifierQNameEntry -> tupleIdentifierQNameEntry.getKey().name,
.stream().collect(Collectors.toMap(tupleIdentifierQNameEntry -> tupleIdentifierQNameEntry.getKey().getName(),
Entry::getValue));
}

public Optional<String> getImportAliasFor(String ns, String iModelName) {
QName lookup = new QName(ns, iModelName);
return this.importAliases.entrySet().stream().filter(kv -> kv.getValue().equals(lookup)).map(kv -> kv.getKey().name).findFirst();
return this.importAliases.entrySet().stream().filter(kv -> kv.getValue().equals(lookup)).map(kv -> kv.getKey().getName()).findFirst();
}

public QName getImportNamespaceAndNameforAlias(String iAlias) {
Expand Down Expand Up @@ -502,7 +503,7 @@ public void addPMMLImportInfo(DMNImportPMMLInfo info) {

public Map<String, DMNImportPMMLInfo> getPmmlImportInfo() {
return pmmlImportInfo.entrySet()
.stream().collect(Collectors.toMap(tupleIdentifierQNameEntry -> tupleIdentifierQNameEntry.getKey().name,
.stream().collect(Collectors.toMap(tupleIdentifierQNameEntry -> tupleIdentifierQNameEntry.getKey().getName(),
Entry::getValue));

}
Expand Down Expand Up @@ -562,56 +563,4 @@ public List<DMNModel> getImportChainDirectChildModels() {
}
}

static TupleIdentifier createTupleIdentifier(String id, String name) {
return new TupleIdentifier(id, name);
}

static TupleIdentifier createTupleIdentifierById(String id) {
return new TupleIdentifier(id, null);
}

static TupleIdentifier createTupleIdentifierByName(String name) {
return new TupleIdentifier(generateIdFromName(name), name);
}

static String generateIdFromName(String name) {
return String.format("%s-%s", AUTO_GENERATED_ID_PREFIX, Objects.hash(name));
}

public static class TupleIdentifier {
private final String id;
private final String name;

public TupleIdentifier(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TupleIdentifier that)) {
return false;
}
// This "null" availability it is to allow for search based only on id or name
boolean equalId = id == null || that.id == null || id.equals(that.id);
boolean equalName = name == null || that.name == null || name.equals(that.name);
return equalId && equalName;
}

@Override
public int hashCode() {
return Objects.hash(1); // we have to consider "null" comparisons, so everything should go in same "bucket"
}
}
}
Loading

0 comments on commit e6bc83b

Please sign in to comment.