Alloy metamodel :: define local state and global state - models

We need to create a metamodel of Alloy models. We have some doubts about how to model global and local state inside our model. We have this model so far:
open util/ordering[Estado] as E
sig Estado{
}
//an alloy model is composed by a set of signatures
sig Model {
sigs : set Signature
}
// each signature has name
sig Signature {
nameSig : one Name
}
sig Name {
}
sig Atom {
nameAtom : one Name
}
sig Instance {
atom : set Atom -> Estado,
instance : set Estado
}
pred solve [m : Model, i : Instance, e : Estado] {
-- every signature name must be different and they all should be part of the signatures
(i.atom.e).(nameAtom) in (m.sigs).(nameSig)
}
pred valid[m : Model] {
all n : Name | lone nameSig.n & m.sigs
}
pred invs [e : Estado]{
-- every sig make part of the model
all s : Signature | s in Model.sigs
all m : Model | valid[m]
all m : Model, i : Instance | solve[m, i, e]
all a : Atom | a in (Instance.atom).e
}
-- invariants
fact invs1 {
all e : Estado | invs[e]
}
----------------------------------------------------------------------------------
-- Será que estes predicados são sobre os atomos ou sobre as instancias?
----------------------------------------------------------------------------------
--pred mantemAtoms[e,e' : Estado, i : Instance]{
-- i.atom.e' = i.atom.e
--}
-- run { some e,e' : Estado, i : Instance | mantemAtoms[e, e', i] } for 3 but exactly 1 Model, exactly 2 Estado
--check addAtoms {
--all e,e' : Estado, a : Atom, i : Instance | invs[e] and addAtoms[e, e', a, i] => invs[e']
--}
pred addAtoms[e,e':Estado, a : Atom, i : Instance]{
--pre
a not in i.(atom.e)
--pos
atom.e' = atom.e + i -> a
instance.e' = instance.e + i
--frame
}
run { some e,e' : Estado, i : Instance, a : Atom | addAtoms[e, e', a, i] }
for 3 but exactly 1 Model, exactly 2 Estado
--check addAtoms {
--all e,e' : Estado, a : Atom, i : Instance | invs[e] and addAtoms[e, e', a, i] => invs[e']
--}
pred excludeAtoms[e,e' : Estado, i : Instance]{
--pre
i in instance.e
--pos
atom.e'= atom.e - i -> i.(atom.e)
instance.e' = instance.e - i
--frame
}
The question is how to model the local and global state inside such a model? We know what are the differences and how to model each state in a specific model but this is different.

Since you are interested in metamodelling, i.e. modelling Alloy models themselves, the Model signature encodes one particular model and the state associated with it can directly represent the global state (i.e. global state can be modelled as state associated with an instance of Model). Local state, in turn, can be associated with instances of Signature signature (which are in turn associated with a particular instance of Model). Effectively, this approach can be thought of as viewing models "one level" higher, where a model is not a top-level entity but an instance of a signature. (The question is quite general and vague -- I hope my understanding of the question was appropriate and this answers the question.)

Related

How can I access to text data (value) of feature importance from a traiend model on Vertex AI with Python

