diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index e78c106..cd3d92d 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -73,11 +73,13 @@ subTerms.put(new Position(), (T) this); } for (int i = 0; i < children.size(); i++) { - HashMap terms = children.get(i).getSubTerms(clazz); - for (Entry term: terms.entrySet()) { - Position pos = term.getKey(); - pos.addHeadOrder(i); - subTerms.put(pos, term.getValue()); + if (children.get(i) != null) { + HashMap terms = children.get(i).getSubTerms(clazz); + for (Entry term: terms.entrySet()) { + Position pos = term.getKey(); + pos.addHeadOrder(i); + subTerms.put(pos, term.getValue()); + } } } return subTerms; @@ -117,6 +119,8 @@ @Override public Expression unify(Expression another) { if (another instanceof Variable) return (Expression) this.clone(); + if (this instanceof Constant) return (Expression) this.clone(); + if (another instanceof Constant) return (Expression) another.clone(); if (another instanceof Term) { Term anotherTerm = (Term) another; if (!symbol.equals(anotherTerm.symbol)) return null; @@ -232,7 +236,11 @@ public Object clone() { Term newTerm = new Term(symbol); for (Expression e: children) { - newTerm.addChild((Expression) e.clone()); + if (e != null) { + newTerm.addChild((Expression) e.clone()); + } else { + newTerm.addChild(null); + } } newTerm.type = type; return newTerm; @@ -247,7 +255,11 @@ String delimiter = ""; for (int i = 1; i < children.size(); i++) { Expression e = children.get(i); - exp += (delimiter + e.toString()); + if (e != null) { + exp += (delimiter + e.toString()); + } else { + exp += (delimiter + "null"); + } delimiter = ","; } return exp + ")"; diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 6d8e176..0b5f65b 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -262,7 +262,7 @@ lookup.setSignature(new Type[] {null, typeMap, null}); addMember.setSignature(new Type[] {typeJson, typeJson, typeString, null}); dot.setSignature(new Type[] {null, typeJson, typeString}); - dotParam.setSignature(new Type[] {null, null, null}); + dotParam.setSignature(new Type[] {null, typeJson, null}); pi.setSignature(new Type[] {typeDouble}); E.setSignature(new Type[] {typeDouble}); sqrt.setSignature(new Type[] {typeDouble, typeDouble}); diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java index ea5f9f2..aea0590 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java @@ -21,20 +21,6 @@ super(symbol); } - public Type getType() { - if (symbol.equals(DataConstraintModel.dotParam)) { - Type valueType = null; - if (getChild(1) instanceof Term) { - valueType = ((Term) getChild(1)).getType(); - } else if (getChild(1) instanceof Variable) { - valueType = ((Variable) getChild(1)).getType(); - } - if (valueType != null) return valueType; - } - return super.getType(); - } - - @Override public Expression getInverseMap(Expression outputValue, Position targetPos) { if (targetPos.isEmpty()) return outputValue; @@ -42,15 +28,15 @@ int i = targetPos.removeHeadOrder(); Symbol[] inverseSymbols = symbol.getInverses(); if (i == 0) { - if (symbol.equals(DataConstraintModel.dot) && getChildren().size() >= 2) { + if (symbol == DataConstraintModel.dot && getChildren().size() >= 2) { // this term is `json.key`. Expression expJson = getChild(0); Expression expKey = getChild(1); - JsonType jsonType = null; + Type jsonType = null; if (expJson instanceof Variable) { - jsonType = (JsonType) ((Variable) expJson).getType(); + jsonType = ((Variable) expJson).getType(); } else if (expJson instanceof Term) { - jsonType = (JsonType) ((Term) expJson).getType(); + jsonType = ((Term) expJson).getType(); } String keyName = null; if (expKey instanceof Constant) { @@ -62,8 +48,11 @@ Set keySet = new HashSet<>(); if (jsonType == null || jsonType == DataConstraintModel.typeJson) { keySet.add(keyName); - } else { - keySet.addAll(jsonType.getKeys()); + } else if (jsonType instanceof JsonType) { + keySet.addAll(((JsonType) jsonType).getKeys()); + if (keySet.size() == 0) { + keySet.add(keyName); + } } for (String key: keySet) { Term addMemberTerm = new Term(DataConstraintModel.addMember); // addMember(jsonTerm, key, v) @@ -79,7 +68,7 @@ LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, jsonTerm); // v -> addMember(jsonTerm, key, v) inverseSymbols = new Symbol[] { lambdaAbstraction }; } - } else if (symbol.equals(DataConstraintModel.dotParam) && getChildren().size() >= 2) { + } else if (symbol == DataConstraintModel.dotParam && getChildren().size() >= 2) { // this term is `json.{param}`. Expression expListOrMap = getChild(0); Expression expKey = getChild(1); @@ -91,9 +80,9 @@ } Type keyType = null; if (expKey instanceof Variable) { - keyType = (JsonType) ((Variable) expKey).getType(); + keyType = ((Variable) expKey).getType(); } else if (expKey instanceof Term) { - keyType = (JsonType) ((Term) expKey).getType(); + keyType = ((Term) expKey).getType(); } if (jsonType != null && keyType != null) { if (DataConstraintModel.typeList.isAncestorOf(jsonType) || keyType.equals(DataConstraintModel.typeInt)) { diff --git a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java index 0c980bc..852fa34 100644 --- a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java +++ b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java @@ -424,7 +424,7 @@ paramType = ((Term) paramTerm).getType(); } Term term = null; - if (paramType != null && DataTransferModel.typeInt.isAncestorOf(paramType)) { + if (literalOrLeftCurlyBracket.equals(LEFT_CURLY_BRACKET)) { term = new JsonAccessor(DataTransferModel.dotParam); } else { term = new JsonAccessor(DataTransferModel.dot);