Skip to content

Commit

Permalink
added subcommand tina
Browse files Browse the repository at this point in the history
  • Loading branch information
dalzilio committed Feb 24, 2020
1 parent b6ea045 commit 186a60a
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 5 deletions.
2 changes: 1 addition & 1 deletion benchmarks/models.csv
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ simple,SimpleLoadBal-COL-02,14,13,32,65,916
token,TokenRing-COL-005,1,2,36,156,166
TokenRing-COL-050,TokenRing-COL-050,1,2,2601,127551,-1
VehicularWifi-COL,VehicularWifi-COL-none,21,41,115566,378492,-1
referendum-intrange,Referendum-COL-010,4,3,301,201,-1
referendum-intrange,Referendum-COL-010,4,3,31,21,59050
2 changes: 1 addition & 1 deletion benchmarks/simple/referendum-intrange.pnml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
<structure>
<declarations>
<namedsort id="Voters" name="Voters">
<finiteintrange start="1" end="100"/>
<finiteintrange start="1" end="10"/>
</namedsort>
<variabledecl id="varv" name="v">
<usersort declaration="Voters"/>
Expand Down
214 changes: 213 additions & 1 deletion benchmarks/simple/referendum.pnml
Original file line number Diff line number Diff line change
@@ -1,2 +1,214 @@
<?xml version="1.0"?>
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml"><net id="Referendum-COL-0010" type="http://www.pnml.org/version-2009/grammar/symmetricnet"><page id="DocumentDefaultPage"><place id="ready"><name><text>ready</text></name><type><text>Dot</text><structure><usersort declaration="dot"/></structure></type><hlinitialMarking><text>1'(dot)</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><dotconstant/></subterm></numberof></structure></hlinitialMarking></place><place id="voted_no"><name><text>voted_no</text></name><type><text>Voters</text><structure><usersort declaration="Voters"/></structure></type></place><place id="voted_yes"><name><text>voted_yes</text></name><type><text>Voters</text><structure><usersort declaration="Voters"/></structure></type></place><place id="voting"><name><text>voting</text></name><type><text>Voters</text><structure><usersort declaration="Voters"/></structure></type></place><transition id="start"><name><text>start</text></name></transition><transition id="no"><name><text>no</text></name></transition><transition id="yes"><name><text>yes</text></name></transition><arc id="arc10" source="start" target="voting"><hlinscription><text>1'[(Voters.all)]</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><all><usersort declaration="Voters"/></all></subterm></numberof></structure></hlinscription></arc><arc id="arc11" source="ready" target="start"><hlinscription><text>1'(dot)</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><dotconstant/></subterm></numberof></structure></hlinscription></arc><arc id="arc12" source="no" target="voted_no"><hlinscription><text>1'[(v)]</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><variable refvariable="varv"/></subterm></numberof></structure></hlinscription></arc><arc id="arc13" source="voting" target="no"><hlinscription><text>1'[(v)]</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><variable refvariable="varv"/></subterm></numberof></structure></hlinscription></arc><arc id="arc14" source="yes" target="voted_yes"><hlinscription><text>1'[(v)]</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><variable refvariable="varv"/></subterm></numberof></structure></hlinscription></arc><arc id="arc15" source="voting" target="yes"><hlinscription><text>1'[(v)]</text><structure><numberof><subterm><numberconstant value="1"><positive/></numberconstant></subterm><subterm><variable refvariable="varv"/></subterm></numberof></structure></hlinscription></arc></page><name><text>Referendum-COL-010</text></name><declaration><structure><declarations><namedsort id="Voters" name="Voters"><cyclicenumeration><feconstant id="Voters1" name="1"/><feconstant id="Voters2" name="2"/><feconstant id="Voters3" name="3"/><feconstant id="Voters4" name="4"/><feconstant id="Voters5" name="5"/><feconstant id="Voters6" name="6"/><feconstant id="Voters7" name="7"/><feconstant id="Voters8" name="8"/><feconstant id="Voters9" name="9"/><feconstant id="Voters10" name="10"/></cyclicenumeration></namedsort><variabledecl id="varv" name="v"><usersort declaration="Voters"/></variabledecl><namedsort id="dot" name="Dot"><dot/></namedsort></declarations></structure></declaration></net></pnml>
<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
<net id="Referendum-COL-0010" type="http://www.pnml.org/version-2009/grammar/symmetricnet">
<page id="DocumentDefaultPage">
<place id="ready">
<name>
<text>ready</text>
</name>
<type>
<text>Dot</text>
<structure>
<usersort declaration="dot"/>
</structure>
</type>
<hlinitialMarking>
<text>1'(dot)</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<dotconstant/>
</subterm>
</numberof>
</structure>
</hlinitialMarking>
</place>
<place id="voted_no">
<name>
<text>voted_no</text>
</name>
<type>
<text>Voters</text>
<structure>
<usersort declaration="Voters"/>
</structure>
</type>
</place>
<place id="voted_yes">
<name>
<text>voted_yes</text>
</name>
<type>
<text>Voters</text>
<structure>
<usersort declaration="Voters"/>
</structure>
</type>
</place>
<place id="voting">
<name>
<text>voting</text>
</name>
<type>
<text>Voters</text>
<structure>
<usersort declaration="Voters"/>
</structure>
</type>
</place>
<transition id="start">
<name>
<text>start</text>
</name>
</transition>
<transition id="no">
<name>
<text>no</text>
</name>
</transition>
<transition id="yes">
<name>
<text>yes</text>
</name>
</transition>
<arc id="arc10" source="start" target="voting">
<hlinscription>
<text>1'[(Voters.all)]</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<all>
<usersort declaration="Voters"/>
</all>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
<arc id="arc11" source="ready" target="start">
<hlinscription>
<text>1'(dot)</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<dotconstant/>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
<arc id="arc12" source="no" target="voted_no">
<hlinscription>
<text>1'[(v)]</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<variable refvariable="varv"/>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
<arc id="arc13" source="voting" target="no">
<hlinscription>
<text>1'[(v)]</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<variable refvariable="varv"/>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
<arc id="arc14" source="yes" target="voted_yes">
<hlinscription>
<text>1'[(v)]</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<variable refvariable="varv"/>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
<arc id="arc15" source="voting" target="yes">
<hlinscription>
<text>1'[(v)]</text>
<structure>
<numberof>
<subterm>
<numberconstant value="1">
<positive/>
</numberconstant>
</subterm>
<subterm>
<variable refvariable="varv"/>
</subterm>
</numberof>
</structure>
</hlinscription>
</arc>
</page>
<name>
<text>Referendum-COL-010</text>
</name>
<declaration>
<structure>
<declarations>
<namedsort id="Voters" name="Voters">
<cyclicenumeration>
<feconstant id="Voters1" name="1"/>
<feconstant id="Voters2" name="2"/>
<feconstant id="Voters3" name="3"/>
<feconstant id="Voters4" name="4"/>
<feconstant id="Voters5" name="5"/>
<feconstant id="Voters6" name="6"/>
<feconstant id="Voters7" name="7"/>
<feconstant id="Voters8" name="8"/>
<feconstant id="Voters9" name="9"/>
<feconstant id="Voters10" name="10"/>
</cyclicenumeration>
</namedsort>
<variabledecl id="varv" name="v">
<usersort declaration="Voters"/>
</variabledecl>
<namedsort id="dot" name="Dot">
<dot/>
</namedsort>
</declarations>
</structure>
</declaration>
</net>
</pnml>
2 changes: 1 addition & 1 deletion cmd/lola.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// an error for PNML files that contain a core net description.
var lolaCmd = &cobra.Command{
Use: "lola -i file.pnml",
Short: "generates a net file in the LoLa format from a PNML file describing a high-level net",
Short: "generates a net file in the LoLa format",
Run: func(cmd *cobra.Command, args []string) {
lolaConvert(lolaFileName)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/pnml.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// and generates a P/T net equivalent, also in PNML format.
var pnmlCmd = &cobra.Command{
Use: "pnml -i file.pnml",
Short: "generates a P/T net file in PNML format from a PNML file describing a high-level net",
Short: "generates a P/T net file in PNML format",
Run: func(cmd *cobra.Command, args []string) {
pnmlConvert(pnmlFileName)
},
Expand Down
124 changes: 124 additions & 0 deletions cmd/tina.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2020. LAAS-CNRS, Vertics. All rights reserved.
// Use of this source code is governed by the CeCILL-B license
// that can be found in the LICENSE file.

package cmd

import (
"bufio"

"github.com/dalzilio/mcc/corenet"
"github.com/dalzilio/mcc/hlnet"
"github.com/dalzilio/mcc/pnml"

"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

// tinaCmd takes a PNML model for a high level net, with a .pnml extension, and
// generates a P/T net equivalent in the .net format used in TINA.
var tinaCmd = &cobra.Command{
Use: "tina -i file.pnml",
Short: "generates a P/T net file in .net format",
Run: func(cmd *cobra.Command, args []string) {
tinaConvert(tinaFileName)
},
}

var tinaFileName string
var tinaOutFileName string
var tinaUseName bool
var tinaUseComplexPNames bool

var tinaLogger *log.Logger

func init() {
RootCmd.AddCommand(tinaCmd)
tinaCmd.Flags().StringVarP(&tinaFileName, "file", "i", "", "name of the input file (.pnml)")
tinaCmd.Flags().StringVarP(&tinaOutFileName, "out", "o", "", "basename of the output file (without extension, default to input file basename) or - for stdout")
tinaCmd.Flags().BoolVar(&tinaUseName, "name", false, "use PNML (document) name for the output file")

tinaLogger = log.New(os.Stderr, "MCC TINA:", 0)
}

func tinaConvert(filename string) {
// we capture panics so that that we don't pollute stdout if something goes wrong
defer func() {
if r := recover(); r != nil {
tinaLogger.Println("Error in generation: cannot compute")
os.Exit(1)
}
}()

if filename == "" {
tinaLogger.Println("Bad command line! Input file mandatory. Use option -i")
os.Exit(1)
return
}

if filepath.Ext(filename) != ".pnml" {
tinaLogger.Println("Wrong file extension!")
os.Exit(1)
return
}

xmlFile, err := os.Open(filename)
if err != nil {
tinaLogger.Println("Error opening file:", err)
os.Exit(1)
return
}
defer xmlFile.Close()

decoder := pnml.NewDecoder(xmlFile)
var p = new(pnml.Net)
err = decoder.Build(p)
if err != nil {
tinaLogger.Println("Error decoding PNML file:", err)
os.Exit(1)
return
}

// compute name of the output file. There are three possible cases.
_, file := filepath.Split(filename)
outfile := tinaOutFileName
if outfile == "" {
outfile = file[0 : len(file)-len(".pnml")]
}
if tinaUseName {
outfile = p.Name
}

p.SetSliced(true)
p.SetVerbose(pnml.QUIET)
// p.SetVerbose(pnml.MINIMAL)
// set the semantic of "overflowing" enumeration types
p.SetFES(false)
hl, err := hlnet.Build(p)
if err != nil {
hlnetLogger.Println("Error decoding PNML file:", err)
os.Exit(1)
return
}

cn := corenet.Build(p, hl)

var out *os.File
if outfile == "-" {
out = os.Stdout
} else {
out, err = os.Create(outfile + ".net")
if err != nil {
hlnetLogger.Println("Error creating result file: ", err)
os.Exit(1)
return
}
defer out.Close()
}
w := bufio.NewWriter(out)
cn.Write(w)
w.Flush()
}

0 comments on commit 186a60a

Please sign in to comment.