I am working on Predictive Modeling with AutoML on Vertex AI and got the trained model. I checked its feature importance Graphically at Model Tab on Vertex AI, and then I want to have its text data of feature importance with the below code, but can only see it and can not get each of items as value.
------------------------ Python Code
from google.cloud import aiplatform_v1 as aiplatform2
api_endpoint = 'us-central1-aiplatform.googleapis.com'
client_options = {"api_endpoint": api_endpoint} # api_endpoint is required for client_options
client_model = aiplatform2.services.model_service.ModelServiceClient(client_options=client_options)
project_id = 'this is my project id'
location = 'us-central1'
model_id = 'my trained id'
model_name = f'projects/{project_id}/locations/{location}/models/{model_id}'
list_eval_request = aiplatform2.types.ListModelEvaluationsRequest(parent=model_name)
list_eval = client_model.list_model_evaluations(request=list_eval_request)
list_eval.model_evaluations
------------------------ Get this just visually on Notebook
[name: "projects/*********/locations/us-central1/models/*********/evaluations/*********"
metrics_schema_uri: "gs://google-cloud-aiplatform/schema/modelevaluation/regression_metrics_1.0.0.yaml"
metrics {
struct_value {
fields {
key: "meanAbsoluteError"
value {
number_value: 2863.7043
}
}
fields {
key: "meanAbsolutePercentageError"
value {
number_value: 197.63817
}
------------------------ Question
How can I access to the "key" and its "value".
Example key: "meanAbsoluteError" / value : number_value: 2863.7043
I get it like this
for a in list_eval.model_evaluations[0].metrics:
b = str(list_eval.model_evaluations[0].metrics[a])
v_str = a + ' : ' + b
print(v_str)
rootMeanSquaredLogError : 1.2712421
rootMeanSquaredError : 26191.564
rSquared : 0.31798086
meanAbsoluteError : 5698.832
meanAbsolutePercentageError : 262.9534
"metrics" is simply a dictionary

How do I interop with this Javascript code, from Fable F#?

I want to create a binding of the Plotly.js library to Fable.
I am looking at this js code
import React from 'react';
import Plot from 'react-plotly.js';
class App extends React.Component {
render() {
return (
<Plot
data={[
{
x: [1, 2, 3],
y: [2, 6, 3],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
},
{type: 'bar', x: [1, 2, 3], y: [2, 5, 3]},
]}
layout={ {width: 320, height: 240, title: 'A Fancy Plot'} }
/>
);
}
}
and my (faulty) attempt of creating a simple test binding looks like this
open Fable.Core
open Fable.Core.JsInterop
open Browser.Types
open Fable.React
// module Props =
type Chart =
|X of int list
|Y of int List
|Type of string
type IProp =
| Data of obj list
let inline plot (props: IProp) : ReactElement =
ofImport "Plot" "react-plotly.js" props []
let myTrace = createObj [
"x" ==> [1,2,3]
"y" ==> [2,6,3]
"type" ==> "scatter"
"mode" ==> "lines"
]
let myData = Data [myTrace]
let testPlot = plot myData
But obviously it does not work. How do I get it to work? Also, what does {[...]} mean? I am new to Javascript, and as far as I know {...} denotes an object which must contain name value pairs, and [...] denotes an array. So {[...]} seems to denote an object with a single nameless member that is an array, but as far as I know, there are no objects with nameless members.
I have been able to reproduce the example you linked. Please note that I don't Plotly and that I went the empiric way and so things can probably be improved :)
I have created the code as I would probably have done it if I had to use it in my production app. So there is a bit more code than in your question because I don't use createObj.
If you don't like the typed DSL you can always simplify it, remove it and use createObj or anonymous record like I did for the marker property :)
You need to install both react-plotly.js plotly.js in your project.
open Fable.Core.JsInterop
open Fable.Core
open Fable.React
// Define props using DUs this helps create a typed version of the React props
// You can then transform a list of props into an object using `keyValueList`
[<RequireQualifiedAccess>]
type LayoutProps =
| Title of string
| Width of int
| Height of int
// GraphType is marked as a `StringEnum` this means
// the value will be replace at compile time with
// their string representation so:
// `Scatter` becomes `"scatter"`
// You can customise the output by using `[<CompiledName("MyCustomName")>]
[<RequireQualifiedAccess; StringEnum>]
type GraphType =
| Scatter
| Bar
[<RequireQualifiedAccess; StringEnum>]
type GraphMode =
| Lines
| Points
| Markers
| Text
| None
[<RequireQualifiedAccess>]
type DataProps =
| X of obj array
| Y of obj array
| Type of GraphType
| Marker of obj
// This is an helpers to generate the `flagList` waited by Plotly, if you don't like it you can just remove
// member and replace it with `| Mode of string` and so you have to pass the string by yourself
static member Mode (modes : GraphMode seq) : DataProps =
let flags =
modes
|> Seq.map unbox<string> // This is safe to do that because GraphMode is a StringEnum
|> String.concat "+"
unbox ("mode", flags)
[<RequireQualifiedAccess>]
type PlotProps =
| Nothing // Should have real props here is there exist more than Data and Layout
// Here notes that we are asking for an `Array` or Data
// Array being the type expected by the JavaScript library
// `DataProps seq` is our way to represents props
static member Data (dataList : (DataProps seq) array) : PlotProps =
let datas =
dataList
|> Array.map (fun v ->
keyValueList CaseRules.LowerFirst v // Transform the list of props into a JavaScript object
)
unbox ("data", datas)
static member Layout (props : LayoutProps seq) : PlotProps =
unbox ("layout", keyValueList CaseRules.LowerFirst props)
// All the example I saw from react-plotly was using this factory function to transform the plotly library into a React component
// Even, the example you shown if you look at the Babel tab in the live example
let createPlotlyComponent (plotly : obj) = import "default" "react-plotly.js/factory"
// Immport the plotly.js library
let plotlyLib : obj = import "default" "plotly.js"
// Apply the factory on the plotly library
let Plot : obj = createPlotlyComponent plotlyLib
// Helper function to instantiate the react components
// This is really low level, in general we use `ofImport` like you did but if I use `ofImport` then I got a React error
let inline renderPlot (plot : obj) (props : PlotProps list) =
ReactBindings.React.createElement(plot, (keyValueList CaseRules.LowerFirst props), [])
let root =
// Here we can render the plot using our Typed DSL
renderPlot
Plot
[
PlotProps.Data
[|
[
DataProps.X [| 1; 2; 3 |]
DataProps.Y [| 2; 6; 3 |]
DataProps.Type GraphType.Scatter
DataProps.Mode
[
GraphMode.Lines
GraphMode.Points
]
DataProps.Marker {| color = "red" |}
]
[
DataProps.Type GraphType.Bar
DataProps.X [| 1; 2; 3 |]
DataProps.Y [| 2; 5; 3 |]
]
|]
PlotProps.Layout
[
LayoutProps.Width 640
LayoutProps.Height 480
LayoutProps.Title "A Fancy Plot"
]
]
I'm a bit late to the party here, but wanted to give you a different option if you're still looking to use plotly.js with Fable.
I've been working on bindings for plotly.js for the past month or so, and it's in a pretty usable state as of now. That being said, I wouldn't say it's production ready.
This is what the example you want to convert would look like written with Feliz.Plotly:
open Feliz
open Feliz.Plotly
let chart () =
Plotly.plot [
plot.traces [
traces.scatter [
scatter.x [ 1; 2; 3 ]
scatter.y [ 2; 6; 3 ]
scatter.mode [
scatter.mode.lines
scatter.mode.markers
]
scatter.marker [
marker.color color.red
]
]
traces.bar [
bar.x [ 1; 2; 3 ]
bar.y [ 2; 5; 3 ]
]
]
plot.layout [
layout.width 320
layout.height 240
layout.title [
title.text "A Fancy Plot"
]
]
]
You can find more information out here.

How to add variable on a set (Alloy)

module carona
open util/ordering[Time]
sig Time {}
sig Car {
passengers : set Passager
}
one sig UFCG{
students: set Student -> Time
}
abstract sig Student{
region: Region -> Time
}
sig Owner extends Student{
ownerCar : Car
}
sig Passager extends Student{
}
abstract sig Region{ }
one sig Norte,Sul,Leste,Oeste,Centro extends Region{ }
fact Owner{
all o: Owner | #o.ownerCar = 1
}
fact Passager{
all p:Passager | one p.~passengers
}
fact Car{
all c:Car| one c.~ownerCar
all c:Car| #c.passengers <= 4
all c:Car| c.passengers.region=(c.~ownerCar).region
}
fact UFCG {
all passager: Passager, ufcg : UFCG, t:Time | passager in (ufcg.students).t
all owner: Owner, ufcg: UFCG,t:Time | owner in (ufcg.students).t
}
fact Traces{
init[first]
all pre: Time-last | let pos = pre.next |
lone u:UFCG|
some s:Student|
addStudent[s,u,pre,pos]
}
pred init[t:Time]{
one u:UFCG | no (u.students).t
}
pred addStudent[s:Student, u: UFCG, before, after: Time]{
(s !in (u.students).before)
(u.students).after = (u.students).before + s
}
After I run and show the instances created over time, my pred to add Passenger doesn't seem to work, nothing changes or even create. How do i properly add items on a set?
My program, when run, does not show any instance. That is, I can not add anything over time.
Well, for one thing, in the UFCG fact the first quantified formula says that at every time every passenger is in the UFCG student set, this preventing any change in that set.

How can I create a Bacon.Property representing a property in a referenced object?

I have started playing with Bacon.js, and I have come across a problem I can't find any example for.
I have a set of nodes, which may reference other nodes.
It looks like this in imperative code:
alice = { name: "Alice" }
bob = { name: "Bob" }
carol = { name: "Carol" }
alice.likes = bob
bob.likes = carol
alice.likes.name //=> "Bob"
bob.name = "Bobby"
alice.likes.name //=> "Bobby"
alice.likes = carol
alice.likes.name //=> "Carol"
Now, I would like to have a Property for alice.likes.name, which has to change
whenever alice.likes points to a different object, or the name property of the
current object in alice.likes changes.
I have come up with the code below (LiveScript syntax), which correctly logs 3 messages: Bob, Bobby, Carol.
I'm using Bus for testing purposes.
mkBus = (initialValue) ->
bus = new Bacon.Bus()
property = bus.toProperty(initialValue)
property.push = (newValue) -> bus.push(newValue)
property
alice = { pName: mkBus("Alice"), pLikes: mkBus(null) }
bob = { pName: mkBus("Bob"), pLikes: mkBus(null) }
carol = { pName: mkBus("Carol"), pLikes: mkBus(null) }
alice.pLikes.onValue (person) ->
if person
person.pName.onValue (vName) ->
console.log vName
alice.pLikes.push(bob)
bob.pLikes.push(carol)
# Change name
bob.pName.push("Bobby")
# Change reference
alice.pLikes.push(carol)
Question: How can I make a Property that represents the name of alice.likes.name?
I.e:
nameOfAlicesFavourite.onValue (vName) ->
console.log vName
I'm new to FRP, so let me know if I'm doing something horribly wrong.
Thanks #Bergi for pointing me to flatMap.
flatMap creates a new stream for every event in the source. I used flatMapLatest, so that only the name changes from the latest liked person are transmitted on the output stream.
Here's the code:
nameOfAlicesFavourite = alice.pLikes.flatMapLatest (person) ->
if person
person.pName
else
Bacon.never!

stuck making simple grammar for filter language

I have tried and get close but keep getting stuck. Input language is like this
('aaa' eq '42') and ('bbb' gt 'zzz') or (....) and (....)
ie a set of clauses of the form left op right joined by 'and' or 'or'. THere can be 1 or more clauses
This seemed simple to me but I am sure I have started getting too complicated
grammar filter;
options {
language=CSharp2;
output=AST;
}
tokens {
ROOT;
}
OPEN_PAREN
: '(';
CLOSE_PAREN
: ')';
SINGLE_QUOTE
: '\'' ;
AND : 'and';
OR : 'or';
GT : 'gt';
GE : 'ge';
EQ : 'eq';
LT : 'lt';
LE : 'le';
fragment
ID : ('a'..'z' | 'A'..'Z' )+;
STRING : SINGLE_QUOTE ID SINGLE_QUOTE;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = Hidden; } ;
//public root : filter -> ^(ROOT filter);
public filter
: clause^
| lc=clause join rc=clause ->^(join $lc $rc)
;
left : STRING;
right : STRING;
clause
: OPEN_PAREN left op right CLOSE_PAREN //-> ^(op left right)
;
join : AND
| OR
;
op : GT|GE|LT|LE|EQ;
when I run this in C# I get 'more than one node as root'
Also I am not sure how I can do the N joins

Resources