Utilisation du moteur de calcul sur publi.codes, v0

pull/913/head
Mael 2019-11-19 15:53:15 +01:00 committed by Maxime Quandalle
parent dbb210bea2
commit 911c71aec2
6 changed files with 89 additions and 24 deletions

View File

@ -229,16 +229,21 @@ export let translateAll = (translations, flatRules) => {
)
}
const rulesList = Object.entries(rawRules).map(([dottedName, rule]) => ({
dottedName,
...rule
}))
const rulesToList = rulesObject =>
Object.entries(rulesObject).map(([dottedName, rule]) => ({
dottedName,
...rule
}))
export const buildFlatRules = rulesObject =>
rulesToList(rulesObject).map(enrichRule)
// On enrichit la base de règles avec des propriétés dérivées de celles du YAML
export let rules = translateAll(translations, rulesList).map(rule =>
export let rules = translateAll(translations, rulesToList(rawRules)).map(rule =>
enrichRule(rule)
)
export let rulesFr = rulesList.map(rule => enrichRule(rule))
export let rulesFr = buildFlatRules(rawRules)
export let findParentDependencies = (rules, rule) => {
// A parent dependency means that one of a rule's parents is not just a namespace holder, it is a boolean question. E.g. is it a fixed-term contract, yes / no

View File

@ -6,12 +6,23 @@ import reduceReducers from 'reduce-reducers'
import { combineReducers, Reducer } from 'redux'
import { analysisWithDefaultsSelector } from 'Selectors/analyseSelectors'
import { SavedSimulation } from 'Selectors/storageSelectors'
import { DottedName, Rule } from 'Types/rule'
import { DottedName } from 'Types/rule'
import i18n, { AvailableLangs } from '../i18n'
import inFranceAppReducer, { Company } from './inFranceAppReducer'
import storageRootReducer from './storageReducer'
function explainedVariable(state: DottedName | null = null, action: Action) {
function rules(state = null, action) {
switch (action.type) {
case 'SET_RULES':
return action.rules
default:
return state
}
}
function explainedVariable(
state: DottedName | null = null,
action: Action
): DottedName | null {
switch (action.type) {
case 'EXPLAIN_VARIABLE':
return action.variableName
@ -267,7 +278,7 @@ const existingCompanyReducer = (state, action: Action) => {
const mainReducer = (state, action: Action) =>
combineReducers({
lang,
rules: defaultTo(null) as Reducer<Array<Rule>>,
rules,
explainedVariable,
// We need to access the `rules` in the simulation reducer
simulation: (a: Simulation | null = null, b: Action): Simulation | null =>

View File

@ -18,7 +18,12 @@ function Router({ basename, language }) {
language={language}
reduxMiddlewares={[]}
initialStore={{
rules
rules,
simulation: {
config: {
objectifs: ['a']
}
}
}}
>
<RouterSwitch />

View File

@ -0,0 +1,17 @@
import React from 'react'
export const Header = () => (
<header css="text-align: center">
<h1 css="">
<span css="border: 3px solid var(--colour); color: var(--colour); padding: 0.1rem 0.4rem 0.1rem 0.6rem ; width: 5rem">
publi
</span>
<span css="background: var(--colour); color: white; padding: 0.1rem 0.6rem 0.1rem 0.3rem; width: 5rem; border: 3px solid var(--colour)">
codes
</span>
</h1>
<p css="width: 28rem; margin: 0 auto; font-size: 120%">
Un nouveau langage de calcul pour encoder les algorithmes d'intérêt
public.
</p>
</header>
)

View File

@ -3,6 +3,8 @@ import exemple2 from '!!raw-loader!./exemple2.yaml'
import ColoredYaml from 'Components/rule/ColoredYaml'
import React, { useEffect } from 'react'
import emoji from 'react-easy-emoji'
import { Header } from './Header'
import Yo from './Yo'
export default function Landing() {
useEffect(() => {
@ -21,20 +23,8 @@ export default function Landing() {
return (
<div className="app-container">
<div className="app-content ui__ container">
<div css="text-align: center">
<h1 css="">
<span css="border: 3px solid var(--colour); color: var(--colour); padding: 0.1rem 0.4rem 0.1rem 0.6rem ; width: 5rem">
publi
</span>
<span css="background: var(--colour); color: white; padding: 0.1rem 0.6rem 0.1rem 0.3rem; width: 5rem; border: 3px solid var(--colour)">
codes
</span>
</h1>
<p css="width: 28rem; margin: 0 auto; font-size: 120%">
Un nouveau langage de calcul pour encoder les algorithmes d'intérêt
public.
</p>
</div>
<Header />
<Yo />
<h2>Pourquoi ?</h2>
<p>
Certains algorithmes sont bien trop importants pour être maintenus

View File

@ -0,0 +1,37 @@
import { buildFlatRules } from 'Engine/rules'
import { safeLoad } from 'js-yaml'
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { analysisWithDefaultsSelector } from 'Selectors/analyseSelectors'
import { setSimulationConfig } from '../../actions/actions'
let initialInput = `
a:
formule: 10
b:
formule: a + 18
`
export default function Yo() {
const [ready, setReady] = useState(false)
const dispatch = useDispatch()
const stateConfig = useSelector(state => state.simulation?.config)
useEffect(() => {
dispatch({
type: 'SET_RULES',
rules: buildFlatRules(safeLoad(initialInput))
})
dispatch(setSimulationConfig({ objectifs: ['b'] }))
setReady(true)
}, [])
return (
<div>
Saissez des formules
{ready && <Results />}
</div>
)
}
export const Results = () => {
const analysis = useSelector(state => analysisWithDefaultsSelector(state))
return <div>{analysis.targets[0].nodeValue}</div>
}