Skip to content

Commit

Permalink
Issue #92 Implemented constraint checking after syntax errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
seidewitz committed Jan 6, 2020
1 parent 299323a commit b174c44
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
19 changes: 4 additions & 15 deletions org.modeldriven.alf/src/org/modeldriven/alf/execution/AlfBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.nio.file.Paths;
import java.util.Collection;

import org.modeldriven.alf.syntax.common.ConstraintViolation;
import org.modeldriven.alf.syntax.common.SourceProblem;
import org.modeldriven.alf.syntax.expressions.QualifiedName;
import org.modeldriven.alf.syntax.units.NamespaceDefinition;
Expand All @@ -25,7 +24,7 @@

public abstract class AlfBase {

public static final String ALF_VERSION = "1.1.0h";
public static final String ALF_VERSION = "1.1.0i/maint-1";

protected boolean isVerbose = false;

Expand All @@ -46,10 +45,7 @@ public UnitDefinition resolve(String unitName) {
}
}

public Collection<ConstraintViolation> check(
UnitDefinition unit) {
Collection<ConstraintViolation> violations = null;

public void check(UnitDefinition unit, Collection<SourceProblem> problems) {
if (unit != null) {
NamespaceDefinition definition = unit.getDefinition();
if (unit.getImpl().resolveStub()) {
Expand All @@ -58,16 +54,9 @@ public Collection<ConstraintViolation> check(
}

RootNamespace root = RootNamespace.getRootScope();
root.deriveAll();

violations = root.checkConstraints();

if (violations.isEmpty()) {
this.printVerbose("No constraint violations.");
}
root.deriveAll();
problems.addAll(root.checkConstraints());
}

return violations;
}

// NOTE: Presumes that the problems are ordered by file name and then by line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.TreeSet;

import org.modeldriven.alf.uml.ElementFactory;
import org.modeldriven.alf.uml.StereotypeApplication;
Expand All @@ -21,7 +22,6 @@
import org.modeldriven.alf.fuml.mapping.units.ClassifierDefinitionMapping;
import org.modeldriven.alf.fuml.units.RootNamespaceImpl;
import org.modeldriven.alf.mapping.MappingError;
import org.modeldriven.alf.syntax.common.ConstraintViolation;
import org.modeldriven.alf.syntax.common.ElementReference;
import org.modeldriven.alf.syntax.common.SourceProblem;
import org.modeldriven.alf.syntax.expressions.QualifiedName;
Expand Down Expand Up @@ -94,8 +94,6 @@ public UnitDefinition parse(String unitName, boolean isFileName) {
unit = this.resolve(unitName);
}

this.printSourceProblems(rootScopeImpl.getParsingErrors());

return unit instanceof MissingUnit? null: unit;
}

Expand Down Expand Up @@ -195,13 +193,13 @@ protected void parseOptionWithArg(String option, String arg) {

public UnitDefinition process(UnitDefinition unit) {
if (unit != null) {
this.getRootScopeImpl().clearParsingErrors();
Collection<ConstraintViolation> violations = this.check(unit);
Collection<SourceProblem> parsingErrors = this.getRootScopeImpl().getParsingErrors();
this.printSourceProblems(parsingErrors.isEmpty()? violations: parsingErrors);
Collection<SourceProblem> problems = new TreeSet<>();
this.check(unit, problems);
problems.addAll(this.getRootScopeImpl().getParsingErrors());
this.printSourceProblems(problems);
if (this.isPrint) {
unit.print(true);
} else if (!this.isParseOnly && parsingErrors.isEmpty() && violations.isEmpty()) {
} else if (!this.isParseOnly && problems.isEmpty()) {
NamespaceDefinition definition = unit.getDefinition();
if (definition.getImpl().isTemplate()) {
this.println(definition.getName() + " is a template.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ public Collection<Member> resolve(String name, boolean classifierOnly) {
UnitDefinition unit = this.resolveModelUnit(qualifiedName);
if (unit != null && !(unit instanceof MissingUnit)) {
Member member = unit.getDefinition();
members.add(member);
NamespaceDefinition self = this.getSelf();
self.addOwnedMember(member);
self.addMember(member);
member.setNamespace(self);
if (member != null) {
members.add(member);
NamespaceDefinition self = this.getSelf();
self.addOwnedMember(member);
self.addMember(member);
member.setNamespace(self);
}
}
}
return members;
Expand Down Expand Up @@ -131,7 +133,7 @@ public UnitDefinition resolveModelUnit(QualifiedName qualifiedName) {

public UnitDefinition resolveModelFile(String path)
throws java.io.FileNotFoundException {
UnitDefinition unit = this.parsedUnitCache.get(path);
UnitDefinition unit = this.parsedUnitCache.get(path.toLowerCase());
if (unit != null) {
return unit instanceof MissingUnit? null: unit;
} else {
Expand All @@ -141,18 +143,14 @@ public UnitDefinition resolveModelFile(String path)
if (isVerbose) {
System.out.println("Parsed " + path);
}
if (!parser.getProblems().isEmpty()) {
this.addProblems(parser.getProblems());
this.cacheMissingUnit(path);
return null;
}
unit.getImpl().addImplicitImports();
this.cacheUnit(path, unit);
return unit;
unit.getImpl().addImplicitImports();
this.cacheUnit(path.toLowerCase(), unit);
this.addProblems(parser.getProblems());
return unit;
} catch (RuntimeException e) {
System.out.println("Parse failed: " + path);
System.out.println(e);
this.cacheMissingUnit(path);
this.cacheMissingUnit(path.toLowerCase());
return null;
}
}
Expand All @@ -163,6 +161,9 @@ private void cacheMissingUnit(String path) {
}

private void cacheUnit(String path, UnitDefinition unit) {
if (unit == null) {
unit = new MissingUnit(path);
}
this.parsedUnitCache.put(path, unit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ public UnitDefinition resolveUnit(QualifiedName qualifiedName) {
return this.getModelNamespace().resolveUnit(qualifiedName);
}

protected RootNamespaceImpl getRootNamespaceImpl() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ public ModelNamespace getSelf() {

public NamespaceDefinition getModelNamespace(UnitDefinition unit) {
NamespaceDefinition definition = unit.getDefinition();
NamespaceDefinition modelScope = definition.getNamespace();
if (modelScope == null) {
// NOTE: The model scope for a unit must include the unit itself,
// so that it can refer to itself recursively.
modelScope = this.getSelf();
modelScope.getMember(); // To ensure computation of derived attributes.
modelScope.addOwnedMember(definition);
modelScope.addMember(definition);
definition.setNamespace(modelScope);
if (definition == null) {
return null;
} else {
NamespaceDefinition modelScope = definition.getNamespace();
if (modelScope == null) {
// NOTE: The model scope for a unit must include the unit itself,
// so that it can refer to itself recursively.
modelScope = this.getSelf();
modelScope.getMember(); // To ensure computation of derived attributes.
modelScope.addOwnedMember(definition);
modelScope.addMember(definition);
definition.setNamespace(modelScope);
}
return modelScope;
}
return modelScope;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,17 +609,19 @@ protected static void addAllMembers(Collection<Member> members,
}
}

protected static void addMember(Member member, Map<String, Collection<Member>> map) {
String name = member.getName();
if (name == null) {
name = "";
}
Collection<Member> members = map.get(name);
if (members == null) {
members = new ArrayList<Member>();
map.put(name, members);
protected static void addMember(Member member, Map<String, Collection<Member>> map) {
if (member != null) {
String name = member.getName();
if (name == null) {
name = "";
}
Collection<Member> members = map.get(name);
if (members == null) {
members = new ArrayList<Member>();
map.put(name, members);
}
members.add(member);
}
members.add(member);
}

@Override
Expand Down

0 comments on commit b174c44

Please sign in to comment.