Skip to content

Commit

Permalink
Added RExp#addAttribute(String, RExp) method
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Dec 15, 2024
1 parent 8a30027 commit 828f9a7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pmml-rexp/src/main/java/org/jpmml/rexp/RExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ private RExp findAttribute(String name, boolean required){
return null;
}

public void addAttribute(String name, RExp rexp){
addAttribute(new RPair(new RString(name), rexp, null));
}

public void addAttribute(RPair pair){
RPair attributes = getAttributes();

if(attributes == null){
setAttributes(pair);

return;
}

while(attributes != null){
RPair next = attributes.getNext();

if(next == null){
break;
}

attributes = next;
}

attributes.setNext(pair);
}

public RPair getAttributes(){
return this.attributes;
}
Expand Down
55 changes: 55 additions & 0 deletions pmml-rexp/src/test/java/org/jpmml/rexp/RGenericVectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024 Villu Ruusmann
*
* This file is part of JPMML-R
*
* JPMML-R is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-R 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-R. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.rexp;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class RGenericVectorTest {

@Test
public void addAttributes(){
List<RExp> values = Arrays.asList(
new RIntegerVector(Collections.emptyList(), null),
new RDoubleVector(Collections.emptyList(), null),
new RStringVector(Collections.emptyList(), null)
);

RGenericVector genericVector = new RGenericVector(values, null);

assertFalse(genericVector.hasAttribute("class"));
assertFalse(genericVector.hasAttribute("names"));

genericVector.addAttribute("class", new RStringVector(Arrays.asList("data.frame"), null));

assertTrue(genericVector.hasAttribute("class"));
assertFalse(genericVector.hasAttribute("names"));

genericVector.addAttribute("names", new RStringVector(Arrays.asList("A", "B", "C"), null));

assertTrue(genericVector.hasAttribute("class"));
assertTrue(genericVector.hasAttribute("names"));
}
}

0 comments on commit 828f9a7

Please sign in to comment.