use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.
the class Main method rewriteModel.
/**
* Perform various rewrites that cannot be done in JastAdd.
*
* JastAdd rewrite rules can only rewrite the current node using
* node-local information. ("The code in the body of the rewrite may
* access and rearrange the nodes in the subtree rooted at A, but not any
* other nodes in the AST. Furthermore, the code may not have any other
* side effects." --
* http://jastadd.org/web/documentation/reference-manual.php#Rewrites)
*
* We use this method to generate Exception constructors and the
* information in ABS.Productline.
*
* @param m the model.
* @param productname The name of the product or null.
* @throws WrongProgramArgumentException
*/
private static void rewriteModel(Model m, String productname) throws WrongProgramArgumentException {
// Generate reflective constructors for all features
ProductLine pl = m.getProductLine();
if (pl != null) {
// Let's assume the module and datatype names in abslang.abs did
// not get changed, and just crash otherwise. If you're here
// because of a NPE: Hi! Make the standard library and this code
// agree about what the feature reflection module is called.
ModuleDecl modProductline = null;
DataTypeDecl featureDecl = null;
FunctionDecl currentFeatureFun = null;
FunctionDecl productNameFun = null;
for (ModuleDecl d : m.getModuleDecls()) {
if (d.getName().equals(Constants.PL_NAME)) {
modProductline = d;
break;
}
}
if (modProductline == null) {
throw new WrongProgramArgumentException("Internal error: did not find module " + Constants.PL_NAME + "(should have been defined in the abslang.abs standard library)");
}
for (Decl d : modProductline.getDecls()) {
if (d instanceof DataTypeDecl && d.getName().equals("Feature")) {
featureDecl = (DataTypeDecl) d;
} else if (d instanceof FunctionDecl && d.getName().equals("product_features")) {
currentFeatureFun = (FunctionDecl) d;
} else if (d instanceof FunctionDecl && d.getName().equals("product_name")) {
productNameFun = (FunctionDecl) d;
}
}
// Adjust Feature datatype
featureDecl.setDataConstructorList(new List<>());
for (Feature f : pl.getFeatures()) {
// TODO: when/if we incorporate feature parameters into the
// productline feature declarations (as we should), we need to
// adjust the DataConstructor arguments here.
featureDecl.addDataConstructorNoTransform(new DataConstructor(f.getName(), new List<>()));
}
// Adjust product_name() function
productNameFun.setFunctionDef(new ExpFunctionDef(new StringLiteral(productname)));
// Adjust product_features() function
ProductDecl p = null;
if (productname != null)
p = m.findProduct(productname);
if (p != null) {
DataConstructorExp feature_arglist = new DataConstructorExp("Cons", new List<>());
DataConstructorExp current = feature_arglist;
for (Feature f : p.getProduct().getFeatures()) {
DataConstructorExp next = new DataConstructorExp("Cons", new List<>());
// TODO: when/if we incorporate feature parameters into
// the productline feature declarations (as we should), we
// need to adjust the DataConstructorExp arguments here.
current.addParamNoTransform(new DataConstructorExp(f.getName(), new List<>()));
current.addParamNoTransform(next);
current = next;
}
current.setConstructor("Nil");
currentFeatureFun.setFunctionDef(new ExpFunctionDef(feature_arglist));
}
}
m.flushTreeCache();
}
use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.
the class Main method mainMethod.
public int mainMethod(Absc arguments) {
int result = 0;
boolean done = false;
this.arguments = arguments;
try {
if (arguments.backend != null) {
if (arguments.backend.maude) {
result = Math.max(result, MaudeCompiler.doMain(arguments));
done = true;
}
if (arguments.backend.java) {
result = Math.max(result, JavaBackend.doMain(arguments));
done = true;
}
if (arguments.backend.erlang) {
result = Math.max(result, ErlangBackend.doMain(arguments));
done = true;
}
if (arguments.backend.prolog) {
result = Math.max(result, PrologBackend.doMain(arguments));
done = true;
}
if (arguments.backend.coreabs) {
result = Math.max(result, CoreAbsBackend.doMain(arguments));
done = true;
}
if (arguments.backend.json) {
result = Math.max(result, Tester.doMain(arguments));
done = true;
}
if (arguments.backend.prettyprint) {
result = Math.max(result, PrettyPrinterBackEnd.doMain(arguments));
done = true;
}
if (arguments.backend.outline) {
result = Math.max(result, OutlinePrinterBackEnd.doMain(arguments));
done = true;
}
if (arguments.backend.dumpProducts) {
Model m = parse(arguments.files);
if (m.hasParserErrors()) {
// parse should have already printed errors
result = Math.max(result, 1);
} else {
Iterator<ProductDecl> pi = m.getProductDecls().iterator();
while (pi.hasNext()) {
System.out.print(pi.next().getName());
if (pi.hasNext())
System.out.print(' ');
}
}
done = true;
}
}
if (!done) {
// no backend selected, just do type-checking
Model m = parse(arguments.files);
if (m.hasParserErrors() || m.hasErrors() || m.hasTypeErrors()) {
printErrorMessage();
result = 1;
}
}
} catch (InternalBackendException e) {
// don't print stack trace here
printError(e.getMessage());
result = 1;
} catch (Exception e) {
if (e.getMessage() == null) {
e.printStackTrace();
}
assert e.getMessage() != null : e.toString();
printError(e.getMessage());
result = 1;
}
return result;
}
use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.
the class CheckSPLCommand method analyzeMTVL.
private void analyzeMTVL(Model m) {
if (m.hasMTVL()) {
if (solve) {
if (parent.verbose)
System.out.println("Searching for solutions for the feature model...");
ChocoSolver s = m.instantiateCSModel();
System.out.print(s.getSolutionsAsString());
}
if (minimise != null) {
if (parent.verbose)
System.out.println("Searching for minimum solutions of " + minimise + " for the feature model...");
ChocoSolver s = m.instantiateCSModel();
System.out.print(s.minimiseToString(minimise));
}
if (maximise != null) {
if (parent.verbose)
System.out.println("Searching for maximum solutions of " + maximise + " for the feature model...");
ChocoSolver s = m.instantiateCSModel();
// System.out.print(s.maximiseToInt(product));
s.addConstraint(ChocoSolver.eqeq(s.vars.get(maximise), s.maximiseToInt(maximise)));
ChocoSolver s1 = m.instantiateCSModel();
int i = 1;
while (s1.solveAgain()) {
System.out.println("------ " + (i++) + "------");
System.out.print(s1.getSolutionsAsString());
}
}
if (solveall) {
if (parent.verbose)
System.out.println("Searching for all solutions for the feature model...");
ChocoSolver solver = m.instantiateCSModel();
System.out.print(solver.getSolutionsAsString());
}
if (solveWithProduct != null) {
ProductDecl solveWithDecl = null;
try {
solveWithDecl = m.findProduct(solveWithProduct);
} catch (WrongProgramArgumentException e) {
// nothing to do
}
if (solveWithDecl != null) {
if (parent.verbose)
System.out.println("Searching for solution that includes " + solveWithProduct + "...");
ChocoSolver s = m.instantiateCSModel();
HashSet<Constraint> newcs = new HashSet<>();
solveWithDecl.getProduct().getProdConstraints(s.vars, newcs);
for (Constraint c : newcs) s.addConstraint(c);
System.out.println("checking solution:\n" + s.getSolutionsAsString());
} else {
System.out.println("Product '" + solveWithProduct + "' not found.");
}
}
if (minWith != null) {
ProductDecl minWithDecl = null;
try {
minWithDecl = m.findProduct(minWith);
} catch (WrongProgramArgumentException e) {
// nothing to do
}
if (minWithDecl != null) {
if (parent.verbose)
System.out.println("Searching for solution that includes " + minWith + "...");
ChocoSolver s = m.instantiateCSModel();
HashSet<Constraint> newcs = new HashSet<>();
s.addIntVar("difference", 0, 50);
m.getDiffConstraints(minWithDecl.getProduct(), s.vars, newcs, "difference");
for (Constraint c : newcs) s.addConstraint(c);
System.out.println("checking solution: " + s.minimiseToString("difference"));
} else {
System.out.println("Product '" + minWith + "' not found.");
}
}
if (maxProduct) {
if (parent.verbose)
System.out.println("Searching for solution with maximum number of features ...");
ChocoSolver s = m.instantiateCSModel();
HashSet<Constraint> newcs = new HashSet<>();
s.addIntVar("noOfFeatures", 0, 50);
if (m.getMaxConstraints(s.vars, newcs, "noOfFeatures")) {
for (Constraint c : newcs) s.addConstraint(c);
System.out.println("checking solution: " + s.maximiseToString("noOfFeatures"));
} else {
System.out.println("---No solution-------------");
}
}
if (checkProduct != null) {
ProductDecl checkProductDecl = null;
try {
checkProductDecl = m.findProduct(checkProduct);
} catch (WrongProgramArgumentException e) {
// nothing to do
}
if (checkProductDecl == null) {
System.out.println("Product '" + checkProduct + "' not found, cannot check.");
} else {
ChocoSolver s = m.instantiateCSModel();
Map<String, Integer> guess = checkProductDecl.getProduct().getSolution();
System.out.println("checking solution: " + s.checkSolution(guess, m));
}
}
if (numbersol) {
ChocoSolver s = m.instantiateCSModel();
// did we call m.dropAttributes() previously?
if (ignoreattr) {
System.out.println("Number of solutions found (without attributes): " + s.countSolutions());
} else {
System.out.println("Number of solutions found: " + s.countSolutions());
}
}
}
}
use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.
the class DeltaOrderingTest method properSorting1.
@Test
public void properSorting1() throws DeltaModellingException, WrongProgramArgumentException {
Model model = assertParse("module Test;" + "delta D1;" + "delta D2;" + "productline PL;" + "features A,B;" + "delta D1 when A;" + "delta D2 after D1 when B;" + "product P1(A);" + "product P2(A, B);");
model.evaluateAllProductDeclarations();
ProductDecl prod = model.findProduct("P1");
ProductLine pl = model.getProductLine();
Set<String> deltaids = pl.findApplicableDeltas(prod.getProduct());
List<String> sorted_deltaids = pl.sortDeltas(deltaids);
assertArrayEquals(new String[] { "D1" }, sorted_deltaids.toArray());
prod = model.findProduct("P2");
deltaids = pl.findApplicableDeltas(prod.getProduct());
sorted_deltaids = pl.sortDeltas(deltaids);
assertArrayEquals(new String[] { "D1", "D2" }, sorted_deltaids.toArray());
assertFalse(model.typeCheck().containsErrors());
}
use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.
the class SearchSolutionsTest method CheckEmptyProduct.
@Test
public void CheckEmptyProduct() throws WrongProgramArgumentException {
Model model = assertParse(withoutProducLine);
ChocoSolver s = model.instantiateCSModel();
model.evaluateAllProductDeclarations();
ProductDecl product = model.findProduct("P");
Map<String, Integer> guess = product.getProduct().getSolution();
assertEquals(true, s.checkSolution(guess, model));
}
Aggregations