All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.io.github.moonlightsuite.moonlight.io.FormulaToTaliro Maven / Gradle / Ivy

Go to download

MoonLight is a light-weight Java-tool for monitoring temporal, spatial and spatio-temporal properties of distributed complex systems, such as Cyber-Physical Systems and Collective Adaptive Systems.

The newest version!
/**
 *
 */
package io.github.moonlightsuite.moonlight.io;

import io.github.moonlightsuite.moonlight.formula.classic.AndFormula;
import io.github.moonlightsuite.moonlight.formula.AtomicFormula;
import io.github.moonlightsuite.moonlight.formula.temporal.EventuallyFormula;
import io.github.moonlightsuite.moonlight.core.formula.Formula;
import io.github.moonlightsuite.moonlight.core.formula.FormulaVisitor;
import io.github.moonlightsuite.moonlight.formula.temporal.GloballyFormula;
import io.github.moonlightsuite.moonlight.formula.temporal.HistoricallyFormula;
import io.github.moonlightsuite.moonlight.core.formula.Interval;
import io.github.moonlightsuite.moonlight.formula.classic.NegationFormula;
import io.github.moonlightsuite.moonlight.formula.temporal.OnceFormula;
import io.github.moonlightsuite.moonlight.formula.classic.OrFormula;
import io.github.moonlightsuite.moonlight.formula.temporal.SinceFormula;
import io.github.moonlightsuite.moonlight.formula.temporal.UntilFormula;

import java.util.Arrays;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * @author loreti
 *
 */
public class FormulaToTaliro implements FormulaVisitor {

	@Override
	public String visit(AtomicFormula atomicFormula, String parameters) {
		return atomicFormula.getAtomicId();
	}

	@Override
	public String visit(AndFormula andFormula, String parameters) {
        return "( " + andFormula.getFirstArgument().accept(this, parameters) +
        		" /\\ " + andFormula.getSecondArgument().accept(this, parameters) + " )";
	}



	@Override
	public String visit(NegationFormula negationFormula, String parameters) {
        return "! (" + negationFormula.getArgument().accept(this, parameters) +")";
	}

	@Override
	public String visit(OrFormula orFormula, String parameters) {
        return "( " + orFormula.getFirstArgument().accept(this, parameters) + " \\/ " + orFormula.getSecondArgument().accept(this, parameters) + " )";
	}

	@Override
	public String visit(EventuallyFormula eventuallyFormula, String parameters) {
        return "( <>_" + intervalToTaliro(eventuallyFormula.getInterval()) + " " + eventuallyFormula.getArgument().accept(this, parameters)+ " )";
	}

	private String intervalToTaliro(Interval interval) {
        return "[" + interval.getStart() + "," + interval.getEnd() + "]";
	}

	@Override
	public String visit(GloballyFormula globallyFormula, String parameters) {
        return "( []_" + intervalToTaliro(globallyFormula.getInterval()) + " " + globallyFormula.getArgument().accept(this, parameters) + " )";
	}

	@Override
	public String visit(UntilFormula untilFormula, String parameters) {
        return "( " + untilFormula.getFirstArgument().accept(this, parameters) + " U_" + intervalToTaliro(untilFormula.getInterval()) + " " + untilFormula.getSecondArgument().accept(this, parameters) + " )";
	}


	@Override
	public String visit(SinceFormula sinceFormula, String parameters) {
		throw new IllegalArgumentException("Past formulas are not compatible with Taliro!");
	}

	@Override
	public String visit(HistoricallyFormula historicallyFormula, String parameters) {
		throw new IllegalArgumentException("Past formulas are not compatible with Taliro!");
	}

	@Override
	public String visit(OnceFormula onceFormula, String parameters) {
		throw new IllegalArgumentException("Past formulas are not compatible with Taliro!");
	}

	public String toTaliro(Formula formula) {
		return "psi ='"+formula.accept(this, null)+"';";
	}

	public String createPrefix(Map variables){
		BiFunction prefix = (name,index) -> "pred("+index+").str = \'"+name+"\';\npred("+index+").A   =  "+ createPredicateMAtrix(variables.size(), index-1)+";\npred("+index+").b   =  0;\n";
		StringBuffer buffer = new StringBuffer();
		buffer.append("pred = struct();\n");
//		String[] names = variables.getVariableNames();
//		for (int i = 0; i < names.length; i++) {
//			buffer.append(prefix.apply(names[i],i+1));
//		}
		variables.forEach((v,i) -> {
			buffer.append(prefix.apply(v,i+1));
		});
		buffer.append("taliro=@(X,T) fw_taliro(psi,pred,X,T);\n");
		return buffer.toString();
	}

	private String createPredicateMAtrix(int n, int index){
		if (n==1){
			return "1";}
		int[] array = new int[n];
		array[index]=-1;
		return Arrays.toString(array);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy