Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wim #38

Merged
merged 3 commits into from
Sep 6, 2022
Merged

Wim #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
mavenCentral()
mavenCentral()
maven {
url 'https://jitpack.io'
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.21'

implementation group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.3'
implementation group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0.1'
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation group: 'org.apache.velocity', name: 'velocity', version: '1.7'
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
implementation group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.+'
implementation group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.3.1'
implementation group: 'com.owlike', name: 'genson', version: '1.6'
implementation group: 'com.owlike', name: 'genson-java-datetime', version: '1.6'
implementation group: 'com.github.ajalt', name: 'clikt', version: '2.8.0'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69'


testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
}

compileKotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void addAttribute(Attribute attribute) {
this.put("attributes", attributes);
}

public void addMasterIdAttribute(Metaobject master) {
MasterRefAttribute masterRef = new MasterRefAttribute(master);
public void addMasterIdAttribute(Metaobject master, String dependencyName) {
MasterRefAttribute masterRef = new MasterRefAttribute(master, dependencyName);
this.addAttribute(masterRef);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public BusinessObjectTypeContext build() {

Metaobject master = Util.findById(model.getMetamodel().getMetaobjects().getMetaobject(), dependency.getMaster());

this.ctx.addMasterIdAttribute(master);
this.ctx.addMasterIdAttribute(master, dependency.getName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ public class DependencyNode {
private Metaobject bot;
private DependencyNode directMaster;
private boolean last;
private String dependencyName;

public DependencyNode(Metaobject bot) {
this.bot = bot;
this.directMaster = null;
this.last = true;
}

public void addDirectMaster(Metaobject master) {
public void addDirectMaster(Metaobject master, String dependencyName) {
this.directMaster = new DependencyNode(master);
this.last = false;
this.dependencyName = dependencyName;
}

public Metaobject getBOT() { return this.bot; }
public DependencyNode next() { return this.directMaster; }
public boolean isLast() { return this.last; }
public String getDependencyName() { return this.dependencyName; }

public void setDependencyName(String name) { this.dependencyName = name; }

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ private void addDirectDependencies() {

if (!dependency.getDependent().equals(this.BONode.getBOT().getId()))
continue;
//System.out.println(dependency.getName());

Metaobject master = Util.findById(metamodel.getMetaobjects().getMetaobject(), dependency.getMaster());
Metaobject dpd = Util.findById(metamodel.getMetaobjects().getMetaobject(), dependency.getDependent());


if(dpd.getId().equals(this.BONode.getBOT().getId())) {
//System.out.println("Direct Dependency Added For " + this.BONode.getBOT().getName() + ": " + master.getName());
this.BONode.addDirectMaster(master);
this.BONode.addDirectMaster(master, dependency.getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public InputValidatorContext build() {

Metaobject master = Util.findById(model.getMetaobjects().getMetaobject(), dependency.getMaster());

Attribute attr = new MasterRefAttribute(master);
Attribute attr = new MasterRefAttribute(master, dependency.getName());
this.ctx.addInputParameter(attr);
}

Expand Down
25 changes: 15 additions & 10 deletions src/main/java/be/unamur/generator/context/MPCStringGenerator.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package be.unamur.generator.context;

import be.unamur.metamodel.Metadependency;
import be.unamur.metamodel.Metamodel;
import be.unamur.metamodel.Metamultiplepropagationconstraint;
import be.unamur.metamodel.Metaobject;

import java.math.BigInteger;
import java.util.stream.Collectors;

public class MPCStringGenerator {
public static String generateMPCCheckCode(Metamodel metamodel, Metaobject mo, Metamultiplepropagationconstraint mpc) {
Expand Down Expand Up @@ -38,45 +40,48 @@ public static String getRootMasterBusinessObject(DependencyNode rootDependencyNo
sb.append(" Supplier<BusinessObject> ").append(supplierName).append(mpcId).append("Supplier").append(" = () -> {\n");
sb.append(" //Fetching " + next.getBOT().getName()).append("\n");
sb.append(" ");
sb.append(next.getBOT().getName()).append(" mpc_").append(Util.getStringWithFirstLowerCap(next.getBOT().getName())).append(" = null;\n");
sb.append(next.getBOT().getName()).append(" mpc_").append(Util.getStringWithFirstLowerCap(next.getBOT().getName())).append("_").append(rootDependencyNode.getDependencyName()).append(" = null;\n");
sb.append(" try {\n");
sb.append(" mpc_").append(Util.getStringWithFirstLowerCap(next.getBOT().getName()));
sb.append(" mpc_").append(Util.getStringWithFirstLowerCap(next.getBOT().getName())).append("_").append(rootDependencyNode.getDependencyName());
sb.append(" = (").append(next.getBOT().getName()).append(") ").append("StubHelper.findBusinessObject(ctx, object.get");
sb.append(next.getBOT().getName()).append("Id()");
sb.append(next.getBOT().getName()).append("Id_").append(rootDependencyNode.getDependencyName()).append("()");
sb.append(");\n");
sb.append(" } catch(BusinessObjectNotFoundException e) { \n");
sb.append(" throw new FailedEventHandlingException(\"Could not check MPC:\" + e); \n");
sb.append(" }\n");

DependencyNode last = rootDependencyNode;
while(!next.isLast()) {
sb.append("\n");
DependencyNode current = next;
next = next.next();
//current.setDependencyName(rootDependencyNode.getDependencyName());

//sb.append("\n \n");
sb.append(getIndirectBusinessObject(current, next));
sb.append(getIndirectBusinessObject(current, next, last));

if(!next.isLast())
sb.append("\n");
last = current;
}

sb.append("\n\n");
sb.append(" return mpc_" + Util.getStringWithFirstLowerCap(next.getBOT().getName())).append(";\n");
sb.append(" return mpc_" + Util.getStringWithFirstLowerCap(next.getBOT().getName())).append("_").append(last.getDependencyName()).append(";\n");
sb.append(" };");

return sb.toString();
}

private static String getIndirectBusinessObject(DependencyNode previousDn, DependencyNode dn) {
private static String getIndirectBusinessObject(DependencyNode previousDn, DependencyNode dn, DependencyNode previouslyFetched) {
StringBuilder sb = new StringBuilder();

sb.append(" //Fetching " + dn.getBOT().getName()).append("\n");
sb.append(" ");
sb.append(dn.getBOT().getName()).append(" mpc_").append(Util.getStringWithFirstLowerCap(dn.getBOT().getName())).append(" = null;\n");
sb.append(dn.getBOT().getName()).append(" mpc_").append(Util.getStringWithFirstLowerCap(dn.getBOT().getName())).append("_").append(previousDn.getDependencyName()).append(" = null;\n");
sb.append(" try {\n");
sb.append(" mpc_").append(Util.getStringWithFirstLowerCap(dn.getBOT().getName()));
sb.append(" = (").append(dn.getBOT().getName()).append(") StubHelper.findBusinessObject(ctx, mpc_").append(Util.getStringWithFirstLowerCap(previousDn.getBOT().getName())).append(".");
sb.append("get").append(dn.getBOT().getName()).append("Id()");
sb.append(" mpc_").append(Util.getStringWithFirstLowerCap(dn.getBOT().getName())).append("_").append(previousDn.getDependencyName());
sb.append(" = (").append(dn.getBOT().getName()).append(") StubHelper.findBusinessObject(ctx, mpc_").append(Util.getStringWithFirstLowerCap(previousDn.getBOT().getName())).append("_").append(previouslyFetched.getDependencyName()).append(".");
sb.append("get").append(dn.getBOT().getName()).append("Id_").append(previousDn.getDependencyName()).append("()");
sb.append(");\n");
sb.append(" } catch(BusinessObjectNotFoundException e) { \n");
sb.append(" throw new FailedEventHandlingException(\"Could not check MPC:\" + e); \n");
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/be/unamur/generator/context/Master.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
public class Master {
private String name;
private boolean limitedToOne;
private String dependencyName;

public Master(String name, boolean limitedToOne) {
public Master(String name, boolean limitedToOne, String dependencyName) {
this.name = name;
this.limitedToOne = limitedToOne;
this.dependencyName = dependencyName;
}

public String getName() { return this.name; }
public boolean isLimitedToOne() { return this.limitedToOne; }
public String getDependencyName() { return this.dependencyName; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import be.unamur.metamodel.Metaobject;

public class MasterRefAttribute extends Attribute {
public MasterRefAttribute(Metaobject master) {
super("String", master.getName().substring(0, 1).toLowerCase() + master.getName().substring(1) + "Id");
public MasterRefAttribute(Metaobject master, String dependencyName) {
super("String", master.getName().substring(0, 1).toLowerCase() + master.getName().substring(1) + "Id" + "_" + dependencyName);
}

public boolean isMasterRef() { return true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public void addMethod(SpecificStateMethod method) {
}

@SuppressWarnings("unchecked")
public void addMasterBotRef(Metaobject master, boolean limitedToOne) {
public void addMasterBotRef(Metaobject master, boolean limitedToOne, String depdencyName) {
ArrayList<Master> masterRefs = this.get("masters") == null ? new ArrayList<>() : (ArrayList<Master>)this.get("masters");
masterRefs.add(new Master(Util.getStringWithFirstLowerCap(master.getName()), limitedToOne));
masterRefs.add(new Master(Util.getStringWithFirstLowerCap(master.getName()), limitedToOne, depdencyName));

this.put("masters", masterRefs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public SpecificStateContext build() {
//@TODO: For now: no difference between mandatory/optional
boolean limitedToOne = dependencyType.substring(dependencyType.indexOf("_")+1, dependencyType.length()).equals("1");

this.specificCtx.addMasterBotRef(master, limitedToOne);
this.specificCtx.addMasterBotRef(master, limitedToOne, dependency.getName());
this.specificCtx.addMasterImport(master);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/be/unamur/runtime/CollaborationSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.owlike.genson.annotation.JsonCreator;
import com.owlike.genson.annotation.JsonIgnore;
import com.owlike.genson.annotation.JsonProperty;
import org.hyperledger.fabric.contract.Context;
import be.unamur.runtime.exception.CollaborationSetupException;

import org.hyperledger.fabric.contract.Context;


public class CollaborationSetup {
private boolean setupFinalized;
private String participantsHandlerPK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.owlike.genson.Genson;

import java.util.HashMap;
import java.util.Map;
import java.time.LocalDateTime;


public class ${botName}InputValidator {
private static final Genson genson = new Genson();
Expand Down
28 changes: 15 additions & 13 deletions src/main/resources/be.unamur.generator.templates/specificState.vm
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ public class $BOTName${util.getStringWithFirstCap($stateName)}State extends $nam
#foreach($master in $masters)
#set($masterName = $master.getName())
//$util.getStringWithFirstCap($masterName)
if(!StubHelper.exists(ctx, object.get${util.getStringWithFirstCap($masterName)}Id()))
throw new FailedEventHandlingException("Master with id " + object.get$util.getStringWithFirstCap($masterName)Id() + " does not exist");
if(!StubHelper.exists(ctx, object.get${util.getStringWithFirstCap($masterName)}Id_${master.getDependencyName()}()))
throw new FailedEventHandlingException("Master with id " + object.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}() + " does not exist");

//Retrieve Master ($master.getName())
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName) = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id());
//Retrieve Master ($master.getName()_$master.getDependencyName())
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName)_$master.getDependencyName() = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id_$master.getDependencyName()());

#if($master.isLimitedToOne())
//Master $master.getName() can only have one dependent of type $BOTName. Checking if one already exists
if(StubHelper.hasLivingDependentsOfType(ctx, $util.getStringWithFirstLowerCap($masterName), object))
throw new FailedEventHandlingException("Master with id " + object.get$util.getStringWithFirstCap($masterName)Id() + " already has a living dependent of type $BOTName (limited to one)");
if(StubHelper.hasLivingDependentsOfType(ctx, $util.getStringWithFirstLowerCap($masterName)_$master.getDependencyName(), object))
throw new FailedEventHandlingException("Master with id " + object.get$util.getStringWithFirstCap($masterName)Id_$master.getDependencyName()() + " already has a living dependent of type $BOTName (limited to one)");
#end
#end
#end

##Disabled MPC in Wim Version
#if($mpcPresent)
//Checking Multiple Propagation Constraints
#foreach($mpcCheck in $mpcChecks)
Expand Down Expand Up @@ -120,8 +121,9 @@ public class $BOTName${util.getStringWithFirstCap($stateName)}State extends $nam
#foreach($master in $masters)
#set($masterName = $master.getName())
//$util.getStringWithFirstCap($masterName)
## $util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName) = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id());
${util.getStringWithFirstLowerCap($masterName)}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName), ctx);
## $util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()} = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}());
${util.getStringWithFirstLowerCap($masterName)}_${master.getDependencyName()}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()}, ctx);

#end
#end
}
Expand Down Expand Up @@ -193,7 +195,7 @@ public class $BOTName${util.getStringWithFirstCap($stateName)}State extends $nam
$BOTName currentLedgerObject = ($BOTName)StubHelper.findBusinessObject(ctx, object.getId());
#foreach($master in $masters)
#set($masterName = $master.getName())
if(!object.get$util.getStringWithFirstCap($masterName)Id().equals(currentLedgerObject.get$util.getStringWithFirstCap($masterName)Id()))
if(!object.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}().equals(currentLedgerObject.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}()))
throw new FailedEventHandlingException("The master of a Business Object (" + object.getId() + ") cannot be changed");
#end
#end
Expand All @@ -206,8 +208,8 @@ public class $BOTName${util.getStringWithFirstCap($stateName)}State extends $nam
#foreach($master in $masters)
#set($masterName = $master.getName())
//$util.getStringWithFirstCap($masterName)
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName) = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id());
${util.getStringWithFirstLowerCap($masterName)}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName), ctx);
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()} = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}());
${util.getStringWithFirstLowerCap($masterName)}_${master.getDependencyName()}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()}, ctx);
#end
#end
}
Expand Down Expand Up @@ -295,8 +297,8 @@ public class $BOTName${util.getStringWithFirstCap($stateName)}State extends $nam
#foreach($master in $masters)
#set($masterName = $master.getName())
//$util.getStringWithFirstCap($masterName)
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName) = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id());
${util.getStringWithFirstLowerCap($masterName)}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName), ctx);
$util.getStringWithFirstCap($masterName) $util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()} = ($util.getStringWithFirstCap($masterName))StubHelper.findBusinessObject(ctx, object.get$util.getStringWithFirstCap($masterName)Id_${master.getDependencyName()}());
${util.getStringWithFirstLowerCap($masterName)}_${master.getDependencyName()}.getCurrentState().$method.getName()($util.getStringWithFirstLowerCap($masterName)_${master.getDependencyName()}, ctx);
#end
#end
}
Expand Down