Skip to content

Commit

Permalink
Unintuitive parsing of arc expressions - fix 2092211 (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
srba authored Jan 7, 2025
2 parents c9330c6 + f9a3142 commit c1b9bb6
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 174 deletions.
79 changes: 61 additions & 18 deletions src/main/java/dk/aau/cs/io/LoadTACPN.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
package dk.aau.cs.io;

import dk.aau.cs.model.CPN.*;
import dk.aau.cs.model.CPN.Expressions.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import java.util.function.Consumer;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import dk.aau.cs.model.CPN.Color;
import dk.aau.cs.model.CPN.ColorMultiset;
import dk.aau.cs.model.CPN.ColorType;
import dk.aau.cs.model.CPN.Expressions.AddExpression;
import dk.aau.cs.model.CPN.Expressions.AllExpression;
import dk.aau.cs.model.CPN.Expressions.AndExpression;
import dk.aau.cs.model.CPN.Expressions.ArcExpression;
import dk.aau.cs.model.CPN.Expressions.ColorExpression;
import dk.aau.cs.model.CPN.Expressions.DotConstantExpression;
import dk.aau.cs.model.CPN.Expressions.EqualityExpression;
import dk.aau.cs.model.CPN.Expressions.GreaterThanEqExpression;
import dk.aau.cs.model.CPN.Expressions.GreaterThanExpression;
import dk.aau.cs.model.CPN.Expressions.GuardExpression;
import dk.aau.cs.model.CPN.Expressions.InequalityExpression;
import dk.aau.cs.model.CPN.Expressions.LessThanEqExpression;
import dk.aau.cs.model.CPN.Expressions.LessThanExpression;
import dk.aau.cs.model.CPN.Expressions.NotExpression;
import dk.aau.cs.model.CPN.Expressions.NumberOfExpression;
import dk.aau.cs.model.CPN.Expressions.OrExpression;
import dk.aau.cs.model.CPN.Expressions.PredecessorExpression;
import dk.aau.cs.model.CPN.Expressions.ScalarProductExpression;
import dk.aau.cs.model.CPN.Expressions.SubtractExpression;
import dk.aau.cs.model.CPN.Expressions.SuccessorExpression;
import dk.aau.cs.model.CPN.Expressions.TupleExpression;
import dk.aau.cs.model.CPN.Expressions.UserOperatorExpression;
import dk.aau.cs.model.CPN.Expressions.VariableExpression;
import dk.aau.cs.model.CPN.ProductType;
import dk.aau.cs.model.CPN.Variable;
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
import dk.aau.cs.util.FormatException;
import dk.aau.cs.util.NameTransformer;
import dk.aau.cs.util.Require;
import dk.aau.cs.util.Tuple;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.*;
import java.util.function.Consumer;

public class LoadTACPN { //the import feature for CPN and load for TACPN share similarities. These similarities are shared here. Feel free to find a better name for this class

Expand Down Expand Up @@ -224,47 +255,59 @@ public ArcExpression parseArcExpression(Node node) throws FormatException {
return parseNumberOfExpression(node);
} else if (name.equals("add")) {
Vector<ArcExpression> constituents = new Vector<>();

Node child = skipWS(node.getFirstChild());
while (child != null) {
ArcExpression subterm = parseArcExpression(child);
constituents.add(subterm);
child = skipWS(child.getNextSibling());
}
return new AddExpression(constituents);

AddExpression addExpr = new AddExpression(constituents);
for (ArcExpression expr : constituents) {
expr.setParent(addExpr);
}

return addExpr;
} else if (name.equals("subtract")) {
Node headchild = skipWS(node.getFirstChild());
ArcExpression headexp = parseArcExpression(headchild);

Node nextchild = skipWS(headchild.getNextSibling());
while (nextchild != null) {
ArcExpression nextexp = parseArcExpression(nextchild);
headexp = new SubtractExpression(headexp, nextexp);
SubtractExpression subExpr = new SubtractExpression(headexp, nextexp);
headexp.setParent(subExpr);
nextexp.setParent(subExpr);

headexp = subExpr;
nextchild = skipWS(nextchild.getNextSibling());
}

return headexp;
} else if (name.equals("scalarproduct")) {
Node scalar = skipWS(node.getFirstChild());
Integer scalarval = parseNumberConstantExpression(scalar);

Node child = skipWS(scalar.getNextSibling());
ArcExpression childexp = parseArcExpression(child);

return new ScalarProductExpression(scalarval, childexp);
}else if (name.equals("all")){

ScalarProductExpression scalarExpr = new ScalarProductExpression(scalarval, childexp);
childexp.setParent(scalarExpr);
return scalarExpr;
} else if (name.equals("all")){
ColorType ct = parseUserSort(node);
Vector<ColorExpression> ceVector = new Vector<>();
ceVector.add(new AllExpression(ct));
return new NumberOfExpression(1,ceVector);

} else if (name.matches("subterm|structure")) {
Node child = skipWS(node.getFirstChild());
return parseArcExpression(child);
} else if (name.matches("tuple")){
Vector<ColorExpression> ceVector = new Vector<>();
ceVector.add(parseColorExpression(node));
return new NumberOfExpression(1, ceVector);
} else{
} else {
throw new FormatException(String.format("Could not parse %s as an arc expression\n", name));
}
}
Expand Down
42 changes: 35 additions & 7 deletions src/main/java/dk/aau/cs/io/writeTACPN.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package dk.aau.cs.io;

import dk.aau.cs.model.CPN.*;
import dk.aau.cs.model.CPN.Expressions.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import dk.aau.cs.model.CPN.Color;
import dk.aau.cs.model.CPN.ColorType;
import dk.aau.cs.model.CPN.Expressions.AddExpression;
import dk.aau.cs.model.CPN.Expressions.AllExpression;
import dk.aau.cs.model.CPN.Expressions.AndExpression;
import dk.aau.cs.model.CPN.Expressions.ArcExpression;
import dk.aau.cs.model.CPN.Expressions.DotConstantExpression;
import dk.aau.cs.model.CPN.Expressions.EqualityExpression;
import dk.aau.cs.model.CPN.Expressions.Expression;
import dk.aau.cs.model.CPN.Expressions.GreaterThanEqExpression;
import dk.aau.cs.model.CPN.Expressions.GreaterThanExpression;
import dk.aau.cs.model.CPN.Expressions.InequalityExpression;
import dk.aau.cs.model.CPN.Expressions.LessThanEqExpression;
import dk.aau.cs.model.CPN.Expressions.LessThanExpression;
import dk.aau.cs.model.CPN.Expressions.NotExpression;
import dk.aau.cs.model.CPN.Expressions.NumberOfExpression;
import dk.aau.cs.model.CPN.Expressions.OrExpression;
import dk.aau.cs.model.CPN.Expressions.PredecessorExpression;
import dk.aau.cs.model.CPN.Expressions.ScalarProductExpression;
import dk.aau.cs.model.CPN.Expressions.SubtractExpression;
import dk.aau.cs.model.CPN.Expressions.SuccessorExpression;
import dk.aau.cs.model.CPN.Expressions.TupleExpression;
import dk.aau.cs.model.CPN.Expressions.UserOperatorExpression;
import dk.aau.cs.model.CPN.Expressions.VariableExpression;
import dk.aau.cs.model.CPN.ProductType;
import dk.aau.cs.model.CPN.Variable;
import dk.aau.cs.model.tapn.TimedArcPetriNetNetwork;
import dk.aau.cs.model.tapn.TimedPlace;
import dk.aau.cs.model.tapn.TimedTransition;
import dk.aau.cs.util.Require;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import pipe.gui.petrinet.dataLayer.DataLayer;
import pipe.gui.petrinet.graphicElements.Arc;
import pipe.gui.petrinet.graphicElements.Transition;
Expand Down Expand Up @@ -127,7 +152,7 @@ else if(expression instanceof UserOperatorExpression) {
}

}
else if(expression instanceof VariableExpression) {
else if (expression instanceof VariableExpression) {
Element variableElement = document.createElement("variable");
variableElement.setAttribute("refvariable", ((VariableExpression) expression).getVariable().getId());
structureElement.appendChild(variableElement);
Expand All @@ -151,10 +176,12 @@ else if (expression instanceof PredecessorExpression) {
else if (expression instanceof AddExpression) {
Element addElement = document.createElement("add");
AddExpression expr = (AddExpression) expression;
for (ArcExpression arcExpression: expr.getAddExpression()) {

for (ArcExpression arcExpression : expr.getAddExpression()) {
Element subtermElement = document.createElement("subterm");
addElement.appendChild(parseArcExpression(arcExpression,document,subtermElement));
addElement.appendChild(parseArcExpression(arcExpression, document, subtermElement));
}

structureElement.appendChild(addElement);
}
else if (expression instanceof SubtractExpression) {
Expand All @@ -164,6 +191,7 @@ else if (expression instanceof SubtractExpression) {
subtractElement.appendChild(subtermLeftElement);
subtractElement.appendChild(subtermRightElement);
SubtractExpression expr = (SubtractExpression) expression;

subtractElement.appendChild(parseArcExpression(expr.getLeftExpression(), document, subtermLeftElement));
subtractElement.appendChild(parseArcExpression(expr.getRightExpression(), document, subtermRightElement));
structureElement.appendChild(subtractElement);
Expand Down
77 changes: 40 additions & 37 deletions src/main/java/dk/aau/cs/model/CPN/Expressions/AddExpression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dk.aau.cs.model.CPN.Expressions;

import java.util.Set;
import java.util.Vector;

import dk.aau.cs.model.CPN.Color;
import dk.aau.cs.model.CPN.ColorMultiset;
import dk.aau.cs.model.CPN.ColorType;
Expand All @@ -8,15 +11,19 @@
import dk.aau.cs.model.CPN.Variable;
import dk.aau.cs.util.Require;

import java.util.Objects;
import java.util.Set;
import java.util.Vector;
import java.util.stream.Collectors;

public class AddExpression extends ArcExpression {

private final Vector<ArcExpression> constituents;

public AddExpression(ArcExpression left, ArcExpression right) {
Require.notNull(left);
Require.notNull(right);
Vector<ArcExpression> constituents = new Vector<>();
constituents.add(left);
constituents.add(right);
this.constituents = constituents;
}

public AddExpression(Vector<ArcExpression> constituents) {
Require.notNull(constituents);
Require.that(constituents.size() > 0, "Constituents can't be empty");
Expand All @@ -33,7 +40,7 @@ public AddExpression(AddExpression otherExpr) {
this.constituents = new Vector<>(otherExpr.constituents);
}

public Vector<ArcExpression> getAddExpression (){return constituents;}
public Vector<ArcExpression> getAddExpression () { return constituents; }

public ColorMultiset eval(ExpressionContext context) {
ColorMultiset result = null;
Expand Down Expand Up @@ -70,13 +77,16 @@ public ArcExpression replace(Expression object1, Expression object2){
@Override
public ArcExpression replace(Expression object1, Expression object2, boolean replaceAllInstances) {
if (object1 == this && object2 instanceof ArcExpression) {
ArcExpression obj2 = (ArcExpression) object2;
ArcExpression obj2 = (ArcExpression)object2;
obj2.setParent(parent);
return obj2;
} else {
for (int i = 0; i < constituents.size(); i++) {
constituents.set(i, constituents.get(i).replace(object1, object2, replaceAllInstances));
for (int i = 0; i < constituents.size(); ++i) {
ArcExpression newConstituent = constituents.get(i).replace(object1, object2, replaceAllInstances);
constituents.set(i, newConstituent);
newConstituent.setParent(this);
}

return this;
}
}
Expand All @@ -92,7 +102,11 @@ public ArcExpression deepCopy() {
for (ArcExpression expr : constituents) {
constituentsCopy.add(expr.deepCopy());
}
return new AddExpression(constituentsCopy);

ArcExpression copy = new AddExpression(constituentsCopy);
copy.setParent(parent);

return copy;
}

@Override
Expand Down Expand Up @@ -128,33 +142,21 @@ public void getValues(ExprValues exprValues) {
@Override
public ExprStringPosition[] getChildren() {
ExprStringPosition[] children = new ExprStringPosition[constituents.size()];
int i = 0;
int endPrev = 0;
for (ArcExpression p : constituents) {

int start = 1;
int end = 0;

if (i == 0) {
end = start + p.toString().length();
endPrev = end;
} else {
start = endPrev + 3;
end = start + p.toString().length();

endPrev = end;
int currentPosition = addParentheses() ? 1 : 0;

for (int i = 0; i < constituents.size(); ++i) {
ArcExpression constituent = constituents.get(i);

if (i > 0) {
currentPosition += 3;
}

ExprStringPosition pos = new ExprStringPosition(start, end, p);

children[i] = pos;
i++;

int constituentEnd = currentPosition + constituent.toString().length();
children[i] = new ExprStringPosition(currentPosition, constituentEnd, constituent);
currentPosition = constituentEnd;
}

return children;



}

@Override
Expand Down Expand Up @@ -182,13 +184,14 @@ public boolean containsColor(Color color) {
return false;
}

@Override
public String toString() {

StringBuilder res = new StringBuilder("(" + constituents.get(0).toString());
StringBuilder res = new StringBuilder(addParentheses() ? "(" + constituents.get(0).toString() : constituents.get(0).toString());
for (int i = 1; i < constituents.size(); ++i) {
res.append(" + ").append(constituents.get(i).toString());
}
return res + ")";

return addParentheses() ? res.append(")").toString() : res.toString();
}

public String toTokenString() {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/dk/aau/cs/model/CPN/Expressions/ArcExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
import dk.aau.cs.model.CPN.ColorType;

public abstract class ArcExpression extends Expression {

protected ArcExpression parent;

public ArcExpression() {

}
public ArcExpression() {}

public ArcExpression(ArcExpression otherExpr) {
this.parent = otherExpr.parent;
}

@Override
public abstract ArcExpression replace(Expression object1, Expression object2,boolean replaceAllInstances);
public abstract ArcExpression replace(Expression object1, Expression object2);

@Override
public abstract ArcExpression replace(Expression object1, Expression object2);

public ArcExpression getParent() {return parent;}
public ArcExpression getParent() { return parent; }

public void setParent(ArcExpression parent) {this.parent = parent; }
public void setParent(ArcExpression parent) { this.parent = parent; }

@Override
public abstract ArcExpression copy();
Expand All @@ -31,12 +30,15 @@ public ArcExpression(ArcExpression otherExpr) {
@Override
public abstract Expression findFirstPlaceHolder();


public abstract ColorMultiset eval(ExpressionContext context);

public abstract Integer weight();

public abstract ArcExpression getExprWithNewColorType(ColorType ct);

public abstract ArcExpression getExprConverted(ColorType oldCt, ColorType newCt);

public boolean addParentheses() {
return parent != null && parent.getClass() != getClass();
}
}
Loading

0 comments on commit c1b9bb6

Please sign in to comment.