Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
3 changes: 2 additions & 1 deletion src/main/java/org/clafer/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public static void main(String[] args) throws Exception {
accepts( "n", "Specify the maximum number of instances." ).withRequiredArg().ofType( Integer.class );
accepts( "noprint", "Don't print the instances to the console or a file");
accepts( "output", "Output instances to the given file." ).withRequiredArg().ofType( File.class ).describedAs( "text file" );
accepts( "plantuml", "Print the clafer model as PlantUML" );
accepts( "prettify", "Use simple and pretty output format (not formal)." );
accepts( "sysml", "Print the instances as SysMLv2" );
accepts( "repl", "Run in REPL (interactive) mode." );
accepts( "scope", "Override the default global scope value." ).withRequiredArg().ofType( Integer.class );
accepts( "search", "PreferSmallerInstances/PreferLargerInstances/Random" ).withRequiredArg().ofType( ClaferSearchStrategy.class );
accepts( "sysml", "Print the instances as SysMLv2" );
accepts( "time", "Time how long it takes to find all instances (and print if it is turned on");
accepts( "v", "Run in validation mode; checks all assertions." );
accepts( "version", "Display the tool version" );
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/clafer/cli/Normal.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.clafer.objective.Objective;
import org.clafer.scope.Scope;
import org.clafer.ast.AstModel;
import org.plantuml.ast.PlantumlProgram;
import org.plantuml.compiler.AstPlantumlCompiler;
import org.plantuml.pprinter.PlantumlPrinter;
import org.sysml.ast.SysmlProperty;
import org.sysml.ast.SysmlPropertyDef;
import org.sysml.compiler.AstSysmlCompiler;
Expand Down Expand Up @@ -45,16 +48,36 @@ public static void runNormal(JavascriptFile javascriptFile, OptionSet options,
int index = 0; // instance id
boolean prettify = options.has("prettify");
boolean sysml = options.has("sysml");
boolean plantuml = options.has("plantuml");
boolean printOff = options.has("noprint");
boolean dataTackingOn = options.has("dataFile");
boolean timeOn = options.has("time");

// check for conflicting options
if (plantuml && sysml) {
System.err.println("Bad CLI config: both plantuml and sysml are selected");
return;
}

File dataFile;
PrintStream dataStream = null;
if (dataTackingOn) {
dataFile = (File) options.valueOf("dataFile");
dataStream = new PrintStream(dataFile);
}

if (plantuml) {
AstModel top = javascriptFile.getModel();
AstPlantumlCompiler compiler = new AstPlantumlCompiler();
PlantumlProgram prog = compiler.compile(top);
PlantumlPrinter pprinter = new PlantumlPrinter(outStream);
pprinter.visit(prog, "");
if (dataStream != null){
dataStream.close();
}
return;
}

double elapsedTime;

long startTime = System.nanoTime();
Expand Down Expand Up @@ -117,5 +140,10 @@ public static void runNormal(JavascriptFile javascriptFile, OptionSet options,
System.out.println("Generated " + (n == -1 ? "all " : "") + index + " optimal instance(s) within the scope\n");
}
}

// make sure to close this resource
if (dataStream != null){
dataStream.close();
}
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.plantuml.ast;

import java.io.IOException;

public class PlantumlConnection implements PlantumlExpr {
private final String fromObj;
private final String toObj;
private final char fromConn;
private final char toConn;

private final char lineChar;

private final String label;

public PlantumlConnection(String fromObj, String toObj, char fromConn, char toConn, String label){
this.fromObj = fromObj;
this.toObj = toObj;
this.fromConn = fromConn;
this.toConn = toConn;
this.label = label;
this.lineChar = '-';
}

public PlantumlConnection(String fromObj, String toObj, char fromConn, char toConn, String label, char lineChar){
this.fromObj = fromObj;
this.toObj = toObj;
this.fromConn = fromConn;
this.toConn = toConn;
this.label = label;
this.lineChar = lineChar;
}

public String getFromObj(){
return fromObj;
}

public String getToObj(){
return toObj;
}

public char getToConn(){
return toConn;
}

public char getFromConn(){
return fromConn;
}

public String getLabel(){
return label;
}

public char getLineChar(){
return lineChar;
}

@Override
public <A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException {
return visitor.visit(this, a);
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlExpr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.plantuml.ast;

import java.io.IOException;

public interface PlantumlExpr {
/**
* Dynamic dispatch on the visitor.
*
* @param <A> the parameter type
* @param <B> the return type
* @param visitor the visitor
* @param a the parameter
* @return the return value
*/
<A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException;
}
21 changes: 21 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlExprVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.plantuml.ast;

import java.io.IOException;

/**
* AST Visitor
*
* We make AST visitors capable of throwing IOExecptions as it's convenient for pretty printers
* However, we could likely get rid of this throw some type of interface conversion.
*/
public interface PlantumlExprVisitor<A, B> {
B visit(PlantumlProgram plantumlProgram, A a) throws IOException;

B visit(PlantumlObject plantumlObject, A a) throws IOException;

B visit(PlantumlPropertyGroup plantumlPropertyGroup, A a) throws IOException;

B visit(PlantumlProperty plantumlProperty, A a) throws IOException;

B visit(PlantumlConnection plantumlConnection, A a) throws IOException;
}
10 changes: 10 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.plantuml.ast;

public interface PlantumlId {
/**
* PlantUML <language-identifier>
*
* @return the name of the identifier
*/
String getName();
}
27 changes: 27 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.plantuml.ast;

import java.io.IOException;

public class PlantumlObject implements PlantumlExpr, PlantumlId {
private final String name;
private final PlantumlPropertyGroup[] propertyGroups;

public PlantumlObject(String name, PlantumlPropertyGroup[] propertyGroups) {
this.name = name;
this.propertyGroups = propertyGroups;
}

public PlantumlPropertyGroup[] getPropertyGroups() {
return propertyGroups;
}

@Override
public String getName() {
return name;
}

@Override
public <A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException {
return visitor.visit(this, a);
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlProgram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.plantuml.ast;

import org.sysml.ast.SysmlExpr;
import org.sysml.ast.SysmlExprVisitor;

import java.io.IOException;

/**
* Main PlantUML Program Element
*
* This is likely quite wrong. For our use case, we consider a PlantUML program as a collection
* of objects and connections.
*/
public class PlantumlProgram implements PlantumlExpr {
private PlantumlObject[] objects;
private PlantumlConnection[] connections;

public PlantumlProgram() {
this.objects = new PlantumlObject[0];
this.connections = new PlantumlConnection[0];
}

public PlantumlProgram(PlantumlObject[] objects, PlantumlConnection[] connections) {
this.objects = objects;
this.connections = connections;

}

public PlantumlObject[] getObjects(){
return objects;
}

public PlantumlConnection[] getConnections(){
return connections;
}

@Override
public <A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException {
return visitor.visit(this, a);
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.plantuml.ast;

import java.io.IOException;

public class PlantumlProperty implements PlantumlExpr {
private final String prop;

public PlantumlProperty(String prop) {
this.prop = prop;
}

public String getProp(){
return prop;
}

@Override
public <A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException {
return visitor.visit(this, a);
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/plantuml/ast/PlantumlPropertyGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.plantuml.ast;

import java.io.IOException;

public class PlantumlPropertyGroup implements PlantumlId, PlantumlExpr {

private final String name;
private PlantumlProperty[] properties;

public PlantumlPropertyGroup(String name, PlantumlProperty[] properties) {
this.properties = properties;
this.name = name;
}

public PlantumlProperty[] getProperties() {
return this.properties;
}

@Override
public String getName() {
return name;
}

@Override
public <A, B> B accept(PlantumlExprVisitor<A, B> visitor, A a) throws IOException {
return visitor.visit(this, a);
}
}
Loading