Skip to content

Commit

Permalink
Les 3 visiteurs EvalNum, EvalShow, EvalToExp dans le package net.cofa…
Browse files Browse the repository at this point in the history
…res.visiteur.eval
  • Loading branch information
pascalfares committed Apr 14, 2019
1 parent 550c661 commit 1ccc7d5
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 36 deletions.
18 changes: 18 additions & 0 deletions ExpMath/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.license>gpl30</netbeans.hint.license>
</properties>
</project-shared-configuration>
6 changes: 3 additions & 3 deletions ExpMath/src/main/java/net/cofares/model/Add.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ private Add(Expression e1, Expression e2) {
}
@Override
public Integer evalNum() {
return en.evalNum(this);
return en.eval(this);
}

@Override
public Expression eval() {
return Const.create(e1.evalNum()+e2.evalNum());
return e.eval(this);
}

@Override
public String show() {
return "(" + e1.show() + "+" + e2.show() + ")" ;
return es.eval(this);
}

}
6 changes: 3 additions & 3 deletions ExpMath/src/main/java/net/cofares/model/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ private Const(Integer i){

@Override
public Expression eval() {
return this;
return e.eval(this);
}

@Override
public String show() {
return "Const:"+getI();
return es.eval(this);
}

@Override
public Integer evalNum() {
return en.evalNum(this);
return en.eval(this);
}

public static void main(String args[]) {
Expand Down
24 changes: 0 additions & 24 deletions ExpMath/src/main/java/net/cofares/model/EvalNum.java

This file was deleted.

8 changes: 7 additions & 1 deletion ExpMath/src/main/java/net/cofares/model/Expression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package net.cofares.model;

import net.cofares.visiteur.eval.EvalNum;
import net.cofares.visiteur.eval.EvalShow;
import net.cofares.visiteur.eval.EvalToExp;

/**
* La grammaire
*
Expand Down Expand Up @@ -27,7 +31,9 @@
* @author Pascal Fares
*/
public abstract class Expression {
static EvalNum en = new EvalNum(); //Le visiteur Eval NUM TODO: faire que tous les visiteur evaluateur soit une startegie
static EvalNum en = new EvalNum(); //Les visiteur
static EvalToExp e = new EvalToExp();
static EvalShow es = new EvalShow();
// à injecter apr un setter, à la création une evaluateur par defaut.

//Factory Expression create : style visiteur
Expand Down
39 changes: 37 additions & 2 deletions ExpMath/src/main/java/net/cofares/model/ExpressionB.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,46 @@
*/
package net.cofares.model;

import net.cofares.visiteur.eval.EvalNum;
import net.cofares.visiteur.eval.EvalShow;
import net.cofares.visiteur.eval.EvalToExp;

/**
* omolememte composition, Abstract Factory + template
* @author Acer
*/
public abstract class ExpressionB extends Expression{

Expression e1;
Expression e2;
/**
* @return the e1
*/
public Expression getE1() {
return e1;
}

/**
* @param e1 the e1 to set
*/
public void setE1(Expression e1) {
this.e1 = e1;
}

/**
* @return the e2
*/
public Expression getE2() {
return e2;
}

/**
* @param e2 the e2 to set
*/
public void setE2(Expression e2) {
this.e2 = e2;
}

protected Expression e1;
protected Expression e2;


/**
Expand All @@ -25,6 +57,9 @@ public abstract class ExpressionB extends Expression{
*/
public static ExpressionB create(String op, Expression e1, Expression e2) {
en = new EvalNum(); //TODO le passer en injection (c' est en fait une tratégie d'évaluation
e=new EvalToExp();
es=new EvalShow();

if (op.equals("+")) {
return Add.create(e1, e2);
} else if (op.equals("-")) {
Expand Down
7 changes: 4 additions & 3 deletions ExpMath/src/main/java/net/cofares/model/Sous.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ private Sous(Expression e1, Expression e2) {
this.e1=e1;
this.e2=e2;
}

@Override
public Integer evalNum() {
return en.evalNum(this);
return en.eval(this);
}

@Override
public Expression eval() {
return Const.create(e1.evalNum()-e2.evalNum());
return e.eval(this);
}

@Override
public String show() {
return "(" + e1.show() + "-" + e2.show() + ")" ;
return es.eval(this);
}

}
35 changes: 35 additions & 0 deletions ExpMath/src/main/java/net/cofares/visiteur/eval/Eval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 pfares
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.cofares.visiteur.eval;

import net.cofares.model.Add;
import net.cofares.model.Const;
import net.cofares.model.Sous;

/**
*
* @author pfares
*/
public interface Eval<T> {

T eval(Const c);

T eval(Add a);

T eval(Sous a);

}
31 changes: 31 additions & 0 deletions ExpMath/src/main/java/net/cofares/visiteur/eval/EvalNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.cofares.visiteur.eval;

import net.cofares.model.Add;
import net.cofares.model.Const;
import net.cofares.model.Sous;

/**
* Le visiteur EvalNum
* @author Pascal Fares
*/
public class EvalNum implements Eval<Integer> {
@Override
public Integer eval(Const c){
return c.getI();
}

@Override
public Integer eval(Add a){
return a.getE1().evalNum()+a.getE2().evalNum();
}

@Override
public Integer eval(Sous a){
return a.getE1().evalNum()-a.getE2().evalNum();
}
}
44 changes: 44 additions & 0 deletions ExpMath/src/main/java/net/cofares/visiteur/eval/EvalShow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 pfares
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.cofares.visiteur.eval;

import net.cofares.model.Add;
import net.cofares.model.Const;
import net.cofares.model.Sous;

/**
* LE visiteur evalShow
* @author pfares
*/
public class EvalShow implements Eval<String> {

@Override
public String eval(Const c) {
return "Const:"+c.getI();
}

@Override
public String eval(Add a) {
return "("+a.getE1().show()+"+"+a.getE2().show()+ ")";
}

@Override
public String eval(Sous a) {
return "("+a.getE1().show()+"-"+a.getE2().show()+ ")";
}

}
45 changes: 45 additions & 0 deletions ExpMath/src/main/java/net/cofares/visiteur/eval/EvalToExp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 pfares
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.cofares.visiteur.eval;

import net.cofares.model.Add;
import net.cofares.model.Const;
import net.cofares.model.Expression;
import net.cofares.model.Sous;

/**
* LE visiteur eval
* @author pfares
*/
public class EvalToExp implements Eval<Expression> {

@Override
public Expression eval(Const c) {
return c;
}

@Override
public Expression eval(Add a) {
return Const.create(a.getE1().evalNum()+a.getE2().evalNum());
}

@Override
public Expression eval(Sous a) {
return Const.create(a.getE1().evalNum()-a.getE2().evalNum());
}

}

0 comments on commit 1ccc7d5

Please sign in to comment.